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

91 lines
3.2 KiB
JavaScript
Raw Normal View History

import { expect } from 'chai';
2015-08-12 15:35:10 +00:00
import { curry1, curry2, curry3, curry4, _ } from '../../lib/util/curry';
2015-08-12 06:41:03 +00:00
describe('curry1', () => {
it('can curry one arguments', () => {
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', () => {
const curried = curry1((a, b, c) => [a, b, c]);
expect(curried(1, 2, 3, 4)).to.eql([1, 2, 3]);
2015-08-12 06:41:03 +00:00
});
});
describe('curry2', () => {
it('can curry two arguments', () => {
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', () => {
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 06:41:03 +00:00
});
2015-08-12 15:35:10 +00:00
it('can use the placeholder', () => {
const curried = curry2((a, b, c, d) => [a, b, c, d]);
expect(curried(_, 2)(1, 3, 4)).to.eql([1, 2, 3, 4]);
2015-08-12 15:35:10 +00:00
});
2015-08-12 06:41:03 +00:00
});
describe('curry3', () => {
it('can curry three arguments', () => {
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', () => {
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 06:41:03 +00:00
});
2015-08-12 15:35:10 +00:00
it('can use the placeholder', () => {
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]);
2015-08-12 15:35:10 +00:00
});
2015-08-12 06:41:03 +00:00
});
2015-08-12 07:53:29 +00:00
describe('curry4', () => {
it('can curry four arguments', () => {
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', () => {
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 07:53:29 +00:00
});
2015-08-12 15:35:10 +00:00
it('can use the placeholder', () => {
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
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
expect(curried('f', 2, _, 4)(3, 5, 6)).to.eql(['f', 2, 3, 4, 5, 6]);
2015-08-12 15:35:10 +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
expect(curried('j', _, _, 4)(2, 3, 5, 6)).to.eql(['j', 2, 3, 4, 5, 6]);
2015-08-12 15:35:10 +00:00
expect(curried(_, _, _, 4)('k', 2, 3, 5, 6)).to.eql(['k', 2, 3, 4, 5, 6]);
2015-08-12 15:35:10 +00:00
});
2015-08-12 07:53:29 +00:00
});