FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix: observable array reduce bug (#6219) · NativeScript/NativeScript@b028dd9 · GitHub

Commit b028dd9

Browse files
authored andcommitted
fix: observable array reduce bug (#6219)
* 5868 ObservableArray Reduce Bug * 5868 ObservableArray Reduce Bug tslint fixes
1 parent efd5f8d commit b028dd9

2 files changed

Lines changed: 33 additions & 16 deletions

File tree

‎tests/app/data/observable-array-tests.ts‎

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as TKUnit from "../TKUnit";
22
import { Label } from "tns-core-modules/ui/label";
3-
43
// >> observable-array-require
54
import { ObservableArray, ChangedData, ChangeType } from "tns-core-modules/data/observable-array";
65
// << observable-array-require
@@ -115,7 +114,7 @@ export const test_ObservableArray_popShouldRemoveTheLastElement = function () {
115114
// >> (hide)
116115
const viewBase = new Label();
117116
viewBase.set("testProperty", 0);
118-
viewBase.bind({ sourceProperty: "length", targetProperty: "testProperty" }, array);
117+
viewBase.bind({sourceProperty: "length", targetProperty: "testProperty"}, array);
119118
// << (hide)
120119
const result = array.pop();
121120
// << observable-array-join-pop
@@ -158,7 +157,7 @@ export const test_ObservableArray_pushShouldAppendNewElement = function () {
158157
// >> (hide)
159158
const viewBase = new Label();
160159
viewBase.set("testProperty", 0);
161-
viewBase.bind({ sourceProperty: "length", targetProperty: "testProperty" }, array);
160+
viewBase.bind({sourceProperty: "length", targetProperty: "testProperty"}, array);
162161
// << (hide)
163162
const result = array.push(4);
164163
// << observable-array-push
@@ -197,7 +196,7 @@ export const test_ObservableArray_pushShouldAppendNewElements = function () {
197196
// >> (hide)
198197
const viewBase = new Label();
199198
viewBase.set("testProperty", 0);
200-
viewBase.bind({ sourceProperty: "length", targetProperty: "testProperty" }, array);
199+
viewBase.bind({sourceProperty: "length", targetProperty: "testProperty"}, array);
201200
// << (hide)
202201
const result = array.push(4, 5, 6);
203202
// << observable-array-push-multiple
@@ -236,7 +235,7 @@ export const test_ObservableArray_pushShouldAppendNewElementsFromSourceArray = f
236235
// >> (hide)
237236
const viewBase = new Label();
238237
viewBase.set("testProperty", 0);
239-
viewBase.bind({ sourceProperty: "length", targetProperty: "testProperty" }, array);
238+
viewBase.bind({sourceProperty: "length", targetProperty: "testProperty"}, array);
240239
// << (hide)
241240
const result = array.push([4, 5, 6]);
242241
// << observable-array-push-source
@@ -283,7 +282,7 @@ export const test_ObservableArray_shiftShouldRemoveTheFirstElement = function ()
283282
// >> (hide)
284283
const viewBase = new Label();
285284
viewBase.set("testProperty", 0);
286-
viewBase.bind({ sourceProperty: "length", targetProperty: "testProperty" }, array);
285+
viewBase.bind({sourceProperty: "length", targetProperty: "testProperty"}, array);
287286
// << (hide)
288287
const result = array.shift();
289288
// << observable-array-shift
@@ -355,7 +354,7 @@ export const test_ObservableArray_spliceShouldRemoveSpecifiedNumberOfElementsSta
355354
// >> (hide)
356355
const viewBase = new Label();
357356
viewBase.set("testProperty", 0);
358-
viewBase.bind({ sourceProperty: "length", targetProperty: "testProperty" }, array);
357+
viewBase.bind({sourceProperty: "length", targetProperty: "testProperty"}, array);
359358
// << (hide)
360359
const result = array.splice(1, 2);
361360
// << observable-array-splice
@@ -431,7 +430,7 @@ export const test_ObservableArray_unshiftShouldInsertNewElementsFromTheStart = f
431430
// >> (hide)
432431
const viewBase = new Label();
433432
viewBase.set("testProperty", 0);
434-
viewBase.bind({ sourceProperty: "length", targetProperty: "testProperty" }, array);
433+
viewBase.bind({sourceProperty: "length", targetProperty: "testProperty"}, array);
435434
// << (hide)
436435
const result = array.unshift(4, 5);
437436
// << observable-array-unshift
@@ -633,6 +632,20 @@ export const test_reduce_isDefined = function () {
633632
TKUnit.assert(typeof (array.reduce) === "function", "Method 'reduce()' should be defined!");
634633
};
635634

635+
export const test_reduce_without_initial_value = function () {
636+
const sa = [1, 2, 3];
637+
let array: ObservableArray<number> = new ObservableArray(sa);
638+
const result = array.reduce((a, b) => a + b);
639+
TKUnit.assertEqual(result, 6, "ObservableArray reduce function broken when initialValue is missing");
640+
};
641+
642+
export const test_reduce_with_initial_value = function () {
643+
const sa = [1, 2, 3];
644+
let array: ObservableArray<number> = new ObservableArray(sa);
645+
const result = array.reduce((a, b) => a + b, 5);
646+
TKUnit.assertEqual(result, 11, "ObservableArray reduce function broken when Initial Value is passed.");
647+
};
648+
636649
export const test_reduceRight_isDefined = function () {
637650
TKUnit.assert(typeof (array.reduceRight) === "function", "Method 'reduceRight()' should be defined!");
638651
};

‎tns-core-modules/data/observable-array/observable-array.ts‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export class ObservableArray<T> extends observable.Observable implements observa
4949
getItem(index: number): T {
5050
return this._array[index];
5151
}
52+
5253
setItem(index: number, value: T) {
5354
let oldValue = this._array[index];
5455
this._array[index] = value;
@@ -68,6 +69,7 @@ export class ObservableArray<T> extends observable.Observable implements observa
6869
get length(): number {
6970
return this._array.length;
7071
}
72+
7173
set length(value: number) {
7274
if (types.isNumber(value) && this._array && this._array.length !== value) {
7375
this.splice(value, this._array.length - value);
@@ -151,7 +153,7 @@ export class ObservableArray<T> extends observable.Observable implements observa
151153
}
152154

153155
/**
154-
* Reverses the elements in an Array.
156+
* Reverses the elements in an Array.
155157
*/
156158
reverse(): T[] {
157159
return this._array.reverse();
@@ -172,7 +174,7 @@ export class ObservableArray<T> extends observable.Observable implements observa
172174
return result;
173175
}
174176

175-
/**
177+
/**
176178
* Returns a section of an array.
177179
* @param start The beginning of the specified portion of the array.
178180
* @param end The end of the specified portion of the array.
@@ -281,7 +283,7 @@ export class ObservableArray<T> extends observable.Observable implements observa
281283

282284
/**
283285
* Performs the specified action for each element in an array.
284-
* @param callbackfn A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the array.
286+
* @param callbackfn A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the array.
285287
* @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.
286288
*/
287289
forEach(callbackfn: (value: T, index: number, array: T[]) => void, thisArg?: any): void {
@@ -290,16 +292,16 @@ export class ObservableArray<T> extends observable.Observable implements observa
290292

291293
/**
292294
* Calls a defined callback function on each element of an array, and returns an array that contains the results.
293-
* @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.
295+
* @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.
294296
* @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.
295297
*/
296298
map<U>(callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): U[] {
297299
return this._array.map(callbackfn, thisArg);
298300
}
299301

300302
/**
301-
* Returns the elements of an array that meet the condition specified in a callback function.
302-
* @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.
303+
* Returns the elements of an array that meet the condition specified in a callback function.
304+
* @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.
303305
* @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.
304306
*/
305307
filter(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any): T[] {
@@ -312,19 +314,21 @@ export class ObservableArray<T> extends observable.Observable implements observa
312314
* @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.
313315
*/
314316
reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue?: T): T {
315-
return this._array.reduce(callbackfn, initialValue);
317+
return initialValue ? this._array.reduce(callbackfn, initialValue) : this._array.reduce(callbackfn);
316318
}
317319

318320
/**
319321
* 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.
320-
* @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.
322+
* @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.
321323
* @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.
322324
*/
323325
reduceRight(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue?: T): T {
324326
return this._array.reduceRight(callbackfn, initialValue);
325327
}
326328
}
329+
327330
export interface ObservableArray<T> {
328331
on(eventNames: string, callback: (data: observable.EventData) => void, thisArg?: any);
332+
329333
on(event: "change", callback: (args: observableArrayDef.ChangedData<T>) => void, thisArg?: any);
330334
}

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL