difference

说明

创建一个具有唯一array值的数组,每个值不包含在其他给定的数组中

源码

用法

difference.test.ts
1
import { difference } from "../../utility/array/difference";
2
3
describe('difference tests', () => {
4
test('difference should receive an array being composed of number and return the processed array', () => {
5
const received = {
6
origin: [1, 2, 3, 4, 5],
7
filter: [1, 4],
8
};
9
const expected = [2, 3, 5];
10
11
const result = difference<number>(received.origin, ...received.filter);
12
13
for (const [i, v] of result.entries()) {
14
expect(v).toBe(expected[i]);
15
}
16
});
17
18
test('difference should return a new array', () => {
19
const received = {
20
origin: [100, 200, 300, 300, 500],
21
filter: [300],
22
};
23
const expected = [100, 200, 500];
24
25
const result = difference<number>(received.origin, ...received.filter);
26
27
for (const [i, v] of result.entries()) {
28
expect(v).toBe(expected[i]);
29
}
30
31
received.origin.push(800);
32
33
for (const [i, v] of result.entries()) {
34
expect(v).toBe(expected[i]);
35
}
36
});
37
38
test('difference should receive an array being composed of any value and return a new array', () => {
39
const received = {
40
origin: [null, undefined, NaN, '', 0, false, function () { }, {}, []],
41
filter: [function () { }, [], null],
42
};
43
const expected = [undefined, NaN, '', 0, false, function () { }, {}, []];
44
45
const result = difference<any>(received.origin, ...received.filter);
46
47
expect(result.length).toBe(expected.length);
48
});
49
});
Copied!
最近更新 1yr ago
复制链接