/
array.js
209 lines (183 loc) · 4.83 KB
/
array.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
/* See license.txt for terms of usage */
define([
"firebug/lib/trace",
],
function(FBTrace) {
// ********************************************************************************************* //
// Constants
const Ci = Components.interfaces;
const Cu = Components.utils;
var Arr = {};
// ********************************************************************************************* //
// Arrays
Arr.isArray = Array.isArray || function(obj)
{
return Object.prototype.toString.call(obj) === "[object Array]";
};
Arr.isArrayLike = function(obj)
{
try
{
if (typeof obj !== "object")
return false;
if (!isFinite(obj.length))
return false;
if (Arr.isArray(obj))
return true;
if (typeof obj.callee === "function") // arguments
return true;
if (typeof obj.splice === "function") // jQuery etc.
return true;
var str = Object.prototype.toString.call(obj);
if (str === "[object HTMLCollection]" || str === "[object NodeList]" ||
str === "[object DOMTokenList]")
{
return true;
}
}
catch (exc) {}
return false;
};
// At least sometimes the keys will be on user-level window objects
Arr.keys = function(map)
{
var keys = [];
try
{
for (var name in map) // enumeration is safe
keys.push(name); // name is string, safe
}
catch (exc)
{
// Sometimes we get exceptions trying to iterate properties
}
return keys; // return is safe
};
Arr.values = function(map)
{
var values = [];
try
{
for (var name in map)
{
try
{
values.push(map[name]);
}
catch (exc)
{
// Sometimes we get exceptions trying to access properties
if (FBTrace.DBG_ERRORS)
FBTrace.dumpPropreties("lib.values FAILED ", exc);
}
}
}
catch (exc)
{
// Sometimes we get exceptions trying to iterate properties
if (FBTrace.DBG_ERRORS)
FBTrace.dumpPropreties("lib.values FAILED ", exc);
}
return values;
};
Arr.remove = function(list, item)
{
for (var i = 0; i < list.length; ++i)
{
if (list[i] == item)
{
list.splice(i, 1);
return true;
}
}
return false;
};
Arr.sliceArray = function(array, index)
{
var slice = [];
for (var i = index; i < array.length; ++i)
slice.push(array[i]);
return slice;
};
Arr.cloneArray = function(array, fn)
{
var newArray = [], len = array.length;
if (fn)
for (var i = 0; i < len; ++i)
newArray.push(fn(array[i]));
else
for (var i = 0; i < len; ++i)
newArray.push(array[i]);
return newArray;
};
Arr.extendArray = function(array, array2)
{
var newArray = [];
newArray.push.apply(newArray, array);
newArray.push.apply(newArray, array2);
return newArray;
};
Arr.arrayInsert = function(array, index, other)
{
// Prepare arguments for Array.splice()
// 1) index: at which to start inserting the 'other' array.
// 2) howMany: elements to remove (none in this case)
// 3-N) elements: to insert
var args = [index, 0];
args.push.apply(args, other);
// Insert 'other' array into 'array'
array.splice.apply(array, args);
return array;
};
// xxxFlorent: [ES6-SET] [ES6-SPREAD]
/**
* Filter out unique values of an array, saving only the first occurrence of
* every value. In case the array is sorted, a faster path is taken.
*/
Arr.unique = function(ar, sorted)
{
var ret = [], len = ar.length;
if (sorted)
{
for (var i = 0; i < len; ++i)
{
// Skip duplicated entries
if (i && ar[i-1] === ar[i])
continue;
ret.push(ar[i]);
}
}
else
{
// Keep a map whose ","-prefixed keys represent the values that have
// occurred so far in the array (this avoids overwriting e.g. __proto__).
var map = {};
for (var i = 0; i < len; ++i)
{
if (!map.hasOwnProperty("," + ar[i]))
{
ret.push(ar[i]);
map["," + ar[i]] = 1;
}
}
}
return ret;
};
/**
* Sort an array and eliminate duplicates from it.
*/
Arr.sortUnique = function(ar, sortFunc)
{
return Arr.unique(ar.slice().sort(sortFunc), true);
};
/**
* Merge together two arrays, sort the result, and eliminate any duplicates.
*/
Arr.merge = function(arr1, arr2, sortFunc)
{
return Arr.sortUnique(arr1.concat(arr2), sortFunc);
};
// ********************************************************************************************* //
return Arr;
// ********************************************************************************************* //
});