difference

源码

https://github.com/ddzy/ts-utility-plugins/tree/master/src/ddzy/utility/array/difference

说明

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

用法

difference.ts
difference.test.ts
import { difference } from "./ddzy/utility/array/difference";
// ? 纯数字数组
const s1 = [1, 2, 3, 4, 5];
const p1 = difference(s1, 1, 4);
console.log(p1);
console.log('--------------');
// ? 不应该改变源数组
const s2 = [100, 200, 300, 300, 500];
const p2 = difference<number>(s2, 300);
console.log(p2);
s2.push(800);
console.log(p2);
console.log('-----------------');
// ? 任意值数组
const s3: any[] = [null, undefined, NaN, '', 0, false, function () { }, {}, []];
const p3 = difference(s3, function () { }, [], null);
console.log(p3);
console.log('----------');
import { difference } from "../../utility/array/difference";
describe('difference tests', () => {
test('difference should receive an array being composed of number and return the processed array', () => {
const received = {
origin: [1, 2, 3, 4, 5],
filter: [1, 4],
};
const expected = [2, 3, 5];
const result = difference<number>(received.origin, ...received.filter);
for (const [i, v] of result.entries()) {
expect(v).toBe(expected[i]);
}
});
test('difference should return a new array', () => {
const received = {
origin: [100, 200, 300, 300, 500],
filter: [300],
};
const expected = [100, 200, 500];
const result = difference<number>(received.origin, ...received.filter);
for (const [i, v] of result.entries()) {
expect(v).toBe(expected[i]);
}
received.origin.push(800);
for (const [i, v] of result.entries()) {
expect(v).toBe(expected[i]);
}
});
test('difference should receive an array being composed of any value and return a new array', () => {
const received = {
origin: [null, undefined, NaN, '', 0, false, function () { }, {}, []],
filter: [function () { }, [], null],
};
const expected = [undefined, NaN, '', 0, false, function () { }, {}, []];
const result = difference<any>(received.origin, ...received.filter);
expect(result.length).toBe(expected.length);
});
});