-
Notifications
You must be signed in to change notification settings - Fork 0
/
underscore.js
210 lines (179 loc) · 4.64 KB
/
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
var underscore = require('underscore');
var _ = exports;
var map = function(list, results, transform) {
if (list.length === 0){
return results;
} else {
return map(list.slice(1), results.concat(transform(list[0], results.length)), transform);
}
};
exports.map = function(list, transform) {
return map(list, [], transform);
};
var filter = function(list, results, condition) {
if (list.length === 0){
return results;
} else {
return filter(list.slice(1), (condition(list[0]) ? results.concat(list[0]) : results), condition);
}
}
exports.filter = function(list, condition) {
return filter(list, [], condition);
};
exports.reduce = function(list, iterator, result, context) {
if (arguments.length < 3) {
return exports.reduce(list.slice(1), iterator, list[0]);
} else if (list.length === 0) {
return result;
} else {
return exports.reduce(list.slice(1), iterator, iterator(result, list[0]), context)
}
}
var range = function(num, stop, step) {
if (num >= stop) {
return [];
} else {
return [num].concat(range(num + step, stop, step));
}
};
//rewrite with def!!
exports.range = function() {
var start = 0, stop, step = 1, list = [];
if (arguments.length === 1) {
stop = arguments[0];
} else if (arguments.length === 2) {
start = arguments[0];
stop = arguments[1];
} else {
start = arguments[0];
stop = arguments[1];
step = arguments[2];
}
return range(start, stop, step);
}
exports.union = function(){
return underscore.uniq(Array.prototype.concat.apply([], arguments));
}
var intersection = function(a, b) {
var results = [];
for (i = 0; i < a.length; i += 1) {
if (b.indexOf(a[i]) >= 0) {
results.push(a[i]);
}
}
return results;
};
var intersection = function(a, b) {
return exports.filter(a, function(val) {
return b.indexOf(val) >= 0;
});
};
exports.intersection = function() {
var lists = Array.prototype.slice.apply(arguments);
return underscore.uniq(exports.reduce(lists, intersection));
}
var difference = function(a, b) {
return exports.filter(a, function(val) { return b.indexOf(val) < 0; });
};
exports.difference = function() {
var lists = Array.prototype.slice.apply(arguments);
return exports.reduce(lists, difference);
}
exports.uniq = function(array, iterator, isSorted) {
if (array.length === 0){
return [];
} else {
var first = array[0];
var rest = array.slice(1);
return [first].concat(exports.uniq(exports.filter(rest, function(val){
return val !== first;
})));
}
};
exports.isArray = function(val) {
return Object.prototype.toString.call(val) === '[object Array]';
};
var shallow = function(){
if (array.length === 0){
return [];
} else {
var first = array[0];
var rest = array.slice(1);
if (first = []){
return first.concat(rest)}
}
};
_.flatten = function(array, shallow) {
if (shallow) {
return Array.prototype.concat.apply([], array);
} else if (array.length === 0){
return [];
} else {
var first = array[0];
return (_.isArray(first) ? _.flatten(first) : [first])
.concat(_.flatten(array.slice(1)));
}
};
_.pluck = function(list, propertyName){
return _.map(list, function(obj){
return obj[propertyName];
});
};
_.each = function(list, iterator) {
var type = Object.prototype.toString.call(list);
if (type === '[object Array]') {
for (var i = 0; i < list.length; i += 1) {
iterator(list[i], i, list);
}
} else if (type === '[object Object]') {
for (var prop in list) {
iterator(list[value], prop, list);
}
}
};
_.invoke = function(list, methodName) {
_.each(list, function(element) {
element[methodName]();
})
};
//console.log(_.invoke(['5', '7', '9', 'bob'], 'toUpperCase'));
_.contains = function(list, value) {
return list.indexOf(value) >= 0
};
//console.log(_.contains([5, 8, 9, 3], 8));
_.find = function(list, predicate) {
for (i = 0; i < list.length; i += 1) {
if (predicate(list[i])) {
return list[i];
}
}
};
//console.log(_.find([5, 8, 9, 10],function(num){ return num % 2 == 0; }));
var list = [
{a: 1, b: 2},
{a: 2, b: 2},
{a: 1, b: 3},
{a: 1, b: 4},
{a: 2, b: 4}
];
_.findWhere = function(list, properties) {
for (var i = 0; i < list.length; i += 1) {
if (_.isMatch(list[i], properties)) {
return list[i];
}
}
};
//console.log(_.findWhere(list, {a: 1, b: 3}));
_.isMatch = function(obj, properties) {
for (var key in properties) {
if (obj[key] !== properties[key]) {
return false;
}
}
return true;
}
_.where = function(list, properties) {
return _.filter(list, function(obj) {
return _.isMatch(obj, properties);
});
};