forked from codemix/fast.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
241 lines (210 loc) · 6.11 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
'use strict';
/**
* # Constructor
*
* Provided as a convenient wrapper around Fast functions.
*
* ```js
* var arr = fast([1,2,3,4,5,6]);
*
* var result = arr.filter(function (item) {
* return item % 2 === 0;
* });
*
* result instanceof Fast; // true
* result.length; // 3
* ```
*
*
* @param {Array} value The value to wrap.
*/
function Fast (value) {
if (!(this instanceof Fast)) {
return new Fast(value);
}
this.value = value || [];
}
module.exports = exports = Fast;
Fast.array = require('./array');
Fast['function'] = Fast.fn = require('./function');
Fast.object = require('./object');
Fast.string = require('./string');
Fast.apply = Fast['function'].apply;
Fast.bind = Fast['function'].bind;
Fast.partial = Fast['function'].partial;
Fast.partialConstructor = Fast['function'].partialConstructor;
Fast['try'] = Fast.attempt = Fast['function']['try'];
Fast.assign = Fast.object.assign;
Fast.cloneObject = Fast.object.clone; // @deprecated use fast.object.clone()
Fast.keys = Fast.object.keys;
Fast.values = Fast.object.values;
Fast.clone = require('./clone');
Fast.map = require('./map');
Fast.filter = require('./filter');
Fast.forEach = require('./forEach');
Fast.reduce = require('./reduce');
Fast.reduceRight = require('./reduceRight');
Fast.cloneArray = Fast.array.clone; // @deprecated use fast.array.clone()
Fast.concat = Fast.array.concat;
Fast.some = Fast.array.some;
Fast.every = Fast.array.every;
Fast.indexOf = Fast.array.indexOf;
Fast.lastIndexOf = Fast.array.lastIndexOf;
Fast.pluck = Fast.array.pluck;
Fast.fill = Fast.array.fill;
Fast.intern = Fast.string.intern;
/**
* # Concat
*
* Concatenate multiple arrays.
*
* @param {Array|mixed} item, ... The item(s) to concatenate.
* @return {Fast} A new Fast object, containing the results.
*/
Fast.prototype.concat = function Fast$concat () {
var length = this.value.length,
arr = new Array(length),
i, item, childLength, j;
for (i = 0; i < length; i++) {
arr[i] = this.value[i];
}
length = arguments.length;
for (i = 0; i < length; i++) {
item = arguments[i];
if (Array.isArray(item)) {
childLength = item.length;
for (j = 0; j < childLength; j++) {
arr.push(item[j]);
}
}
else {
arr.push(item);
}
}
return new Fast(arr);
};
/**
* Fast Map
*
* @param {Function} fn The visitor function.
* @param {Object} thisContext The context for the visitor, if any.
* @return {Fast} A new Fast object, containing the results.
*/
Fast.prototype.map = function Fast$map (fn, thisContext) {
return new Fast(Fast.map(this.value, fn, thisContext));
};
/**
* Fast Filter
*
* @param {Function} fn The filter function.
* @param {Object} thisContext The context for the filter function, if any.
* @return {Fast} A new Fast object, containing the results.
*/
Fast.prototype.filter = function Fast$filter (fn, thisContext) {
return new Fast(Fast.filter(this.value, fn, thisContext));
};
/**
* Fast Reduce
*
* @param {Function} fn The reducer function.
* @param {mixed} initialValue The initial value, if any.
* @param {Object} thisContext The context for the reducer, if any.
* @return {mixed} The final result.
*/
Fast.prototype.reduce = function Fast$reduce (fn, initialValue, thisContext) {
return Fast.reduce(this.value, fn, initialValue, thisContext);
};
/**
* Fast Reduce Right
*
* @param {Function} fn The reducer function.
* @param {mixed} initialValue The initial value, if any.
* @param {Object} thisContext The context for the reducer, if any.
* @return {mixed} The final result.
*/
Fast.prototype.reduceRight = function Fast$reduceRight (fn, initialValue, thisContext) {
return Fast.reduceRight(this.value, fn, initialValue, thisContext);
};
/**
* Fast For Each
*
* @param {Function} fn The visitor function.
* @param {Object} thisContext The context for the visitor, if any.
* @return {Fast} The Fast instance.
*/
Fast.prototype.forEach = function Fast$forEach (fn, thisContext) {
Fast.forEach(this.value, fn, thisContext);
return this;
};
/**
* Fast Some
*
* @param {Function} fn The matcher predicate.
* @param {Object} thisContext The context for the matcher, if any.
* @return {Boolean} True if at least one element matches.
*/
Fast.prototype.some = function Fast$some (fn, thisContext) {
return Fast.some(this.value, fn, thisContext);
};
/**
* Fast Every
*
* @param {Function} fn The matcher predicate.
* @param {Object} thisContext The context for the matcher, if any.
* @return {Boolean} True if at all elements match.
*/
Fast.prototype.every = function Fast$every (fn, thisContext) {
return Fast.some(this.value, fn, thisContext);
};
/**
* Fast Index Of
*
* @param {mixed} target The target to lookup.
* @param {Number} fromIndex The index to start searching from, if known.
* @return {Number} The index of the item, or -1 if no match found.
*/
Fast.prototype.indexOf = function Fast$indexOf (target, fromIndex) {
return Fast.indexOf(this.value, target, fromIndex);
};
/**
* Fast Last Index Of
*
* @param {mixed} target The target to lookup.
* @param {Number} fromIndex The index to start searching from, if known.
* @return {Number} The last index of the item, or -1 if no match found.
*/
Fast.prototype.lastIndexOf = function Fast$lastIndexOf (target, fromIndex) {
return Fast.lastIndexOf(this.value, target, fromIndex);
};
/**
* Reverse
*
* @return {Fast} A new Fast instance, with the contents reversed.
*/
Fast.prototype.reverse = function Fast$reverse () {
return new Fast(this.value.reverse());
};
/**
* Value Of
*
* @return {Array} The wrapped value.
*/
Fast.prototype.valueOf = function Fast$valueOf () {
return this.value;
};
/**
* To JSON
*
* @return {Array} The wrapped value.
*/
Fast.prototype.toJSON = function Fast$toJSON () {
return this.value;
};
/**
* Item Length
*/
Object.defineProperty(Fast.prototype, 'length', {
get: function () {
return this.value.length;
}
});