castArray

说明

将给定的值强制转成数组

源码

用法

castArray.test.ts
1
import { castArray } from "../../utility/array/castArray";
2
3
describe('castArray', () => {
4
test('castArray should convert the passed value to array forcibly', () => {
5
interface IObjParams {
6
age: number,
7
};
8
const obj: IObjParams = {
9
age: 21,
10
};
11
12
const received = [
13
0,
14
undefined,
15
null,
16
'ddzy',
17
obj,
18
];
19
const expected = [
20
[0],
21
[undefined],
22
[null],
23
['ddzy'],
24
[obj],
25
];
26
27
for (const [i, v] of received.entries()) {
28
const result = castArray<typeof v>(v) as any[];
29
30
expect(result[0]).toBe(expected[i][0]);
31
}
32
});
33
34
test('castArray should return it immediatly when receive an array', () => {
35
const arr: number[] = [];
36
37
const received = [
38
arr,
39
];
40
const expected = [
41
arr,
42
];
43
44
for (const [i, v] of received.entries()) {
45
const result = castArray<number[]>(v) as number[];
46
47
expect(result).toBe(expected[i]);
48
}
49
});
50
});
Copied!
最近更新 1yr ago
复制链接