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 * 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
|
||||||
@ -115,7 +114,7 @@ export const test_ObservableArray_popShouldRemoveTheLastElement = function () {
|
|||||||
// >> (hide)
|
// >> (hide)
|
||||||
const viewBase = new Label();
|
const viewBase = new Label();
|
||||||
viewBase.set("testProperty", 0);
|
viewBase.set("testProperty", 0);
|
||||||
viewBase.bind({ sourceProperty: "length", targetProperty: "testProperty" }, array);
|
viewBase.bind({sourceProperty: "length", targetProperty: "testProperty"}, array);
|
||||||
// << (hide)
|
// << (hide)
|
||||||
const result = array.pop();
|
const result = array.pop();
|
||||||
// << observable-array-join-pop
|
// << observable-array-join-pop
|
||||||
@ -158,7 +157,7 @@ export const test_ObservableArray_pushShouldAppendNewElement = function () {
|
|||||||
// >> (hide)
|
// >> (hide)
|
||||||
const viewBase = new Label();
|
const viewBase = new Label();
|
||||||
viewBase.set("testProperty", 0);
|
viewBase.set("testProperty", 0);
|
||||||
viewBase.bind({ sourceProperty: "length", targetProperty: "testProperty" }, array);
|
viewBase.bind({sourceProperty: "length", targetProperty: "testProperty"}, array);
|
||||||
// << (hide)
|
// << (hide)
|
||||||
const result = array.push(4);
|
const result = array.push(4);
|
||||||
// << observable-array-push
|
// << observable-array-push
|
||||||
@ -197,7 +196,7 @@ export const test_ObservableArray_pushShouldAppendNewElements = function () {
|
|||||||
// >> (hide)
|
// >> (hide)
|
||||||
const viewBase = new Label();
|
const viewBase = new Label();
|
||||||
viewBase.set("testProperty", 0);
|
viewBase.set("testProperty", 0);
|
||||||
viewBase.bind({ sourceProperty: "length", targetProperty: "testProperty" }, array);
|
viewBase.bind({sourceProperty: "length", targetProperty: "testProperty"}, array);
|
||||||
// << (hide)
|
// << (hide)
|
||||||
const result = array.push(4, 5, 6);
|
const result = array.push(4, 5, 6);
|
||||||
// << observable-array-push-multiple
|
// << observable-array-push-multiple
|
||||||
@ -236,7 +235,7 @@ export const test_ObservableArray_pushShouldAppendNewElementsFromSourceArray = f
|
|||||||
// >> (hide)
|
// >> (hide)
|
||||||
const viewBase = new Label();
|
const viewBase = new Label();
|
||||||
viewBase.set("testProperty", 0);
|
viewBase.set("testProperty", 0);
|
||||||
viewBase.bind({ sourceProperty: "length", targetProperty: "testProperty" }, array);
|
viewBase.bind({sourceProperty: "length", targetProperty: "testProperty"}, array);
|
||||||
// << (hide)
|
// << (hide)
|
||||||
const result = array.push([4, 5, 6]);
|
const result = array.push([4, 5, 6]);
|
||||||
// << observable-array-push-source
|
// << observable-array-push-source
|
||||||
@ -283,7 +282,7 @@ export const test_ObservableArray_shiftShouldRemoveTheFirstElement = function ()
|
|||||||
// >> (hide)
|
// >> (hide)
|
||||||
const viewBase = new Label();
|
const viewBase = new Label();
|
||||||
viewBase.set("testProperty", 0);
|
viewBase.set("testProperty", 0);
|
||||||
viewBase.bind({ sourceProperty: "length", targetProperty: "testProperty" }, array);
|
viewBase.bind({sourceProperty: "length", targetProperty: "testProperty"}, array);
|
||||||
// << (hide)
|
// << (hide)
|
||||||
const result = array.shift();
|
const result = array.shift();
|
||||||
// << observable-array-shift
|
// << observable-array-shift
|
||||||
@ -355,7 +354,7 @@ export const test_ObservableArray_spliceShouldRemoveSpecifiedNumberOfElementsSta
|
|||||||
// >> (hide)
|
// >> (hide)
|
||||||
const viewBase = new Label();
|
const viewBase = new Label();
|
||||||
viewBase.set("testProperty", 0);
|
viewBase.set("testProperty", 0);
|
||||||
viewBase.bind({ sourceProperty: "length", targetProperty: "testProperty" }, array);
|
viewBase.bind({sourceProperty: "length", targetProperty: "testProperty"}, array);
|
||||||
// << (hide)
|
// << (hide)
|
||||||
const result = array.splice(1, 2);
|
const result = array.splice(1, 2);
|
||||||
// << observable-array-splice
|
// << observable-array-splice
|
||||||
@ -431,7 +430,7 @@ export const test_ObservableArray_unshiftShouldInsertNewElementsFromTheStart = f
|
|||||||
// >> (hide)
|
// >> (hide)
|
||||||
const viewBase = new Label();
|
const viewBase = new Label();
|
||||||
viewBase.set("testProperty", 0);
|
viewBase.set("testProperty", 0);
|
||||||
viewBase.bind({ sourceProperty: "length", targetProperty: "testProperty" }, array);
|
viewBase.bind({sourceProperty: "length", targetProperty: "testProperty"}, array);
|
||||||
// << (hide)
|
// << (hide)
|
||||||
const result = array.unshift(4, 5);
|
const result = array.unshift(4, 5);
|
||||||
// << observable-array-unshift
|
// << observable-array-unshift
|
||||||
@ -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!");
|
||||||
};
|
};
|
||||||
|
@ -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);
|
||||||
@ -151,7 +153,7 @@ export class ObservableArray<T> extends observable.Observable implements observa
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reverses the elements in an Array.
|
* Reverses the elements in an Array.
|
||||||
*/
|
*/
|
||||||
reverse(): T[] {
|
reverse(): T[] {
|
||||||
return this._array.reverse();
|
return this._array.reverse();
|
||||||
@ -172,7 +174,7 @@ export class ObservableArray<T> extends observable.Observable implements observa
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a section of an array.
|
* Returns a section of an array.
|
||||||
* @param start The beginning of the specified portion of the array.
|
* @param start The beginning of the specified portion of the array.
|
||||||
* @param end The end of the specified portion of the array.
|
* @param end The end of the specified portion of the array.
|
||||||
@ -281,7 +283,7 @@ export class ObservableArray<T> extends observable.Observable implements observa
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Performs the specified action for each element in an array.
|
* Performs the specified action for each element in an array.
|
||||||
* @param callbackfn A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the array.
|
* @param callbackfn A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the array.
|
||||||
* @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.
|
* @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.
|
||||||
*/
|
*/
|
||||||
forEach(callbackfn: (value: T, index: number, array: T[]) => void, thisArg?: any): void {
|
forEach(callbackfn: (value: T, index: number, array: T[]) => void, thisArg?: any): void {
|
||||||
@ -290,7 +292,7 @@ export class ObservableArray<T> extends observable.Observable implements observa
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Calls a defined callback function on each element of an array, and returns an array that contains the results.
|
* Calls a defined callback function on each element of an array, and returns an array that contains the results.
|
||||||
* @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.
|
* @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.
|
||||||
* @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.
|
* @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.
|
||||||
*/
|
*/
|
||||||
map<U>(callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): U[] {
|
map<U>(callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): U[] {
|
||||||
@ -298,8 +300,8 @@ export class ObservableArray<T> extends observable.Observable implements observa
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the elements of an array that meet the condition specified in a callback function.
|
* Returns the elements of an array that meet the condition specified in a callback function.
|
||||||
* @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.
|
* @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.
|
||||||
* @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.
|
* @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.
|
||||||
*/
|
*/
|
||||||
filter(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any): T[] {
|
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
|
|||||||
* @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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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.
|
* 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.
|
||||||
* @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.
|
* @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.
|
||||||
* @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.
|
||||||
*/
|
*/
|
||||||
reduceRight(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue?: T): T {
|
reduceRight(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue?: T): T {
|
||||||
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);
|
||||||
}
|
}
|
Reference in New Issue
Block a user