forked from NativeScript/NativeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobservable-array.ts
More file actions
324 lines (270 loc) · 12.1 KB
/
observable-array.ts
File metadata and controls
324 lines (270 loc) · 12.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
import observable = require("data/observable");
import observableArrayDef = require("data/observable-array");
export class ChangeType implements observableArrayDef.ChangeType {
static Add = "add";
static Delete = "delete";
static Update = "update";
static Splice = "splice";
}
var CHANGE = "change";
export module knownEvents {
export var change = CHANGE;
}
export class ObservableArray<T> extends observable.Observable implements observableArrayDef.ObservableArray<T> { // implements Array<T> {
//[n: number]: T;
private _array: Array<any>;
private _addArgs: observableArrayDef.ChangedData<T>;
private _deleteArgs: observableArrayDef.ChangedData<T>;
constructor() {
super();
if (arguments.length === 1 && Array.isArray(arguments[0])) {
this._array = arguments[0].slice();
}
else {
this._array = Array.apply(null, arguments);
}
this._addArgs = {
eventName: CHANGE, object: this,
action: ChangeType.Add,
index: null,
removed: new Array(),
addedCount: 1
};
this._deleteArgs = {
eventName: CHANGE, object: this,
action: ChangeType.Delete,
index: null,
removed: null,
addedCount: 0
};
}
getItem(index: number): T {
return this._array[index];
}
setItem(index: number, value: T) {
this._array[index] = value;
this.notify(<observableArrayDef.ChangedData<T>>{
eventName: CHANGE, object: this,
action: ChangeType.Update,
index: index,
removed: new Array(1),
addedCount: 1
});
}
/**
* Gets or sets the length of the array. This is a number one higher than the highest element defined in an array.
*/
get length(): number {
return this._array.length;
}
/**
* Returns a string representation of an array.
*/
toString(): string {
return this._array.toString();
}
toLocaleString(): string {
return this._array.toLocaleString();
}
/**
* Combines two or more arrays.
* @param items Additional items to add to the end of array1.
*/
concat(): T[] {
this._addArgs.index = this._array.length;
var result = this._array.concat.apply(this._array, arguments);
return result;
}
/**
* Adds all the elements of an array separated by the specified separator string.
* @param separator A string used to separate one element of an array from the next in the resulting String. If omitted, the array elements are separated with a comma.
*/
join(separator?: string): string {
return this._array.join(separator);
}
/**
* Removes the last element from an array and returns it.
*/
pop(): T {
this._deleteArgs.index = this._array.length - 1;
var result = this._array.pop();
this._deleteArgs.removed = [result];
this.notify(this._deleteArgs);
this._notifyLengthChange();
return result;
}
/**
* Appends new elements to an array, and returns the new length of the array.
* @param item New element of the Array.
*/
push(): number {
this._addArgs.index = this._array.length;
if (arguments.length === 1 && Array.isArray(arguments[0])) {
var source = <Array<T>>arguments[0];
for (var i = 0, l = source.length; i < l; i++) {
this._array.push(source[i]);
}
}
else {
this._array.push.apply(this._array, arguments);
}
this._addArgs.addedCount = this._array.length - this._addArgs.index;
this.notify(this._addArgs);
this._notifyLengthChange();
return this._array.length;
}
_notifyLengthChange() {
var lengthChangedData = this._createPropertyChangeData("length", this._array.length);
this.notify(lengthChangedData);
}
/**
* Reverses the elements in an Array.
*/
reverse(): T[] {
return this._array.reverse();
}
/**
* Removes the first element from an array and returns it.
*/
shift(): T {
var result = this._array.shift();
this._deleteArgs.index = 0;
this._deleteArgs.removed = [result];
this.notify(this._deleteArgs);
this._notifyLengthChange();
return result;
}
/**
* Returns a section of an array.
* @param start The beginning of the specified portion of the array.
* @param end The end of the specified portion of the array.
*/
slice(start?: number, end?: number): T[] {
return this._array.slice(start, end);
}
/**
* Sorts an array.
* @param compareFn The name of the function used to determine the order of the elements. If omitted, the elements are sorted in ascending, ASCII character order.
*/
sort(compareFn?: (a: T, b: T) => number): T[] {
return this._array.sort(compareFn);
}
/**
* Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements.
* @param start The zero-based location in the array from which to start removing elements.
* @param deleteCount The number of elements to remove.
* @param items Elements to insert into the array in place of the deleted elements.
*/
splice(start: number, deleteCount?: number): T[] {
var length = this._array.length;
var result = this._array.splice.apply(this._array, arguments);
this.notify(<observableArrayDef.ChangedData<T>>{
eventName: CHANGE, object: this,
action: ChangeType.Splice,
index: start,
removed: result,
addedCount: this._array.length > length ? this._array.length - length : 0
});
if (this._array.length !== length) {
this._notifyLengthChange();
}
return result;
}
/**
* Inserts new elements at the start of an array.
* @param items Elements to insert at the start of the Array.
*/
unshift(): number {
var length = this._array.length;
var result = this._array.unshift.apply(this._array, arguments);
this._addArgs.index = 0;
this._addArgs.addedCount = result - length;
this.notify(this._addArgs);
this._notifyLengthChange();
return result;
}
/**
* Returns the index of the first occurrence of a value in an array.
* @param searchElement The value to locate in the array.
* @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0.
*/
indexOf(searchElement: T, fromIndex?: number): number {
var index = fromIndex ? fromIndex : 0;
for (var i = index, l = this._array.length; i < l; i++) {
if (this._array[i] === searchElement) {
return i;
}
}
return -1;
}
/**
* Returns the index of the last occurrence of a specified value in an array.
* @param searchElement The value to locate in the array.
* @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at the last index in the array.
*/
lastIndexOf(searchElement: T, fromIndex?: number): number {
var index = fromIndex ? fromIndex : this._array.length - 1;
for (var i = index; i >= 0; i--) {
if (this._array[i] === searchElement) {
return i;
}
}
return -1;
}
/**
* Determines whether all the members of an array satisfy the specified test.
* @param callbackfn A function that accepts up to three arguments. The every method calls the callbackfn function for each element in array1 until the callbackfn returns false, or until the end of the array.
* @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
*/
every(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any): boolean {
return this._array.every(callbackfn, thisArg);
}
/**
* Determines whether the specified callback function returns true for any element of an array.
* @param callbackfn A function that accepts up to three arguments. The some method calls the callbackfn function for each element in array1 until the callbackfn returns true, or until the end of the array.
* @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
*/
some(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any): boolean {
return this._array.some(callbackfn, thisArg);
}
/**
* Performs the specified action for each element in an array.
* @param callbackfn A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the array.
* @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
*/
forEach(callbackfn: (value: T, index: number, array: T[]) => void, thisArg?: any): void {
this._array.forEach(callbackfn, thisArg);
}
/**
* Calls a defined callback function on each element of an array, and returns an array that contains the results.
* @param callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.
* @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
*/
map<U>(callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): U[] {
return this._array.map(callbackfn, thisArg);
}
/**
* Returns the elements of an array that meet the condition specified in a callback function.
* @param callbackfn A function that accepts up to three arguments. The filter method calls the callbackfn function one time for each element in the array.
* @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
*/
filter(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any): T[] {
return this._array.filter(callbackfn, thisArg);
}
/**
* Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
* @param callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.
* @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.
*/
reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue?: T): T {
return this._array.reduce(callbackfn, initialValue);
}
/**
* Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
* @param callbackfn A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array.
* @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.
*/
reduceRight(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue?: T): T {
return this._array.reduceRight(callbackfn, initialValue);
}
}