Comparing version 0.2.2 to 0.2.3
90
flyd.js
@@ -43,2 +43,5 @@ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.flyd = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
endStream.listeners.push(s); | ||
s.toJSON = function() { | ||
return s(); | ||
}; | ||
if (arguments.length > 0) s(initialValue); | ||
@@ -189,3 +192,3 @@ return s; | ||
* | ||
* Similair to `map` except that the returned stream is empty. Use `on` for doing | ||
* Similar to `map` except that the returned stream is empty. Use `on` for doing | ||
* side effects in reaction to stream changes. Use the returned stream only if you | ||
@@ -348,3 +351,3 @@ * need to manually end it. | ||
* @param {stream} stream - the values stream | ||
* @return {stream} a new stram with the functions applied to values | ||
* @return {stream} a new stream with the functions applied to values | ||
* | ||
@@ -640,6 +643,6 @@ * @example | ||
/** | ||
* Returns a curried equivalent of the provided function, with the | ||
* specified arity. The curried function has two unusual capabilities. | ||
* First, its arguments needn't be provided one at a time. If `g` is | ||
* `R.curryN(3, f)`, the following are equivalent: | ||
* Returns a curried equivalent of the provided function, with the specified | ||
* arity. The curried function has two unusual capabilities. First, its | ||
* arguments needn't be provided one at a time. If `g` is `R.curryN(3, f)`, the | ||
* following are equivalent: | ||
* | ||
@@ -653,4 +656,4 @@ * - `g(1)(2)(3)` | ||
* "gaps", allowing partial application of any combination of arguments, | ||
* regardless of their positions. If `g` is as above and `_` is `R.__`, | ||
* the following are equivalent: | ||
* regardless of their positions. If `g` is as above and `_` is `R.__`, the | ||
* following are equivalent: | ||
* | ||
@@ -667,2 +670,3 @@ * - `g(1, 2, 3)` | ||
* @memberOf R | ||
* @since v0.5.0 | ||
* @category Function | ||
@@ -676,7 +680,5 @@ * @sig Number -> (* -> a) -> (* -> a) | ||
* | ||
* var addFourNumbers = function() { | ||
* return R.sum([].slice.call(arguments, 0, 4)); | ||
* }; | ||
* var sumArgs = (...args) => R.sum(args); | ||
* | ||
* var curriedAddFourNumbers = R.curryN(4, addFourNumbers); | ||
* var curriedAddFourNumbers = R.curryN(4, sumArgs); | ||
* var f = curriedAddFourNumbers(1, 2); | ||
@@ -695,3 +697,3 @@ * var g = f(3); | ||
module.exports = function _arity(n, fn) { | ||
// jshint unused:vars | ||
/* eslint-disable no-unused-vars */ | ||
switch (n) { | ||
@@ -714,4 +716,7 @@ case 0: return function() { return fn.apply(this, arguments); }; | ||
},{}],4:[function(require,module,exports){ | ||
var _isPlaceholder = require('./_isPlaceholder'); | ||
/** | ||
* Optimized internal two-arity curry function. | ||
* Optimized internal one-arity curry function. | ||
* | ||
@@ -725,6 +730,4 @@ * @private | ||
return function f1(a) { | ||
if (arguments.length === 0) { | ||
if (arguments.length === 0 || _isPlaceholder(a)) { | ||
return f1; | ||
} else if (a != null && a['@@functional/placeholder'] === true) { | ||
return f1; | ||
} else { | ||
@@ -736,4 +739,5 @@ return fn.apply(this, arguments); | ||
},{}],5:[function(require,module,exports){ | ||
},{"./_isPlaceholder":7}],5:[function(require,module,exports){ | ||
var _curry1 = require('./_curry1'); | ||
var _isPlaceholder = require('./_isPlaceholder'); | ||
@@ -751,18 +755,13 @@ | ||
return function f2(a, b) { | ||
var n = arguments.length; | ||
if (n === 0) { | ||
return f2; | ||
} else if (n === 1 && a != null && a['@@functional/placeholder'] === true) { | ||
return f2; | ||
} else if (n === 1) { | ||
return _curry1(function(b) { return fn(a, b); }); | ||
} else if (n === 2 && a != null && a['@@functional/placeholder'] === true && | ||
b != null && b['@@functional/placeholder'] === true) { | ||
return f2; | ||
} else if (n === 2 && a != null && a['@@functional/placeholder'] === true) { | ||
return _curry1(function(a) { return fn(a, b); }); | ||
} else if (n === 2 && b != null && b['@@functional/placeholder'] === true) { | ||
return _curry1(function(b) { return fn(a, b); }); | ||
} else { | ||
return fn(a, b); | ||
switch (arguments.length) { | ||
case 0: | ||
return f2; | ||
case 1: | ||
return _isPlaceholder(a) ? f2 | ||
: _curry1(function(_b) { return fn(a, _b); }); | ||
default: | ||
return _isPlaceholder(a) && _isPlaceholder(b) ? f2 | ||
: _isPlaceholder(a) ? _curry1(function(_a) { return fn(_a, b); }) | ||
: _isPlaceholder(b) ? _curry1(function(_b) { return fn(a, _b); }) | ||
: fn(a, b); | ||
} | ||
@@ -772,4 +771,5 @@ }; | ||
},{"./_curry1":4}],6:[function(require,module,exports){ | ||
},{"./_curry1":4,"./_isPlaceholder":7}],6:[function(require,module,exports){ | ||
var _arity = require('./_arity'); | ||
var _isPlaceholder = require('./_isPlaceholder'); | ||
@@ -783,4 +783,5 @@ | ||
* @param {Number} length The arity of the curried function. | ||
* @return {array} An array of arguments received thus far. | ||
* @param {Array} received An array of arguments received thus far. | ||
* @param {Function} fn The function to curry. | ||
* @return {Function} The curried function. | ||
*/ | ||
@@ -796,4 +797,3 @@ module.exports = function _curryN(length, received, fn) { | ||
if (combinedIdx < received.length && | ||
(received[combinedIdx] == null || | ||
received[combinedIdx]['@@functional/placeholder'] !== true || | ||
(!_isPlaceholder(received[combinedIdx]) || | ||
argsIdx >= arguments.length)) { | ||
@@ -806,3 +806,3 @@ result = received[combinedIdx]; | ||
combined[combinedIdx] = result; | ||
if (result == null || result['@@functional/placeholder'] !== true) { | ||
if (!_isPlaceholder(result)) { | ||
left -= 1; | ||
@@ -812,7 +812,15 @@ } | ||
} | ||
return left <= 0 ? fn.apply(this, combined) : _arity(left, _curryN(length, combined, fn)); | ||
return left <= 0 ? fn.apply(this, combined) | ||
: _arity(left, _curryN(length, combined, fn)); | ||
}; | ||
}; | ||
},{"./_arity":3}]},{},[1])(1) | ||
},{"./_arity":3,"./_isPlaceholder":7}],7:[function(require,module,exports){ | ||
module.exports = function _isPlaceholder(a) { | ||
return a != null && | ||
typeof a === 'object' && | ||
a['@@functional/placeholder'] === true; | ||
}; | ||
},{}]},{},[1])(1) | ||
}); |
@@ -42,2 +42,5 @@ 'use strict'; | ||
endStream.listeners.push(s); | ||
s.toJSON = function() { | ||
return s(); | ||
}; | ||
if (arguments.length > 0) s(initialValue); | ||
@@ -346,3 +349,3 @@ return s; | ||
* @param {stream} stream - the values stream | ||
* @return {stream} a new stram with the functions applied to values | ||
* @return {stream} a new stream with the functions applied to values | ||
* | ||
@@ -349,0 +352,0 @@ * @example |
# flyd-aftersilence | ||
Buffers values from a source stream in an array and emits it after a | ||
specified duration of silence from the source stream. | ||
Buffers values from a source stream into an array and emits the array once the source stream has had the specified duration of silence. | ||
__Graph__ | ||
```marbles | ||
(ticks represent 10ms) | ||
a: {-1-2-3--5-6-} | ||
afterSilence(20, a): {--------.---} | ||
[1,2,3] | ||
``` | ||
__Signature__ | ||
`Stream a -> Stream b` | ||
`(Integer, Stream a) -> Stream b` | ||
@@ -12,11 +20,13 @@ __Example__ | ||
```javascript | ||
afterSilence(function(values) { | ||
console.log('You achieved a combo of ' + values.length + '!'); | ||
}, afterSilence(1000, birdsShot); | ||
``` | ||
const afterSilence = require('flyd/module/aftersilence') | ||
```javascript | ||
afterSilence(function(values) { | ||
console.log('You typed: "' + values.join('') + '" without any pauses'); | ||
}, afterSilence(300, characterTyped); | ||
const source = flyd.stream() | ||
const result = flyd.afterSilence(100, source) | ||
source(1); source(2); source(3) | ||
result() // undefined | ||
// wait 100ms and print result | ||
setTimeout(() => console.log(result()), 100) | ||
// -> prints [1,2,3] | ||
``` |
@@ -5,3 +5,3 @@ var flyd = require('../../lib'); | ||
module.exports = function(diffFunc, s) { | ||
module.exports = flyd.curryN(2, function(diffFunc, s) { | ||
var prevS = previous(s); | ||
@@ -11,2 +11,2 @@ return flyd.combine(function() { | ||
}, [s, prevS]); | ||
}; | ||
}); |
# flyd-diff | ||
diff function for Flyd. | ||
Like map, but the mapping function gets the previous and the current value of the stream. | ||
Similar to map, but this function gets both the previous and the current value of the stream. | ||
# Usage | ||
__Graph__ | ||
``` | ||
var velocity = flyd.stream(0); | ||
var acceleration = diff(function (previous, current) { | ||
return current - previous; | ||
}, velocity); | ||
velocity(2)(5)(1); | ||
a: {-1-1-2-3-5-} | ||
diff(add, a): {---2-3-5-8-} | ||
``` | ||
__Signature__ | ||
`((a, a) -> a) -> Stream a -> Stream a` | ||
__Usage__ | ||
``` | ||
const diff = require('flyd/module/diff') | ||
const velocity = flyd.stream(0) | ||
const acceleration = diff((previous, current) => current - previous, velocity) | ||
velocity(2)(5) | ||
acceleration() // 3 | ||
velocity(1) | ||
acceleration() // -4 | ||
``` |
@@ -6,3 +6,3 @@ var flyd = require('../../lib'); | ||
return flyd.combine(function(s, self) { | ||
if (!eq(s.val, prev)) { | ||
if (!self.hasVal || !eq(s.val, prev)) { | ||
self(s.val); | ||
@@ -9,0 +9,0 @@ prev = s.val; |
# flyd-droprepeats | ||
Drops consecutively repeated values from a | ||
[Flyd](https://github.com/paldepind/flyd) stream. | ||
[Flyd](https://github.com/paldepind/flyd) stream. Equality is determined by reference. | ||
## API | ||
## dropRepeats(s) | ||
### dropRepeats(s) | ||
__Graph__ | ||
Drops repeated values from stream `s`. Equality is determined by reference. | ||
``` | ||
a: {---11--12-2-3-4-} | ||
dropRepeats(a): {---1----2---3-4-} | ||
``` | ||
@@ -19,15 +22,12 @@ __Signature__ | ||
```js | ||
var dropRepeats = require('flyd-droprepeats').dropRepeats; | ||
var append = function(arr, x) { | ||
return arr.concat(x); | ||
}; | ||
const dropRepeats = require('flyd/module/droprepeats').dropRepeats | ||
var s = flyd.stream(); | ||
var noRepeats = dropRepeats(s); | ||
var collect = flyd.scan(append, [], noRepeats); | ||
s(1)(2)(2)(3); | ||
const s = flyd.stream() | ||
const noRepeats = dropRepeats(s) | ||
const collect = flyd.scan((ls, n) => ls.concat(n), [], noRepeats) | ||
s(1)(2)(2)(3) | ||
collect() // [1, 2, 3] | ||
``` | ||
### dropRepeatsWith(fn, s) | ||
## dropRepeatsWith(fn, s) | ||
@@ -39,13 +39,17 @@ Drops repeated values from stream `s`, but also takes a function `fn` that | ||
`(a -> b -> Boolean) -> Stream a -> Stream a` | ||
`(a -> a -> Boolean) -> Stream a -> Stream a` | ||
__Usage__ | ||
```js | ||
var dropRepeatsWith = require('flyd-droprepeats').dropRepeatsWith; | ||
var s = flyd.stream(); | ||
const dropRepeatsWith = require('flyd/module/droprepeats').dropRepeatsWith | ||
const s = flyd.stream() | ||
// Ramda's `equals` determines equality by value | ||
var noRepeats = dropRepeatsWith(R.equals, s); | ||
var collect = flyd.scan(append, [], noRepeats); | ||
s({ foo: 'bar' }); | ||
s({ foo: 'bar' }); | ||
const R = require('ramda') | ||
const noRepeats = dropRepeatsWith(R.equals, s) | ||
const collect = flyd.scan((ls, n) => ls.concat(n), [], noRepeats) | ||
s({ foo: 'bar' }) | ||
s({ foo: 'bar' }) | ||
collect() // [{ foo: 'bar' }] | ||
``` |
@@ -41,2 +41,16 @@ var flyd = require('../../../lib'); | ||
it('always includes first value so we can safely test for equality', function() { | ||
var s = stream(); | ||
var all = collect(dropRepeatsWith(function(a, b) { | ||
return a[0] === b[0] || a[1] === b[1]; | ||
}, s)); | ||
s([1, 2]); | ||
s([1, 3]); | ||
s([2, 3]); | ||
assert.deepEqual(all(), [ | ||
[1, 2], | ||
[2, 3] | ||
]); | ||
}); | ||
it('is curried', function() { | ||
@@ -43,0 +57,0 @@ var s = stream(); |
# flyd-every | ||
Takes a number of milliseconds t and creates a stream of the current time updated every t. | ||
Takes a number of milliseconds `ms` and creates a stream of the current time updated every `ms`. | ||
The stream emits current timestamp values using `Date.now()` (eg `1475273004713`) | ||
__Graph__ | ||
``` | ||
(ticks represent 10ms) | ||
every(30): {--t--t--t--t--t--> | ||
``` | ||
__Signature__ | ||
@@ -11,7 +20,11 @@ | ||
```javascript | ||
var everySecond = every(1000); | ||
flyd.on(function(time) { | ||
// I'm called once every second | ||
console.log('Current time is, time'); | ||
}, everySecond); | ||
const every = require('flyd/module/every') | ||
const everySecond = every(1000) | ||
// Print once every second | ||
flyd.on(time => console.log('Current time is', time), everySecond) | ||
// count every second | ||
const count = flyd.scan(n => n + 1, 0, everySecond) | ||
flyd.map(c => console.log(c), count) // 0 ... 1 ... 2 ... 3 ... 4 ... 5 | ||
``` |
# flyd-filter | ||
Filter function for Flyd. | ||
# Usage | ||
Using a predicate function, select only the elements of a stream that the predicate function finds truthy. | ||
__Graph__ | ||
``` | ||
a: {1--2--3--4--5--} | ||
filter(isEven, a): {---2-----4-----} | ||
``` | ||
__Signature__ | ||
`(a -> Boolean) -> Stream a -> Stream a` | ||
__Usage__ | ||
```javascript | ||
var numbers = flyd.stream(); | ||
var largeNumbers = filter(over5, numbers); | ||
flyd.map(function(n) { | ||
// called with 6, 7 and 10 | ||
}, largeNumbers); | ||
const filter = require('flyd/module/filter') | ||
const numbers = flyd.stream() | ||
const largeNumbers = filter(n => n > 5, numbers) | ||
// Collect large numbers into an array | ||
const collect = flyd.scan((ls, n) => ls.concat(n), [], largeNumbers) | ||
numbers(2)(6)(5)(3)(7)(10)(5); | ||
collect() // [6, 7, 10] | ||
``` |
@@ -21,3 +21,3 @@ var flyd = require('../../lib'); | ||
*/ | ||
module.exports = function(f, s) { | ||
module.exports = flyd.curryN(2, function(f, s) { | ||
// Internal state to end flat map stream | ||
@@ -48,2 +48,2 @@ var flatEnd = flyd.stream(1); | ||
return flatStream; | ||
}; | ||
}); |
# flyd-flatmap | ||
flatMap function for Flyd. | ||
# Usage | ||
Flatten a stream of streams into a single stream of values. This is useful for | ||
composing nested streams together to get a single stream of the final result | ||
values. | ||
__Graph__ | ||
``` | ||
a: {---.------.----} | ||
{a-b} {a-b} | ||
flatMap(a): {---a-b----a-b--} | ||
``` | ||
__Signature__ | ||
`(a -> Stream b) -> Stream a -> Stream b` | ||
__Usage__ | ||
``` | ||
const flatMap = require('flyd/module/flatmap') | ||
const request = require('flyd-ajax') | ||
// Form submit events | ||
const submit = flyd.stream() | ||
// Every time the form is submitted, we make an ajax request | ||
// We get a flattened stream of the ajax responses | ||
const responses = flatMap( | ||
()=> request({method: 'get', url: 'http://spacejam.com'}).load | ||
, submit) | ||
submit(true) | ||
flyd.map(resp => console.log(resp), responses) | ||
// {body: "SpaceJam...", status: 200, ...} | ||
const R = require('ramda') | ||
const click$ = flyd.stream() | ||
// Once a user makes a click, we want to make three different ajax requests. | ||
// But, each ajax request must happen in sequence, and we want a stream of the final result value | ||
const response$ = R.compose( | ||
flatMap(requestStream3) | ||
, flatMap(requestStream2) | ||
, flatMap(requestStream1) | ||
)(click$) | ||
// response$ will be a stream of final ajax responses from requestStream3 | ||
// requestStream1 will trigger on every click | ||
// requestStream2 will trigger when requestStream1 is completed | ||
// requestStream3 will trigger when requestStream2 is completed | ||
// finally, response$ will have all the values of the result of requestStream3 | ||
``` | ||
# flyd-forwardto | ||
Forward values from one stream into another existing stream. | ||
Create a new stream that passes all values through a function and forwards them | ||
to a target stream. | ||
This function is inspired by the [Elm | ||
function](http://package.elm-lang.org/packages/elm-lang/core/2.0.1/Signal#forwardTo) | ||
of the same name. | ||
__Graph__ | ||
``` | ||
a: {1---2---3---} | ||
forwardTo(a, parseInt): {--2---3---2-} | ||
flyd.map(square, a): {1-4-4-9-9-4-} | ||
``` | ||
__Signature__ | ||
@@ -16,14 +23,21 @@ | ||
```javascript | ||
// We create a stream of numbers | ||
var numbers = flyd.stream(); | ||
// And another stream that squares the numbers | ||
var squaredNumbers = flyd.map(function(n) { return n*n; }, numbers); | ||
// Add a logger just for show | ||
flyd.map(function(n) { console.log(n); }, squaredNumbers); | ||
// For some reason we have a lot of string of numbers, but we can't | ||
// send them down the `numbers` stream. That would wreck havoc in the | ||
// squaring function. `forwardTo` to the resque! | ||
var stringNumbers = forwardTo(numbers, parseInt); | ||
stringNumbers('7'); // `49` is logged | ||
const forwardTo = require('flyd/module/forwardto') | ||
const R = require('ramda') | ||
// A stream of numbers | ||
const numbers = flyd.stream() | ||
// Another stream that squares the numbers | ||
const squaredNumbers = flyd.map(R.square, numbers) | ||
// A stream of numbers as strings | ||
// we want to convert them to ints and forward them into the numbers stream above: | ||
const stringNumbers = forwardTo(numbers, parseInt) | ||
stringNumbers('7') | ||
squaredNumbers() // -> 49 | ||
numbers(4) | ||
squaredNumbers() // -> 16 | ||
stringNumbers('9') | ||
squaredNumbers() // -> 81 | ||
``` | ||
# flyd-inlast | ||
Creates a stream with emits a list of all values from the source stream that where emitted in a specified duration. | ||
Creates a stream that emits an array of all values from a source stream which where emitted in a specified duration. | ||
Using a duration `ms`, it will collect all values that are emitted within an `ms` time period, buffering them into an array. | ||
"Get a list of values that were emitted from a stream __in the last__ n milliseconds" | ||
Also see [aftersilence](/module/aftersilence). | ||
__Graph__ | ||
``` | ||
(each tick is 10ms) | ||
a: {--2----3--4-----} | ||
inlast(40, a): {--.----.--.-----} | ||
[2] [3][3,4] | ||
``` | ||
__Signature__ | ||
``` | ||
Integer -> Stream a -> Stream [a] | ||
``` | ||
__Usage__ | ||
```js | ||
const inlast = require('flyd/module/inlast') | ||
const s = flyd.stream() | ||
const in = inlast(100, s) | ||
in() // -> [] | ||
s(1)(2)(3); in() // -> [1,2,3] | ||
// emit a first value, wait 200ms, then emit a second value. | ||
// in stream will only emit the second most recent value, discarding the older one. | ||
s(1) | ||
setTimeout(()=> { | ||
s(2) | ||
in() // -> [2] | ||
}, 200) | ||
``` |
# flyd-keepwhen | ||
keepWhen function for Flyd. | ||
Keeps values from the second stream when the first stream is true (true as in | ||
`=== true`). | ||
Keeps values from the second stream when the first stream is true (truth is tested with `!== false`). | ||
# Usage | ||
__Graph__ | ||
``` | ||
var closeDropdown = keepWhen(popupOpen, backgroundClicked); | ||
flyd.map(closeDropdown, function() { | ||
// Do stuff. | ||
}); | ||
a: {-t---f---t----} | ||
b: {---1---2---3--} | ||
keepWhen(a, b): {---1-------3--} | ||
``` | ||
__Signature__ | ||
`Stream Boolean -> Stream a -> Stream a` | ||
__Usage__ | ||
``` | ||
const keepWhen = require('flyd/module/keepwhen') | ||
const source = flyd.stream() | ||
const test = flyd.stream() | ||
const result = keepWhen(test, source) | ||
// when stream must !== false | ||
source(1) | ||
result() // -> 1 | ||
source(2) | ||
result() // -> 2 | ||
test(true) | ||
source(3) | ||
result() // -> 3 | ||
test(false) | ||
source(4) | ||
result() // -> 3 | ||
test('hi') | ||
source(5) | ||
result() // -> 5 | ||
// null or undefined are considered truthy for source | ||
test(null) | ||
source(6) | ||
result() // -> 6 | ||
// only false will prevent values from emitting | ||
test(false) | ||
source(7) | ||
result() // -> 6 | ||
``` |
# flyd-lift | ||
Lift function for [Flyd](https://github.com/paldepind/flyd). | ||
# Usage | ||
Merge the latest values from multiple streams into a single stream using a function. | ||
Emits a new value every time any source stream has a new value. | ||
__Graph__ | ||
``` | ||
a: {---1----2----} | ||
b: {-----1----2--} | ||
lift(add, a, b): {-----2--3-4--} | ||
``` | ||
__Signature__ | ||
`( ((a, b, ...) -> c), (Stream a, Stream b, ...) ) -> Stream c` | ||
__Usage__ | ||
```javascript | ||
var addThree = function(a, b, c) { | ||
return a + b + c; | ||
}; | ||
const lift = require('flyd/module/lift') | ||
var n1 = stream(1), | ||
n2 = stream(4), | ||
n3 = stream(9); | ||
const n1 = flyd.stream(1) | ||
const n2 = flyd.stream(4) | ||
const n3 = flyd.stream(9) | ||
var sum = lift(addThree, n1, n2, n3); | ||
const addThree = (a, b, c) => a + b + c | ||
const sum = lift(addThree, n1, n2, n3) | ||
sum(); // 14 | ||
sum() // 14 | ||
n2(5) | ||
sum() // 15 | ||
``` |
# flyd-mergeall | ||
Flyd module for merging several streams into one. | ||
__Graph__ | ||
``` | ||
a: {---1---2---3---} | ||
b: {--1-----2-----3} | ||
mergeAll([a,b]): {--11---22--3--3} | ||
``` | ||
__Signature__ | ||
@@ -11,10 +19,15 @@ | ||
```javascript | ||
var s1 = flyd.stream(); | ||
var s2 = flyd.stream(); | ||
var s3 = flyd.stream(); | ||
var merged = mergeAll([s1, s2, s3]); | ||
s1(1); | ||
s2(2); | ||
console.log(merged()); // logs 2 | ||
const mergeAll = require('flyd/module/mergeall') | ||
const s1 = flyd.stream() | ||
const s2 = flyd.stream() | ||
const s3 = flyd.stream() | ||
var merged = mergeAll([s1, s2, s3]) | ||
s1(1) | ||
merged() // 1 | ||
s2(2) | ||
merged() // 2 | ||
s3(3) | ||
merged() // 3 | ||
``` | ||
# flyd-obj | ||
Functions for working with Flyd stream in objects. | ||
- Convert object values from plain values into streams with __streamProps__ | ||
- Convert object values from streams into plain values with __extractProps__ | ||
- Convert a plain object with stream values into a single stream of plain objects with __stream__ | ||
These functions perform these conversions recursively in nested objects. | ||
## streamProps(obj) | ||
Given an object of normal values, turn every value into a stream. It will | ||
stream nested properties recursively. | ||
__Signature__ | ||
`Object a -> Object (Stream a)` | ||
__Usage__ | ||
```js | ||
const flydObj = require('flyd/module/object') | ||
const obj = {x: 1, y: {z: 1}} | ||
const objOfStreams = flydObj.streamProps(obj) | ||
objOfStreams.x() // 1 | ||
objOfStreams.y.z() // 2 | ||
``` | ||
## extractProps(obj) | ||
Given an object that contains streams for values, extract everything into an | ||
object of regular values. It will extract nested properties recursively. | ||
__Signature__ | ||
`Object (Stream a) -> Object a` | ||
__Usage__ | ||
```js | ||
const objOfStreams = {x: flyd.stream(1), y: {z: flyd.stream(2)}} | ||
const obj = flydObj.extractProps(objOfStreams) | ||
//obj is {x: 1, y: {z: 2}} | ||
``` | ||
## stream(obj) | ||
Given an object containing streams, combine all the streams into a single stream of plain objects. | ||
__Signature__ | ||
`Object (Stream a) -> Stream (Object a)` | ||
__Usage__ | ||
```js | ||
const click = flyd.stream() | ||
const message = flyd.map(() => 'Hello world!', state.click) | ||
const state = {click, message} | ||
const stateStream = flydObj.stream(state) | ||
stateStream() // {click: undefined, message: undefined} | ||
click(true) | ||
stateStream() // {click: true, message: 'Hello World!'} | ||
``` | ||
# flyd-previous | ||
previous function for Flyd. | ||
Returns a stream that is always one value behind the original stream. | ||
# Usage | ||
__Graph__ | ||
``` | ||
var previousState = previous(state); | ||
a: {---1---2---3---4} | ||
previous(a): {-------1---2---3} | ||
``` | ||
__Signature__ | ||
`Stream a -> Stream a` | ||
__Usage__ | ||
``` | ||
const previous = require('flyd/module/previous') | ||
const s = flyd.stream() | ||
const prev = previous(s) | ||
s(1) | ||
prev() // undefined | ||
s(2) | ||
prev() // 1 | ||
s(3) | ||
prev() // 2 | ||
``` |
# flyd-sampleon | ||
sampleOn for Flyd. | ||
Samples from the second stream every time an event occurs on the first | ||
Sample from the second stream every time an event occurs on the first | ||
stream. | ||
__Graph__ | ||
``` | ||
a: {--t--t--t-------t} | ||
b: {---1---2---3---4-} | ||
sampleOn(a, b): {-----1--2-------4} | ||
``` | ||
__Signature__ | ||
@@ -14,7 +21,19 @@ | ||
```javascript | ||
// Assume `sendBtnClicked` emits whenever a send button is pressed and | ||
// `messageText` is a stream of the current content of an input field. | ||
// Then `sendMessage` emits the content of the text field whenever the button | ||
// is pressed. | ||
var sendMessage = sampleOn(sendBtnClicked, messageText); | ||
const sampleOn = require('flyd/module/sampleon') | ||
const sample = flyd.stream() | ||
const on = flyd.stream() | ||
const result = sampleOn(on, sample) | ||
on(true) | ||
result() // undefined | ||
sample(1) | ||
on(true) | ||
result() // 1 | ||
sample(2) | ||
sample(3) | ||
on(true) | ||
result() // 3 | ||
``` |
# flyd-scanmerge | ||
Flyd module for conveniently merging and scanning several streams into one. | ||
scanmerge takes an array of pairs of streams and scan functions. It merges all those streams using the given functions into a single stream. | ||
A common use case is to take many UI event streams, pair each one with an updater function, so they all combine into a single UI state object. | ||
__Graph__ | ||
``` | ||
n1: {2---3----2---3} | ||
n2: {--2---2----1--} | ||
scanMerge([[n1, add], [n2, sub]], 0): {2-0-3-1--3-2-5} | ||
``` | ||
__Signature__ | ||
@@ -8,29 +20,28 @@ | ||
__Example__ | ||
__Usage__ | ||
```javascript | ||
var add = flyd.stream(0); | ||
var sub = flyd.stream(0); | ||
var mult = flyd.stream(1); | ||
var res = scanMerge([ | ||
[add, function(sum, n) { return sum + n; }], | ||
[sub, function(sum, n) { return sum - n; }], | ||
[mult, function(sum, n) { return sum * n; }], | ||
], 0); | ||
add(5); sub(8); sub(4); add(12); mult(3); | ||
console.log(res); // logs 15 | ||
const scanMerge = require('flyd/module/scanmerge') | ||
const add = flyd.stream(0) | ||
const sub = flyd.stream(0) | ||
const mult = flyd.stream(1) | ||
const res = scanMerge([ | ||
[add, function(sum, n) { return sum + n; }] | ||
, [sub, function(sum, n) { return sum - n; }] | ||
, [mult, function(sum, n) { return sum * n; }] | ||
], 0) | ||
add(5); sub(8); sub(4); add(12); mult(3) | ||
res() // 15 | ||
``` | ||
```javascript | ||
var addItem = flyd.stream(); | ||
var rmItem = flyd.stream(); | ||
var items = scanMerge([ | ||
[addItem, function(list, item) { return list.concat([item]); }], | ||
[rmItem, function(list, item) { | ||
return list.filter(function(elm) { return elm !== item; }); | ||
}] | ||
], []); | ||
addItem(1)(2)(3)(4)(5); | ||
rmItem(3); | ||
console.log(items()); logs [1, 2, 4, 5] | ||
const append = flyd.stream() | ||
const remove = flyd.stream() | ||
const items = scanMerge([ | ||
[append, (list, elem) => list.concat(elem)], | ||
, [remove, (list, item) => list.filter(elm => elm !== item)] | ||
], []) | ||
append(1)(2)(3)(4)(5) | ||
remove(3) | ||
items() // [1,2,4,5] | ||
``` |
@@ -6,2 +6,10 @@ # flyd-switchlatest | ||
__Graph__ | ||
``` | ||
a: {--.----.----} | ||
{ab} {a-b} | ||
switchLatest(a): {--ab---a-b--} | ||
``` | ||
__Signature__ | ||
@@ -14,8 +22,12 @@ | ||
```javascript | ||
var chatrooms = flyd.stream(); | ||
var messagesStreams = flyd.map(function(id) { | ||
return createMessageStream(id); | ||
}, chatrooms); | ||
var currentMessages = switchLatest(messagesStreams); | ||
const switchLatest = require('flyd/module/switchlatest') | ||
const chatrooms = flyd.stream() | ||
// For each chatroom on the chatrooms stream, create a stream of chat messages. | ||
// This gives us a series of streams nested within a parent stream. | ||
const messages = flyd.map(createMessageStream, chatrooms) | ||
// Create a single, unnested stream of chat messages | ||
const currentMessages = switchLatest(messagesStreams) | ||
``` | ||
var flyd = require('../../lib'); | ||
module.exports = function(src, term) { | ||
module.exports = flyd.curryN(2, function(src, term) { | ||
return flyd.endsOn(flyd.merge(term, src.end), flyd.combine(function(src, self) { | ||
self(src()); | ||
}, [src])); | ||
}; | ||
}); |
# flyd-takeuntil | ||
Emit values from a stream until a second stream emits a value. | ||
__Graph__ | ||
``` | ||
a: {---1---2---3---4} | ||
b: {---------x------} | ||
takeUntil(a, b): {---1---2--------} | ||
``` | ||
__Signature__ | ||
@@ -8,7 +16,20 @@ | ||
__Example__ | ||
__Usage__ | ||
```javascript | ||
// `s` will emit all values on `source` until `endStream` emits a value or ends | ||
var s = takeUntil(source, endStream); | ||
const takeUntil = require('flyd/module/takeuntil') | ||
const source = flyd.stream() | ||
const end = flyd.stream() | ||
const result = takeUntil(source, end) | ||
source(1)(2) | ||
result() // 2 | ||
source(3) | ||
result() // 3 | ||
end(true) | ||
result() // 3 | ||
source(4)(5) | ||
result() // 3 | ||
``` |
{ | ||
"name": "flyd", | ||
"version": "0.2.2", | ||
"version": "0.2.3", | ||
"description": "The less is more, modular, functional reactive programming library", | ||
@@ -18,7 +18,8 @@ "main": "lib/index.js", | ||
"coveralls": "^2.11.2", | ||
"eslint": "^1.10.3", | ||
"eslint": "^2.7.0", | ||
"istanbul": "^0.3.15", | ||
"mocha": "^2.2.1", | ||
"mocha-lcov-reporter": "0.0.2", | ||
"transducers.js": "0.3.x" | ||
"transducers.js": "0.3.x", | ||
"uglifyjs": "^2.4.10" | ||
}, | ||
@@ -31,3 +32,3 @@ "scripts": { | ||
"post-to-coveralls-io": "istanbul cover _mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | coveralls && rm -rf ./coverage", | ||
"build": "browserify -s flyd lib/index.js > flyd.js" | ||
"build": "browserify -s flyd lib/index.js > flyd.js && uglifyjs flyd.js -o flyd.min.js" | ||
}, | ||
@@ -34,0 +35,0 @@ "repository": { |
@@ -618,27 +618,32 @@ # Flyd | ||
If you're created a module for Flyd open an issue or send a pull request and it | ||
If you've created a module for Flyd, open an issue or send a pull request, and it | ||
will be added to this list. | ||
* [flyd/module/filter](module/filter) – Filter values from stream based on predicate. | ||
* [flyd/module/lift](module/lift) – Maps a function taking _n_ parameters over _n_ streams. | ||
* [flyd/module/flatmap](module/flatmap) – Maps a function over a stream of streams and flattens the result to a single stream. | ||
* [flyd/module/switchlatest](module/switchlatest) – Flattens a stream of streams. The result stream reflects changes from the last stream only. | ||
* [flyd/module/keepwhen](module/keepwhen) – Keep values from one stream only when another stream is true. | ||
* [flyd/module/obj](module/obj) – Functions for working with stream in objects. | ||
* [flyd/module/sampleon](module/sampleon) – Samples from a stream every time an event occurs on another stream. | ||
* [flyd/module/scanmerge](module/scanmerge) – Merge and scan several streams into one. | ||
* [flyd/module/mergeall](module/mergeall) – Merge a list of streams. | ||
* [flyd/module/takeuntil](module/takeuntil) – Emit values from a stream until a second stream emits a value. | ||
* [flyd/module/forwardto](module/forwardto) – Create a new stream that passes all values through a function and forwards them to a target stream. | ||
* [flyd-cacheUntil](https://github.com/ThomWright/flyd-cacheUntil) – Cache a stream's output until triggered by another stream. | ||
* [flyd/module/droprepeats](module/droprepeats) – Drop repeated values from a stream. | ||
* [flyd-keyboard](https://github.com/raine/flyd-keyboard) – Keyboard events as streams. | ||
* [flyd-glob](https://github.com/StreetStrider/flyd-glob) – File glob and watch for Flyd. | ||
* Time related | ||
* [flyd/module/every](module/every) – Takes a number of milliseconds t and creates a stream of the current time updated every t. | ||
* [flyd/module/aftersilence](module/aftersilence) – Buffers values from a source stream in an array and emits it after a specified duration of silence from the source stream. | ||
* [flyd/module/inlast](module/inlast) – Creates a stream that emits a list of all values from the source stream that were emitted in a specified duration. | ||
* [flyd-onAnimationFrame](https://github.com/ThomWright/flyd-onAnimationFrame) – Emits values from a source stream on successive animation frames. | ||
* [flyd-timeInterval](https://github.com/ThomWright/flyd-timeInterval) – Records the time interval between consecutive values emitted from a stream. | ||
Modules listed with names in the format `flyd/module/filter` are builtin to the main `flyd` module and can be required with `require('flyd/module/filter')`. Other modules must be installed first with npm. | ||
| Module | Description | | ||
| --- | --- | --- | | ||
| [flyd/module/**filter**](module/filter) | Filter values from stream based on predicate. | | ||
| [flyd/module/**lift**](module/lift) | Maps a function taking _n_ parameters over _n_ streams. | | ||
| [flyd/module/**flatmap**](module/flatmap) | Maps a function over a stream of streams and flattens the result to a single stream. | | ||
| [flyd/module/**switchlatest**](module/switchlatest) | Flattens a stream of streams. The result stream reflects changes from the last stream only. | | ||
| [flyd/module/**keepwhen**](module/keepwhen) | Keep values from one stream only when another stream is true. | | ||
| [flyd/module/**obj**](module/obj) | Functions for working with stream in objects. | | ||
| [flyd/module/**sampleon**](module/sampleon) | Samples from a stream every time an event occurs on another stream. | | ||
| [flyd/module/**scanmerge**](module/scanmerge) | Merge and scan several streams into one. | | ||
| [flyd/module/**mergeall**](module/mergeall) | Merge a list of streams. | | ||
| [flyd/module/**takeuntil**](module/takeuntil) | Emit values from a stream until a second stream emits a value. | | ||
| [flyd/module/**forwardto**](module/forwardto) | Create a new stream that passes all values through a function and forwards them to a target stream. | | ||
| [flyd/module/**droprepeats**](module/droprepeats) | Drop repeated values from a stream. | | ||
| [**flyd-cacheUntil**](https://github.com/ThomWright/flyd-cacheUntil) | Cache a stream's output until triggered by another stream. | | ||
| [**flyd-keyboard**](https://github.com/raine/flyd-keyboard) | Keyboard events as streams. | | ||
| [**flyd-glob**](https://github.com/StreetStrider/flyd-glob) | File glob and watch for Flyd. | | ||
| [**flyd-skip**](https://github.com/littlehaker/flyd-skip) | Skip function for flyd. | | ||
| **Time related** | | ||
| [flyd/module/**every**](module/every) | Takes a number of milliseconds t and creates a stream of the current time updated every t. | | ||
| [flyd/module/**aftersilence**](module/aftersilence) | Buffers values from a source stream in an array and emits it after a specified duration of silence from the source stream. | | ||
| [flyd/module/**inlast**](module/inlast) | Creates a stream that emits a list of all values from the source stream that were emitted in a specified duration. | | ||
| [**flyd-onAnimationFrame**](https://github.com/ThomWright/flyd-onAnimationFrame) | Emits values from a source stream on successive animation frames. | | ||
| [**flyd-timeInterval**](https://github.com/ThomWright/flyd-timeInterval) | Records the time interval between consecutive values emitted from a stream. | | ||
## Misc | ||
@@ -645,0 +650,0 @@ |
@@ -31,2 +31,18 @@ var assert = require('assert'); | ||
}); | ||
it('can works with JSON.stringify', function() { | ||
var obj = { | ||
num: stream(23), | ||
str: stream('string'), | ||
obj: stream({is_object: true}) | ||
}; | ||
var expected_outcome = { | ||
num: 23, | ||
str: 'string', | ||
obj: { | ||
is_object: true | ||
} | ||
}; | ||
var jsonObject = JSON.parse(JSON.stringify(obj)); | ||
assert.deepEqual(jsonObject, expected_outcome); | ||
}); | ||
it("let's explicit `undefined` flow down streams", function() { | ||
@@ -33,0 +49,0 @@ var result = []; |
Sorry, the diff of this file is not supported yet
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
390751
100
9769
724
10
2
16