From c0438df9a7c5f739016516b802a9d40d4fa2ab70 Mon Sep 17 00:00:00 2001 From: Nicu Date: Fri, 19 Oct 2018 17:14:47 +0300 Subject: [PATCH] fix(observable-array): reduce no longer ignores zero as initial value (#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 --- tests/app/data/observable-array-tests.ts | 28 +++++++++++++++++++ .../data/observable-array/observable-array.ts | 8 +++--- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/tests/app/data/observable-array-tests.ts b/tests/app/data/observable-array-tests.ts index 2a1ee14bd..474cb4af5 100644 --- a/tests/app/data/observable-array-tests.ts +++ b/tests/app/data/observable-array-tests.ts @@ -646,6 +646,34 @@ export const test_reduce_with_initial_value = function () { TKUnit.assertEqual(result, 11, "ObservableArray reduce function broken when Initial Value is passed."); }; +export const test_reduce_with_zero_as_initial_value = function () { + const sa = [{prop: 1}, {prop: 2}, {prop: 3}]; + let array: ObservableArray = new ObservableArray(sa); + const result = array.reduce((a, b) => a + b.prop, 0); + TKUnit.assertEqual(result, 6, "ObservableArray reduce function broken when Initial Value is zero."); +}; + export const test_reduceRight_isDefined = function () { TKUnit.assert(typeof (array.reduceRight) === "function", "Method 'reduceRight()' should be defined!"); }; + +export const test_reduceRight_without_initial_value = function () { + const sa = [1, 2, 3]; + let array: ObservableArray = new ObservableArray(sa); + const result = array.reduceRight((a, b) => a + b); + TKUnit.assertEqual(result, 6, "ObservableArray reduceRight function broken when initialValue is missing"); +}; + +export const test_reduceRight_with_initial_value = function () { + const sa = [1, 2, 3]; + let array: ObservableArray = new ObservableArray(sa); + const result = array.reduceRight((a, b) => a + b, 5); + TKUnit.assertEqual(result, 11, "ObservableArray reduceRight function broken when Initial Value is passed."); +}; + +export const test_reduceRight_with_zero_as_initial_value = function () { + const sa = [{prop: 1}, {prop: 2}, {prop: 3}]; + let array: ObservableArray = new ObservableArray(sa); + const result = array.reduceRight((a, b) => a + b.prop, 0); + TKUnit.assertEqual(result, 6, "ObservableArray reduceRight function broken when Initial Value is zero."); +}; diff --git a/tns-core-modules/data/observable-array/observable-array.ts b/tns-core-modules/data/observable-array/observable-array.ts index 25740ed77..e133512e8 100644 --- a/tns-core-modules/data/observable-array/observable-array.ts +++ b/tns-core-modules/data/observable-array/observable-array.ts @@ -1,4 +1,4 @@ -import * as observable from "../observable"; +import * as observable from "../observable"; import * as observableArrayDef from "."; import * as types from "../../utils/types"; @@ -314,7 +314,7 @@ export class ObservableArray extends observable.Observable implements observa * @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 initialValue ? this._array.reduce(callbackfn, initialValue) : this._array.reduce(callbackfn); + return initialValue !== undefined ? this._array.reduce(callbackfn, initialValue) : this._array.reduce(callbackfn); } /** @@ -323,7 +323,7 @@ export class ObservableArray extends observable.Observable implements observa * @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); + return initialValue !== undefined ? this._array.reduceRight(callbackfn, initialValue) : this._array.reduceRight(callbackfn); } } @@ -331,4 +331,4 @@ export interface ObservableArray { on(eventNames: string, callback: (data: observable.EventData) => void, thisArg?: any); on(event: "change", callback: (args: observableArrayDef.ChangedData) => void, thisArg?: any); -} \ No newline at end of file +}