Skip to content

Instantly share code, notes, and snippets.

@slugbyte
Created August 5, 2020 11:02
Show Gist options
  • Save slugbyte/8c72fd6067494644c7231a138dde9c6a to your computer and use it in GitHub Desktop.
Save slugbyte/8c72fd6067494644c7231a138dde9c6a to your computer and use it in GitHub Desktop.
simple fp
export const identity = (data) => () => data;
export const partial = (fn, ...defaults) => (...args) => fn(...defaults, ...args);
export const partialFlip = (fn, ...defaults) => (...args) => fn(...args, ...defaults);
export const map = (fn) => (data) => Array.prototype.map.bind(data)(fn);
export const filter = (fn) => (data) => Array.prototype.filter.bind(data)(fn);
export const reduce = (fn, initial) => (data) => Array.prototype.reduce.bind(data)(fn, initial);
export const compose = (...fns) => (data) => reduce((result, next) => next(result), data)(fns);
export const composeFlip = (...fns) => compose(...fns.reverse());
export const compoesArray = (args) => compose(...args)
export const compoesArrayFlip = (args) => composeFlip(...args)
export const get = (query, DEFAULT) => (current) => {
const keys = query.trim().split('.');
for (let i = 0; i < keys.length; i++) {
try {
const next = current[keys[i]];
if (next) {
current = next;
} else {
return DEFAULT;
}
} catch (e) {
return DEFAULT;
}
}
return current;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment