Skip to content

Commit

Permalink
feat: allow plain function usage of fn.compose
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Mar 11, 2019
1 parent 0943d8c commit 2bafef7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,10 @@ Applies the functions in reverse argument-list order.

_f1.compose(f2, f3, f4)(…args) =def f1(f2(f3(f4(…arg))))_

`compose` can also be used in plain function form as:

_compose(f1, f2, f3, f4)(…args) =def f1(f2(f3(f4(…arg))))_

#### fn.copy() _(es5-ext/function/#/copy)_

Produces copy of given function
Expand Down
6 changes: 4 additions & 2 deletions function/#/compose.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";

var callable = require("../../object/valid-callable")
var isValue = require("../../object/is-value")
, callable = require("../../object/valid-callable")
, aFrom = require("../../array/from");

var apply = Function.prototype.apply
Expand All @@ -9,7 +10,8 @@ var apply = Function.prototype.apply

module.exports = function (fnIgnored/*, …fnn*/) {
var fns, first;
fns = [this].concat(aFrom(arguments));
var args = aFrom(arguments);
fns = isValue(this) ? [this].concat(args) : args;
fns.forEach(callable);
fns = fns.reverse();
first = fns[0];
Expand Down
5 changes: 4 additions & 1 deletion test/function/#/compose.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ var f = function (a, b) { return ["a", arguments.length, a, b]; }
, g = function (a) { return ["b", arguments.length].concat(a); }
, h = function (a) { return ["c", arguments.length].concat(a); };

module.exports = function (t, a) { a.deep(t.call(h, g, f)(1, 2), ["c", 1, "b", 1, "a", 2, 1, 2]); };
module.exports = function (t, a) {
a.deep(t.call(h, g, f)(1, 2), ["c", 1, "b", 1, "a", 2, 1, 2]);
a.deep(t(h, g, f)(1, 2), ["c", 1, "b", 1, "a", 2, 1, 2]);
};

0 comments on commit 2bafef7

Please sign in to comment.