forked from montagejs/collections
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shim-function.js
59 lines (50 loc) · 1.81 KB
/
shim-function.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
module.exports = Function;
/**
A utility to reduce unnecessary allocations of <code>function () {}</code>
in its many colorful variations. It does nothing and returns
<code>undefined</code> thus makes a suitable default in some circumstances.
@function external:Function.noop
*/
Function.noop = function () {
};
/**
A utility to reduce unnecessary allocations of <code>function (x) {return
x}</code> in its many colorful but ultimately wasteful parameter name
variations.
@function external:Function.identity
@param {Any} any value
@returns {Any} that value
*/
Function.identity = function (value) {
return value;
};
/**
A utility for creating a comparator function for a particular aspect of a
figurative class of objects.
@function external:Function.by
@param {Function} relation A function that accepts a value and returns a
corresponding value to use as a representative when sorting that object.
@param {Function} compare an alternate comparator for comparing the
represented values. The default is <code>Object.compare</code>, which
does a deep, type-sensitive, polymorphic comparison.
@returns {Function} a comparator that has been annotated with
<code>by</code> and <code>compare</code> properties so
<code>sorted</code> can perform a transform that reduces the need to call
<code>by</code> on each sorted object to just once.
*/
Function.by = function (by , compare) {
compare = compare || Object.compare;
by = by || Function.identity;
var compareBy = function (a, b) {
return compare(by(a), by(b));
};
compareBy.compare = compare;
compareBy.by = by;
return compareBy;
};
// TODO document
Function.get = function (key) {
return function (object) {
return Object.get(object, key);
};
};