updeep-remeda/test/util/curry-spec.js

111 lines
3.7 KiB
JavaScript
Raw Normal View History

2017-04-19 00:55:46 +00:00
import { expect } from 'chai'
import { curry1, curry2, curry3, curry4, _ } from '../../lib/util/curry'
2015-08-12 06:41:03 +00:00
describe('curry1', () => {
it('can curry one arguments', () => {
2017-04-19 00:55:46 +00:00
const addOne = curry1(x => x + 1)
expect(addOne(3)).to.equal(4)
expect(addOne()(3)).to.equal(4)
})
2015-08-12 06:41:03 +00:00
it('will take up to two extra arguments', () => {
2017-04-19 00:55:46 +00:00
const curried = curry1((a, b, c) => [a, b, c])
expect(curried(1, 2, 3, 4)).to.eql([1, 2, 3])
})
it('returns a fn with arity of 1', () => {
2017-04-19 00:55:46 +00:00
const curried = curry1((a, b, c) => [a, b, c])
expect(curried).to.have.length(1)
})
})
2015-08-12 06:41:03 +00:00
describe('curry2', () => {
it('can curry two arguments', () => {
2017-04-19 00:55:46 +00:00
const add = curry2((x, y) => x + y)
expect(add(3)(4)).to.equal(7)
expect(add()(3)()(4)).to.equal(7)
expect(add(3, 4)).to.equal(7)
})
2015-08-12 06:41:03 +00:00
it('will take up to two extra arguments', () => {
2017-04-19 00:55:46 +00:00
const curried = curry2((a, b, c, d) => [a, b, c, d])
expect(curried(1, 2, 3, 4, 5)).to.eql([1, 2, 3, 4])
})
2015-08-12 15:35:10 +00:00
it('can use the placeholder', () => {
2017-04-19 00:55:46 +00:00
const curried = curry2((a, b, c, d) => [a, b, c, d])
expect(curried(_, 2)(1, 3, 4)).to.eql([1, 2, 3, 4])
})
it('returns a fn with arity of 2', () => {
2017-04-19 00:55:46 +00:00
const curried = curry2((a, b, c, d) => [a, b, c, d])
expect(curried).to.have.length(2)
})
})
2015-08-12 06:41:03 +00:00
describe('curry3', () => {
it('can curry three arguments', () => {
2017-04-19 00:55:46 +00:00
const add = curry3((x, y, z) => x + y + z)
expect(add(3, _)(4)(5)).to.equal(12)
expect(add()(3)()(4, 5)).to.equal(12)
expect(add(3, 4, 5)).to.equal(12)
})
2015-08-12 06:41:03 +00:00
it('will take up to two extra arguments', () => {
2017-04-19 00:55:46 +00:00
const curried = curry3((a, b, c, d, e) => [a, b, c, d, e])
expect(curried(1, 2, 3, 4, 5, 6)).to.eql([1, 2, 3, 4, 5])
})
2015-08-12 15:35:10 +00:00
it('can use the placeholder', () => {
2017-04-19 00:55:46 +00:00
const curried = curry3((a, b, c, d, e) => [a, b, c, d, e])
expect(curried(_, 2)('a', 3, 4, 5)).to.eql(['a', 2, 3, 4, 5])
expect(curried('b', _, 3)(2, 4, 5)).to.eql(['b', 2, 3, 4, 5])
expect(curried(_, 2, 3)('c', 4, 5)).to.eql(['c', 2, 3, 4, 5])
expect(curried(_, _, 3)('d', 2, 4, 5)).to.eql(['d', 2, 3, 4, 5])
})
it('returns a fn with arity of 3', () => {
2017-04-19 00:55:46 +00:00
const curried = curry3((a, b, c, d, e) => [a, b, c, d, e])
expect(curried).to.have.length(3)
})
})
2015-08-12 07:53:29 +00:00
describe('curry4', () => {
it('can curry four arguments', () => {
2017-04-19 00:55:46 +00:00
const add = curry4((x, y, z, u) => x + y + z + u)
expect(add(3, _)(4)(5)(6)).to.equal(18)
expect(add()(3)()(4, 5, 6)).to.equal(18)
expect(add(3, 4, 5, 6)).to.equal(18)
})
2015-08-12 07:53:29 +00:00
it('will take up to two extra arguments', () => {
2017-04-19 00:55:46 +00:00
const curried = curry4((a, b, c, d, e, f) => [a, b, c, d, e, f])
expect(curried(1, 2, 3, 4, 5, 6, 7)).to.eql([1, 2, 3, 4, 5, 6])
})
2015-08-12 15:35:10 +00:00
it('can use the placeholder', () => {
2017-04-19 00:55:46 +00:00
const curried = curry4((a, b, c, d, e, f) => [a, b, c, d, e, f])
expect(curried(_, 2)('a', 3, 4, 5, 6)).to.eql(['a', 2, 3, 4, 5, 6])
expect(curried(_, 2, 3)('b', 4, 5, 6)).to.eql(['b', 2, 3, 4, 5, 6])
expect(curried(_, 2, 3, 4)('c', 5, 6)).to.eql(['c', 2, 3, 4, 5, 6])
2015-08-12 15:35:10 +00:00
2017-04-19 00:55:46 +00:00
expect(curried('d', _, 3)(2, 4, 5, 6)).to.eql(['d', 2, 3, 4, 5, 6])
expect(curried('e', _, 3, 4)(2, 5, 6)).to.eql(['e', 2, 3, 4, 5, 6])
2015-08-12 15:35:10 +00:00
2017-04-19 00:55:46 +00:00
expect(curried('f', 2, _, 4)(3, 5, 6)).to.eql(['f', 2, 3, 4, 5, 6])
2015-08-12 15:35:10 +00:00
2017-04-19 00:55:46 +00:00
expect(curried(_, _, 3)('g', 2, 4, 5, 6)).to.eql(['g', 2, 3, 4, 5, 6])
expect(curried(_, _, 3, 4)('h', 2, 5, 6)).to.eql(['h', 2, 3, 4, 5, 6])
expect(curried(_, 2, _, 4)('i', 3, 5, 6)).to.eql(['i', 2, 3, 4, 5, 6])
2015-08-12 15:35:10 +00:00
2017-04-19 00:55:46 +00:00
expect(curried('j', _, _, 4)(2, 3, 5, 6)).to.eql(['j', 2, 3, 4, 5, 6])
2015-08-12 15:35:10 +00:00
2017-04-19 00:55:46 +00:00
expect(curried(_, _, _, 4)('k', 2, 3, 5, 6)).to.eql(['k', 2, 3, 4, 5, 6])
})
it('returns a fn with arity of 4', () => {
2017-04-19 00:55:46 +00:00
const curried = curry4((a, b, c, d, e, f) => [a, b, c, d, e, f])
expect(curried).to.have.length(4)
})
})