Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix iterator protocol support + npm build scripts #44

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ var transform = compose(
);
```

`compose` is a provided function that simply turns `compose(f, g)` into `x => f(g(x))`. You use it to build up transformations. The above transformation would always run the map and filter **only twice** because only two items are needed, and it short-circuits once it gets two items. Again, this is done without laziness, read more [here](http://jlongster.com/Transducers.js--A-JavaScript-Library-for-Transformation-of-Data).
`compose` is a provided function that simply turns `compose(f, g)` into `x => f(g(x))`. You use it to build up transformations. The above transformation would always run the map and filter **only twice** becaue only two items are needed, and it short-circuits once it gets two items. Again, this is done without laziness, read more [here](http://jlongster.com/Transducers.js--A-JavaScript-Library-for-Transformation-of-Data).

There are also 2 transducers available for taking collections and "catting" them into the transformation stream:

Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
"version": "0.3.2",
"author": "James Long <[email protected]>",
"main": "./transducers.js",
"scripts": {
"test": "mocha ./tests/tests.js",
"build": "webpack",
"build:uglify": "NODE_ENV=production webpack",
"prepublish": "npm -s run build && npm test"
},
"repository": {
"type": "git",
"url": "https://github.com/jlongster/transducers.js.git"
Expand Down
36 changes: 34 additions & 2 deletions tests/tests.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
var expect = require('expect.js');
var Immutable = require('immutable');
var t = require('../transducers');
var { reduce, transformer, toArray, toObj, toIter, iterate, push, merge, empty,
var { reduce, transformer, toArray, toObj, toIter, iterator, push, merge, empty,
transduce, seq, into, compose, map, filter, remove,
cat, mapcat, keep, dedupe, take, takeWhile,
drop, dropWhile, partition, partitionBy,
interpose, repeat, takeNth } = t;

var context = { num: 5 };

var iterProtocol = Symbol && typeof Symbol.iterator !== 'undefined' ? Symbol.iterator : '@@iterator';
// utility

function first(x) {
Expand Down Expand Up @@ -413,7 +414,7 @@ describe('', () => {
{ foo: 2, bar: 3 });
});

it('iter should work', function() {
it('toIter should work', function() {
var nums = {
i: 0,
next: function() {
Expand All @@ -425,8 +426,39 @@ describe('', () => {
};

var lt = toIter(nums, map(x => x * 2));

expect(lt[iterProtocol]()).to.equal(lt);
expect(lt instanceof t.LazyTransformer).to.be.ok();
expect(toArray(lt, take(5)),
[0, 2, 4, 6, 8]);

var iter = toIter(nums);
expect(iter[iterProtocol]()).to.equal(iter);
expect(toArray(iter, take(5)),
[0, 1, 2, 3, 4]);
});

it('iterator should work', function() {
var obj = {a: 1, b: 2, c: 3};
var nums = [1, 2, 3];
var broken = {
i: 1,
next: function() {
return {
value: this.i++,
done: this.i <= 3
}
}
};

var [objIter, numsIter, brokenIter] = [obj, nums, broken].map(iterator);

expect(objIter[iterProtocol](), objIter);
expect(numsIter[iterProtocol](), numsIter);
expect(brokenIter[iterProtocol](), brokenIter);

expect(toObj(objIter), {a: 1, b: 2, c: 3});
expect(toArray(numsIter), [1, 2, 3]);
expect(toArray(brokenIter), [1, 2, 3]);
});
});
24 changes: 19 additions & 5 deletions transducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ function iterator(coll) {
// Basic duck typing to accept an ill-formed iterator that doesn't
// conform to the iterator protocol (all iterators should have the
// @@iterator method and return themselves, but some engines don't
// have that on generators like older v8)
return coll;
// have that on generators like older v8) and wrap it.

return coll[protocols.iterator] ? coll : new WrappedIterator(coll);
}
else if(isArray(coll)) {
return new ArrayIterator(coll);
Expand All @@ -46,6 +47,15 @@ function iterator(coll) {
}
}

function WrappedIterator(iter) {
this.wrapped = iter;
}

WrappedIterator.prototype.next = function() {
return this.wrapped.next.apply(this.wrapped, arguments);
};
WrappedIterator.prototype[protocols.iterator] = returnThis;

function ArrayIterator(arr) {
this.arr = arr;
this.index = 0;
Expand All @@ -62,6 +72,7 @@ ArrayIterator.prototype.next = function() {
done: true
}
};
ArrayIterator.prototype[protocols.iterator] = returnThis;

function ObjectIterator(obj) {
this.obj = obj;
Expand All @@ -81,6 +92,7 @@ ObjectIterator.prototype.next = function() {
done: true
}
};
ObjectIterator.prototype[protocols.iterator] = returnThis;

// helpers

Expand Down Expand Up @@ -895,9 +907,7 @@ function LazyTransformer(xform, coll) {
this.stepper = new Stepper(xform, iterator(coll));
}

LazyTransformer.prototype[protocols.iterator] = function() {
return this;
}
LazyTransformer.prototype[protocols.iterator] = returnThis;

LazyTransformer.prototype.next = function() {
this['@@transducer/step']();
Expand Down Expand Up @@ -929,6 +939,10 @@ function range(n) {
return arr;
}

function returnThis() {
return this;
}

module.exports = {
reduce: reduce,
transformer: transformer,
Expand Down