Comparing version 0.1.0 to 0.1.1
@@ -1,2 +0,2 @@ | ||
!function(n,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e(n.flume=n.flume||{})}(this,function(n){"use strict";function e(n,e){return e={exports:{}},n(e,e.exports),e.exports}function t(n){return function(e,t){return[null,n(t)]}}function r(n,e){function t(n,t){var r=e(n,t);return[r,r]}return{init:n,process:t}}function u(n){return n}function i(n){return n=n||u,function(e,t){return[null,x([n(t),t]).then(o)]}}function o(n){return n[0]?n[1]:m}function f(n,e){function t(n,t){return[n,e(n,t)]}return{init:n,process:t}}function c(){return""}function s(n){function e(e,t){var r=(e+t).split(n);return[r.slice(-1),r.slice(0,-1)]}return{init:c,process:e}}var l="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},a=e(function(n,e){!function(n,t){"function"==typeof n.define&&n.define.amd?n.define(t):"object"==typeof e?t(!0):n.flume=t()}(l,function(e){function t(){return new c}function r(n){function e(n,e,r){var u=g(t.inputs,n),i=u.length,o=-1;for(r=_(i,r||x);++o<i;)u[o].handle(e,r);return t}var t={};return t.dispatch=e,t.inputs=a(t,n),t}function u(n,e){return new f(n,e)}function i(n){return o({"flume:error":n})}function o(n){return{process:w({"*":d},n)}}function f(n,e){this.type=n,this.value=e}function c(){this.defType="input"}function s(n){this.defType="processor",this.init=b(n.init,x),this.process=n.process}function l(n,e,t,r){this.graph=n,this.def=e,this.child=t,this.index=r,this.handle=y(this)}function a(n,e,t,r){var u=e.length-1;if(u<0)return[];for(u&&(t=new l(n,h(e[u]),t,r));--u>0;)t=new l(n,h(e[u]),t,0);return p(n,e[0],t)}function p(n,e,t){e=q(e);for(var r,u=[],i=-1,o=e.length;++i<o;)if(r=e[i],Array.isArray(r))u.push.apply(u,a(n,r,t,i));else{if(!(r instanceof c))throw Error("Expected input or array but got "+S(r));u.push(new l(n,r,t,i))}return u}function h(n){if("function"==typeof n)n={process:{"flume:value":n}};else if("function"==typeof(n||0).process)n=w(n,{process:{"flume:value":n.process}});else if("object"!==S((n||0).process))throw Error("Expected function or object matching processor shape but got "+S(n));return new s(n)}function d(n,e){return[e]}function y(n){return{input:v,processor:m}[n.def.defType](n)}function v(n){return function(e,t){n.child.handle(e,n,t)}}function m(n){function e(n){s.push(n),l||t()}function t(){a=s.shift(),a&&r()}function r(){return l=!0,h().then(k).then(f,c)}function i(){var e=n.def.process,t=e[a.msg.type]||e["*"];return t?t(p,a.msg.value,a.parent.def,n.graph):[p,a.msg]}function o(e){var r=a.end;l=!1,a=null,n.child?n.child.handle(e,n,r):r(),t()}function f(n){var e;Array.isArray(n)?(p=n[0],e=n.length>1?n[1]:p):e=D,o(e)}function c(e){if(!n.child)throw new B(e);o(u("flume:error",e))}var s=[],l=!1,a=null,p=n.def.init(),h=T(i);return function(n,t,r){n=q(n).map(E);for(var u=-1,i=n.length-1;++u<i;)e({msg:n[u],parent:t,end:x});i>-1&&e({msg:n[i],parent:t,end:r})}}function g(n,e){for(var t,r=[],u=n.length,i=-1;++i<u;)t=n[i],t.def===e&&r.push(t);return r}function w(){for(var n,e={},t=Array.prototype.slice.call(arguments),r=-1,u=t.length;++r<u;){n=t[r];for(var i in n)n.hasOwnProperty(i)&&(e[i]=n[i])}return e}function x(){return null}function b(n,e){return void 0===n?e:n}function A(n){return n}function j(n){throw n}function E(n){return n instanceof f?n:u("flume:value",n)}function q(n){return Array.isArray(n)?n:[n]}function S(n){return null!==n?typeof n:"null"}function _(n,e){var t=0;return function(){++t>=n&&e.apply(null,arguments)}}function T(n){return function(){try{var e=n.apply(this,arguments);return F(e)}catch(t){if(t instanceof B)throw t.error;return P(t)}}}function F(n){return z(n)?n:O(n)}function O(n){return new C(n,(!1))}function P(n){return new C(n,(!0))}function k(n){return Array.isArray(n)&&n.filter(z).length>0?M(n):F(n)}function M(n){function e(){return F(n[++f]).then(t)}function t(n){u.push(n)}function r(){return u}for(var u=[],i=n.length,o=-1,f=-1,c=O(null);++o<i;)c=c.then(e);return c.then(r)}function z(n){return!!(n||0).then}function B(n){this.error=n}function C(n,e){this.value=n,this.isFailure=e}var D=u("flume:nil",null);return C.prototype.then=T(function(n,e){return this.isFailure?(e||j).call(this,this.value):(n||A).call(this,this.value)}),e?void(n.exports={create:r,input:t,message:u,trap:o,except:i,nil:D,conj:w,maybeAsync:T,resolveSeq:k}):{create:r,input:t,message:u,trap:o,except:i,nil:D,conj:w,maybeAsync:T,resolveSeq:k}})}),p=a.create,h=a.input,d=a.message,y=a.trap,v=a.except,m=a.nil,g=a.conj,w=a.maybeAsync,x=a.resolveSeq;n.map=t,n.reduce=r,n.filter=i,n.sink=f,n.strsplit=s,n.__moduleExports=a,n.create=p,n.input=h,n.message=d,n.trap=y,n.except=v,n.nil=m,n.conj=g,n.maybeAsync=w,n.resolveSeq=x,Object.defineProperty(n,"__esModule",{value:!0})}); | ||
!function(n,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e(n.flume=n.flume||{})}(this,function(n){"use strict";function e(n,e){return e={exports:{}},n(e,e.exports),e.exports}function t(n){return function(e,t,r){return[null,n(t,r)]}}function r(n,e){function t(n,t,r){try{var u=e(n,t,r);return[u,u]}catch(i){console.log(i)}}return arguments.length<2&&(e=n,n=o),e=u(e),{init:n,process:t}}function u(n){if("function"==typeof n)return n;if(Array.isArray(n))return i(n);throw Error("Unsupported reducer type given to reduce(): "+typeof n)}function i(n){var e=f;return"function"==typeof n[n.length-1]&&(e=n[n.length-1],n=n.slice(0,-1)),function(t,r,u){for(var i,o=u.source,f=-1,c=n.length;++f<c;)if(i=n[f],i[0]===o)return i[1](t,r,u);return e(t,r,u)}}function o(){return null}function f(n,e){return e}function c(n){return n}function s(n){return n=n||c,function(e,t,r){return[null,E([n(t,r),t]).then(l)]}}function l(n){return n[0]?n[1]:A}function p(n,e){function t(n,t,r){return[n,e(n,t,r)]}return{init:n,process:t}}function a(){return""}function h(n){function e(e,t){var r=(e+t).split(n);return[r.slice(-1),r.slice(0,-1)]}return{init:a,process:e}}var d="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},y=e(function(n,e){!function(n,t){"function"==typeof n.define&&n.define.amd?n.define(t):"object"==typeof e?t(!0):n.flume=t()}(d,function(e){function t(){return new c}function r(n){function e(n,e,r){var u=m(t.inputs,n),i=u.length,o=-1;for(r=_(i,r||x);++o<i;)u[o].handle(e,r);return t}var t={};return t.dispatch=e,t.inputs=p(t,n),t}function u(n,e){return new f(n,e)}function i(n){return o({"flume:error":n})}function o(n){return{process:w({"*":d},n)}}function f(n,e){this.type=n,this.value=e}function c(){this.defType="input"}function s(n){this.defType="processor",this.init=A(n.init,x),this.process=n.process}function l(n,e,t,r){this.graph=n,this.def=e,this.child=t,this.index=r,this.handle=y(this)}function p(n,e,t,r){var u=e.length-1;if(u<0)return[];for(u&&(t=new l(n,h(e[u]),t,r));--u>0;)t=new l(n,h(e[u]),t,0);return a(n,e[0],t)}function a(n,e,t){e=q(e);for(var r,u=[],i=-1,o=e.length;++i<o;)if(r=e[i],Array.isArray(r))u.push.apply(u,p(n,r,t,i));else{if(!(r instanceof c))throw Error("Expected input or array but got "+S(r));u.push(new l(n,r,t,i))}return u}function h(n){if("function"==typeof n)n={process:{"flume:value":n}};else if("function"==typeof(n||0).process)n=w(n,{process:{"flume:value":n.process}});else if("object"!==S((n||0).process))throw Error("Expected function or object matching processor shape but got "+S(n));return new s(n)}function d(n,e){return[e]}function y(n){return{input:v,processor:g}[n.def.defType](n)}function v(n){return function(e,t){n.child.handle(e,n,n,t)}}function g(n){function e(n){s.push(n),l||t()}function t(){p=s.shift(),p&&r()}function r(){return l=!0,h().then(k).then(f,c)}function i(){var e=n.def.process,t=e[p.msg.type]||e["*"];return t?t(a,p.msg.value,{source:p.source.def,parent:p.parent.def,dispatch:n.graph.dispatch}):[a,p.msg]}function o(e){var r=p;l=!1,p=null,n.child?n.child.handle(e,n,r.source,r.end):r.end(),t()}function f(n){var e;Array.isArray(n)?(a=n[0],e=n.length>1?n[1]:a):e=C,o(e)}function c(e){if(!n.child)throw new z(e);o(u("flume:error",e))}var s=[],l=!1,p=null,a=n.def.init(),h=T(i);return function(n,t,r,u){n=q(n).map(E);for(var i=-1,o=n.length-1,f={source:r,parent:t};++i<o;)e(w(f,{msg:n[i],end:x}));o>-1&&e(w(f,{msg:n[o],end:u}))}}function m(n,e){for(var t,r=[],u=n.length,i=-1;++i<u;)t=n[i],t.def===e&&r.push(t);return r}function w(){for(var n,e={},t=Array.prototype.slice.call(arguments),r=-1,u=t.length;++r<u;){n=t[r];for(var i in n)n.hasOwnProperty(i)&&(e[i]=n[i])}return e}function x(){return null}function A(n,e){return void 0===n?e:n}function b(n){return n}function j(n){throw n}function E(n){return n instanceof f?n:u("flume:value",n)}function q(n){return Array.isArray(n)?n:[n]}function S(n){return null!==n?typeof n:"null"}function _(n,e){var t=0;return function(){++t>=n&&e.apply(null,arguments)}}function T(n){return function(){try{var e=n.apply(this,arguments);return F(e)}catch(t){if(t instanceof z)throw t.error;return P(t)}}}function F(n){return U(n)?n:O(n)}function O(n){return new B(n,(!1))}function P(n){return new B(n,(!0))}function k(n){return Array.isArray(n)&&n.filter(U).length>0?M(n):F(n)}function M(n){function e(){return F(n[++f]).then(t)}function t(n){u.push(n)}function r(){return u}for(var u=[],i=n.length,o=-1,f=-1,c=O(null);++o<i;)c=c.then(e);return c.then(r)}function U(n){return!!(n||0).then}function z(n){this.error=n}function B(n,e){this.value=n,this.isFailure=e}var C=u("flume:nil",null);return B.prototype.then=T(function(n,e){return this.isFailure?(e||j).call(this,this.value):(n||b).call(this,this.value)}),e?void(n.exports={create:r,input:t,message:u,trap:o,except:i,nil:C,conj:w,maybeAsync:T,resolveSeq:k}):{create:r,input:t,message:u,trap:o,except:i,nil:C,conj:w,maybeAsync:T,resolveSeq:k}})}),v=y.create,g=y.input,m=y.message,w=y.trap,x=y.except,A=y.nil,b=y.conj,j=y.maybeAsync,E=y.resolveSeq;n.map=t,n.reduce=r,n.filter=s,n.sink=p,n.strsplit=h,n.__moduleExports=y,n.create=v,n.input=g,n.message=m,n.trap=w,n.except=x,n.nil=A,n.conj=b,n.maybeAsync=j,n.resolveSeq=E,Object.defineProperty(n,"__esModule",{value:!0})}); | ||
//# sourceMappingURL=flume.min.js.map |
{ | ||
"name": "flume-js", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "define event-prone applications as a tree of functions", | ||
@@ -33,3 +33,3 @@ "license": "MIT", | ||
"dependencies": { | ||
"flume-core": "~0.1.0" | ||
"flume-core": "~0.1.1" | ||
}, | ||
@@ -36,0 +36,0 @@ "devDependencies": { |
@@ -7,4 +7,4 @@ import {nil, resolveSeq} from './core'; | ||
fn = fn || identity; | ||
return function filterFn(_, v) { | ||
return [null, resolveSeq([fn(v), v]).then(test)]; | ||
return function filterFn(_, v, opts) { | ||
return [null, resolveSeq([fn(v, opts), v]).then(test)]; | ||
}; | ||
@@ -11,0 +11,0 @@ } |
export default function map(fn) { | ||
return function mapFn(_, v) { | ||
return [null, fn(v)]; | ||
return function mapFn(_, v, opts) { | ||
return [null, fn(v, opts)]; | ||
}; | ||
} |
@@ -1,2 +0,9 @@ | ||
export default function reduce(init, fn) { | ||
export default function reduce(init, reducer) { | ||
if (arguments.length < 2) { | ||
reducer = init; | ||
init = retNull; | ||
} | ||
reducer = parseReducer(reducer); | ||
return { | ||
@@ -7,6 +14,49 @@ init: init, | ||
function reduceFn(state, v) { | ||
var res = fn(state, v); | ||
function reduceFn(state, v, opts) { | ||
try{ | ||
var res = reducer(state, v, opts); | ||
return [res, res]; | ||
} catch(e) { console.log(e); } | ||
} | ||
} | ||
function parseReducer(obj) { | ||
if (typeof obj == 'function') return obj; | ||
if (Array.isArray(obj)) return caseSourceOfReducer(obj); | ||
throw new Error('Unsupported reducer type given to reduce(): ' + typeof obj); | ||
} | ||
function caseSourceOfReducer(pairs) { | ||
var defaultFn = identityProcessor; | ||
if (typeof pairs[pairs.length - 1] == 'function') { | ||
defaultFn = pairs[pairs.length - 1]; | ||
pairs = pairs.slice(0, -1); | ||
} | ||
return function caseSourceOfFn(state, v, opts) { | ||
var source = opts.source; | ||
var i = -1; | ||
var n = pairs.length; | ||
var pair; | ||
while (++i < n) { | ||
pair = pairs[i]; | ||
if (pair[0] === source) return pair[1](state, v, opts); | ||
} | ||
return defaultFn(state, v, opts); | ||
}; | ||
} | ||
function retNull() { | ||
return null; | ||
} | ||
function identityProcessor(state, v) { | ||
return v; | ||
} |
@@ -7,5 +7,5 @@ export default function sink(init, fn) { | ||
function sinkFn(state, v) { | ||
return [state, fn(state, v)]; | ||
function sinkFn(state, v, opts) { | ||
return [state, fn(state, v, opts)]; | ||
} | ||
} |
@@ -11,3 +11,3 @@ import test from 'ava'; | ||
const graph = [src] | ||
.concat(filter(v => !(v % 2))) | ||
.concat(filter((v, {source}) => (v + (src === source)) % 2)) | ||
.concat(capture(res)); | ||
@@ -14,0 +14,0 @@ |
@@ -11,3 +11,3 @@ import test from 'ava'; | ||
const graph = [src] | ||
.concat(map(v => v * 2)) | ||
.concat(map((v, {source}) => (v + (src === source)) * 2)) | ||
.concat(capture(res)); | ||
@@ -19,3 +19,3 @@ | ||
t.deepEqual(res, [4, 6]); | ||
t.deepEqual(res, [6, 8]); | ||
}); |
@@ -11,3 +11,3 @@ import test from 'ava'; | ||
const graph = [src] | ||
.concat(reduce(() => 10, (total, v) => total + v)) | ||
.concat(reduce(() => 10, (total, v, {source}) => total + v + (source === src))) | ||
.concat(capture(res)); | ||
@@ -19,6 +19,22 @@ | ||
t.deepEqual(res, [12, 15]); | ||
t.deepEqual(res, [13, 17]); | ||
}); | ||
test('no init', t => { | ||
const src = input(); | ||
const res = []; | ||
const graph = [src] | ||
.concat(reduce((total, v) => (total || 0) + v)) | ||
.concat(capture(res)); | ||
create(graph) | ||
.dispatch(src, 2) | ||
.dispatch(src, 3); | ||
t.deepEqual(res, [2, 5]); | ||
}); | ||
test('promise support', async t => { | ||
@@ -40,1 +56,59 @@ const src = input(); | ||
}); | ||
test('source pairs reducer', t => { | ||
const src1 = input(); | ||
const src2 = input(); | ||
const src3 = input(); | ||
const res = []; | ||
const graph = [[src1, src2, src3]] | ||
.concat(reduce(() => 0, [ | ||
[src1, (total, v) => total + v], | ||
[src2, (total, v) => total - v] | ||
])) | ||
.concat(capture(res)); | ||
create(graph) | ||
.dispatch(src1, 2) | ||
.dispatch(src2, 3) | ||
.dispatch(src2, 8) | ||
.dispatch(src1, 7) | ||
.dispatch(src3, 9) | ||
.dispatch(src1, 4); | ||
t.deepEqual(res, [2, -1, -9, -2, 9, 13]); | ||
}); | ||
test('source pairs reducer default', t => { | ||
const src1 = input(); | ||
const src2 = input(); | ||
const src3 = input(); | ||
const res = []; | ||
const graph = [[src1, src2, src3]] | ||
.concat(reduce(() => 0, [ | ||
[src1, (total, v) => total + v], | ||
[src2, (total, v) => total - v], | ||
(total, v) => total * v | ||
])) | ||
.concat(capture(res)); | ||
create(graph) | ||
.dispatch(src1, 2) | ||
.dispatch(src2, 3) | ||
.dispatch(src2, 8) | ||
.dispatch(src1, 7) | ||
.dispatch(src3, 9) | ||
.dispatch(src1, 4); | ||
t.deepEqual(res, [2, -1, -9, -2, -18, -14]); | ||
}); | ||
test('unsupported reducer type', t => { | ||
t.throws( | ||
() => create([input].concat(reduce(23))), | ||
'Unsupported reducer type given to reduce(): number'); | ||
}); |
@@ -11,3 +11,3 @@ import test from 'ava'; | ||
const graph = [src] | ||
.concat(sink(() => 10, (total, v) => total + v)) | ||
.concat(sink(() => 10, (total, v, {source}) => total + v + (source == src))) | ||
.concat(capture(res)); | ||
@@ -19,3 +19,3 @@ | ||
t.deepEqual(res, [12, 13]); | ||
t.deepEqual(res, [13, 14]); | ||
}); | ||
@@ -22,0 +22,0 @@ |
Sorry, the diff of this file is not supported yet
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
36320
344
Updatedflume-core@~0.1.1