Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update to es6 #5

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": [ "es2015" ]
}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#### joe made this: https://goel.io/joe

#####=== Node ===#####
#dist
dist

# Logs
logs
Expand Down Expand Up @@ -54,4 +56,3 @@ Icon
Network Trash Folder
Temporary Items
.apdisk

Expand Down
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ before_install:
- "export DISPLAY=:99.0"
- "sh -e /etc/init.d/xvfb start"
node_js:
- '0.10'
- node
- 6
- 5
- 4
29 changes: 0 additions & 29 deletions gulpfile.js

This file was deleted.

26 changes: 0 additions & 26 deletions index.es6

This file was deleted.

36 changes: 0 additions & 36 deletions index.js

This file was deleted.

21 changes: 12 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
"name": "fj-curry",
"version": "1.0.0",
"description": "curry a function",
"main": "index.js",
"main": "dist/index.js",
"scripts": {
"test": "gulp test"
"test": "mocha --compilers js:babel-core/register",
"build": "babel --out-dir dist --ignore *.test.js src"
},
"repository": {
"type": "git",
Expand All @@ -25,13 +26,15 @@
},
"homepage": "https://github.com/fp-js/fj-curry",
"devDependencies": {
"gulp": "^3.8.10",
"gulp-6to5": "^3.0.0",
"gulp-rename": "^1.2.0",
"gulp-run": "^1.6.6",
"gulp-sourcemaps": "^1.3.0",
"gulp-watch": "^4.1.0",
"babel-cli": "^6.18.0",
"babel-core": "^6.21.0",
"babel-loader": "^6.2.10",
"babel-preset-es2015": "^6.18.0",
"babel-register": "^6.18.0",
"chai": "^3.5.0",
"mocha": "^3.1.2",
"prova": "^2.1.1",
"sinon": "^1.12.2"
"sinon": "^1.12.2",
"sinon-chai": "^2.8.0"
}
}
22 changes: 22 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"use strict";

const _curry = (arity, fn, curryArgs = []) => {
return (...arg) => {
var concatArgs = [...curryArgs, ...arg];
// If current function arity greater than number of received args
if (arity > concatArgs.length) {
return _curry(arity, fn, concatArgs);
} else {
return fn(...concatArgs);
}
};
};

export const curry = (fn) => _curry(fn.length, fn);

export const curryN = (n, fn) => _curry(n, fn);

export const curry1 = curryN(2, curryN)(1);
export const curry2 = curryN(2, curryN)(2);
export const curry3 = curryN(2, curryN)(3);
export const curry4 = curryN(2, curryN)(4);
138 changes: 138 additions & 0 deletions src/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import chai, { expect } from 'chai';
import sinonChai from 'sinon-chai';
import { spy } from 'sinon';
import {
curry,
curryN,
curry1,
curry2,
curry3,
curry4
} from './index.js';

chai.use(sinonChai);

describe('fj-curry', () => {

let spiedCallback;
beforeEach(() => {
spiedCallback = spy();
});
const a1 = (w) => spiedCallback();
const a2 = (w, x) => spiedCallback();
const a3 = (w, x, y) => spiedCallback();
const a4 = (w, x, y, z) => spiedCallback();

describe('curry', () => {

it('should exist', () => {
expect(curry).to.be.ok;
});

it('should return a function', () => {
expect(curry(x => x)).to.be.a('function');
});

it('should accept functions of arbitrary arities', () => {
expect(curry(a1)).to.be.a('function');
expect(curry(a2)).to.be.a('function');
expect(curry(a3)).to.be.a('function');
});

it('should delay invocation of curried functions', () => {
const lift1 = curry(a1);
const lift2 = curry(a2);
const lift3 = curry(a3);

lift2(null);
lift3(null, null);
lift3(null)(null);
expect(spiedCallback).to.have.not.been.called;

lift1(null);
expect(spiedCallback).to.have.been.calledOnce;
lift2(null);
expect(spiedCallback).to.have.been.calledOnce;
lift2(null)(null);
expect(spiedCallback).to.have.been.calledTwice;
lift3(null, null);
expect(spiedCallback).to.have.been.calledTwice;
lift3(null, null)(null);
expect(spiedCallback).to.have.been.calledThrice;
lift3(null)(null)(null);
expect(spiedCallback).to.have.callCount(4);
lift3(null, null, null);
expect(spiedCallback).to.have.callCount(5);
});
});

describe('curryN', () => {

it('should accept two arguments', () => {
expect(curryN.length).to.equal(2);
});

it('should be curriable', () => {
expect(curry(curryN(1))).to.be.a('function');
});

it('should accept N of arbitrary size', () => {
const lift1 = curryN(1, a1);
const lift2 = curryN(2, a2);
const lift3 = curryN(3, a3);

lift1(1);
lift2(2)(3);
lift3(4)(5)(6);

expect(spiedCallback).to.have.been.calledThrice;
});
})

describe('curry1', () => {

it('should invoke curried function after passed single argument', () => {
const lift1 = curry1(a1);
lift1(null);

expect(spiedCallback).to.have.been.calledOnce;
});
});

describe('curry2', () => {

it('should invoke curried function after passed two arguments', () => {
const lift2 = curry2(a2);
lift2(null, null);

expect(spiedCallback).to.have.been.calledOnce;
});
});

describe('curry3', () => {

it('should invoke curried function after passed three arguments', () => {
const lift3 = curry3(a3);
lift3(null, null, null);

expect(spiedCallback).to.have.been.calledOnce;
});
});

describe('curry4', () => {

it('should invoke curried function after passed four arguments', () => {
const lift4 = curry4(a4);
lift4(null, null, null, null);

expect(spiedCallback).to.have.been.calledOnce;
});

it('should invoke curried function irrespective of its arity', () => {
const lift4 = curry4(a1);
lift4(null)(null)(null)(null);

expect(spiedCallback).to.have.been.calledOnce;
});
});
});
77 changes: 0 additions & 77 deletions test.es6

This file was deleted.

Loading