This repository was archived by the owner on Feb 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCollection.js
468 lines (466 loc) · 13.5 KB
/
Collection.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
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
//-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
// Definition of Collection.
Pot.update({
/**
* @lends Pot
*/
/**
* Collection.
* Array utilities.
*
* Treated as an array of arguments given then
* return it as an array.
*
* @see Pot.Collection.arrayize
*
* @param {*} object A target object.
* @param {Number} (index) Optional, The first index to
* slice the array.
* @return {Array} Return an array of result.
*
* @name Pot.Collection
* @type Function
* @function
* @class
* @static
* @public
*/
Collection : function(/*object[, index]*/) {
return arrayize.apply(null, arguments);
}
});
update(Pot.Collection, {
/**
* @lends Pot.Collection
*/
/**
* Treated as an array of arguments given then
* return it as an array.
*
*
* @example
* debug(arrayize(null)); // [null]
* debug(arrayize((void 0))); // [undefined]
* debug(arrayize(true)); // [true]
* debug(arrayize(false)); // [false]
* debug(arrayize(new Boolean(true))); // [Boolean(false)]
* debug(arrayize(Boolean)); // [Boolean]
* debug(arrayize('')); // ['']
* debug(arrayize('hoge')); // ['hoge']
* debug(arrayize(new String('hoge'))); // [String {'hoge'}]
* debug(arrayize(String)); // [String]
* debug(arrayize(100)); // [100]
* debug(arrayize(-100)); // [-100]
* debug(arrayize(NaN)); // [NaN]
* debug(arrayize(12410.505932095032)); // [12410.505932095032]
* debug(arrayize(new Number(100))); // [Number {100}]
* debug(arrayize(Number)); // [Number]
* debug(arrayize(Error('error'))); // [Error {'error'}]
* debug(arrayize(new Error('error'))); // [Error {'error'}]
* debug(arrayize(Error)); // [Error]
* debug(arrayize(/(foo|bar)/i)); // [/(foo|bar)/i]
* debug(arrayize(new RegExp('hoge'))); // [/hoge/]
* debug(arrayize(new RegExp())); // [/(?:)/]
* debug(arrayize(RegExp)); // [RegExp]
* debug(arrayize(TypeError)); // [TypeError]
* debug(arrayize(encodeURI)); // [encodeURI]
* debug(arrayize(window)); // [window]
* debug(arrayize(document)); // [document]
* debug(arrayize(document.body)); // [body]
* debug(arrayize([])); // []
* debug(arrayize(new Array(1, 2, 3))); // [1, 2, 3]
* debug(arrayize([1, 2, 3])); // [1, 2, 3]
* debug(arrayize(Array)); // [Array]
* debug(arrayize(Array.prototype)); // [Array.prototype]
* debug(arrayize([[]])); // [[]]
* debug(arrayize([[100]])); // [[100]]
* debug(arrayize({})); // [{}]
* debug(arrayize({foo: 'bar'})); // [{foo: 'bar'}]
* debug(arrayize(new Object())); // [Object {}]
* debug(arrayize(new Object('foo'))); // [Object {'foo'}]
* debug(arrayize(Object)); // [Object]
* debug(arrayize(document.getElementsByTagName('div')));
* // @results [<div/>, <div/>, <div/> ...]
* (function(a, b, c) {
* debug(arrayize(arguments));
* // @results [1, 2, 3]
* })(1, 2, 3);
* (function(a, b, c) {
* debug(arrayize(arguments, 2));
* // @results [3]
* })(1, 2, 3);
*
*
* @param {*} object A target object.
* @param {Number} (index) Optional, The first index to
* slice the array.
* @return {Array} Return an array of result.
* @type Function
* @function
* @public
* @static
*/
arrayize : arrayize,
/**
* Merge some arrays or objects to base object.
* The type of first argument will be to base type.
*
*
* @example
* var array1 = [1, 2, 3];
* var array2 = [4, 5, 6];
* var result = merge(array1, array2);
* debug(result);
* // @results [1, 2, 3, 4, 5, 6]
* var result = merge([], 1, 2, 'foo', {bar: 3});
* debug(result);
* // @results [1, 2, 'foo', {bar: 3}]
*
*
* @example
* var obj1 = {foo: 1, bar: 2};
* var obj2 = {baz: 3};
* var result = merge(obj1, obj2);
* debug(result);
* // @results {foo: 1, bar: 2, baz: 3}
* var result = merge({}, {foo: 1}, {bar: 2});
* debug(result);
* // @results {foo: 1, bar: 2}
*
*
* @example
* var s1 = 'foo';
* var s2 = 'bar';
* var result = merge(s1, s2);
* debug(result);
* // @results 'foobar'
*
*
* @param {Array|*} (...) Array(s) to merge.
* @return {Array|*} Result of merged.
* @type Function
* @function
* @static
* @public
*/
merge : function(/*[...args]*/) {
var result = null, args = arrayize(arguments), arg = args[0];
switch (args.length) {
case 0:
break;
case 1:
result = arrayize(arg);
break;
default:
if (isArrayLike(arg)) {
result = concat.apply([], args);
} else if (isObject(arg)) {
args.unshift({});
result = update.apply(null, args);
} else if (isString(arg)) {
result = ArrayProto.join.call(args, '');
} else {
result = args;
}
}
return result;
},
/**
* Returns an array with the given unique array.
* Keep order without sorting.
*
*
* @example
* debug(unique(
* [1, 2, 3, 4, 5, 3, 5, 'a', 3, 'b', 'a', 'c', 2, 5]
* ));
* // @results [1, 2, 3, 4, 5, 'a', 'b', 'c']
*
* @example
* debug(unique(
* [5, 7, 8, 3, 6, 1, 7, 2, 3, 8, 4, 2, 9, 5]
* ));
* // @results [5, 7, 8, 3, 6, 1, 2, 4, 9]
*
* @example
* debug(unique(
* ['1', 1, '2', 2, 0, '0', '', null, false, (void 0)],
* true
* ));
* // @results ['1', '2', 0, null]
*
* @example
* debug(unique(
* ['abc', 'ABC', 'Foo', 'bar', 'foO', 'BaR'],
* false, true
* ));
* // @results ['abc', 'Foo', 'bar']
*
*
* @example
* debug(unique(
* {a: 1, b: 2, c: 3, d: 1, e: 3, f: 2}
* ));
* // @results {a: 1, b: 2, c: 3}
*
* @example
* debug(unique(
* {foo: 1, bar: 2, FOo: 3, Bar: '1', baZ: '2'},
* true, true
* ));
* // @results {foo: 1, bar: 2, FOo: 3}
*
* @example
* debug(unique(
* {a: 1, b: 2, c: 3, d: '1', e: '3', f: 5},
* true
* ));
* // @results {a: 1, b: 2, c: 3, f: 5}
*
*
* @example
* debug(unique('abcABCabc-----foobarBaZ'));
* // @results 'abcABC-forZ'
* debug(unique('abcABCabc-----foobarBaZ', true, true));
* // @results 'abc-forZ'
*
*
* @example
* debug(unique(
* [1, 1, [123], (function(a) { return a;}),
* [123], {a: 5}, (function(a) { return a; }), {a: 5}]
* ));
* // @results [1, [123], (function() { return a; }), {a: 5}]
*
*
* @param {Array|Object|String|*} object Target object.
* (no change in this object).
* @param {Boolean} (loose) If passed TRUE then
* will be compared by
* loose operator (==),
* the default is
* strict comparison (===).
* @param {Boolean} (ignoreCase) If passed TRUE then will be
* ignored case sensitive.
* @return {Array|Object|String|*} Array with unique values.
* @type Function
* @function
* @static
* @public
*/
unique : (function() {
var
iCase, useStrict,
/**@ignore*/
cmpCase = function (c1, c2) {
var value1, value2;
if (iCase &&
c1 != null && c1.toLowerCase &&
c2 != null && c2.toLowerCase) {
value1 = c1.toLowerCase();
value2 = c2.toLowerCase();
} else {
value1 = c1;
value2 = c2;
}
return useStrict && value1 === value2 || value1 == value2;
},
/**@ignore*/
cmp = function(a, b) {
var v1, v2;
if (iCase) {
v1 = stringify(a).toLowerCase();
v2 = stringify(b).toLowerCase();
} else {
v1 = a;
v2 = b;
}
if (useStrict && v1 === v2 || v1 == v2) {
return true;
} else if (Pot.Struct && Pot.Struct.equals) {
return Pot.Struct.equals(a, b, cmpCase);
} else {
return false;
}
};
return function(object, loose, ignoreCase) {
var results = [], args = arguments,
me = Pot.Collection.unique, i, j, len,
array, dups = [];
args = arrayize(args);
useStrict = !loose;
iCase = !!ignoreCase;
if (object) {
if (isArray(object)) {
len = object.length;
if (len) {
for (i = 0; i < len; i++) {
for (j = i + 1; j < len; j++) {
try {
if (cmp(object[i], object[j])) {
dups[j] = i;
}
} catch (e) {}
}
if (!(i in dups)) {
try {
results[results.length] = object[i];
} catch (e) {}
}
}
}
} else if (isObject(object)) {
results = {};
array = [];
each(object, function(val, key) {
array[array.length] = [key, val];
});
len = array.length;
for (i = 0; i < len; i++) {
for (j = i + 1; j < len; j++) {
try {
if (cmp(array[i][1], array[j][1])) {
dups[j] = i;
}
} catch (e) {}
}
if (!(i in dups)) {
try {
results[array[i][0]] = array[i][1];
} catch (e) {}
}
}
} else if (isString(object)) {
args[0] = object.split('');
results = me.apply(null, args).join('');
} else {
results = object;
}
}
return results;
};
}()),
/**
* @lends Pot.Collection
*/
/**
* Convert to one-dimensional array from multi-dimensional array.
*
*
* @example
* debug(flatten(
* [1,2,3,[4,5,6,[7,8,[9],10],11],12]
* ));
* // @results [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
*
*
* @param {Array} array A target array.
* @return {Array} An array which has only one dimension.
* @type Function
* @function
* @static
* @public
*/
flatten : function flatten(array) {
var results = [], i, len, item, items;
if (!isArray(array)) {
results[results.length] = array;
} else {
items = arrayize(array);
len = items.length;
for (i = 0; i < len; i++) {
item = items[i];
if (isArray(item)) {
push.apply(results, flatten(item));
} else {
results[results.length] = item;
}
}
}
return results;
},
/**
* Sorting with humaneness. (natural sort)
*
* Based: alphanum-sorting library that is below link.
* @link http://www.davekoelle.com/alphanum.html
*
*
* @example
* debug(alphanumSort(
* ['a10', 'a2', 'a100', 'a1', 'a12']
* ));
* // @results ['a1', 'a2', 'a10', 'a12', 'a100']
*
*
* @example
* var arr = [{v: 'a10'}, {v: 'a2'}, {v: 'a100'}, {v: 'a1'}];
* debug(alphanumSort(arr, function(item) {
* // Specify variable (property name).
* return item.v;
* }));
* // @results [{v: 'a1'}, {v: 'a2'}, {v: 'a10'}, {v: 'a100'}]
*
*
* @param {Array} array A target array.
* @param {Function} (func) Callback function if need specify arguments.
* @return {Array} An array of result `array`.
* @type Function
* @function
* @static
* @public
*/
alphanumSort : (function() {
/**@ignore*/
function chunkify(t) {
var tz = [], x = 0, y = -1, n = 0, i, j, m;
while ((i = (j = t.charAt(x++)).charCodeAt(0))) {
m = (i == 46 || (i >= 48 && i <= 57));
if (m !== n) {
tz[++y] = '';
n = m;
}
tz[y] += j;
}
return tz;
}
/**@ignore*/
function alphanumCase(a, b) {
var aa, bb, c, d, i;
aa = chunkify(stringify(a).toLowerCase());
bb = chunkify(stringify(b).toLowerCase());
for (i = 0; (aa[i] && bb[i]); i++) {
if (aa[i] !== bb[i]) {
c = +aa[i];
d = +bb[i];
if (c == aa[i] && d == bb[i]) {
return c - d;
} else {
return (aa[i] > bb[i]) ? 1 : -1;
}
}
}
return aa.length - bb.length;
}
return function(array, func) {
if (isArray(array)) {
if (isFunction(func)) {
array.sort(function(a, b) {
return alphanumCase(func(a), func(b));
});
} else {
array.sort(alphanumCase);
}
}
return array;
};
}())
});
// Update Pot object,
Pot.update({
arrayize : Pot.Collection.arrayize,
merge : Pot.Collection.merge,
unique : Pot.Collection.unique,
flatten : Pot.Collection.flatten,
alphanumSort : Pot.Collection.alphanumSort
});