From 5436d0453b77c8e2d95f9b50426047fc338da0a6 Mon Sep 17 00:00:00 2001 From: Robert Martin Date: Sat, 19 Jan 2013 16:23:39 -0500 Subject: [PATCH 1/3] add 'node_modules/' to be ignored by git --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 1377554..ef5f69b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ *.swp +node_modules/ From 93471b713bc1cd9c9dbf5bf977274e92029b2df7 Mon Sep 17 00:00:00 2001 From: Robert Martin Date: Sat, 19 Jan 2013 16:26:15 -0500 Subject: [PATCH 2/3] simplify method and improve testing for head() The head() method was implemented so that it does NOT remove the top value of the list. This refactor makes that more clear, and adds tests to document that behavior. This commit should not change the behavior of head() at all. --- lazy.js | 6 +----- test/head.js | 34 +++++++++++++++++++++------------- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/lazy.js b/lazy.js index 9ff3520..18871e2 100644 --- a/lazy.js +++ b/lazy.js @@ -75,11 +75,7 @@ function Lazy (em, opts) { } self.head = function (f) { - var lazy = newLazy(); - lazy.on(dataName, function g (x) { - f(x) - lazy.removeListener(dataName, g) - }) + self.once(dataName, f); } self.tail = function () { diff --git a/test/head.js b/test/head.js index b275362..b225c11 100644 --- a/test/head.js +++ b/test/head.js @@ -2,25 +2,33 @@ var assert = require('assert'); var Lazy = require('..'); var expresso = expresso; -function range(i, j) { - var r = []; - for (;i Date: Sat, 19 Jan 2013 16:32:08 -0500 Subject: [PATCH 3/3] add pop() with tests. The head() method looks at the top value, but does not remove it from the lazy list. The new method, pop(), will remove the value. lazy.pop(f) == lazy.head(f); lazy.skip(1) --- lazy.js | 5 +++++ readme.txt | 1 + test/pop.js | 27 +++++++++++++++++++++++++++ 3 files changed, 33 insertions(+) create mode 100644 test/pop.js diff --git a/lazy.js b/lazy.js index 18871e2..c2f308a 100644 --- a/lazy.js +++ b/lazy.js @@ -74,6 +74,11 @@ function Lazy (em, opts) { ); } + self.pop = function (f) { + self.head(f); + return self.skip(1); + } + self.head = function (f) { self.once(dataName, f); } diff --git a/readme.txt b/readme.txt index 6952a60..d497cfe 100644 --- a/readme.txt +++ b/readme.txt @@ -153,6 +153,7 @@ Supports the following operations: * lazy.product(f) * lazy.foldr(op, i, f) * lazy.skip(n) + * lazy.pop(f) * lazy.head(f) * lazy.tail(f) * lazy.join(f) diff --git a/test/pop.js b/test/pop.js new file mode 100644 index 0000000..e49a318 --- /dev/null +++ b/test/pop.js @@ -0,0 +1,27 @@ +var assert = require('assert'); +var Lazy = require('..'); +var expresso = expresso; + +exports['pop'] = function () { + var lazy = new Lazy; + var executed = 0; + + var expect = function(n) { + return function(x) { + executed++; + assert.eql(x, n); + } + } + + lazy.pop(expect(77)) + .pop(expect(99)) + .join(expect([11, 55, 33])); + + [77, 99, 11, 55, 33].forEach(function (x) { + lazy.emit('data', x); + }); + lazy.emit('end'); + + assert.equal(executed, 3); +} +