From d31a77395dd9c0b62e213896651739736218120b Mon Sep 17 00:00:00 2001 From: Eric Churchill Date: Fri, 6 Jan 2017 18:01:52 -0800 Subject: [PATCH 1/6] remove old files --- index.es6 | 26 ---------------- index.js | 36 ---------------------- test.es6 | 77 ----------------------------------------------- test.js | 90 ------------------------------------------------------- 4 files changed, 229 deletions(-) delete mode 100644 index.es6 delete mode 100644 index.js delete mode 100644 test.es6 delete mode 100644 test.js diff --git a/index.es6 b/index.es6 deleted file mode 100644 index ad81eea..0000000 --- a/index.es6 +++ /dev/null @@ -1,26 +0,0 @@ -let _curry = (fn, curryArgs = []) => { - return (...args) => { - var concatArgs = [...concatArgs, ...args]; - - // If function arity greater than number of received args - if (fn.length > concatArgs.length) { - return _curry(fn, concatArgs); - } else { - return fn(...concatArgs); - } - }; -}; - -export function curry(fn) { - return _curry(fn); -} - -// curryN now unnecessary -export function curryN(n, fn) { - return _curry(n, fn); -} - -export var curry1 = curryN(2, curryN)(1); -export var curry2 = curryN(2, curryN)(2); -export var curry3 = curryN(2, curryN)(3); -export var curry4 = curryN(2, curryN)(4); diff --git a/index.js b/index.js deleted file mode 100644 index 591e8d4..0000000 --- a/index.js +++ /dev/null @@ -1,36 +0,0 @@ -"use strict"; - -exports.curry = curry; -exports.curryN = curryN; -var slice = Array.prototype.slice; - -var _curry = function (n, fn) { - var curryArgs = arguments[2] === undefined ? [] : arguments[2]; - return function () { - for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { - args[_key] = arguments[_key]; - } - - var concatArgs = curryArgs.concat(args); - - if (n > concatArgs.length) { - return _curry(n, fn, concatArgs); - } else { - return fn.apply(null, slice.call(concatArgs, 0, n)); - } - }; -}; - -function curry(fn) { - return _curry(fn.length, fn); -} - -function curryN(n, fn) { - return _curry(n, fn); -} - -var curry1 = exports.curry1 = curryN(2, curryN)(1); -var curry2 = exports.curry2 = curryN(2, curryN)(2); -var curry3 = exports.curry3 = curryN(2, curryN)(3); -var curry4 = exports.curry4 = curryN(2, curryN)(4); -exports.__esModule = true; \ No newline at end of file diff --git a/test.es6 b/test.es6 deleted file mode 100644 index b2af5f2..0000000 --- a/test.es6 +++ /dev/null @@ -1,77 +0,0 @@ -import test from 'prova'; -import { stub } from 'sinon'; -import { curry, curryN, curry1, curry2, curry3, curry4 } from './'; - - -test('fj-curry#curry', (t) => { - t.plan(5); - - let spy = stub().returns(true); - function func(a, b, c) { - return spy.apply(this, arguments); - } - - t.equal(typeof curry, 'function'); - - t.equal(curry(func)(1, 2)(3, 4), true); - t.ok(spy.calledWith(1, 2, 3)); - - t.equal(curry(func)(1)(2, 3), true); - t.ok(spy.calledWith(1, 2, 3)); -}); - -test('fj-curry#curryN', (t) => { - t.plan(7); - - let spy = stub().returns(true); - - t.equal(typeof curryN, 'function'); - t.equal(curryN(1, spy)(1, 2), true); - t.ok(spy.calledWith(1)); - - t.equal(curryN(3, spy)(1, 2)(3, 4), true); - t.ok(spy.calledWith(1, 2, 3)); - - t.equal(curryN(3, spy)(1)(2, 3), true); - t.ok(spy.calledWith(1, 2, 3)); -}); - -test('fj-curry#curry1', (t) => { - t.plan(3); - - let spy = stub().returns(true); - - t.equal(typeof curry1, 'function'); - t.equal(curry1(spy)(1, 2), true); - t.ok(spy.calledWith(1)); -}); - -test('fj-curry#curry2', (t) => { - t.plan(3); - - let spy = stub().returns(true); - - t.equal(typeof curry2, 'function'); - t.equal(curry2(spy)(1)(2, 3), true); - t.ok(spy.calledWith(1, 2)); -}); - -test('fj-curry#curry3', (t) => { - t.plan(3); - - let spy = stub().returns(true); - - t.equal(typeof curry3, 'function'); - t.equal(curry3(spy)(1)(2)(3, 4), true); - t.ok(spy.calledWith(1, 2, 3)); -}); - -test('fj-curry#curry4', (t) => { - t.plan(3); - - let spy = stub().returns(true); - - t.equal(typeof curry4, 'function'); - t.equal(curry4(spy)(1)(2)(3)(4, 5), true); - t.ok(spy.calledWith(1, 2, 3, 4)); -}); diff --git a/test.js b/test.js deleted file mode 100644 index 56e13a1..0000000 --- a/test.js +++ /dev/null @@ -1,90 +0,0 @@ -"use strict"; - -var _interopRequire = function (obj) { return obj && obj.__esModule ? obj["default"] : obj; }; - -var test = _interopRequire(require("prova")); - -var stub = require("sinon").stub; -var _ = require("./"); - -var curry = _.curry; -var curryN = _.curryN; -var curry1 = _.curry1; -var curry2 = _.curry2; -var curry3 = _.curry3; -var curry4 = _.curry4; - - - -test("fj-curry#curry", function (t) { - t.plan(5); - - var spy = stub().returns(true); - function func(a, b, c) { - return spy.apply(this, arguments); - } - - t.equal(typeof curry, "function"); - - t.equal(curry(func)(1, 2)(3, 4), true); - t.ok(spy.calledWith(1, 2, 3)); - - t.equal(curry(func)(1)(2, 3), true); - t.ok(spy.calledWith(1, 2, 3)); -}); - -test("fj-curry#curryN", function (t) { - t.plan(7); - - var spy = stub().returns(true); - - t.equal(typeof curryN, "function"); - t.equal(curryN(1, spy)(1, 2), true); - t.ok(spy.calledWith(1)); - - t.equal(curryN(3, spy)(1, 2)(3, 4), true); - t.ok(spy.calledWith(1, 2, 3)); - - t.equal(curryN(3, spy)(1)(2, 3), true); - t.ok(spy.calledWith(1, 2, 3)); -}); - -test("fj-curry#curry1", function (t) { - t.plan(3); - - var spy = stub().returns(true); - - t.equal(typeof curry1, "function"); - t.equal(curry1(spy)(1, 2), true); - t.ok(spy.calledWith(1)); -}); - -test("fj-curry#curry2", function (t) { - t.plan(3); - - var spy = stub().returns(true); - - t.equal(typeof curry2, "function"); - t.equal(curry2(spy)(1)(2, 3), true); - t.ok(spy.calledWith(1, 2)); -}); - -test("fj-curry#curry3", function (t) { - t.plan(3); - - var spy = stub().returns(true); - - t.equal(typeof curry3, "function"); - t.equal(curry3(spy)(1)(2)(3, 4), true); - t.ok(spy.calledWith(1, 2, 3)); -}); - -test("fj-curry#curry4", function (t) { - t.plan(3); - - var spy = stub().returns(true); - - t.equal(typeof curry4, "function"); - t.equal(curry4(spy)(1)(2)(3)(4, 5), true); - t.ok(spy.calledWith(1, 2, 3, 4)); -}); \ No newline at end of file From d98d317f7baa2f3b1dbf305014e6e7e28bb955ff Mon Sep 17 00:00:00 2001 From: Eric Churchill Date: Fri, 6 Jan 2017 18:02:39 -0800 Subject: [PATCH 2/6] update meta configuration files and package.json --- .babelrc | 3 +++ .gitignore | 3 ++- package.json | 16 ++++++++-------- 3 files changed, 13 insertions(+), 9 deletions(-) create mode 100644 .babelrc diff --git a/.babelrc b/.babelrc new file mode 100644 index 0000000..59cc857 --- /dev/null +++ b/.babelrc @@ -0,0 +1,3 @@ +{ + "presets": [ "es2015" ] +} diff --git a/.gitignore b/.gitignore index 46dbba4..d55b58a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,8 @@ #### joe made this: https://goel.io/joe #####=== Node ===##### +#dist +dist # Logs logs @@ -54,4 +56,3 @@ Icon Network Trash Folder Temporary Items .apdisk - diff --git a/package.json b/package.json index e9b92d2..f11d197 100644 --- a/package.json +++ b/package.json @@ -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", @@ -25,12 +26,11 @@ }, "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-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" } From 34cd49b89b727501f091221cacb2dc75b1e3faf6 Mon Sep 17 00:00:00 2001 From: Eric Churchill Date: Fri, 6 Jan 2017 18:03:41 -0800 Subject: [PATCH 3/6] move new files to source directory --- src/index.js | 23 ++++++++++++ src/index.test.js | 91 +++++++++++++++++++++++++++++++++++++++++++++++ test/mocha.opts | 1 + 3 files changed, 115 insertions(+) create mode 100644 src/index.js create mode 100644 src/index.test.js create mode 100644 test/mocha.opts diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..5c94e42 --- /dev/null +++ b/src/index.js @@ -0,0 +1,23 @@ +"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(1, curryN)(1); +export const curry2 = curryN(2, curryN)(2); +export const curry3 = curryN(3, curryN)(3); +export const curry4 = curryN(4, curryN)(4); diff --git a/src/index.test.js b/src/index.test.js new file mode 100644 index 0000000..349975e --- /dev/null +++ b/src/index.test.js @@ -0,0 +1,91 @@ +import { expect } from 'chai'; +import { spy } from 'sinon'; +import { + curry, + curryN, + curry1, + curry2, + curry3, + curry4 +} from './index.js'; + +describe('fj-curry', () => { + + describe('curry', () => { + + it('should exist', () => { + expect(curry).to.be.ok; + }); + // t.plan(5); + // + // let spy = stub().returns(true); + // function func(a, b, c) { + // return spy.apply(this, arguments); + // } + // + // t.equal(typeof curry, 'function'); + // + // t.equal(curry(func)(1, 2)(3, 4), true); + // t.ok(spy.calledWith(1, 2, 3)); + // + // t.equal(curry(func)(1)(2, 3), true); + // t.ok(spy.calledWith(1, 2, 3)); + // }); + }); + + // it('fj-curry#curryN', (t) => { + // t.plan(7); + // + // let spy = stub().returns(true); + // + // t.equal(typeof curryN, 'function'); + // t.equal(curryN(1, spy)(1, 2), true); + // t.ok(spy.calledWith(1)); + // + // t.equal(curryN(3, spy)(1, 2)(3, 4), true); + // t.ok(spy.calledWith(1, 2, 3)); + // + // t.equal(curryN(3, spy)(1)(2, 3), true); + // t.ok(spy.calledWith(1, 2, 3)); + // }); + // + // test('fj-curry#curry1', (t) => { + // t.plan(3); + // + // let spy = stub().returns(true); + // + // t.equal(typeof curry1, 'function'); + // t.equal(curry1(spy)(1, 2), true); + // t.ok(spy.calledWith(1)); + // }); + // + // test('fj-curry#curry2', (t) => { + // t.plan(3); + // + // let spy = stub().returns(true); + // + // t.equal(typeof curry2, 'function'); + // t.equal(curry2(spy)(1)(2, 3), true); + // t.ok(spy.calledWith(1, 2)); + // }); + // + // test('fj-curry#curry3', (t) => { + // t.plan(3); + // + // let spy = stub().returns(true); + // + // t.equal(typeof curry3, 'function'); + // t.equal(curry3(spy)(1)(2)(3, 4), true); + // t.ok(spy.calledWith(1, 2, 3)); + // }); + // + // test('fj-curry#curry4', (t) => { + // t.plan(3); + // + // let spy = stub().returns(true); + // + // t.equal(typeof curry4, 'function'); + // t.equal(curry4(spy)(1)(2)(3)(4, 5), true); + // t.ok(spy.calledWith(1, 2, 3, 4)); + // }); +}) diff --git a/test/mocha.opts b/test/mocha.opts new file mode 100644 index 0000000..6880534 --- /dev/null +++ b/test/mocha.opts @@ -0,0 +1 @@ +src/index.test.js From e1f6af4f606bcc7b50e2f23037ecbbff5815810e Mon Sep 17 00:00:00 2001 From: Eric Churchill Date: Fri, 6 Jan 2017 22:03:02 -0800 Subject: [PATCH 4/6] rewrite tests for mocha and delete gulpfile --- gulpfile.js | 29 ------- package.json | 3 +- src/index.js | 7 +- src/index.test.js | 191 +++++++++++++++++++++++++++++----------------- 4 files changed, 124 insertions(+), 106 deletions(-) delete mode 100644 gulpfile.js diff --git a/gulpfile.js b/gulpfile.js deleted file mode 100644 index 072bce0..0000000 --- a/gulpfile.js +++ /dev/null @@ -1,29 +0,0 @@ -var gulp = require('gulp'), - watch = require('gulp-watch'), - run = require('gulp-run'), - sourcemaps = require('gulp-sourcemaps'), - rename = require('gulp-rename'), - to5 = require('gulp-6to5'); - -gulp.task('6to5', function() { - return gulp.src('**/*.es6') - .pipe(sourcemaps.init()) - .pipe(to5({ - experimental: true, - loose: 'all' - })) - .pipe(sourcemaps.write()) - .pipe(rename({ - extname: '.js' - })) - .pipe(gulp.dest('./')); -}); - -gulp.task('test', ['6to5'], function() { - return gulp.src('test.js') - .pipe(run('node test -b -l phantom -e -q')); -}); - -gulp.task('default', ['test'], function() { - gulp.watch('**/*.es6', ['test']); -}); diff --git a/package.json b/package.json index f11d197..0af5ce7 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "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" } } diff --git a/src/index.js b/src/index.js index 5c94e42..2f615a1 100644 --- a/src/index.js +++ b/src/index.js @@ -3,7 +3,6 @@ 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); @@ -17,7 +16,7 @@ export const curry = (fn) => _curry(fn.length, fn); export const curryN = (n, fn) => _curry(n, fn); -export const curry1 = curryN(1, curryN)(1); +export const curry1 = curryN(2, curryN)(1); export const curry2 = curryN(2, curryN)(2); -export const curry3 = curryN(3, curryN)(3); -export const curry4 = curryN(4, curryN)(4); +export const curry3 = curryN(2, curryN)(3); +export const curry4 = curryN(2, curryN)(4); diff --git a/src/index.test.js b/src/index.test.js index 349975e..bb693e3 100644 --- a/src/index.test.js +++ b/src/index.test.js @@ -1,4 +1,5 @@ -import { expect } from 'chai'; +import chai, { expect } from 'chai'; +import sinonChai from 'sinon-chai'; import { spy } from 'sinon'; import { curry, @@ -9,83 +10,129 @@ import { 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; }); - // t.plan(5); - // - // let spy = stub().returns(true); - // function func(a, b, c) { - // return spy.apply(this, arguments); - // } - // - // t.equal(typeof curry, 'function'); - // - // t.equal(curry(func)(1, 2)(3, 4), true); - // t.ok(spy.calledWith(1, 2, 3)); - // - // t.equal(curry(func)(1)(2, 3), true); - // t.ok(spy.calledWith(1, 2, 3)); - // }); + + 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; + }); }); - // it('fj-curry#curryN', (t) => { - // t.plan(7); - // - // let spy = stub().returns(true); - // - // t.equal(typeof curryN, 'function'); - // t.equal(curryN(1, spy)(1, 2), true); - // t.ok(spy.calledWith(1)); - // - // t.equal(curryN(3, spy)(1, 2)(3, 4), true); - // t.ok(spy.calledWith(1, 2, 3)); - // - // t.equal(curryN(3, spy)(1)(2, 3), true); - // t.ok(spy.calledWith(1, 2, 3)); - // }); - // - // test('fj-curry#curry1', (t) => { - // t.plan(3); - // - // let spy = stub().returns(true); - // - // t.equal(typeof curry1, 'function'); - // t.equal(curry1(spy)(1, 2), true); - // t.ok(spy.calledWith(1)); - // }); - // - // test('fj-curry#curry2', (t) => { - // t.plan(3); - // - // let spy = stub().returns(true); - // - // t.equal(typeof curry2, 'function'); - // t.equal(curry2(spy)(1)(2, 3), true); - // t.ok(spy.calledWith(1, 2)); - // }); - // - // test('fj-curry#curry3', (t) => { - // t.plan(3); - // - // let spy = stub().returns(true); - // - // t.equal(typeof curry3, 'function'); - // t.equal(curry3(spy)(1)(2)(3, 4), true); - // t.ok(spy.calledWith(1, 2, 3)); - // }); - // - // test('fj-curry#curry4', (t) => { - // t.plan(3); - // - // let spy = stub().returns(true); - // - // t.equal(typeof curry4, 'function'); - // t.equal(curry4(spy)(1)(2)(3)(4, 5), true); - // t.ok(spy.calledWith(1, 2, 3, 4)); - // }); -}) + 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; + }); + }); +}); From 3b75d5421037a4bb9a794205b4d3095915372f36 Mon Sep 17 00:00:00 2001 From: Eric Churchill Date: Sun, 8 Jan 2017 11:32:12 -0800 Subject: [PATCH 5/6] update .travis.yml --- .travis.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 453adf9..b6e7571 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 From bb74649bf1f6b4ec5449dacccdcf495f2c569690 Mon Sep 17 00:00:00 2001 From: Eric Churchill Date: Sun, 8 Jan 2017 11:49:28 -0800 Subject: [PATCH 6/6] update dev dependencies --- package.json | 2 ++ 1 file changed, 2 insertions(+) diff --git a/package.json b/package.json index 0af5ce7..e9f1bce 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,8 @@ "homepage": "https://github.com/fp-js/fj-curry", "devDependencies": { "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",