This repository has been archived by the owner on Jan 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 86
/
angular-underscore.js
157 lines (131 loc) · 3.55 KB
/
angular-underscore.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
(function (ng, _) {
'use strict';
var
underscoreModule = ng.module('angular-underscore', []),
utilsModule = ng.module('angular-underscore/utils', []),
filtersModule = ng.module('angular-underscore/filters', []);
// begin custom _
function propGetterFactory(prop) {
return function(obj) {return obj[prop];};
}
_._ = _;
// Shiv "min", "max" ,"sortedIndex" to accept property predicate.
_.each(['min', 'max', 'sortedIndex'], function(fnName) {
_[fnName] = _.wrap(_[fnName], function(fn) {
var args = _.toArray(arguments).slice(1);
if(_.isString(args[2])) {
// for "sortedIndex", transmuting str to property getter
args[2] = propGetterFactory(args[2]);
}
else if(_.isString(args[1])) {
// for "min" or "max", transmuting str to property getter
args[1] = propGetterFactory(args[1]);
}
return fn.apply(_, args);
});
});
// Shiv "filter", "reject" to angular's built-in,
// and reserve underscore's feature(works on obj).
ng.injector(['ng']).invoke(['$filter', function($filter) {
_.filter = _.select = _.wrap($filter('filter'), function(filter, obj, exp, comparator) {
if(!(_.isArray(obj))) {
obj = _.toArray(obj);
}
return filter(obj, exp, comparator);
});
_.reject = function(obj, exp) {
// use angular built-in negated predicate
if(_.isString(exp)) {
return _.filter(obj, '!' + exp);
}
var diff = _.bind(_.difference, _, obj);
return diff(_.filter(obj, exp));
};
}]);
// end custom _
// begin register angular-underscore/utils
_.each(_.methods(_), function(methodName) {
function register($rootScope) {$rootScope[methodName] = _.bind(_[methodName], _);}
_.each([
underscoreModule,
utilsModule,
ng.module('angular-underscore/utils/' + methodName, [])
], function(module) {
module.run(['$rootScope', register]);
});
});
// end register angular-underscore/utils
// begin register angular-underscore/filters
var
adapList = [
['map', 'collect'],
['reduce', 'inject', 'foldl'],
['reduceRight', 'foldr'],
['find', 'detect'],
['filter', 'select'],
'where',
'findWhere',
'reject',
'invoke',
'pluck',
'max',
'min',
'sortBy',
'groupBy',
'indexBy',
'countBy',
'shuffle',
'sample',
'toArray',
'size',
['first', 'head', 'take'],
'initial',
'last',
['rest', 'tail', 'drop'],
'compact',
'flatten',
'without',
'partition',
'union',
'intersection',
'difference',
['uniq', 'unique'],
'zip',
'object',
'indexOf',
'lastIndexOf',
'sortedIndex',
'keys',
'values',
'pairs',
'invert',
['functions', 'methods'],
'pick',
'omit',
'tap',
'identity',
'uniqueId',
'escape',
'unescape',
'result',
'template'
];
_.each(adapList, function(filterNames) {
if(!(_.isArray(filterNames))) {
filterNames = [filterNames];
}
var
filter = _.bind(_[filterNames[0]], _),
filterFactory = function() {return filter;};
_.each(filterNames, function(filterName) {
_.each([
underscoreModule,
filtersModule,
ng.module('angular-underscore/filters/' + filterName, [])
], function(module) {
module.filter(filterName, filterFactory);
});
});
});
// end register angular-underscore/filters
}(angular, _));