Skip to content

Commit

Permalink
methods accepts callbacks with one arguments (removing index & iterab…
Browse files Browse the repository at this point in the history
…le parameters) & remove context parameter #8
  • Loading branch information
xgbuils committed May 14, 2017
1 parent 6d395fb commit 8bd3899
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 98 deletions.
8 changes: 3 additions & 5 deletions src/gen/dropWhile.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
const entriesGen = require('../core/entriesGen')

module.exports = function* dropWhile (iterable, cb, context) {
module.exports = function* dropWhile (iterable, cb) {
let next = false
for (const [index, val] of entriesGen(iterable)) {
for (const val of iterable) {
if (next) {
yield val
} else if (!cb.call(context, val, index, iterable)) {
} else if (!cb(val)) {
next = true
yield val
}
Expand Down
6 changes: 2 additions & 4 deletions src/gen/filter.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const entriesGen = require('../core/entriesGen')

module.exports = function* filter (iterable, cb) {
for (const [index, val] of entriesGen(iterable)) {
if (cb(val, index, iterable)) {
for (const val of iterable) {
if (cb(val)) {
yield val
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/gen/map.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const entriesGen = require('../core/entriesGen')

module.exports = function* map (iterable, cb) {
for (const [index, val] of entriesGen(iterable)) {
yield cb(val, index, iterable)
for (const val of iterable) {
yield cb(val)
}
}
8 changes: 3 additions & 5 deletions src/gen/takeWhile.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const entriesGen = require('../core/entriesGen')

module.exports = function* takeWhile (iterable, cb, context) {
for (const [index, val] of entriesGen(iterable)) {
if (cb.call(context, val, index, iterable)) {
module.exports = function* takeWhile (iterable, cb) {
for (const val of iterable) {
if (cb(val)) {
yield val
} else {
return
Expand Down
27 changes: 6 additions & 21 deletions test/fn/dropWhile_test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const {expect} = require('chai')
const Iterum = require('../../src/index.js')
const {range} = Iterum

describe('dropWhile', function () {
it('drop iterable values while value is greater than 5', function () {
Expand All @@ -8,13 +9,11 @@ describe('dropWhile', function () {
expect([...iterum]).to.be.deep.equal([4, 7, 2])
})

it('drop iterable values while sum of first elements is not greater than 10', function () {
const iterum = Iterum([2, 0, 3, 6, 1, 2])
.dropWhile(function (e, index, it) {
return it.slice(0, index + 1)
.reduce((a, b) => a + b) <= 10
})
expect([...iterum]).to.be.deep.equal([6, 1, 2])
it('drop values while are less than 10', function () {
const iterum = range(0, 15)
.dropWhile(e => e < 10)
.slice(0, 5)
expect([...iterum]).to.be.deep.equal([10, 11, 12, 13, 14])
})

it('dropping to end of iterable because condition always match', function () {
Expand All @@ -23,20 +22,6 @@ describe('dropWhile', function () {
expect([...iterum]).to.be.deep.equal([])
})

it('using context parameter', function () {
const context = []
const iterum = Iterum([2, 5, 3, 7])
.dropWhile(function (e) {
const result = e !== 3
if (result) {
this.push(e)
}
return result
}, context)
for (const value of iterum) {} // eslint-disable-line no-unused-vars
expect(context).to.be.deep.equal([2, 5])
})

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])
Expand Down
12 changes: 0 additions & 12 deletions test/fn/filter_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,6 @@ describe('filter', function () {
})
})

describe('using the whole parameters of callback', function () {
it('filter method does not mutate iterum instance behaviour', function () {
const a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
const fn = (e, i, it) => {
return e <= 8 && i % 2 === 0
&& [...it.slice(i)].length <= 8
}
const iterable = Iterum(a).filter(fn)
expect([...iterable]).to.be.deep.equal([...a].filter(fn))
})
})

describe('bad arguments', function () {
it('throws an exception when the first argument is not a function', function () {
const a = [1, 4, 2, 7, 3]
Expand Down
28 changes: 0 additions & 28 deletions test/fn/map_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,34 +30,6 @@ describe('.map', function () {
})
})

describe('using index parameter of callback', function () {
it('map method does not mutate object', function () {
const a = [1, 0, 2, 4]
const fn = (e, i) => e * i
const iterable = Iterum(a)
.map(fn)
expect([...iterable]).to.be.deep.equal([...a].map(fn))
})
})

describe('using some parameters of callback', function () {
it('map method does not mutate iterum instance behaviour', function () {
const a = [1, 2, 3]
const fn = (e, i, it) => [...it.concat([e])]
const iterable = Iterum(a).map(fn)
expect([...iterable]).to.be.deep.equal([...a].map(fn))
})
})

describe('using all parameters of callback', function () {
it('map method does not mutate iterum instance behaviour', function () {
const a = [1, 2, 3, 4, 5, 6]
const fn = (e, index, iterum) => [...iterum.slice(index + e)]
const iterable = Iterum(a).map(fn)
expect([...iterable]).to.be.deep.equal([...a].map(fn))
})
})

describe('bad arguments', function () {
it('throws an exception when the first argument is not a function', function () {
const a = new Map([['a', 'A'], ['b', 'B']])
Expand Down
21 changes: 2 additions & 19 deletions test/fn/takeWhile_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@ describe('takeWhile', function () {
expect([...iterum]).to.be.deep.equal([7, 100])
})

it('take while sum of first elements is not greater than 10', function () {
it('take while values while are is not greater than 10', function () {
const iterum = Iterum([2, 0, 3, 6, 1, 2])
.takeWhile((e, index, itm) => {
return itm.slice(0, index + 1)
.reduce((a, b) => a + b) <= 10
})
.takeWhile(e => e % 5 !== 1)
expect([...iterum]).to.be.deep.equal([2, 0, 3])
})

Expand All @@ -23,20 +20,6 @@ describe('takeWhile', function () {
expect([...iterum]).to.be.deep.equal([])
})

it('using context parameter', function () {
const context = []
const a = [7, 100, 4, 7, 2]
const iterum = Iterum(a).takeWhile(function (e) {
const result = e > 5
if (result) {
this.push(e)
}
return result
}, context)
for (const value of iterum) {} // eslint-disable-line no-unused-vars
expect(context).to.be.deep.equal([7, 100])
})

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])
Expand Down

0 comments on commit 8bd3899

Please sign in to comment.