Last active
March 31, 2025 09:59
-
-
Save tijnjh/67e290a7c4fdcd2459714e5f4039537a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// itpipe.js | |
/** | |
* Creates a proxy that captures method calls for piping | |
* @returns {Proxy} The 'it' proxy object | |
*/ | |
const createItProxy = () => { | |
return new Proxy({}, { | |
get: (target, prop) => { | |
if (typeof prop === 'symbol' || prop === 'inspect') { | |
return () => {}; | |
} | |
return (...args) => (value) => { | |
if (typeof value[prop] === 'function') { | |
return value[prop](...args); | |
} | |
return value[prop]; | |
}; | |
} | |
}); | |
}; | |
/** | |
* The 'it' proxy for creating method calls | |
*/ | |
const it = createItProxy(); | |
/** | |
* Pipes a value through a series of functions | |
* @param {...Function} fns - Functions to pipe through | |
* @returns {Function} A function that takes an initial value and returns the result | |
*/ | |
const pipe = (...fns) => (val) => { | |
return fns.reduce((result, fn) => fn(result), val); | |
}; | |
/** | |
* Composes functions from right to left (reverse of pipe) | |
* @param {...Function} fns - Functions to compose | |
* @returns {Function} A function that takes an initial value and returns the result | |
*/ | |
const compose = (...fns) => (val) => { | |
return fns.reduceRight((result, fn) => fn(result), val); | |
}; | |
module.exports = { pipe, compose, it }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment