mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-16 03:31:45 +08:00
5868 observable array reduce bug (#6219)
* 5868 ObservableArray Reduce Bug * 5868 ObservableArray Reduce Bug tslint fixes
This commit is contained in:

committed by
Alexander Djenkov

parent
a1c570c702
commit
97a7b7ea32
@ -1,6 +1,5 @@
|
||||
import * as TKUnit from "../TKUnit";
|
||||
import { Label } from "tns-core-modules/ui/label";
|
||||
|
||||
// >> observable-array-require
|
||||
import { ObservableArray, ChangedData, ChangeType } from "tns-core-modules/data/observable-array";
|
||||
// << observable-array-require
|
||||
@ -633,6 +632,20 @@ export const test_reduce_isDefined = function () {
|
||||
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 () {
|
||||
TKUnit.assert(typeof (array.reduceRight) === "function", "Method 'reduceRight()' should be defined!");
|
||||
};
|
||||
|
@ -49,6 +49,7 @@ export class ObservableArray<T> extends observable.Observable implements observa
|
||||
getItem(index: number): T {
|
||||
return this._array[index];
|
||||
}
|
||||
|
||||
setItem(index: number, value: T) {
|
||||
let oldValue = this._array[index];
|
||||
this._array[index] = value;
|
||||
@ -68,6 +69,7 @@ export class ObservableArray<T> extends observable.Observable implements observa
|
||||
get length(): number {
|
||||
return this._array.length;
|
||||
}
|
||||
|
||||
set length(value: number) {
|
||||
if (types.isNumber(value) && this._array && 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.
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
export interface ObservableArray<T> {
|
||||
on(eventNames: string, callback: (data: observable.EventData) => void, thisArg?: any);
|
||||
|
||||
on(event: "change", callback: (args: observableArrayDef.ChangedData<T>) => void, thisArg?: any);
|
||||
}
|
Reference in New Issue
Block a user