drop-while-iterable
Advanced tools
Comparing version 0.1.0 to 0.2.0
62
index.js
@@ -5,14 +5,2 @@ const arrayOf = require('immutable-array.of') | ||
function dropWhile (p) { | ||
const obj = Object.create(this.constructor.prototype) | ||
obj.ps = push(p, this.ps) | ||
obj.iterable = this.iterable | ||
return obj | ||
} | ||
function DropWhileIterable (iterable) { | ||
this.iterable = iterable | ||
this.ps = arrayOf([]) | ||
} | ||
function init () { | ||
@@ -30,27 +18,35 @@ this.index = 0 | ||
Object.defineProperties(DropWhileIterable.prototype, { | ||
dropWhile: { | ||
value: dropWhile | ||
}, | ||
[Symbol.iterator]: { | ||
value () { | ||
init.call(this) | ||
const self = this | ||
const iterator = this.iterable[Symbol.iterator]() | ||
let status | ||
return { | ||
next () { | ||
while (!(status = iterator.next()).done) { | ||
status = apply.call(self, status.value) | ||
if (status) { | ||
return status | ||
} | ||
} | ||
return {done: true} | ||
function generator () { | ||
init.call(this) | ||
const self = this | ||
const iterator = this.iterable[Symbol.iterator]() | ||
let status | ||
return { | ||
next () { | ||
while (!(status = iterator.next()).done) { | ||
status = apply.call(self, status.value) | ||
if (status) { | ||
return status | ||
} | ||
} | ||
return {done: true} | ||
} | ||
} | ||
}) | ||
} | ||
module.exports = DropWhileIterable | ||
module.exports = { | ||
of (iterable) { | ||
return { | ||
iterable, | ||
ps: arrayOf([]), | ||
[Symbol.iterator]: generator | ||
} | ||
}, | ||
dropWhile (p, dropWhileIterable) { | ||
return { | ||
ps: push(p, dropWhileIterable.ps), | ||
iterable: dropWhileIterable.iterable, | ||
[Symbol.iterator]: generator | ||
} | ||
} | ||
} |
{ | ||
"name": "drop-while-iterable", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "iterable class that provides dropWhile method", | ||
@@ -33,3 +33,3 @@ "main": "index.js", | ||
"coveralls": "^3.0.0", | ||
"eslint": "^4.18.0", | ||
"eslint": "^4.18.2", | ||
"npm-check": "^5.5.2", | ||
@@ -39,3 +39,3 @@ "nyc": "^11.4.1", | ||
"tap-spec": "^4.1.1", | ||
"tape": "^4.8.0" | ||
"tape": "^4.9.0" | ||
}, | ||
@@ -42,0 +42,0 @@ "dependencies": { |
@@ -18,13 +18,13 @@ # drop-while-iterable | ||
``` javascript | ||
const DropWhileIterable = require('drop-while-iterable') | ||
const I = require('drop-while-iterable') | ||
const iterable = new DropWhileIterable(new Set([4, 2, 7, 8, 4, 7])) // (4 2 7 8 4 7) | ||
.dropWhile(e => e % 2 === 0) // (7 8 4 7) | ||
.dropWhile(e => e > 5) // (4 7) | ||
const first = I.of(new Set([4, 2, 7, 8, 4, 7])) // (4 2 7 8 4 7) | ||
const second = I.dropWhile(e => e % 2 === 0, first) // (7 8 4 7) | ||
const third = I.dropWhile(e => e > 5, second) // (4 7) | ||
// converting to array: | ||
[...iterable] // [4 7] | ||
[...third] // [4 7] | ||
// traversing values: | ||
for (const val of iterable) { | ||
for (const val of third) { | ||
// ... | ||
@@ -34,3 +34,3 @@ } | ||
// creating an iterator that traverses the values | ||
let iterator = iterable[Symbol.iterator]() | ||
let iterator = third[Symbol.iterator]() | ||
iterator.next() // {value: 4, done: false} | ||
@@ -48,4 +48,3 @@ iterator.next() // {value: 7, done: false} | ||
new DropWhileIterable(naturals) // (1 2 3 4 5 6 7 8 9...) | ||
.dropWhile(e => e > 5) // (6 7 8 9 10...) | ||
I.dropWhile(e => e > 5, I.of(naturals)) // (6 7 8 9 10...) | ||
``` | ||
@@ -52,0 +51,0 @@ |
50
test.js
@@ -5,2 +5,4 @@ const test = require('tape') | ||
const DropWhileIterable = require('./') | ||
const iterableOf = DropWhileIterable.of | ||
const dropWhile = DropWhileIterable.dropWhile | ||
@@ -18,3 +20,3 @@ const emptySet = new Set() | ||
t.test('empty set', function (st) { | ||
const iterable = new DropWhileIterable(emptySet) | ||
const iterable = iterableOf(emptySet) | ||
st.deepEqual([...iterable], [], | ||
@@ -25,3 +27,3 @@ 'must return an empty iterable') | ||
t.test('non-empty set', function (st) { | ||
const iterable = new DropWhileIterable(setFromOneToFive) | ||
const iterable = iterableOf(setFromOneToFive) | ||
st.deepEqual([...iterable], arrayFromOneToFive, | ||
@@ -33,3 +35,3 @@ 'must return an iterable with the same values') | ||
t.test('empty map', function (st) { | ||
const iterable = new DropWhileIterable(emptyMap) | ||
const iterable = iterableOf(emptyMap) | ||
st.deepEqual([...iterable], [], | ||
@@ -40,3 +42,3 @@ 'must return an empty iterable') | ||
t.test('non-empty string', function (st) { | ||
const iterable = new DropWhileIterable(map) | ||
const iterable = iterableOf(map) | ||
st.deepEqual([...iterable], arrayMap, | ||
@@ -52,4 +54,3 @@ 'must return an iterable with the same values') | ||
t.test('empty array', function (st) { | ||
const iterable = new DropWhileIterable(emptySet) | ||
.dropWhile(() => true) | ||
const iterable = dropWhile(() => true, iterableOf(emptySet)) | ||
st.deepEqual([...iterable], [], | ||
@@ -60,4 +61,3 @@ 'must return an empty iterable') | ||
t.test('dropping some first values matches', function (st) { | ||
const iterable = new DropWhileIterable(setFromOneToFive) | ||
.dropWhile(lowerThanThree) | ||
const iterable = dropWhile(lowerThanThree, iterableOf(setFromOneToFive)) | ||
st.deepEqual([...iterable], arrayFromThreeToFive, | ||
@@ -68,4 +68,3 @@ 'must return and iterable with the first matched values removed') | ||
t.test('dropping all values', function (st) { | ||
const iterable = new DropWhileIterable(setFromOneToFive) | ||
.dropWhile(() => true) | ||
const iterable = dropWhile(() => true, iterableOf(setFromOneToFive)) | ||
st.deepEqual([...iterable], [], | ||
@@ -76,4 +75,3 @@ 'must return empty iterable if all values match') | ||
t.test('dropping zero items', function (st) { | ||
const iterable = new DropWhileIterable(setFromOneToFive) | ||
.dropWhile(e => e > 1) | ||
const iterable = dropWhile(e => e > 1, iterableOf(setFromOneToFive)) | ||
st.deepEqual([...iterable], arrayFromOneToFive, | ||
@@ -84,6 +82,6 @@ 'must not drop any value if the first value of iterable does not match') | ||
t.test('chaining', function (st) { | ||
const iterable = new DropWhileIterable(setFromOneToFive) // (1 2 3 4 5) | ||
.dropWhile(e => e % 2 === 1) // (2 3 4 5) | ||
.dropWhile(e => e % 2 === 0) // (3 4 5) | ||
.dropWhile(e => e % 2 === 1) // (4 5) | ||
const iterable = dropWhile(e => e % 2 === 1, | ||
dropWhile(e => e % 2 === 0, | ||
dropWhile(e => e % 2 === 1, | ||
iterableOf(setFromOneToFive)))) // (4 5) | ||
st.deepEqual([...iterable], [4, 5], | ||
@@ -94,6 +92,6 @@ 'must remove values over the result of previous dropWhile') | ||
t.test('traversing multiple times (chaining)', function (st) { | ||
const iterable = new DropWhileIterable(setFromOneToFive) // (1 2 3 4 5) | ||
.dropWhile(e => e % 2 === 1) // (2 3 4 5) | ||
.dropWhile(e => e % 2 === 0) // (3 4 5) | ||
.dropWhile(e => e % 2 === 1) // (4 5) | ||
const iterable = dropWhile(e => e % 2 === 1, | ||
dropWhile(e => e % 2 === 0, | ||
dropWhile(e => e % 2 === 1, | ||
iterableOf(setFromOneToFive)))) // (4 5) | ||
for (const item of iterable) { // eslint-disable-line no-unused-vars | ||
@@ -108,8 +106,8 @@ // ... | ||
t.test('using intermediate iterables', function (st) { | ||
const intermediate = new DropWhileIterable(setFromOneToFive) | ||
.dropWhile(lowerThanThree) // (3 4 5) | ||
const first = intermediate | ||
.dropWhile(e => e === 3) // (4 5) | ||
const second = intermediate | ||
.dropWhile(e => e !== 5) // (5) | ||
const intermediate = dropWhile( | ||
lowerThanThree, | ||
iterableOf(setFromOneToFive) | ||
) // (3 4 5) | ||
const first = dropWhile(e => e === 3, intermediate) // (4 5) | ||
const second = dropWhile(e => e !== 5, intermediate) // (5) | ||
st.deepEqual([...first], [4, 5], | ||
@@ -116,0 +114,0 @@ 'first result must be correct') |
8318
150
64