-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add take, takeWhile, drop & dropWhile methods named in issue #5
- Loading branch information
Showing
13 changed files
with
336 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const dropWhile = require('./dropWhile') | ||
const validation = [['Number', 'Undefined']] | ||
|
||
function* drop (n = 1) { | ||
yield* dropWhile.gen.call(this, (_, index) => index < n) | ||
} | ||
|
||
module.exports = { | ||
gen: drop, | ||
validation | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const validation = [['Function']] | ||
|
||
function* dropWhile (cb, context) { | ||
let next = false | ||
for (const [index, val] of this.entries()) { | ||
if (next) { | ||
yield val | ||
} else if (!cb.call(context, val, index, this)) { | ||
next = true | ||
yield val | ||
} | ||
} | ||
} | ||
|
||
module.exports = { | ||
gen: dropWhile, | ||
validation | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ function findEntry (cb, context) { | |
} | ||
} | ||
} | ||
|
||
module.exports = { | ||
fn: findEntry, | ||
validation | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ function indexOf (e) { | |
return findIndex.fn | ||
.call(this, value => value === e) | ||
} | ||
|
||
module.exports = { | ||
fn: indexOf | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const takeWhile = require('./takeWhile') | ||
const validation = [['Number', 'Undefined']] | ||
|
||
function* take (n = 1) { | ||
yield* takeWhile.gen.call(this, (_, index) => index < n) | ||
} | ||
|
||
module.exports = { | ||
gen: take, | ||
validation | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const validation = [['Function']] | ||
|
||
function* takeWhile (cb, context) { | ||
for (const [index, val] of this.entries()) { | ||
if (cb.call(context, val, index, this)) { | ||
yield val | ||
} else { | ||
return | ||
} | ||
} | ||
} | ||
|
||
module.exports = { | ||
gen: takeWhile, | ||
validation | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
const {expect} = require('chai') | ||
const Iterum = require('../../src/index.js') | ||
const {Range} = Iterum | ||
|
||
describe('dropWhile', function () { | ||
it('drop while value is greater than 5', function () { | ||
const iterum = Iterum([7, 100, 4, 7, 2]) | ||
.dropWhile(function (e) { | ||
return e > 5 | ||
}) | ||
expect([...iterum]).to.be.deep.equal([4, 7, 2]) | ||
}) | ||
|
||
it('drop while sum of first elements is not greater than 10', function () { | ||
const iterum = Iterum([2, 0, 3, 6, 1, 2]) | ||
.dropWhile(function (e, index, itm) { | ||
return itm.slice(0, index + 1) | ||
.reduce((a, b) => a + b) <= 10 | ||
}) | ||
expect([...iterum]).to.be.deep.equal([6, 1, 2]) | ||
}) | ||
|
||
it('dropping to end of iterable because condition always match', function () { | ||
const iterum = Iterum([2, 0, 3, 6, 1, 2]) | ||
.dropWhile(function (e) { | ||
return e < 7 | ||
}) | ||
expect([...iterum]).to.be.deep.equal([]) | ||
}) | ||
|
||
describe('converting iterum instance to array', function () { | ||
it('returns the same as converting [Symbol.iterator]() iterator to array', function () { | ||
const iterum = Iterum([7, 100, 4, 7, 2]) | ||
.dropWhile(function (e) { | ||
return e > 5 | ||
}) | ||
const iterator = iterum[Symbol.iterator]() | ||
expect([...iterator]).to.be.deep.equal([...iterum]) | ||
}) | ||
}) | ||
|
||
describe('inmutability', function () { | ||
it('dropWhile method does not mutate object', function () { | ||
const x = Range(8, 3, -1) | ||
x.dropWhile((_, i) => i < 2) | ||
expect([...x]).to.be.deep.equal([8, 7, 6, 5, 4, 3]) | ||
}) | ||
}) | ||
|
||
describe('If it exists value that is an iterum instance,', function () { | ||
it('this value is interpreted as a sequence of values of this iterum instance', function () { | ||
const iterum = Iterum([1, Range(1, 3).repeat(2), 8]) | ||
.dropWhile(function (e) { | ||
return e < 3 | ||
}) | ||
expect([...iterum]).to.be.deep.equal([3, 1, 2, 3, 8]) | ||
}) | ||
}) | ||
|
||
describe('bad arguments', function () { | ||
it('throws an exception when the first argument is not a function', function () { | ||
function foo () { | ||
Range(2, 9, 2).filter(false) | ||
} | ||
expect(foo).to.throw(TypeError, | ||
/^false is not a function$/) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
const {expect} = require('chai') | ||
const Iterum = require('../../src/index.js') | ||
const {Range} = Iterum | ||
|
||
describe('drop', function () { | ||
it('drop 2 values', function () { | ||
const iterum = Iterum([7, 100, 4, 7, 2]) | ||
.drop(3) | ||
expect([...iterum]).to.be.deep.equal([7, 2]) | ||
}) | ||
|
||
it('drop 1 value by default', function () { | ||
const iterum = Iterum([2, 0, 3, 6, 1, 2]) | ||
.drop() | ||
expect([...iterum]).to.be.deep.equal([0, 3, 6, 1, 2]) | ||
}) | ||
|
||
it('drop more values than iterable provide', function () { | ||
const iterum = Iterum([2, 0, 3, 6, 1, 2]) | ||
.drop(10) | ||
expect([...iterum]).to.be.deep.equal([]) | ||
}) | ||
|
||
describe('converting iterum instance to array', function () { | ||
it('returns the same as converting [Symbol.iterator]() iterator to array', function () { | ||
const iterum = Iterum([7, 100, 4, 7, 2]) | ||
.drop(2) | ||
const iterator = iterum[Symbol.iterator]() | ||
expect([...iterator]).to.be.deep.equal([...iterum]) | ||
}) | ||
}) | ||
|
||
describe('inmutability', function () { | ||
it('drop method does not mutate object', function () { | ||
const x = Range(8, 3, -1) | ||
x.drop(4) | ||
expect([...x]).to.be.deep.equal([8, 7, 6, 5, 4, 3]) | ||
}) | ||
}) | ||
|
||
describe('If it exists value that is an iterum instance,', function () { | ||
it('this value is interpreted as a sequence of values of this iterum instance', function () { | ||
const iterum = Iterum([1, Iterum([3, 100, 5]), 8]) | ||
.drop(3) | ||
expect([...iterum]).to.be.deep.equal([5, 8]) | ||
}) | ||
}) | ||
|
||
describe('bad arguments', function () { | ||
it('throws an exception when the first argument is not a function', function () { | ||
function foo () { | ||
Range(2, 9, 2).filter(23) | ||
} | ||
expect(foo).to.throw(TypeError, | ||
/^23 is not a function$/) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
const {expect} = require('chai') | ||
const Iterum = require('../../src/index.js') | ||
const {Range} = Iterum | ||
|
||
describe('takeWhile', function () { | ||
it('take while value is greater than 5', function () { | ||
const iterum = Iterum([7, 100, 4, 7, 2]) | ||
.takeWhile(function (e) { | ||
return e > 5 | ||
}) | ||
expect([...iterum]).to.be.deep.equal([7, 100]) | ||
}) | ||
|
||
it('take while sum of first elements is not greater than 10', function () { | ||
const iterum = Iterum([2, 0, 3, 6, 1, 2]) | ||
.takeWhile(function (e, index, itm) { | ||
return itm.slice(0, index + 1) | ||
.reduce((a, b) => a + b) <= 10 | ||
}) | ||
expect([...iterum]).to.be.deep.equal([2, 0, 3]) | ||
}) | ||
|
||
it('take no value of iterable because condition never match', function () { | ||
const iterum = Iterum([2, 0, 3, 6, 1, 2]) | ||
.takeWhile(e => e >= 7) | ||
expect([...iterum]).to.be.deep.equal([]) | ||
}) | ||
|
||
describe('converting iterum instance to array', function () { | ||
it('returns the same as converting [Symbol.iterator]() iterator to array', function () { | ||
const iterum = Iterum([7, 100, 4, 7, 2]) | ||
.takeWhile(function (e) { | ||
return e > 5 | ||
}) | ||
const iterator = iterum[Symbol.iterator]() | ||
expect([...iterator]).to.be.deep.equal([...iterum]) | ||
}) | ||
}) | ||
|
||
describe('inmutability', function () { | ||
it('takeWhile method does not mutate object', function () { | ||
const x = Range(8, 3, -1) | ||
x.takeWhile((_, i) => i < 2) | ||
expect([...x]).to.be.deep.equal([8, 7, 6, 5, 4, 3]) | ||
}) | ||
}) | ||
|
||
describe('If it exists value that is an iterum instance,', function () { | ||
it('this value is interpreted as a sequence of values of this iterum instance', function () { | ||
const iterum = Iterum([1, Range(1, 8).filter(e => e % 2 === 0), 8]) | ||
.takeWhile(function (e) { | ||
return e < 5 | ||
}) | ||
expect([...iterum]).to.be.deep.equal([1, 2, 4]) | ||
}) | ||
}) | ||
|
||
describe('bad arguments', function () { | ||
it('throws an exception when the first argument is not a function', function () { | ||
function foo () { | ||
Range(2, 9, 2).filter('fizz') | ||
} | ||
expect(foo).to.throw(TypeError, | ||
/^fizz is not a function$/) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
const {expect} = require('chai') | ||
const Iterum = require('../../src/index.js') | ||
const {Range} = Iterum | ||
|
||
describe('take', function () { | ||
it('take 2 values', function () { | ||
const iterum = Iterum([7, 100, 4, 7, 2]) | ||
.take(3) | ||
expect([...iterum]).to.be.deep.equal([7, 100, 4]) | ||
}) | ||
|
||
it('take 1 value by default', function () { | ||
const iterum = Iterum([2, 0, 3, 6, 1, 2]) | ||
.take() | ||
expect([...iterum]).to.be.deep.equal([2]) | ||
}) | ||
|
||
it('take more values than iterable provide', function () { | ||
const iterum = Iterum([2, 0, 3, 6, 1, 2]) | ||
.take(10) | ||
expect([...iterum]).to.be.deep.equal([2, 0, 3, 6, 1, 2]) | ||
}) | ||
|
||
it('take 0 values', function () { | ||
const iterum = Iterum([2, 0, 3, 6, 1, 2]) | ||
.take(0) | ||
expect([...iterum]).to.be.deep.equal([]) | ||
}) | ||
|
||
it('take negative values is the same as take 0 values', function () { | ||
const iterum = Iterum([2, 0, 3, 6, 1, 2]) | ||
.take(-10) | ||
expect([...iterum]).to.be.deep.equal([]) | ||
}) | ||
|
||
describe('converting iterum instance to array', function () { | ||
it('returns the same as converting [Symbol.iterator]() iterator to array', function () { | ||
const iterum = Iterum([7, 100, 4, 7, 2]) | ||
.take(2) | ||
const iterator = iterum[Symbol.iterator]() | ||
expect([...iterator]).to.be.deep.equal([...iterum]) | ||
}) | ||
}) | ||
|
||
describe('inmutability', function () { | ||
it('take method does not mutate object', function () { | ||
const x = Range(8, 3, -1) | ||
x.take(4) | ||
expect([...x]).to.be.deep.equal([8, 7, 6, 5, 4, 3]) | ||
}) | ||
}) | ||
|
||
describe('If it exists value that is an iterum instance,', function () { | ||
it('this value is interpreted as a sequence of values of this iterum instance', function () { | ||
const iterum = Iterum([1, Iterum([3, 100, 5]), 8]) | ||
.take(3) | ||
expect([...iterum]).to.be.deep.equal([1, 3, 100]) | ||
}) | ||
}) | ||
|
||
describe('bad arguments', function () { | ||
it('throws an exception when the first argument is not a function', function () { | ||
function foo () { | ||
Range(2, 9, 2).filter(null) | ||
} | ||
expect(foo).to.throw(TypeError, | ||
/^null is not a function$/) | ||
}) | ||
}) | ||
}) |