callbag-pipe
Advanced tools
Comparing version 1.0.0 to 1.1.0
11
index.js
function pipe(...cbs) { | ||
let cb, | ||
res = false, | ||
i = 0, | ||
n = cbs.length; | ||
for (; i < n; i++) { | ||
cb = cbs[i]; | ||
if (res) res = cb(res); | ||
else res = cb; | ||
} | ||
let res = cbs[0]; | ||
for (let i = 1, n = cbs.length; i < n; i++) res = cbs[i](res); | ||
return res; | ||
@@ -12,0 +5,0 @@ } |
{ | ||
"name": "callbag-pipe", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Utility function for plugging callbags together in chain", | ||
@@ -15,6 +15,12 @@ "repository": { | ||
"license": "MIT", | ||
"keywords": ["callbag"], | ||
"keywords": [ | ||
"callbag" | ||
], | ||
"devDependencies": { | ||
"tape": "^4.8.0" | ||
"tape": "^4.8.0", | ||
"callbag-from-iter": "1.0.x", | ||
"callbag-map": "1.0.x", | ||
"callbag-filter": "1.0.x", | ||
"callbag-iterate": "1.0.x" | ||
} | ||
} |
@@ -65,1 +65,36 @@ # callbag-pipe | ||
``` | ||
## Nesting | ||
To use pipe inside another pipe, you need to give the inner pipe an argument, e.g. `s => pipe(s, ...`: | ||
```js | ||
const interval = require('callbag-interval'); | ||
const observe = require('callbag-observe'); | ||
const combine = require('callbag-combine'); | ||
const pipe = require('callbag-pipe'); | ||
const take = require('callbag-take'); | ||
const map = require('callbag-map'); | ||
pipe( | ||
combine(interval(100), interval(350)), | ||
s => pipe(s, | ||
map(([x, y]) => `X${x},Y${y}`), | ||
take(10) | ||
), | ||
observe(x => console.log(x)) | ||
); | ||
``` | ||
This means you can use pipe to create a new operator: | ||
```js | ||
const mapThenTake = (f, amount) => | ||
s => pipe(s, map(f), take(amount)); | ||
pipe( | ||
combine(interval(100), interval(350)), | ||
mapThenTake(([x, y]) => `X${x},Y${y}`, 10), | ||
observe(x => console.log(x)) | ||
); | ||
``` |
68
test.js
const test = require('tape'); | ||
const fromIter = require('callbag-from-iter'); | ||
const map = require('callbag-map'); | ||
const filter = require('callbag-filter'); | ||
const iterate = require('callbag-iterate'); | ||
const pipe = require('./index'); | ||
@@ -15,2 +19,15 @@ | ||
test('it calls first-order functions in a nested pipe', (t) => { | ||
t.plan(1); | ||
const res = pipe( | ||
2, // 2 | ||
s => pipe(s, | ||
x => x * 10, // 20 | ||
x => x - 3 // 17 | ||
), | ||
x => x + 5 // 22 | ||
); | ||
t.equals(res, 22); | ||
}); | ||
test('it calls higher-order callbacks in sequence LTR', (t) => { | ||
@@ -30,1 +47,52 @@ t.plan(2); | ||
}); | ||
test('it can be nested', (t) => { | ||
t.plan(2); | ||
const res = pipe( | ||
cb => cb(2), // 2 | ||
s => pipe(s, | ||
prev => cb => prev(x => cb(x * 10)), // 20 | ||
prev => cb => prev(x => cb(x - 3)) // 17 | ||
), | ||
prev => cb => prev(x => cb(x + 5)) // 22 | ||
); | ||
t.equals(typeof res, 'function'); | ||
res(x => { | ||
t.equals(x, 22); | ||
t.end(); | ||
}); | ||
}); | ||
test('it works with common callbag utilities', (t) => { | ||
t.plan(2); | ||
const expected = [1, 3]; | ||
pipe( | ||
fromIter([10, 20, 30, 40]), | ||
map(x => x / 10), | ||
filter(x => x % 2), | ||
iterate(x => { | ||
t.equals(x, expected.shift()); | ||
if (expected.length === 0) { | ||
t.end(); | ||
} | ||
}) | ||
); | ||
}); | ||
test('it can be nested with callbag utilities', (t) => { | ||
t.plan(2); | ||
const expected = [1, 3]; | ||
pipe( | ||
fromIter([10, 20, 30, 40]), | ||
s => pipe(s, | ||
map(x => x / 10), | ||
filter(x => x % 2) | ||
), | ||
iterate(x => { | ||
t.equals(x, expected.shift()); | ||
if (expected.length === 0) { | ||
t.end(); | ||
} | ||
}) | ||
); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6396
96
100
5