Skip to content

Commit

Permalink
now reduce & reduceRight has strictly 3 parameters #14 & cb parameter…
Browse files Browse the repository at this point in the history
… is simplified following #8
  • Loading branch information
xgbuils committed Jun 5, 2017
1 parent 907a156 commit 8d4e490
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 107 deletions.
14 changes: 0 additions & 14 deletions src/core/eager-method-factory.js

This file was deleted.

1 change: 0 additions & 1 deletion src/fn/forEach.js

This file was deleted.

4 changes: 3 additions & 1 deletion src/fn/reduce.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
module.exports = require('../core/eager-method-factory.js')([].reduce, 3)
module.exports = function (iterable, cb, initialValue) {
return [...iterable].reduce((acc, val) => cb(acc, val), initialValue)
}
4 changes: 3 additions & 1 deletion src/fn/reduceRight.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
module.exports = require('../core/eager-method-factory.js')([].reduceRight, 3)
module.exports = function (iterable, cb, initialValue) {
return [...iterable].reduceRight((acc, val) => cb(acc, val), initialValue)
}
14 changes: 7 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ const every = require('./fn/every')
const find = require('./fn/find')
const findEntry = require('./fn/findEntry')
const findIndex = require('./fn/findIndex')
const forEach = require('./fn/forEach')
const includes = require('./fn/includes')
const indexOf = require('./fn/indexOf')
const indexOfFrom = require('./fn/indexOfFrom')
Expand All @@ -41,8 +40,10 @@ const some = require('./fn/some')
const take = require('./fn/take')

const number = ['Number']
const fnc = ['Function']
const infiniteIterablesValidation = [[], [Iterable], Infinity]
const functionValidation = [[], ['Function']]
const functionValidation = [[], fnc]
const reduceValidation = [[], fnc, []]
const numberValidation = [[], number]
const twoNumberValidation = [[], number, number]

Expand Down Expand Up @@ -97,9 +98,6 @@ const Iterum = factory({
gen: flatten,
validation: numberValidation
},
forEach: {
fn: forEach
},
includes: {
fn: includes
},
Expand Down Expand Up @@ -150,10 +148,12 @@ const Iterum = factory({
validation: infiniteIterablesValidation
},
reduce: {
fn: reduce
fn: reduce,
validation: reduceValidation
},
reduceRight: {
fn: reduceRight
fn: reduceRight,
validation: reduceValidation
},
repeat: {
gen: repeat,
Expand Down
44 changes: 0 additions & 44 deletions test/fn/forEach_test.js

This file was deleted.

41 changes: 22 additions & 19 deletions test/fn/reduceRight_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,40 @@ const Iterum = require('../../src/index.js')

describe('reduceRight', function () {
it('returns correct value', function () {
const cb = sinon.spy(function (a, b) {
return a - b
})
const value = Iterum([1, 3, 5])
.reduceRight(cb)
expect(value).to.be.deep.equal(1)
const cb = (a, b) => a - b
const value = Iterum([1, 3, 5]).reduceRight(cb, 0)
expect(value).to.be.deep.equal(-9)
})

it('without initial value', function () {
const cb = sinon.spy(function (a, b) {
return a + b
})
const iterum = Iterum([1, 3, 5])
iterum.reduceRight(cb)
iterum.reduceRight(cb, 0)
expect(cb.args).to.be.deep.equal([
[5, 3, 1, iterum],
[8, 1, 0, iterum]
[0, 5],
[5, 3],
[8, 1]
])
})

it('with initial value', function () {
const cb = sinon.spy(function (a, b) {
return a + b
describe('wrong arguments', function () {
it('throws an exception if parameters are not passed', function () {
function test () {
Iterum(new Set([1, 4, 2])).reduceRight()
}
expect(test).to.throw(TypeError,
/^undefined is not a function$/)
})

it('throws an exception if initial value parameter is not passed', function () {
function test () {
Iterum(new Set([1, 4, 2])).reduceRight((a, b) => a + b)
}
expect(test).to.throw(TypeError,
/^argument 2 is required$/)
})
const iterum = Iterum([1, 3, 5])
iterum.reduceRight(cb, 0)
expect(cb.args).to.be.deep.equal([
[0, 5, 2, iterum],
[5, 3, 1, iterum],
[8, 1, 0, iterum]
])
})

describe('static method', function () {
Expand Down
43 changes: 23 additions & 20 deletions test/fn/reduce_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,40 @@ const Iterum = require('../../src/index.js')

describe('reduce', function () {
it('returns correct value', function () {
const cb = sinon.spy(function (a, b) {
return a - b
})
const value = Iterum(Iterum([1, 3, 5]))
.reduce(cb)
expect(value).to.be.deep.equal(-7)
const cb = (a, b) => a - b
const value = Iterum([1, 3, 5]).reduce(cb, 0)
expect(value).to.be.deep.equal(-9)
})

it('without initial value', function () {
const cb = sinon.spy(function (a, b) {
return a + b
})
const iterum = Iterum(Iterum([1, 3, 5]))
iterum.reduce(cb)
const iterum = Iterum([1, 3, 5])
iterum.reduce(cb, 0)
expect(cb.args).to.be.deep.equal([
[1, 3, 1, iterum],
[4, 5, 2, iterum]
[0, 1],
[1, 3],
[4, 5]
])
})

it('with initial value', function () {
const cb = sinon.spy(function (a, b) {
return a + b
describe('wrong arguments', function () {
it('throws an exception if parameters are not passed', function () {
function test () {
Iterum(new Set([1, 4, 2])).reduce()
}
expect(test).to.throw(TypeError,
/^undefined is not a function$/)
})

it('throws an exception if initial value parameter is not passed', function () {
function test () {
Iterum(new Set([1, 4, 2])).reduce((a, b) => a + b)
}
expect(test).to.throw(TypeError,
/^argument 2 is required$/)
})
const iterum = Iterum(Iterum([1, 3, 5]))
iterum.reduce(cb, 0)
expect(cb.args).to.be.deep.equal([
[0, 1, 0, iterum],
[1, 3, 1, iterum],
[4, 5, 2, iterum]
])
})

describe('static method', function () {
Expand Down

0 comments on commit 8d4e490

Please sign in to comment.