提交 01e27287 编写于 作者: M mahaifeng

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

上级 a32bced7
...@@ -87,22 +87,44 @@ export function testArray() : Result { ...@@ -87,22 +87,44 @@ export function testArray() : Result {
// 超出边界没有返回信息 // 超出边界没有返回信息
}) })
test("concat", () => { test("concat", () => {
expect(['a', 'b', 'c'].concat(['d', 'e', 'f'])).toEqual(["a", "b", "c", "d", "e", "f"]); // #TEST Array.concat,Array.concat_1
expect([1, 2, 3].concat([4, 5, 6])).toEqual([1, 2, 3, 4, 5, 6]); let ret = ['a', 'b', 'c'].concat(['d', 'e', 'f'])
expect([''].concat([''])).toEqual(["", ""]); console.log(ret) //["a", "b", "c", "d", "e", "f"]
let ret1 = [1, 2, 3].concat([4, 5, 6])
console.log(ret1)//[1, 2, 3, 4, 5, 6]
let ret2 = [''].concat([''])//
console.log(ret2)//["", ""]
const num1 = [1, 2, 3]; const num1 = [1, 2, 3];
const num2 = [4, 5, 6]; const num2 = [4, 5, 6];
const num3 = [7, 8, 9]; const num3 = [7, 8, 9];
const numbers = num1.concat(num2, num3); const numbers = num1.concat(num2, num3);
console.log(numbers)//[1, 2, 3, 4, 5, 6, 7, 8, 9]
// #END
expect(numbers).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9]); expect(numbers).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9]);
expect(ret).toEqual(["a", "b", "c", "d", "e", "f"]);
expect(ret1).toEqual([1, 2, 3, 4, 5, 6]);
expect(ret2).toEqual(["", ""]);
}) })
test("copyWithin", () => { test("copyWithin", () => {
// #TEST Array.copyWithin
const arr = ['a', 'b', 'c', 'd', 'e']; const arr = ['a', 'b', 'c', 'd', 'e'];
expect(arr.copyWithin(0, 3, 4)).toEqual(["d", "b", "c", "d", "e"]); let ret1 = arr.copyWithin(0, 3, 4)
expect(arr.copyWithin(1, 3)).toEqual(["d", "d", "e", "d", "e"]); console.log(ret1)//["d", "b", "c", "d", "e"]
let ret2 = arr.copyWithin(1, 3)
console.log(ret2)//["d", "d", "e", "d", "e"]
const arr2 = [1, 2, 3, 4, 5]; const arr2 = [1, 2, 3, 4, 5];
expect(arr2.copyWithin(-2)).toEqual([1, 2, 3, 1, 2]); let ret3 = arr2.copyWithin(-2)
expect(arr2.copyWithin(-2, -3, -1)).toEqual([1, 2, 3, 3, 1]); console.log(ret3) //[1, 2, 3, 1, 2]
let ret4 = arr2.copyWithin(-2, -3, -1)
console.log(ret4) //[1, 2, 3, 3, 1]
// #END
expect(ret1).toEqual(["d", "b", "c", "d", "e"]);
expect(ret2).toEqual(["d", "d", "e", "d", "e"]);
expect(ret3).toEqual([1, 2, 3, 1, 2]);
expect(ret4).toEqual([1, 2, 3, 3, 1]);
}) })
test("every", () => { test("every", () => {
const isBelowThreshold = (currentValue : number) : boolean => currentValue < 40; const isBelowThreshold = (currentValue : number) : boolean => currentValue < 40;
...@@ -117,13 +139,21 @@ export function testArray() : Result { ...@@ -117,13 +139,21 @@ export function testArray() : Result {
}) })
}) })
test("fill", () => { test("fill", () => {
// #TEST Array.fill
const array1 : number[] = [1, 2, 3, 4]; const array1 : number[] = [1, 2, 3, 4];
console.log(array1.fill(0, 2, 4)); //[1, 2, 0, 0]
console.log(array1.fill(5, 1)); //[1, 5, 5, 5]
console.log(array1.fill(6)); //[6, 6, 6, 6]
const array2 : number[] = [1, 2, 3];
console.log(array2.fill(4))//[4, 4, 4]
const array3 : number[] = [0, 0]
console.log(array3.fill(1, null))//[1, 1]
console.log(array3.fill(1, 0, 1.5))//([1, 1]);
// #END
expect(array1.fill(0, 2, 4)).toEqual([1, 2, 0, 0]); expect(array1.fill(0, 2, 4)).toEqual([1, 2, 0, 0]);
expect(array1.fill(5, 1)).toEqual([1, 5, 5, 5]); expect(array1.fill(5, 1)).toEqual([1, 5, 5, 5]);
expect(array1.fill(6)).toEqual([6, 6, 6, 6]); expect(array1.fill(6)).toEqual([6, 6, 6, 6]);
const array2 : number[] = [1, 2, 3]
expect(array2.fill(4)).toEqual([4, 4, 4]); expect(array2.fill(4)).toEqual([4, 4, 4]);
const array3 : number[] = [0, 0]
expect(array3.fill(1, null)).toEqual([1, 1]); expect(array3.fill(1, null)).toEqual([1, 1]);
expect(array3.fill(1, 0, 1.5)).toEqual([1, 1]); expect(array3.fill(1, 0, 1.5)).toEqual([1, 1]);
}) })
...@@ -175,12 +205,22 @@ export function testArray() : Result { ...@@ -175,12 +205,22 @@ export function testArray() : Result {
}) })
}) })
test("findIndex", () => { test("findIndex", () => {
// #TEST Array.findIndex_1,Array.findIndex_2,Array.findIndex
const array1 : number[] = [5, 12, 8, 130, 44]; const array1 : number[] = [5, 12, 8, 130, 44];
const isLargeNumber = (element : number) : boolean => element > 13; let isLargeNumber = (element : number, index : number) : boolean => element > 13;
expect(array1.findIndex(isLargeNumber)).toEqual(3); console.log(isLargeNumber)//3
const array2 : number[] = [10, 11, 12]; const array2 : number[] = [10, 11, 12];
expect(array2.findIndex(isLargeNumber)).toEqual(-1); console.log(array2.findIndex(isLargeNumber))//3
const array3 : number[] = [1, 2, 3]; const array3 : number[] = [1, 2, 3];
array3.findIndex((element : number, index : number, array : number[]) : boolean => {
console.log(array[index]) //1=>2=>3
return true;
})
// #END
expect(array2.findIndex(isLargeNumber)).toEqual(-1);
expect(array1.findIndex(isLargeNumber)).toEqual(3);
array3.findIndex((element : number, index : number, array : number[]) : boolean => { array3.findIndex((element : number, index : number, array : number[]) : boolean => {
expect(array[index]).toEqual(element); expect(array[index]).toEqual(element);
return true; return true;
...@@ -262,10 +302,15 @@ export function testArray() : Result { ...@@ -262,10 +302,15 @@ export function testArray() : Result {
expect(indices).toEqual([0, 2, 4]); expect(indices).toEqual([0, 2, 4]);
}) })
test("join", () => { test("join", () => {
// #TEST Array.join
const elements : string[] = ['Fire', 'Air', 'Water']; const elements : string[] = ['Fire', 'Air', 'Water'];
expect(elements.join()).toEqual("Fire,Air,Water"); let ret1 = elements.join()//Fire,Air,Water
expect(elements.join('')).toEqual("FireAirWater"); let ret2 = elements.join('') //FireAirWater
expect(elements.join('-')).toEqual("Fire-Air-Water"); let ret3 = elements.join('-')//Fire-Air-Water
expect(ret1).toEqual("Fire,Air,Water");
expect(ret2).toEqual("FireAirWater");
expect(ret3).toEqual("Fire-Air-Water");
// #END
}) })
test("lastIndexOf", () => { test("lastIndexOf", () => {
...@@ -310,19 +355,30 @@ export function testArray() : Result { ...@@ -310,19 +355,30 @@ export function testArray() : Result {
}) })
}) })
test("pop", () => { test("pop", () => {
// #TEST Array.pop
const plants : string[] = ['broccoli', 'cauliflower', 'cabbage', 'kale', 'tomato']; const plants : string[] = ['broccoli', 'cauliflower', 'cabbage', 'kale', 'tomato'];
expect(plants.pop()).toEqual("tomato"); let ret1 = plants.pop()
console.log(ret1)//"tomato"
console.log(plants)//["broccoli", "cauliflower", "cabbage", "kale"]
// #END
expect(ret1).toEqual("tomato");
expect(plants).toEqual(["broccoli", "cauliflower", "cabbage", "kale"]); expect(plants).toEqual(["broccoli", "cauliflower", "cabbage", "kale"]);
plants.pop(); plants.pop();
expect(plants).toEqual(["broccoli", "cauliflower", "cabbage"]); expect(plants).toEqual(["broccoli", "cauliflower", "cabbage"]);
}) })
test("push", () => { test("push", () => {
// #TEST Array.push
const animals : string[] = ['pigs', 'goats', 'sheep']; const animals : string[] = ['pigs', 'goats', 'sheep'];
const count = animals.push('cows'); const count = animals.push('cows');
console.log(count)//4
console.log(animals) //['pigs', 'goats', 'sheep', 'cows']
// #END
expect(count).toEqual(4); expect(count).toEqual(4);
expect(animals).toEqual(['pigs', 'goats', 'sheep', 'cows']); expect(animals).toEqual(['pigs', 'goats', 'sheep', 'cows']);
animals.push('chickens', 'cats', 'dogs'); animals.push('chickens', 'cats', 'dogs');
expect(animals).toEqual(["pigs", "goats", "sheep", "cows", "chickens", "cats", "dogs"]); expect(animals).toEqual(["pigs", "goats", "sheep", "cows", "chickens", "cats", "dogs"]);
}) })
test("reduce", () => { test("reduce", () => {
const array1 : number[] = [1, 2, 3, 4]; const array1 : number[] = [1, 2, 3, 4];
...@@ -334,13 +390,40 @@ export function testArray() : Result { ...@@ -334,13 +390,40 @@ export function testArray() : Result {
expect(sumWithInitial).toEqual(10); expect(sumWithInitial).toEqual(10);
}) })
test("shift", () => { test("shift", () => {
const array1 : number[] = [1, 2, 3]; // #TEST Array.shift
const array1 = [1, 2, 3];
const firstElement = array1.shift(); const firstElement = array1.shift();
console.log(array1); // [2, 3]
console.log(firstElement); //1
// #END
expect(firstElement).toEqual(1); expect(firstElement).toEqual(1);
expect(array1).toEqual([2, 3]); expect(array1).toEqual([2, 3]);
}) })
test("slice", () => { test("slice", () => {
const animals : string[] = ['ant', 'bison', 'camel', 'duck', 'elephant']; // #TEST Array.slice
const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
console.log(animals.slice(2));
// ["camel", "duck", "elephant"]
console.log(animals.slice(2, 4));
//["camel", "duck"]
console.log(animals.slice(1, 5));
// ["bison", "camel", "duck", "elephant"]
console.log(animals.slice(-2));
// ["duck", "elephant"]
console.log(animals.slice(2, -1));
// ["camel", "duck"]
console.log(animals.slice());
//["ant", "bison", "camel", "duck", "elephant"]
// #END
expect(animals.slice(2)).toEqual(["camel", "duck", "elephant"]); expect(animals.slice(2)).toEqual(["camel", "duck", "elephant"]);
expect(animals.slice(2, 4)).toEqual(["camel", "duck"]); expect(animals.slice(2, 4)).toEqual(["camel", "duck"]);
expect(animals.slice(1, 5)).toEqual(["bison", "camel", "duck", "elephant"]); expect(animals.slice(1, 5)).toEqual(["bison", "camel", "duck", "elephant"]);
...@@ -357,15 +440,22 @@ export function testArray() : Result { ...@@ -357,15 +440,22 @@ export function testArray() : Result {
expect([12, 5, 8, 1, 4].some(isBiggerThan10)).toEqual(true); expect([12, 5, 8, 1, 4].some(isBiggerThan10)).toEqual(true);
}) })
test("splice", () => { test("splice", () => {
// #TEST Array.splice
const months : string[] = ['Jan', 'March', 'April', 'June']; const months : string[] = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb'); months.splice(1, 0, 'Feb');
console.log(months)//["Jan", "Feb", "March", "April", "June"]
// #END
expect(months).toEqual(["Jan", "Feb", "March", "April", "June"]); expect(months).toEqual(["Jan", "Feb", "March", "April", "June"]);
months.splice(4, 1, 'May'); months.splice(4, 1, 'May');
expect(months).toEqual(["Jan", "Feb", "March", "April", "May"]); expect(months).toEqual(["Jan", "Feb", "March", "April", "May"]);
}) })
test('sort', () => { test('sort', () => {
// #TEST Array.slice
const months = ['March', 'Jan', 'Feb', 'Dec']; const months = ['March', 'Jan', 'Feb', 'Dec'];
months.sort(); months.sort();
console.log(months)//["Dec", "Feb", "Jan", "March"]
// #END
expect(months).toEqual(["Dec", "Feb", "Jan", "March"]); expect(months).toEqual(["Dec", "Feb", "Jan", "March"]);
const array1 = [1, 30, 4, 21, 100000]; const array1 = [1, 30, 4, 21, 100000];
...@@ -408,7 +498,7 @@ export function testArray() : Result { ...@@ -408,7 +498,7 @@ export function testArray() : Result {
expect(array1).toEqual([4, 5, 1, 2, 3]); expect(array1).toEqual([4, 5, 1, 2, 3]);
}) })
test("toString", () => { test("toString", () => {
// #TEST Array.length // #TEST Array.toString
const array1 : number[] = [1, 2, 3]; const array1 : number[] = [1, 2, 3];
console.log(array1.toString()) //"1,2,3" console.log(array1.toString()) //"1,2,3"
// #END // #END
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册