pullAll

说明

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

源码

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

用法

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);
  });
});

最后更新于