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

Add iterator helpers to RangeIterable #308

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
27 changes: 27 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2018,6 +2018,33 @@ describe('lmdb-js', function () {
all.should.deep.equal([2, { error: 'Error: test' }, 6, 4, 5, 6]);
expect(finished).to.be.equal(1);
});
it('take', function () {
const ri = new RangeIterable([1, 2, 3, 4]);
ri.take(2).asArray.should.deep.equal([1, 2]);
});
it('drop', function () {
const ri = new RangeIterable([1, 2, 3, 4]);
ri.drop(2).asArray.should.deep.equal([3, 4]);
});
it('reduce', function () {
const ri = new RangeIterable([1, 2, 3, 4]);
ri.reduce((sum, value) => sum + value, 0).should.equal(10);
});
it('some', function () {
const ri = new RangeIterable([1, 2, 3, 4]);
ri.some((value) => value > 2).should.equal(true);
ri.some((value) => value > 4).should.equal(false);
});
it('every', function () {
const ri = new RangeIterable([1, 2, 3, 4]);
ri.every((value) => value > 2).should.equal(false);
ri.every((value) => value > 0).should.equal(true);
});
it('find', function () {
const ri = new RangeIterable([1, 2, 3, 4]);
ri.find((value) => value > 2).should.equal(3);
should.equal(ri.find((value) => value > 4), undefined);
})
});
describe('mixed keys', function () {
this.timeout(10000);
Expand Down
63 changes: 63 additions & 0 deletions util/RangeIterable.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,5 +427,68 @@ export class RangeIterable {
if (index-- === 0) return entry;
}
}

take(limit) {
let iterator = (this.iterator = this.iterate());
let iterable = new RangeIterable();
iterable.iterate = function * () {
for (let i = 0; i < limit; i++) {
yield iterator.next().value;
}
}
return iterable
}

drop(limit) {
let iterator = (this.iterator = this.iterate());
let iterable = new RangeIterable();
iterable.iterate = function * () {
for (let i = 0; i < limit; i++) {
iterator.next();
}
let result;
while ((result = iterator.next()).done !== true) {
yield result.value;
}
}
return iterable;
}

reduce(reducer, initialValue) {
let result = initialValue;
let iterator = (this.iterator = this.iterate());
let iteratorResult;
while ((iteratorResult = iterator.next()).done !== true) {
result = reducer(result, iteratorResult.value)
}
return result;
}

some(callback) {
let iterator = (this.iterator = this.iterate());
let result;
while ((result = iterator.next()).done !== true) {
if (callback(result.value)) { return true }
}
return false
}

every(callback) {
let iterator = (this.iterator = this.iterate());
let result;
while ((result = iterator.next()).done !== true) {
if (!callback(result.value)) { return false }
}
return true
}

find(callback) {
let iterator = (this.iterator = this.iterate());
let result;
while ((result = iterator.next()).done !== true) {
if (callback(result.value)) { return result.value }
}
return undefined
}
}
RangeIterable.prototype.DONE = DONE;
Loading