|
|
@ -3,11 +3,25 @@ const stringify = require('json-stringify-safe'); |
|
|
|
/* Returns true if a promise is passed */ |
|
|
|
const isPromise = prom => prom && (typeof prom.then === 'function'); |
|
|
|
|
|
|
|
/* Lift a value or promise into a function */ |
|
|
|
const liftPromise = (fn, thing) => |
|
|
|
isPromise(thing) |
|
|
|
? thing.then(fn) |
|
|
|
: fn(thing); |
|
|
|
|
|
|
|
module.exports = { |
|
|
|
|
|
|
|
/* Returns true if a promise is passed */ |
|
|
|
isPromise, |
|
|
|
|
|
|
|
/* Lift a value or promise into a function */ |
|
|
|
liftPromise, |
|
|
|
|
|
|
|
/* Pipe a value or promise through any number of unary functions */ |
|
|
|
pipe: (...fns) => |
|
|
|
arg => fns.reduce((acc, fn) => |
|
|
|
liftPromise(fn, acc), arg), |
|
|
|
|
|
|
|
/* Flattens an array of arrays to an array */ |
|
|
|
flatten : arrData => [].concat.apply([], arrData), |
|
|
|
|
|
|
@ -26,13 +40,6 @@ module.exports = { |
|
|
|
/* Resolves an array of Promises */ |
|
|
|
promiseAll : x => Promise.all(x), |
|
|
|
|
|
|
|
/* Pipe a value or promise through any number of unary functions */ |
|
|
|
pipe: (...fns) => |
|
|
|
arg => fns.reduce((acc, fn) => |
|
|
|
isPromise(acc) |
|
|
|
? acc.then(fn) |
|
|
|
: fn(acc), arg), |
|
|
|
|
|
|
|
/* Pass partial arguments and return a function that accepts the rest */ |
|
|
|
partial: (fn, ...args) => (...rest) => fn(...args, ...rest), |
|
|
|
|
|
|
@ -44,12 +51,6 @@ module.exports = { |
|
|
|
x => path.match(new RegExp(`/${x}/?$`)) |
|
|
|
), |
|
|
|
|
|
|
|
/* Lift promises into a function */ |
|
|
|
liftPromise : (fn, thing) => |
|
|
|
isPromise(thing) |
|
|
|
? thing.then(fn) |
|
|
|
: fn(thing), |
|
|
|
|
|
|
|
/* Stringifies object or coerces to string */ |
|
|
|
stringify : obj => |
|
|
|
typeof obj === 'object' |
|
|
|