Permalink
Browse files

cleaned up code, handle array arguments, added array tests and initia…

…l arguments test
  • Loading branch information...
1 parent 563eeb5 commit 75acea44d0667070daf03fd1446def343f92bac1 @rook2pawn committed Jan 7, 2017
Showing with 43 additions and 40 deletions.
  1. +0 −22 README.md
  2. +18 −13 index.js
  3. +2 −2 package.json
  4. +23 −3 test/test-basic.js
View
@@ -42,25 +42,3 @@ Right side partial function application is through .rapply i.e.
var baz = bar(3,7); // y = 3, z = 7
baz(2); // (2*3) + 7 = 13
-
-testing
-=======
-
-testing results (nodeunit):
-
- $ nodeunit *
-
- advanced.js
- ✔ testfn
- ✔ testpartial
- ✔ testpartialRight
-
- basic.js
- ✔ testfn
- ✔ testpartial
- ✔ testpartialRight
-
- OK: 16 assertions (62ms)
-
-
-partial was inspired (and partially lifted) by [partial/curry library ap - substack](https://github.com/substack/node-ap)
View
@@ -1,24 +1,29 @@
-var exports = module.exports = function() {
- var fn = arguments[0];
- var args = [].concat.apply([],arguments).slice(1);
+var partial = function() {
+ var _args = Array.from(arguments);
+ var fn = _args[0];
+ var args = _args.slice(1);
return function() {
- if ((arguments.length + args.length) >= fn.length) {
- return fn.apply(fn, [].concat.apply(args,arguments));
+ if (arguments.length + args.length >= fn.length) {
+ var x = args.concat(Array.from(arguments));
+ return fn(...x);
} else {
- return exports(fn,[].concat.apply(args,arguments));
+ var x = args.concat(Array.from(arguments));
+ return partial(fn,...x)
}
};
};
-
-exports.rapply = function() {
- var fn = arguments[0];
- var args = [].concat.apply([],arguments).slice(1);
+partial.rapply = function() {
+ var _args = Array.from(arguments);
+ var fn = _args[0];
+ var args = _args.slice(1);
return function() {
- var right = [].concat.apply([],arguments);
if ((arguments.length + args.length) >= fn.length) {
- return fn.apply(fn, [].concat.apply(right,args));
+ var x = Array.from(arguments).concat(args);
+ return fn(...x);
} else {
- return exports.rapply(fn,[].concat.apply(right,args));
+ var x = Array.from(arguments).concat(args);
+ return partial.rapply(fn,...x);
}
};
};
+var exports = module.exports = partial;
View
@@ -2,7 +2,7 @@
"author": "David Wee <rook2pawn@gmail.com> (http://rook2pawn.com)",
"name": "partial",
"description": "partial function application - transform f into its partial by partial(f)",
- "version": "0.0.4",
+ "version": "0.0.5",
"homepage": "https://github.com/rook2pawn/node-partial",
"scripts": {
"test": "tape test/test*.js"
@@ -14,6 +14,6 @@
"main": "index.js",
"dependencies": {},
"devDependencies": {
- "tape": "^4.5.1"
+ "tape": "^4.6.3"
}
}
View
@@ -4,27 +4,47 @@ var m3 = function(x,y,z) {
return (x*y) + z;
};
-var foo = partial(m3);
-var baz = partial.rapply(m3);
-
// test that you have a function
test('test the partial is a function', function(t) {
+ var foo = partial(m3);
+ var baz = partial.rapply(m3);
+
t.plan(2);
t.equal('function',typeof foo);
t.equal('function',typeof baz);
});
// tests basic functionality of partial
test('test basic functionality of partial', function(t) {
+ var foo = partial(m3);
+
t.plan(3);
t.equal(foo(2,3,4),10);
t.equal(foo(2,3)(4),10);
t.equal(foo(2,3,4),foo(2,3)(4));
});
+// test argument loading at start of partial declaration
+test('test argument loading at start of partial declaration', function(t) {
+ var foo = partial(m3,2,3);
+ t.plan(1);
+ t.equal(foo(4), 10);
+});
// tests basic functionality of partial.rapply
test('test basic functionality of partial right apply', function(t) {
+ var baz = partial.rapply(m3);
t.plan(3);
t.equal(baz(2,3,4),10);
t.equal(baz(4)(2,3),10);
t.equal(baz(2,3,4),baz(4)(2,3));
})
+
+test('test partial with arrays for arguments', function(t) {
+ t.plan(2);
+ var m = function(logOne, logTwo) {
+ return {one:logOne, two:logTwo}
+ };
+ var foo = partial(m, ['a','b','c']);
+ var result = foo('done');
+ t.equal(typeof foo, 'function')
+ t.deepEqual(result, {one:['a','b','c'], two:'done'})
+})

0 comments on commit 75acea4

Please sign in to comment.