diff --git a/src/util/index.js b/src/util/index.js index fb395e7..6947c03 100644 --- a/src/util/index.js +++ b/src/util/index.js @@ -1,4 +1,11 @@ -const stringify = require('json-stringify-safe'); +const _stringify = require('json-stringify-safe'); + +const isObject = x => x && typeof x === 'object'; + +const stringify = obj => + isObject(obj) + ? (obj.stack || _stringify(obj)) + : obj; /* Returns true if a promise is passed */ const isPromise = prom => prom && (typeof prom.then === 'function'); @@ -11,12 +18,20 @@ const liftPromise = (fn, thing) => module.exports = { + /* Returns true if an object is passed */ + isObject, + /* Returns true if a promise is passed */ isPromise, /* Lift a value or promise into a function */ liftPromise, + /* Returns the element found at the given path or undefined */ + path: obj => + path => + path.reduce((result, segment) => result && result[segment], obj), + /* Pipe a value or promise through any number of unary functions */ pipe: (...fns) => arg => fns.reduce((acc, fn) => @@ -28,6 +43,9 @@ module.exports = { /* Maps a function over an array */ map : fn => x => x.map(fn), + /* Filter an array using provided function */ + filter : fn => x => x.filter(fn), + /* Returns identity */ identity : x => x, @@ -52,10 +70,11 @@ module.exports = { ), /* Stringifies object or coerces to string */ - stringify : obj => - typeof obj === 'object' - ? (obj.stack || stringify(obj)) - : obj, + stringify, + + /* Tagged Stringify */ + taggedStringify: (strings, ...expr) => strings.reduce((acc, curr, i) => + acc + curr + (stringify(expr[i]) || ''), ''), /* Short circuits with given value on pred. Else calls function */ short : (pred, shorter) =>