pullAll

源码

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

说明

移除数组 arr 中所有和给定值相等的元素, 原地操作

用法

pullAll.ts
pullAll.test.ts
pullAll.ts
import { pullAll } from "./ddzy/utility/array/pullAll";
// ? 普通数字数组
const s1 = [1, 2, 3, 1, 2, 3];
const selector1 = [2, 3];
const result1 = pullAll<number>(s1, selector1);
console.log(result1 === s1);
console.log(result1);
console.log('--------------------------');
// ? 对象数组
const s2 = [
{
name: 'duan',
age: 21,
},
{
name: 'zhao',
age: 22,
},
{
name: 'duan',
age: 21,
},
];
const selector2 = [
{
name: 'duan',
age: 21,
},
];
const result2 = pullAll(s2, selector2);
console.log(result2 === s2);
console.log(result2);
console.log('--------------------------');
// ? 混合数组
const s3 = [
0,
19980808,
'duanzhaoyang',
false,
null,
undefined,
Symbol('a'),
function () { },
{},
[],
];
const selector3 = [false, undefined, 19980808, []];
const result3 = pullAll<any>(s3, selector3);
console.log(result3 === s3);
console.log(result3);
pullAll.test.ts
import { pullAll } from "../../utility/array/pullAll";
describe('pullAll tests...', () => {
test('Method pullAll should receive an array composed of number and return the filtered array', () => {
const received = {
origin: [1, 2, 3, 1, 2, 3],
selector: [2, 3],
};
const expected = {
isEqual: true,
result: [1, 1],
};
const result = pullAll<number>(received.origin, received.selector);
expect(result === received.origin).toBe(expected.isEqual);
result.forEach((v, i) => {
expect(v).toBe(expected.result[i]);
});
});
test('Method pullAll should receive an array composed of plain object and return the filtered array', () => {
interface IOriginParams {
name: string,
age: number,
};
const received = {
origin: [
{
name: 'duan',
age: 21,
},
{
name: 'zhao',
age: 22,
},
{
name: 'duan',
age: 21,
},
],
selector: [
{
name: 'duan',
age: 21,
},
],
};
const expected = {
isEqual: true,
result: [
{
name: 'duan',
age: 21,
},
{
name: 'zhao',
age: 22,
},
{
name: 'duan',
age: 21,
},
],
};
const result = pullAll<IOriginParams>(received.origin, received.selector);
expect(result === received.origin).toBe(expected.isEqual);
expect(result.length).toBe(expected.result.length);
});
test('Method pullAll should receive an array composed of mixed value and return the filtered array', () => {
const received = {
origin: [
0,
19980808,
'duanzhaoyang',
false,
null,
undefined,
Symbol('a'),
function () { },
{},
[],
],
selector: [false, undefined, 19980808, []],
};
const expected = {
isEqual: true,
result: [
0,
'duanzhaoyang',
null,
Symbol('a'),
function () { },
{},
[],
],
};
const result = pullAll<any>(received.origin, received.selector);
expect(result === received.origin).toBe(expected.isEqual);
expect(result.length).toBe(expected.result.length);
});
});