Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c0438df

Browse files
surduAlexander Vakrilov
authored andcommitted
fix(observable-array): reduce no longer ignores zero as initial value (NativeScript#6402)
* fix(observable-array): reduce no longer ignores zero as initial value * fix(observable-array): reduceRight now functions properly when initial value is not specified or zero
1 parent a3f1493 commit c0438df

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,34 @@ export const test_reduce_with_initial_value = function () {
646646
TKUnit.assertEqual(result, 11, "ObservableArray reduce function broken when Initial Value is passed.");
647647
};
648648

649+
export const test_reduce_with_zero_as_initial_value = function () {
650+
const sa = [{prop: 1}, {prop: 2}, {prop: 3}];
651+
let array: ObservableArray<any> = new ObservableArray(sa);
652+
const result = array.reduce((a, b) => a + b.prop, 0);
653+
TKUnit.assertEqual(result, 6, "ObservableArray reduce function broken when Initial Value is zero.");
654+
};
655+
649656
export const test_reduceRight_isDefined = function () {
650657
TKUnit.assert(typeof (array.reduceRight) === "function", "Method 'reduceRight()' should be defined!");
651658
};
659+
660+
export const test_reduceRight_without_initial_value = function () {
661+
const sa = [1, 2, 3];
662+
let array: ObservableArray<number> = new ObservableArray(sa);
663+
const result = array.reduceRight((a, b) => a + b);
664+
TKUnit.assertEqual(result, 6, "ObservableArray reduceRight function broken when initialValue is missing");
665+
};
666+
667+
export const test_reduceRight_with_initial_value = function () {
668+
const sa = [1, 2, 3];
669+
let array: ObservableArray<number> = new ObservableArray(sa);
670+
const result = array.reduceRight((a, b) => a + b, 5);
671+
TKUnit.assertEqual(result, 11, "ObservableArray reduceRight function broken when Initial Value is passed.");
672+
};
673+
674+
export const test_reduceRight_with_zero_as_initial_value = function () {
675+
const sa = [{prop: 1}, {prop: 2}, {prop: 3}];
676+
let array: ObservableArray<any> = new ObservableArray(sa);
677+
const result = array.reduceRight((a, b) => a + b.prop, 0);
678+
TKUnit.assertEqual(result, 6, "ObservableArray reduceRight function broken when Initial Value is zero.");
679+
};

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as observable from "../observable";
1+
import * as observable from "../observable";
22
import * as observableArrayDef from ".";
33
import * as types from "../../utils/types";
44

@@ -314,7 +314,7 @@ export class ObservableArray<T> extends observable.Observable implements observa
314314
* @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.
315315
*/
316316
reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue?: T): T {
317-
return initialValue ? this._array.reduce(callbackfn, initialValue) : this._array.reduce(callbackfn);
317+
return initialValue !== undefined ? this._array.reduce(callbackfn, initialValue) : this._array.reduce(callbackfn);
318318
}
319319

320320
/**
@@ -323,12 +323,12 @@ export class ObservableArray<T> extends observable.Observable implements observa
323323
* @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.
324324
*/
325325
reduceRight(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue?: T): T {
326-
return this._array.reduceRight(callbackfn, initialValue);
326+
return initialValue !== undefined ? this._array.reduceRight(callbackfn, initialValue) : this._array.reduceRight(callbackfn);
327327
}
328328
}
329329

330330
export interface ObservableArray<T> {
331331
on(eventNames: string, callback: (data: observable.EventData) => void, thisArg?: any);
332332

333333
on(event: "change", callback: (args: observableArrayDef.ChangedData<T>) => void, thisArg?: any);
334-
}
334+
}

0 commit comments

Comments
 (0)