collection.js
10.1 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/**
* @fileoverview This module has some functions for handling object as collection.
* @author NHN.
* FE Development Lab <dl_javascript@nhn.com>
*/
'use strict';
var type = require('./type');
var object = require('./object');
/**
* Execute the provided callback once for each element present
* in the array(or Array-like object) in ascending order.<br>
* If the callback function returns false, the loop will be stopped.<br>
* Callback function(iteratee) is invoked with three arguments:
* - The value of the element
* - The index of the element
* - The array(or Array-like object) being traversed
* @param {Array} arr The array(or Array-like object) that will be traversed
* @param {function} iteratee Callback function
* @param {Object} [context] Context(this) of callback function
* @memberof tui.util
* @example
* //-- #1. Get Module --//
* var util = require('tui-code-snippet'); // node, commonjs
* var util = tui.util; // distribution file
*
* //-- #2. Use property --//
* var sum = 0;
*
* util.forEachArray([1,2,3], function(value){
* sum += value;
* });
* alert(sum); // 6
*/
function forEachArray(arr, iteratee, context) {
var index = 0;
var len = arr.length;
context = context || null;
for (; index < len; index += 1) {
if (iteratee.call(context, arr[index], index, arr) === false) {
break;
}
}
}
/**
* Execute the provided callback once for each property of object which actually exist.<br>
* If the callback function returns false, the loop will be stopped.<br>
* Callback function(iteratee) is invoked with three arguments:
* - The value of the property
* - The name of the property
* - The object being traversed
* @param {Object} obj The object that will be traversed
* @param {function} iteratee Callback function
* @param {Object} [context] Context(this) of callback function
* @memberof tui.util
* @example
* //-- #1. Get Module --//
* var util = require('tui-code-snippet'); // node, commonjs
* var util = tui.util; // distribution file
*
* //-- #2. Use property --//
* var sum = 0;
*
* util.forEachOwnProperties({a:1,b:2,c:3}, function(value){
* sum += value;
* });
* alert(sum); // 6
**/
function forEachOwnProperties(obj, iteratee, context) {
var key;
context = context || null;
for (key in obj) {
if (obj.hasOwnProperty(key)) {
if (iteratee.call(context, obj[key], key, obj) === false) {
break;
}
}
}
}
/**
* Execute the provided callback once for each property of object(or element of array) which actually exist.<br>
* If the object is Array-like object(ex-arguments object), It needs to transform to Array.(see 'ex2' of example).<br>
* If the callback function returns false, the loop will be stopped.<br>
* Callback function(iteratee) is invoked with three arguments:
* - The value of the property(or The value of the element)
* - The name of the property(or The index of the element)
* - The object being traversed
* @param {Object} obj The object that will be traversed
* @param {function} iteratee Callback function
* @param {Object} [context] Context(this) of callback function
* @memberof tui.util
* @example
* //-- #1. Get Module --//
* var util = require('tui-code-snippet'); // node, commonjs
* var util = tui.util; // distribution file
*
* //-- #2. Use property --//
* var sum = 0;
*
* util.forEach([1,2,3], function(value){
* sum += value;
* });
* alert(sum); // 6
*
* // In case of Array-like object
* var array = Array.prototype.slice.call(arrayLike); // change to array
* util.forEach(array, function(value){
* sum += value;
* });
*/
function forEach(obj, iteratee, context) {
if (type.isArray(obj)) {
forEachArray(obj, iteratee, context);
} else {
forEachOwnProperties(obj, iteratee, context);
}
}
/**
* Execute the provided callback function once for each element in an array, in order,
* and constructs a new array from the results.<br>
* If the object is Array-like object(ex-arguments object),
* It needs to transform to Array.(see 'ex2' of forEach example)<br>
* Callback function(iteratee) is invoked with three arguments:
* - The value of the property(or The value of the element)
* - The name of the property(or The index of the element)
* - The object being traversed
* @param {Object} obj The object that will be traversed
* @param {function} iteratee Callback function
* @param {Object} [context] Context(this) of callback function
* @returns {Array} A new array composed of returned values from callback function
* @memberof tui.util
* @example
* //-- #1. Get Module --//
* var util = require('tui-code-snippet'); // node, commonjs
* var util = tui.util; // distribution file
*
* //-- #2. Use property --//
* var result = util.map([0,1,2,3], function(value) {
* return value + 1;
* });
*
* alert(result); // 1,2,3,4
*/
function map(obj, iteratee, context) {
var resultArray = [];
context = context || null;
forEach(obj, function() {
resultArray.push(iteratee.apply(context, arguments));
});
return resultArray;
}
/**
* Execute the callback function once for each element present in the array(or Array-like object or plain object).<br>
* If the object is Array-like object(ex-arguments object),
* It needs to transform to Array.(see 'ex2' of forEach example)<br>
* Callback function(iteratee) is invoked with four arguments:
* - The previousValue
* - The currentValue
* - The index
* - The object being traversed
* @param {Object} obj The object that will be traversed
* @param {function} iteratee Callback function
* @param {Object} [context] Context(this) of callback function
* @returns {*} The result value
* @memberof tui.util
* @example
* //-- #1. Get Module --//
* var util = require('tui-code-snippet'); // node, commonjs
* var util = tui.util; // distribution file
*
* //-- #2. Use property --//
* var result = util.reduce([0,1,2,3], function(stored, value) {
* return stored + value;
* });
*
* alert(result); // 6
*/
function reduce(obj, iteratee, context) {
var index = 0;
var keys, length, store;
context = context || null;
if (!type.isArray(obj)) {
keys = object.keys(obj);
length = keys.length;
store = obj[keys[index += 1]];
} else {
length = obj.length;
store = obj[index];
}
index += 1;
for (; index < length; index += 1) {
store = iteratee.call(context, store, obj[keys ? keys[index] : index]);
}
return store;
}
/**
* Transform the Array-like object to Array.<br>
* In low IE (below 8), Array.prototype.slice.call is not perfect. So, try-catch statement is used.
* @param {*} arrayLike Array-like object
* @returns {Array} Array
* @memberof tui.util
* @example
* //-- #1. Get Module --//
* var util = require('tui-code-snippet'); // node, commonjs
* var util = tui.util; // distribution file
*
* //-- #2. Use property --//
* var arrayLike = {
* 0: 'one',
* 1: 'two',
* 2: 'three',
* 3: 'four',
* length: 4
* };
* var result = util.toArray(arrayLike);
*
* alert(result instanceof Array); // true
* alert(result); // one,two,three,four
*/
function toArray(arrayLike) {
var arr;
try {
arr = Array.prototype.slice.call(arrayLike);
} catch (e) {
arr = [];
forEachArray(arrayLike, function(value) {
arr.push(value);
});
}
return arr;
}
/**
* Create a new array or plain object with all elements(or properties)
* that pass the test implemented by the provided function.<br>
* Callback function(iteratee) is invoked with three arguments:
* - The value of the property(or The value of the element)
* - The name of the property(or The index of the element)
* - The object being traversed
* @param {Object} obj Object(plain object or Array) that will be traversed
* @param {function} iteratee Callback function
* @param {Object} [context] Context(this) of callback function
* @returns {Object} plain object or Array
* @memberof tui.util
* @example
* //-- #1. Get Module --//
* var util = require('tui-code-snippet'); // node, commonjs
* var util = tui.util; // distribution file
*
* //-- #2. Use property --//
* var result1 = util.filter([0,1,2,3], function(value) {
* return (value % 2 === 0);
* });
* alert(result1); // [0, 2]
*
* var result2 = util.filter({a : 1, b: 2, c: 3}, function(value) {
* return (value % 2 !== 0);
* });
* alert(result2.a); // 1
* alert(result2.b); // undefined
* alert(result2.c); // 3
*/
function filter(obj, iteratee, context) {
var result, add;
context = context || null;
if (!type.isObject(obj) || !type.isFunction(iteratee)) {
throw new Error('wrong parameter');
}
if (type.isArray(obj)) {
result = [];
add = function(subResult, args) {
subResult.push(args[0]);
};
} else {
result = {};
add = function(subResult, args) {
subResult[args[1]] = args[0];
};
}
forEach(obj, function() {
if (iteratee.apply(context, arguments)) {
add(result, arguments);
}
}, context);
return result;
}
/**
* fetching a property
* @param {Array} arr target collection
* @param {String|Number} property property name
* @returns {Array}
* @memberof tui.util
* @example
* //-- #1. Get Module --//
* var util = require('tui-code-snippet'); // node, commonjs
* var util = tui.util; // distribution file
*
* //-- #2. Use property --//
* var objArr = [
* {'abc': 1, 'def': 2, 'ghi': 3},
* {'abc': 4, 'def': 5, 'ghi': 6},
* {'abc': 7, 'def': 8, 'ghi': 9}
* ];
* var arr2d = [
* [1, 2, 3],
* [4, 5, 6],
* [7, 8, 9]
* ];
* util.pluck(objArr, 'abc'); // [1, 4, 7]
* util.pluck(arr2d, 2); // [3, 6, 9]
*/
function pluck(arr, property) {
var result = map(arr, function(item) {
return item[property];
});
return result;
}
module.exports = {
forEachOwnProperties: forEachOwnProperties,
forEachArray: forEachArray,
forEach: forEach,
toArray: toArray,
map: map,
reduce: reduce,
filter: filter,
pluck: pluck
};