forked from threepointone/fn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
224 lines (199 loc) · 4.4 KB
/
index.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// fair caveat, this is code collected from various places, and I don't have tests yet. YET.
"use strict";
module.exports = {
isValue: isValue,
identity: identity,
indexOf: indexOf,
keys: keys,
values: values,
isArray: isArray,
toArray: toArray,
each: each,
extend: extend,
map: map,
times: times,
invoke: invoke,
filter: filter,
find: find,
reduce: reduce,
debounce: debounce,
compose: compose,
chain: chain
};
var slice = [].slice,
has = {}.hasOwnProperty,
toString = {}.toString;
function isValue(v) {
return v != null;
}
function identity(x) {
return x;
}
function indexOf(arr, obj) {
if (arr.indexOf) {
return arr.indexOf(obj);
}
for (var i = 0; i < arr.length; ++i) {
if (arr[i] === obj) {
return i;
}
}
return -1;
}
function keys(obj) {
if (obj !== Object(obj)) {
throw new TypeError('Invalid object');
}
var keys = [];
for (var key in obj) {
if (has.call(obj, key)) {
keys.push(key);
}
}
return keys;
}
function values(obj) {
var values = [];
for (var key in obj) {
if (has.call(obj, key)) {
values.push(obj[key]);
}
}
return values;
}
function isArray(obj) {
if (Array.isArray) {
return Array.isArray(obj);
}
return toString.call(obj) === '[object Array]';
}
function toArray(obj) {
if (!obj) {
return [];
}
if (isArray(obj)) {
return slice.call(obj);
}
if (obj.length === +obj.length) {
return map(obj, identity);
}
return values(obj);
}
function each(obj, fn) {
if (isArray(obj)) {
for (var i = 0, j = obj.length; i < j; i++) {
fn(obj[i], i);
}
} else {
for (var prop in obj) {
if (has.call(obj, prop)) {
fn(obj[prop], prop);
}
}
}
}
function extend(obj) {
var args = slice.call(arguments, 1);
for (var i = 0, j = args.length; i < j; i++) {
var source = args[i];
for (var prop in source) {
if (has.call(source, prop)) {
obj[prop] = source[prop];
}
}
}
return obj;
}
function map(obj, fn) {
var arr = [];
each(obj, function(v, k) {
arr.push(fn(v, k));
});
return arr;
}
function times(n, fn) {
var arr = [];
for (var i = 0; i < n; i++) {
arr[i] = fn(i);
}
return arr;
}
function invoke(obj, fnName) {
var args = slice.call(arguments, 2);
return map(obj, function(v) {
return v[fnName].apply(v, args);
});
}
function filter(arr, fn) {
var ret = [];
for (var i = 0, j = arr.length; i < j; i++) {
if (fn(arr[i], i)) {
ret.push(arr[i]);
}
}
return ret;
}
function find(arr, fn) {
for (var i = 0, j = arr.length; i < j; i++) {
if (fn(arr[i], i)) {
return arr[i];
}
}
return null;
}
function reduce(arr, fn, initial) {
var idx = 0;
var len = arr.length;
var curr = arguments.length === 3 ? initial : arr[idx++];
while (idx < len) {
curr = fn.call(null, curr, arr[idx], ++idx, arr);
}
return curr;
}
function debounce(func, wait, immediate) {
var result;
var timeout = null;
return function() {
var context = this,
args = arguments;
var later = function() {
timeout = null;
if (!immediate) {
result = func.apply(context, args);
}
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) {
result = func.apply(context, args);
}
return result;
};
}
function compose() {
var funcs = arguments;
return function() {
var args = arguments;
each(funcs, function(fn) {
args = [fn.apply(this, args)];
});
return args[0];
};
}
// chaining, à la underscore
function chain(obj) {
if (!(this instanceof chain)) {
return new chain(obj);
}
this._obj = obj;
}
each(module.exports, function(fn, name) {
chain.prototype[name] = function() {
this._obj = fn.apply(this, [this._obj].concat(slice.call(arguments, 0)));
return this;
};
});
chain.prototype.val = function() {
return this._obj;
};