5868 observable array reduce bug (#6219)

* 5868 ObservableArray Reduce Bug

* 5868 ObservableArray Reduce Bug
tslint fixes
This commit is contained in:
Rakesh Girase
2018-08-28 11:54:02 +01:00
committed by Alexander Djenkov
parent a1c570c702
commit 97a7b7ea32
2 changed files with 33 additions and 16 deletions

View File

@ -1,6 +1,5 @@
import * as TKUnit from "../TKUnit"; import * as TKUnit from "../TKUnit";
import { Label } from "tns-core-modules/ui/label"; import { Label } from "tns-core-modules/ui/label";
// >> observable-array-require // >> observable-array-require
import { ObservableArray, ChangedData, ChangeType } from "tns-core-modules/data/observable-array"; import { ObservableArray, ChangedData, ChangeType } from "tns-core-modules/data/observable-array";
// << observable-array-require // << observable-array-require
@ -633,6 +632,20 @@ export const test_reduce_isDefined = function () {
TKUnit.assert(typeof (array.reduce) === "function", "Method 'reduce()' should be defined!"); TKUnit.assert(typeof (array.reduce) === "function", "Method 'reduce()' should be defined!");
}; };
export const test_reduce_without_initial_value = function () {
const sa = [1, 2, 3];
let array: ObservableArray<number> = new ObservableArray(sa);
const result = array.reduce((a, b) => a + b);
TKUnit.assertEqual(result, 6, "ObservableArray reduce function broken when initialValue is missing");
};
export const test_reduce_with_initial_value = function () {
const sa = [1, 2, 3];
let array: ObservableArray<number> = new ObservableArray(sa);
const result = array.reduce((a, b) => a + b, 5);
TKUnit.assertEqual(result, 11, "ObservableArray reduce function broken when Initial Value is passed.");
};
export const test_reduceRight_isDefined = function () { export const test_reduceRight_isDefined = function () {
TKUnit.assert(typeof (array.reduceRight) === "function", "Method 'reduceRight()' should be defined!"); TKUnit.assert(typeof (array.reduceRight) === "function", "Method 'reduceRight()' should be defined!");
}; };

View File

@ -49,6 +49,7 @@ export class ObservableArray<T> extends observable.Observable implements observa
getItem(index: number): T { getItem(index: number): T {
return this._array[index]; return this._array[index];
} }
setItem(index: number, value: T) { setItem(index: number, value: T) {
let oldValue = this._array[index]; let oldValue = this._array[index];
this._array[index] = value; this._array[index] = value;
@ -68,6 +69,7 @@ export class ObservableArray<T> extends observable.Observable implements observa
get length(): number { get length(): number {
return this._array.length; return this._array.length;
} }
set length(value: number) { set length(value: number) {
if (types.isNumber(value) && this._array && this._array.length !== value) { if (types.isNumber(value) && this._array && this._array.length !== value) {
this.splice(value, this._array.length - value); this.splice(value, this._array.length - value);
@ -312,7 +314,7 @@ export class ObservableArray<T> 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. * @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 { reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue?: T): T {
return this._array.reduce(callbackfn, initialValue); return initialValue ? this._array.reduce(callbackfn, initialValue) : this._array.reduce(callbackfn);
} }
/** /**
@ -324,7 +326,9 @@ export class ObservableArray<T> extends observable.Observable implements observa
return this._array.reduceRight(callbackfn, initialValue); return this._array.reduceRight(callbackfn, initialValue);
} }
} }
export interface ObservableArray<T> { export interface ObservableArray<T> {
on(eventNames: string, callback: (data: observable.EventData) => void, thisArg?: any); on(eventNames: string, callback: (data: observable.EventData) => void, thisArg?: any);
on(event: "change", callback: (args: observableArrayDef.ChangedData<T>) => void, thisArg?: any); on(event: "change", callback: (args: observableArrayDef.ChangedData<T>) => void, thisArg?: any);
} }