提交 b22beded 编写于 作者: M mahaifeng

[array]去除手动生成的代码

上级 7bfac2a9
...@@ -47,6 +47,7 @@ export function testArray() : Result { ...@@ -47,6 +47,7 @@ export function testArray() : Result {
}) })
test('convert-native', () => { test('convert-native', () => {
// #TEST Array.toKotlinList
// #ifdef APP-ANDROID // #ifdef APP-ANDROID
let utsArray = ["1", 2, 3.0] let utsArray = ["1", 2, 3.0]
let javaArray = utsArray.toTypedArray(); let javaArray = utsArray.toTypedArray();
...@@ -54,6 +55,10 @@ export function testArray() : Result { ...@@ -54,6 +55,10 @@ export function testArray() : Result {
let convertArrayFromJava = Array.fromNative(javaArray); let convertArrayFromJava = Array.fromNative(javaArray);
let convertArrayFromKotlin = Array.fromNative(kotlinArray); let convertArrayFromKotlin = Array.fromNative(kotlinArray);
console.log(convertArrayFromJava[0] == convertArrayFromKotlin[0])//true
console.log(convertArrayFromJava[0])//"1"
// #END
expect(convertArrayFromJava[0] == convertArrayFromKotlin[0]).toEqual(true); expect(convertArrayFromJava[0] == convertArrayFromKotlin[0]).toEqual(true);
expect(convertArrayFromJava[0]).toEqual("1"); expect(convertArrayFromJava[0]).toEqual("1");
// #endif // #endif
...@@ -124,16 +129,27 @@ export function testArray() : Result { ...@@ -124,16 +129,27 @@ export function testArray() : Result {
expect(ret4).toEqual([1, 2, 3, 3, 1]); expect(ret4).toEqual([1, 2, 3, 3, 1]);
}) })
test("every", () => { test("every", () => {
// #TEST Array.every,Array.every_1,Array.every_2,Array.every_3
const isBelowThreshold = (currentValue : number) : boolean => currentValue < 40; const isBelowThreshold = (currentValue : number) : boolean => currentValue < 40;
const array1 : number[] = [1, 30, 39, 29, 10, 13]; const array1 : number[] = [1, 30, 39, 29, 10, 13];
console.log(array1.every(isBelowThreshold));// true
const array2 : number[] = [1, 30, 39, 29, 10, 13, 41]; const array2 : number[] = [1, 30, 39, 29, 10, 13, 41];
expect(array1.every(isBelowThreshold)).toEqual(true); console.log(array2.every(isBelowThreshold));// false
expect(array2.every(isBelowThreshold)).toEqual(false);
const array3 : number[] = [1, 2, 3]; const array3 : number[] = [1, 2, 3];
array3.every((element : number, index : number, array : number[]) : boolean => {
console.log(array[index])//1=>2->3
return true;
})
// #END
array3.every((element : number, index : number, array : number[]) : boolean => { array3.every((element : number, index : number, array : number[]) : boolean => {
expect(array[index]).toEqual(element); expect(array[index]).toEqual(element);
return true; return true;
}) })
expect(array1.every(isBelowThreshold)).toEqual(true);
expect(array2.every(isBelowThreshold)).toEqual(false);
}) })
test("fill", () => { test("fill", () => {
// #TEST Array.fill // #TEST Array.fill
...@@ -155,9 +171,11 @@ export function testArray() : Result { ...@@ -155,9 +171,11 @@ export function testArray() : Result {
expect(array3.fill(1, 0, 1.5)).toEqual([1, 1]); expect(array3.fill(1, 0, 1.5)).toEqual([1, 1]);
}) })
test("filter", () => { test("filter", () => {
// #TEST Array.filter,Array.filter_1,Array.filter_2,Array.filter_3
const words : string[] = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present']; const words : string[] = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const result = words.filter((word : string) : boolean => word.length > 6); const result = words.filter((word : string) : boolean => word.length > 6);
expect(result).toEqual(["exuberant", "destruction", "present"]); console.log(result);// ["exuberant", "destruction", "present"]
const array1 : number[] = [-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]; const array1 : number[] = [-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
const isPrime = array1.filter((num : number) : boolean => { const isPrime = array1.filter((num : number) : boolean => {
for (let i = 2; num > i; i++) { for (let i = 2; num > i; i++) {
...@@ -168,12 +186,21 @@ export function testArray() : Result { ...@@ -168,12 +186,21 @@ export function testArray() : Result {
} }
return num > 1; return num > 1;
}) })
expect(isPrime).toEqual([2, 3, 5, 7, 11, 13]); console.log(isPrime)//[2, 3, 5, 7, 11, 13]
const array2 : number[] = [1, 2, 3]; const array2 : number[] = [1, 2, 3];
array2.filter((element : number, index : number, array : number[]) : boolean => { array2.filter((element : number, index : number, array : number[]) : boolean => {
expect(array[index]).toEqual(element); expect(array[index]).toEqual(element);
console.log(array[index])//1=>2=>3
return true; return true;
}) })
// #END
array2.filter((element : number, index : number, array : number[]) : boolean => {
expect(array[index]).toEqual(element);
return true;
})
expect(result).toEqual(["exuberant", "destruction", "present"]);
expect(isPrime).toEqual([2, 3, 5, 7, 11, 13]);
}) })
test("find", () => { test("find", () => {
// #TEST Array.find_2 // #TEST Array.find_2
...@@ -232,20 +259,29 @@ export function testArray() : Result { ...@@ -232,20 +259,29 @@ export function testArray() : Result {
expect(arr3.flat(2)).toEqual([1, 2, 3, 4, 5, 6]); expect(arr3.flat(2)).toEqual([1, 2, 3, 4, 5, 6]);
}) })
test("forEach", () => { test("forEach", () => {
// #TEST Array.forEach,Array.forEach_1,Array.forEach_2
const array1 : string[] = ['a', 'b', 'c']; const array1 : string[] = ['a', 'b', 'c'];
array1.forEach((element : string, index : number) => { array1.forEach(element => console.log(element));
expect(array1[index]).toEqual(element) // expected output: "a"
}); // expected output: "b"
// expected output: "c"
const items : string[] = ['item1', 'item2', 'item3']; const items : string[] = ['item1', 'item2', 'item3'];
const copyItems : string[] = []; const copyItems : string[] = [];
items.forEach((item : string) => { items.forEach((item : string) => {
copyItems.push(item); copyItems.push(item);
}); });
console.log(copyItems)//['item1', 'item2', 'item3']
// #END
array1.forEach((element : string, index : number) => {
expect(array1[index]).toEqual(element)
});
expect(copyItems).toEqual(items) expect(copyItems).toEqual(items)
}) })
test("includes", () => { test("includes", () => {
// #TEST Array.includes
const array1 : number[] = [1, 2, 3]; const array1 : number[] = [1, 2, 3];
console.log(array1.includes(2))//true
// #END
expect(array1.includes(2)).toEqual(true); expect(array1.includes(2)).toEqual(true);
const pets : string[] = ['cat', 'dog', 'bat']; const pets : string[] = ['cat', 'dog', 'bat'];
expect(pets.includes('cat')).toEqual(true); expect(pets.includes('cat')).toEqual(true);
...@@ -282,7 +318,17 @@ export function testArray() : Result { ...@@ -282,7 +318,17 @@ export function testArray() : Result {
arr.push(raw); arr.push(raw);
expect(arr.indexOf(raw)).toEqual(2); expect(arr.indexOf(raw)).toEqual(2);
// #TEST Array.indexOf
const beasts : string[] = ['ant', 'bison', 'camel', 'duck', 'bison']; const beasts : string[] = ['ant', 'bison', 'camel', 'duck', 'bison'];
console.log(beasts.indexOf('bison')); // 1
console.log(beasts.indexOf('bison', 2));// 2
console.log(beasts.indexOf('giraffe'));// -1
// #END
expect(beasts.indexOf('bison')).toEqual(1); expect(beasts.indexOf('bison')).toEqual(1);
expect(beasts.indexOf('bison', 2)).toEqual(4); expect(beasts.indexOf('bison', 2)).toEqual(4);
expect(beasts.indexOf('giraffe')).toEqual(-1); expect(beasts.indexOf('giraffe')).toEqual(-1);
...@@ -318,8 +364,12 @@ export function testArray() : Result { ...@@ -318,8 +364,12 @@ export function testArray() : Result {
arr.push(raw); arr.push(raw);
expect(arr.lastIndexOf(raw)).toEqual(2); expect(arr.lastIndexOf(raw)).toEqual(2);
// #TEST Array.lastIndexOf
const animals : string[] = ['Dodo', 'Tiger', 'Penguin', 'Dodo']; const animals : string[] = ['Dodo', 'Tiger', 'Penguin', 'Dodo'];
console.log(animals.lastIndexOf('Dodo'));//3
console.log(animals.lastIndexOf('Tiger'));//1
// #END
expect(animals.lastIndexOf('Dodo')).toEqual(3); expect(animals.lastIndexOf('Dodo')).toEqual(3);
expect(animals.lastIndexOf('Tiger')).toEqual(1); expect(animals.lastIndexOf('Tiger')).toEqual(1);
const array : number[] = [2, 5, 9, 2]; const array : number[] = [2, 5, 9, 2];
...@@ -337,19 +387,26 @@ export function testArray() : Result { ...@@ -337,19 +387,26 @@ export function testArray() : Result {
}) })
test("map", () => { test("map", () => {
// #TEST Array.map,Array.map_1,Array.map_2
const array1 : number[] = [1, 4, 9, 16]; const array1 : number[] = [1, 4, 9, 16];
const map1 = array1.map((x : number) : number => x * 2); const map1 = array1.map((x : number) : number => x * 2);
expect(map1).toEqual([2, 8, 18, 32]); console.log(map1);
// expected output: Array [2, 8, 18, 32]
const numbers : number[] = [1, 4, 9]; const numbers : number[] = [1, 4, 9];
const roots = numbers.map((num : number) : number => num + 1); const roots = numbers.map((num : number) : number => num + 1);
expect(numbers).toEqual([1, 4, 9]);
expect(roots).toEqual([2, 5, 10]);
const array2 : number[] = [1, 2, 3]; const array2 : number[] = [1, 2, 3];
array2.map((element : number, index : number, array : number[]) => {
console.log(array[index]) //1=>2=>3
})
// #END
array2.map((element : number, index : number, array : number[]) => { array2.map((element : number, index : number, array : number[]) => {
expect(array[index]).toEqual(element); expect(array[index]).toEqual(element);
}) })
expect(map1).toEqual([2, 8, 18, 32]);
expect(numbers).toEqual([1, 4, 9]);
expect(roots).toEqual([2, 5, 10]);
}) })
test("pop", () => { test("pop", () => {
// #TEST Array.pop // #TEST Array.pop
...@@ -378,12 +435,15 @@ export function testArray() : Result { ...@@ -378,12 +435,15 @@ export function testArray() : Result {
}) })
test("reduce", () => { test("reduce", () => {
// #TEST Array.reduce,Array.reduce_1,Array.reduce_2,Array.reduce_3,Array.reduce_4,Array.reduce_5
const array1 : number[] = [1, 2, 3, 4]; const array1 : number[] = [1, 2, 3, 4];
const initialValue : number = 0; const initialValue : number = 0;
const sumWithInitial = array1.reduce( const sumWithInitial = array1.reduce(
(previousValue : number, currentValue : number) : number => previousValue + currentValue, (previousValue : number, currentValue : number) : number => previousValue + currentValue,
initialValue initialValue
); );
console.log(sumWithInitial)//10
// #END
expect(sumWithInitial).toEqual(10); expect(sumWithInitial).toEqual(10);
}) })
test("shift", () => { test("shift", () => {
...@@ -429,8 +489,12 @@ export function testArray() : Result { ...@@ -429,8 +489,12 @@ export function testArray() : Result {
expect(animals.slice()).toEqual(["ant", "bison", "camel", "duck", "elephant"]); expect(animals.slice()).toEqual(["ant", "bison", "camel", "duck", "elephant"]);
}) })
test("some", () => { test("some", () => {
// #TEST Array.some
const array : number[] = [1, 2, 3, 4, 5]; const array : number[] = [1, 2, 3, 4, 5];
const even = (element : number) : boolean => element % 2 == 0; const even = (element : number) : boolean => element % 2 == 0;
console.log(array.some(even));//true
// #END
expect(array.some(even)).toEqual(true); expect(array.some(even)).toEqual(true);
const isBiggerThan10 = (element : number) : boolean => element > 10; const isBiggerThan10 = (element : number) : boolean => element > 10;
expect([2, 5, 8, 1, 4].some(isBiggerThan10)).toEqual(false); expect([2, 5, 8, 1, 4].some(isBiggerThan10)).toEqual(false);
...@@ -490,7 +554,15 @@ export function testArray() : Result { ...@@ -490,7 +554,15 @@ export function testArray() : Result {
}) })
test("unshift", () => { test("unshift", () => {
const array1 : number[] = [1, 2, 3]; // #TEST Array.unshift
const array1 = [1, 2, 3];
console.log(array1.unshift(4, 5));
// 5
console.log(array1);
// [4, 5, 1, 2, 3]
// #END
expect(array1.unshift(4, 5)).toEqual(5); expect(array1.unshift(4, 5)).toEqual(5);
expect(array1).toEqual([4, 5, 1, 2, 3]); expect(array1).toEqual([4, 5, 1, 2, 3]);
}) })
...@@ -518,19 +590,31 @@ export function testArray() : Result { ...@@ -518,19 +590,31 @@ export function testArray() : Result {
// expect(array2).toEqual([5, 4, 3, 2, 1]); // expect(array2).toEqual([5, 4, 3, 2, 1]);
}) })
test("reduceRight", () => { test("reduceRight", () => {
// #TEST Array.reduceRight,Array.reduceRight_1,Array.reduceRight_2,Array.reduceRight_3,Array.reduceRight_4,Array.reduceRight_5,Array.reduceRight_6
const array1 : number[][] = [[0, 1], [2, 3], [4, 5]]; const array1 : number[][] = [[0, 1], [2, 3], [4, 5]];
const result1 = array1.reduceRight((accumulator : number[], currentValue : number[]) : number[] => accumulator.concat(currentValue)); const result1 = array1.reduceRight((accumulator : number[], currentValue : number[]) : number[] => accumulator.concat(currentValue));
expect(result1).toEqual([4, 5, 2, 3, 0, 1]); console.log(result1) //[4, 5, 2, 3, 0, 1]
const array2 : number[] = [1, 2, 3, 4]; const array2 : number[] = [1, 2, 3, 4];
const result2 = array2.reduceRight((acc : number, cur : number, index : number, array : number[]) : number => { let result2 = array2.reduceRight((acc : number, cur : number, index : number, array : number[]) : number => {
expect(array[index]).toEqual(cur);
return acc + cur; return acc + cur;
}); });
expect(result2).toEqual(10);
console.log(result2) //10
const result3 = array2.reduceRight((acc : number, cur : number) : number => acc + cur, 5); const result3 = array2.reduceRight((acc : number, cur : number) : number => acc + cur, 5);
console.log(result3) //15
// #END
expect(result3).toEqual(15); expect(result3).toEqual(15);
expect(result1).toEqual([4, 5, 2, 3, 0, 1]);
result2 = array2.reduceRight((acc : number, cur : number, index : number, array : number[]) : number => {
expect(array[index]).toEqual(cur);
return acc + cur;
});
expect(result2).toEqual(10);
}) })
test("flatMap", () => { test("flatMap", () => {
const arr : number[] = [1, 2, 3]; const arr : number[] = [1, 2, 3];
...@@ -571,6 +655,22 @@ export function testArray() : Result { ...@@ -571,6 +655,22 @@ export function testArray() : Result {
// expect(iterator1.next().value).toEqual(2); // expect(iterator1.next().value).toEqual(2);
// expect(iterator1.next().done).toEqual(true); // expect(iterator1.next().done).toEqual(true);
}) })
test("isArray", () => {
// #TEST Array.isArray
console.log(Array.isArray([1, 3, 5]));
// Expected output: true
console.log(Array.isArray('[]'));
// Expected output: false
console.log(Array.isArray(new Array(5)));
// Expected output: true
console.log(Array.isArray(new Int16Array([15, 33])));
// Expected output: false
// #END
})
}) })
} }
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册