Skip to content

Commit

Permalink
feat: add extra capabitlity to extends on fly generation
Browse files Browse the repository at this point in the history
  • Loading branch information
rubeniskov committed Nov 24, 2020
1 parent e308ad3 commit db5ebbf
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
14 changes: 11 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ const {
*/

/**
* Function iterator, [see](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator/next)
* [examples](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators#Advanced_generators)
* @callback TraverseIterator
* @param extra a object or array to extends the current iteration
* @returns {TraverseIteratorResult}
*/

Expand Down Expand Up @@ -223,11 +226,15 @@ const traverseJson = (obj, opts) => {

dive(obj);

const next = () => {
let prefix, value;
const next = (extra) => {
if (extra !== undefined) {
dive(extra, prefix);
}
if (cursor < overall.length) {
let entry = overall[cursor];
let entry = overall[cursor] || [];
[prefix, value] = entry;
if (recursive) {
const [prefix, value] = entry || [];
if (isTraversable(value)) {
dive(value, prefix);
if (!nested) {
Expand All @@ -246,6 +253,7 @@ const traverseJson = (obj, opts) => {

return { value: entry, done: false };
}

return { done: true };
};

Expand Down
54 changes: 54 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,60 @@ test('should iterate recursively through the same key', (t) => {
iterateEqual(t, ientries, expected, true);
});

test('should extends on fly the iterator', (t) => {

const recursiveObject = {
nested: 'foo',
};

const expected = [
[ '/nested', 'foo' ],
[ '/nested/nested', 'foo' ],
[ '/nested/nested/nested', 'foo' ],
[ '/nested/nested/nested/nested', 'foo' ],
[ '/nested/nested/nested/nested/nested', 'foo' ],
];

const iteratee = traverseObject(recursiveObject);

for (let i = 0; i < 5; i ++) {
const { value, done } = iteratee(recursiveObject);
t.deepEqual(value, expected[i]);
t.false(done);
}

const { done } = iteratee();
t.true(done);
});

test('should extends on fly the iterator with nested paths', (t) => {

const recursiveObject = {
nested: {
foo: 'bar',
},
};

const expected = [
[ '/nested', { foo: 'bar' } ],
[ '/nested/nested', { foo: 'bar' } ],
[ '/nested/nested/nested', { foo: 'bar' } ],
[ '/nested/nested/nested/nested', { foo: 'bar' } ],
[ '/nested/nested/nested/nested/nested', { foo: 'bar' } ],
];

const iteratee = traverseObject(recursiveObject, { test: '@nested' });

for (let i = 0; i < 5; i ++) {
const { value, done } = iteratee(recursiveObject);
t.deepEqual(value, expected[i]);
t.false(done);
}

const { done } = iteratee();
t.true(done);
});

test('should works as iterable', (t) => {

const { createIterator } = traverseObject;
Expand Down

0 comments on commit db5ebbf

Please sign in to comment.