Comparing version 0.0.1 to 0.2.0
@@ -112,2 +112,10 @@ /** | ||
/** | ||
* Used as the reduced param to quit reduction if error is present. | ||
* @param {*} val | ||
* @returns {boolean} | ||
*/ | ||
const isReduced = (val) => | ||
typeof val === 'object' && val.errors && val.errors.length > 0; | ||
/** | ||
* Example of simple function composition | ||
@@ -120,3 +128,2 @@ * @param {string} requestBody | ||
requestBody, | ||
'errors', | ||
[ | ||
@@ -127,3 +134,4 @@ parse, | ||
saveToDb, | ||
] | ||
], | ||
isReduced | ||
); | ||
@@ -130,0 +138,0 @@ |
@@ -20,14 +20,14 @@ 'use strict'; | ||
* | ||
* @param {string} acc | ||
* @param {exitProp} exitProp | ||
* @param {*} acc | ||
* @param {array<Function>} fns | ||
* @param {?function} reducedFn | ||
* @returns {Promise} | ||
*/ | ||
var reduceFns = function reduceFns(acc, exitProp, fns) { | ||
var reduceFns = function reduceFns(acc, fns, reducedFn) { | ||
return new Promise(function (resolve) { | ||
if (acc[exitProp] || fns.length === 0) { | ||
if (typeof reducedFn === 'function' && reducedFn(acc) || fns.length === 0) { | ||
resolve(acc); | ||
} else { | ||
Promise.resolve(fns[0](acc)).then(function (x) { | ||
return resolve(reduceFns(x, exitProp, fns.slice(1))); | ||
return resolve(reduceFns(x, fns.slice(1), reducedFn)); | ||
}); | ||
@@ -34,0 +34,0 @@ } |
{ | ||
"name": "galago", | ||
"version": "0.0.1", | ||
"version": "0.2.0", | ||
"description": "galago, very specific helper functions for functional-like programming in js", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/filipdanic/galago", |
# galago | ||
`galago` /ɡəˈleɪɡoʊz/ is a set of very specific, opinionated helper functions for functional-like programming in js. It’s also the name of [this animal.](https://en.wikipedia.org/wiki/Galago) | ||
🚧 Work in progress, but feel free to: | ||
```bash | ||
yarn add galago | ||
# or | ||
npm install galago --save | ||
``` | ||
Or grab the source from `src/index.js` or `lib/index.js` (ES2015) and pluck it into your codebase as vendor code. The world is your oyster. `¯\_(ツ)_/¯` | ||
## `reduceFns` | ||
Recursively reduces an a array of functions (which can even be async) by being a typical compose function / pipe mechanism. It stops reducing the supplied arguments if one of the functions returns the specified error key in it’s response. | ||
Recursively reduces an a array of functions (which can even be async) by being a typical compose function / pipe mechanism. It stops reducing the supplied arguments if one of the functions returns the specified error key in it’s response. | ||
**Info:** | ||
* @param {string} acc | ||
* @param {exitProp} exitProp | ||
* @param {array<Function>} fns | ||
* @param {*} acc - used to provide the initial value | ||
* @param {array<Function>} fns - array of function to execute | ||
* @param {?function} reducedFn - optional function with signature `fn(x: Any): Boolean`; if returning true, exits the call and returns the last result | ||
* @returns {Promise} | ||
@@ -20,5 +28,4 @@ | ||
reduceFns( | ||
requestBody, | ||
'errors', | ||
[ | ||
requestBody, // acc param | ||
[ // list of functions | ||
parse, | ||
@@ -28,3 +35,4 @@ validate, | ||
saveToDb, | ||
] | ||
], | ||
isReduced // reducedFn param | ||
); | ||
@@ -31,0 +39,0 @@ ``` |
@@ -18,9 +18,9 @@ /** | ||
* | ||
* @param {string} acc | ||
* @param {exitProp} exitProp | ||
* @param {*} acc | ||
* @param {array<Function>} fns | ||
* @param {?function} reducedFn | ||
* @returns {Promise} | ||
*/ | ||
const reduceFns = (acc, exitProp, fns) => new Promise((resolve) => { | ||
if (acc[exitProp] || fns.length === 0) { | ||
const reduceFns = (acc, fns, reducedFn) => new Promise((resolve) => { | ||
if ((typeof reducedFn === 'function' && reducedFn(acc)) || fns.length === 0) { | ||
resolve(acc); | ||
@@ -30,3 +30,3 @@ } else { | ||
.resolve(fns[0](acc)) | ||
.then(x => resolve(reduceFns(x, exitProp, fns.slice(1)))); | ||
.then(x => resolve(reduceFns(x, fns.slice(1), reducedFn))); | ||
} | ||
@@ -33,0 +33,0 @@ }); |
70974
279
50