-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from jwoudenberg/master
Added 'previous' and 'diff' modules
- Loading branch information
Showing
7 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[*] | ||
indent_style = space | ||
indent_size = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# flyd-diff | ||
diff function for Flyd. | ||
|
||
Like map, but the mapping function gets the previous and the current value of the stream. | ||
|
||
# Usage | ||
``` | ||
var velocity = flyd.stream(0); | ||
var acceleration = diff(function (previous, current) { | ||
return current - previous; | ||
}, velocity); | ||
velocity(2)(5)(1); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
var flyd = require('flyd'); | ||
|
||
var previous = require('flyd/module/previous'); | ||
|
||
module.exports = function (diffFunc, s) { | ||
var prevS = previous(s); | ||
return flyd.stream([s, prevS], function (self) { | ||
return diffFunc(prevS(), s()); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
var assert = require('assert'); | ||
var flyd = require('flyd'); | ||
var stream = flyd.stream; | ||
|
||
var diff = require('../diff.js'); | ||
|
||
describe('diff', function() { | ||
it('calls the diff function with the previous and new value', function() { | ||
var s = stream(1); | ||
var returnArguments = function () { | ||
return Array.prototype.slice.call(arguments); | ||
} | ||
var d = diff(returnArguments, s); | ||
s(1)(2)(3); | ||
assert.deepEqual(d(), [2, 3]); | ||
}); | ||
|
||
it('starts streaming after the second value is pushed into the source stream', function() { | ||
var s = stream(); | ||
var calls = 0; | ||
flyd.on(); | ||
var d = diff(function () { calls += 1; }, s) | ||
s(1); | ||
assert.equal(calls, 0); | ||
s(2); | ||
assert.equal(calls, 1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# flyd-previous | ||
previous function for Flyd. | ||
|
||
Returns a stream that is always one value behind the original stream. | ||
|
||
# Usage | ||
``` | ||
var previousState = previous(state); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
var flyd = require('flyd'); | ||
|
||
module.exports = function (s) { | ||
var previousValue; | ||
return flyd.stream([s], skipFirstCall(function (self) { | ||
self(previousValue); | ||
previousValue = s(); | ||
})); | ||
}; | ||
|
||
function skipFirstCall (func) { | ||
var functionToCall = function () { | ||
functionToCall = func; | ||
} | ||
return function () { | ||
return functionToCall.apply(this, arguments); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
var assert = require('assert'); | ||
var flyd = require('flyd'); | ||
var stream = flyd.stream; | ||
|
||
var previous = require('../previous.js'); | ||
|
||
describe('previous', function() { | ||
it('is always one value behind the source stream', function() { | ||
var s = stream(1); | ||
var p = previous(s); | ||
s(2)(3)(4); | ||
assert.equal(p(), 3); | ||
}); | ||
|
||
it('starts streaming after the second value is pushed into the source stream', function() { | ||
var s = stream(); | ||
var p = previous(s); | ||
var calls = 0; | ||
flyd.on(function () { calls += 1; }, p); | ||
s(1); | ||
assert.equal(calls, 0); | ||
s(2); | ||
assert.equal(calls, 1); | ||
}); | ||
}); |