-
Notifications
You must be signed in to change notification settings - Fork 1
/
curry.class.js
42 lines (33 loc) · 1.24 KB
/
curry.class.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
var _ = require("lodash");
var Curry = function (placeholded, map, counter) {
this.placeholded = placeholded;
this.map = map || {};
this.sequentialArgCounter = counter || 0;
};
Curry.prototype.parseInterpolateArguments = function () {
var args = Array.prototype.slice.call(arguments);
var that = this;
if(!_.isArray(args[0]) && !_.isObject(args[0])) {
//neither array, nor object but a list of arguments were provided
return _.transform(args, function (accumulator, value) {
accumulator[that.sequentialArgCounter] = value;
that.sequentialArgCounter++;
}, {});
} else {
return this.placeholded.parseInterpolateArguments.apply(this.placeholded, args);
}
};
Curry.prototype.interpolate = function () {
var providedMap = this.parseInterpolateArguments.apply(this, arguments);
var newMap = _.extend({}, this.map, providedMap);
var unprovidedTokens = this.placeholded.unprovidedTokens(newMap);
if(unprovidedTokens) {
return (new Curry(this.placeholded, newMap, this.sequentialArgCounter)).interpolateClosure();
} else {
return this.placeholded.interpolate(newMap);
}
};
Curry.prototype.interpolateClosure = function () {
return _.callback(this.interpolate, this);
};
module.exports = Curry;