-
Notifications
You must be signed in to change notification settings - Fork 11
/
apply.js
32 lines (32 loc) · 984 Bytes
/
apply.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
function toErrorMessage (type, path) {
return 'depject/' + type + ': no functions available at:' + path
}
module.exports = {
reduce: function (funs, path) {
return function (value, context) {
if (!funs.length) throw new Error(toErrorMessage('reduce', path))
return funs.reduce(function (value, fn) {
return fn(value, context)
}, value)
}
},
first: function (funs, path) {
return function (value) {
if (!funs.length) throw new Error(toErrorMessage('first', path))
var args = [].slice.call(arguments)
for (var i = 0; i < funs.length; i++) {
var _value = funs[i].apply(this, args)
if (_value !== undefined) return _value
}
}
},
map: function (funs, path) {
return function (value) {
if (!funs.length) throw new Error(toErrorMessage('map', path))
var args = [].slice.call(arguments)
return funs.map(function (fn) {
return fn.apply(this, args)
})
}
}
}