Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

galago

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

galago - npm Package Compare versions

Comparing version 0.2.0 to 0.2.1

example/transducer.js

6

example/fakeHttp.js

@@ -13,3 +13,3 @@ /**

* Each of the these steps can fail, and might be async. The function at the bottom called
* `fakeHttpEndpoint` shows how you can use the reduceFns utility to easily
* `fakeHttpEndpoint` shows how you can use the composeAsync utility to easily
* compose all of these steps and allow each of them to fail gracefuly with an

@@ -22,3 +22,3 @@ * error message that can be forwarded to the client.

const reduceFns = require('../src/index').reduceFns;
const composeAsync = require('../src/index').composeAsync;
const chalk = require('chalk');

@@ -127,3 +127,3 @@ const log = console.log;

const fakeHttpEndpoint = (requestBody) =>
reduceFns(
composeAsync(
requestBody,

@@ -130,0 +130,0 @@ [

@@ -8,3 +8,3 @@ 'use strict';

* This file exports the functions:
* - reduceFns
* - composeAsync
* - branch2

@@ -14,15 +14,38 @@ * - branchMultiple

/**
* Recursively reduces an a array of functions. In essance this is a
/*
* compose and composeAsync
*
* Recursively reduce an a array of functions. In essance this is a
* compose functions similar to:
* (...fns) => x => fns.reduceLeft((v, f) => f(v), x);
* but it comes with an opinonated and quircky implementation specific
* to web development.
* but it comes with a reducedFn argument that lets you terminate early
* with the result that was generated so far.
*
* the composeAsync function wraps everything in a Promise, even if some of
* the function arguments are not async themselves.
*/
/**
*
* @param {*} acc
* @param {array<Function>} fns
* @param {?function} reducedFn
* @returns {*}
*/
var compose = function compose(acc, fns, reducedFn) {
if (typeof reducedFn === 'function' && reducedFn(acc) || fns.length === 0) {
return acc;
} else {
return compose(fns[0](acc), fns.slice(1), reducedFn);
}
};
/**
*
* @param {*} acc
* @param {array<Function>} fns
* @param {?function} reducedFn
* @returns {Promise}
*/
var reduceFns = function reduceFns(acc, fns, reducedFn) {
var composeAsync = function composeAsync(acc, fns, reducedFn) {
return new Promise(function (resolve) {

@@ -33,3 +56,3 @@ if (typeof reducedFn === 'function' && reducedFn(acc) || fns.length === 0) {

Promise.resolve(fns[0](acc)).then(function (x) {
return resolve(reduceFns(x, fns.slice(1), reducedFn));
return resolve(composeAsync(x, fns.slice(1), reducedFn));
});

@@ -64,4 +87,5 @@ }

exports.reduceFns = reduceFns;
exports.compose = compose;
exports.composeAsync = composeAsync;
exports.branch2 = branch2;
exports.branchMultiple = branchMultiple;
{
"name": "galago",
"version": "0.2.0",
"version": "0.2.1",
"description": "galago, very specific helper functions for functional-like programming in js",

@@ -26,3 +26,4 @@ "homepage": "https://github.com/filipdanic/galago",

"build": "babel src -d lib",
"example:fakeHttp": "node example/fakeHttp.js"
"example:fakeHttp": "node example/fakeHttp.js",
"example:transducer": "node example/transducer.js"
},

@@ -29,0 +30,0 @@ "license": "MIT",

@@ -12,18 +12,36 @@ # galago

## `reduceFns`
## `compose` and `composeAsync`
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.
Both of these functions recursively reduce an a array of functions by being a typical compose function / pipe mechanism. You can terminate the mechanism early by suppyling a `reduced?` function which check the output at each step.
**Info:**
Both functions accept the same three parameters:
* @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}
* `@param {*} — 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
**Usage example:**
`compose` returns `{*}` (whatever you want), while `composeAsync` returns a `{Promise}`.
**Important note:** the functions supplied to `composeAsync` don’t all have to be async/Promises — you can mix and match!
**Usage example for `compose`**
```javascript
const transducer = (data) =>
compose(
data,
[fn1, fn2, fn3, fn4, … fnX]
);
```
[Full explenation and code here.](https://github.com/filipdanic/galago/blob/master/example/transducer.js)
You can also clone the repo and run `yarn run example:transducer` to run this sample code.
**Usage example for `composeAsync`**
```javascript
const fakeHttpEndpoint = (requestBody) =>
reduceFns(
composeAsync(
requestBody, // acc param

@@ -30,0 +48,0 @@ [ // list of functions

@@ -6,3 +6,3 @@ /**

* This file exports the functions:
* - reduceFns
* - composeAsync
* - branch2

@@ -12,15 +12,38 @@ * - branchMultiple

/**
* Recursively reduces an a array of functions. In essance this is a
/*
* compose and composeAsync
*
* Recursively reduce an a array of functions. In essance this is a
* compose functions similar to:
* (...fns) => x => fns.reduceLeft((v, f) => f(v), x);
* but it comes with an opinonated and quircky implementation specific
* to web development.
* but it comes with a reducedFn argument that lets you terminate early
* with the result that was generated so far.
*
* the composeAsync function wraps everything in a Promise, even if some of
* the function arguments are not async themselves.
*/
/**
*
* @param {*} acc
* @param {array<Function>} fns
* @param {?function} reducedFn
* @returns {*}
*/
const compose = (acc, fns, reducedFn) => {
if ((typeof reducedFn === 'function' && reducedFn(acc)) || fns.length === 0) {
return acc;
} else {
return compose(fns[0](acc), fns.slice(1), reducedFn);
}
};
/**
*
* @param {*} acc
* @param {array<Function>} fns
* @param {?function} reducedFn
* @returns {Promise}
*/
const reduceFns = (acc, fns, reducedFn) => new Promise((resolve) => {
const composeAsync = (acc, fns, reducedFn) => new Promise((resolve) => {
if ((typeof reducedFn === 'function' && reducedFn(acc)) || fns.length === 0) {

@@ -31,3 +54,3 @@ resolve(acc);

.resolve(fns[0](acc))
.then(x => resolve(reduceFns(x, fns.slice(1), reducedFn)));
.then(x => resolve(composeAsync(x, fns.slice(1), reducedFn)));
}

@@ -58,4 +81,5 @@ });

exports.reduceFns = reduceFns;
exports.compose = compose;
exports.composeAsync = composeAsync;
exports.branch2 = branch2;
exports.branchMultiple = branchMultiple;
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc