Skip to content

Commit

Permalink
add take, takeWhile, drop & dropWhile methods named in issue #5
Browse files Browse the repository at this point in the history
  • Loading branch information
xgbuils committed Feb 4, 2017
1 parent 73f42bd commit 9bd356c
Show file tree
Hide file tree
Showing 13 changed files with 336 additions and 5 deletions.
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
"no-shadow-restricted-names": "error",
"no-spaced-func": "error",
"no-sparse-arrays": "error",
"no-tabs": "error",
"no-this-before-super": "error",
"no-throw-literal": "error",
"no-trailing-spaces": "error",
Expand Down
11 changes: 11 additions & 0 deletions src/fn/drop.js
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
}
18 changes: 18 additions & 0 deletions src/fn/dropWhile.js
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
}
1 change: 1 addition & 0 deletions src/fn/findEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ function findEntry (cb, context) {
}
}
}

module.exports = {
fn: findEntry,
validation
Expand Down
1 change: 1 addition & 0 deletions src/fn/indexOf.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ function indexOf (e) {
return findIndex.fn
.call(this, value => value === e)
}

module.exports = {
fn: indexOf
}
11 changes: 11 additions & 0 deletions src/fn/take.js
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
}
16 changes: 16 additions & 0 deletions src/fn/takeWhile.js
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
}
16 changes: 12 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const factory = require('./factory.js')
const Range = require('./constructors/range.js')
const Cartesian = require('./constructors/cartesian.js')
const factory = require('./factory')
const Range = require('./constructors/range')
const Cartesian = require('./constructors/cartesian')

const concat = require('./fn/concat')
const drop = require('./fn/drop')
const dropWhile = require('./fn/dropWhile')
const every = require('./fn/every')
const filter = require('./fn/filter')
const find = require('./fn/find')
Expand All @@ -16,6 +18,8 @@ const reduceRight = require('./fn/reduceRight')
const repeat = require('./fn/repeat')
const slice = require('./fn/slice')
const some = require('./fn/some')
const take = require('./fn/take')
const takeWhile = require('./fn/takeWhile')

const compose = require('./fn/compose')

Expand All @@ -37,10 +41,14 @@ const Iterum = factory({
},
lazyMethods: {
concat,
drop,
dropWhile,
filter,
map,
repeat,
slice
slice,
take,
takeWhile
}
})

Expand Down
69 changes: 69 additions & 0 deletions test/fn/dropWhile_test.js
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$/)
})
})
})
58 changes: 58 additions & 0 deletions test/fn/drop_test.js
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$/)
})
})
})
2 changes: 1 addition & 1 deletion test/fn/reduceRight_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('reduceRight', function () {
const iterum = Iterum([1, 3, 5])
iterum.reduceRight(cb)
expect(cb.args).to.be.deep.equal([
[5, 3, 1, iterum],
[5, 3, 1, iterum],
[8, 1, 0, iterum]
])
})
Expand Down
67 changes: 67 additions & 0 deletions test/fn/takeWhile_test.js
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$/)
})
})
})
70 changes: 70 additions & 0 deletions test/fn/take_test.js
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$/)
})
})
})

0 comments on commit 9bd356c

Please sign in to comment.