/
collect.js
42 lines (39 loc) · 1.1 KB
/
collect.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
import _makeTypeErrorFor from "../privates/_makeTypeErrorFor";
import map from "../core/map";
import applyTo from "./applyTo";
/**
* Accepts a series of functions and builds a new function. The functions in the series
* will then be applied, in order, with the values received by the function built with
* <code>collect</code>.<br/>
* The collected results will be returned in an array.
* @example
* var user = {
* id: "jdoe",
* name: "John",
* surname: "Doe",
* scores: [2, 4, 7]
* };
* var getIDAndLastScore = _.collect([_.getKey("id"), _.getPath("scores.-1")]);
*
* getIDAndLastScore(user) // => ["jdoe", 7]
*
* @example
* var minAndMax = _.collect([Math.min, Math.max]);
*
* minAndMax(3, 1, -2, 5, 4, -1) // => [-2, 5]
*
* @memberof module:lamb
* @category Function
* @since 0.35.0
* @param {Function[]} functions
* @returns {Function}
*/
function collect (functions) {
if (!Array.isArray(functions)) {
throw _makeTypeErrorFor(functions, "array");
}
return function () {
return map(functions, applyTo(arguments));
};
}
export default collect;