lodash-es#flatten TypeScript Examples

The following examples show how to use lodash-es#flatten. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: flat-map.spec.ts    From s-libs with MIT License 6 votes vote down vote up
describe('flatMap()', () => {
  it('can handle large arrays', () => {
    expect(() => {
      flatMap(times(1000000, identity), () => [1]);
    }).not.toThrowError();
  });

  //
  // stolen from https://github.com/lodash/lodash
  //

  const array = [1, 2, 3, 4];

  function duplicate(n: number): number[] {
    return [n, n];
  }

  it('should map values in `array` to a new flattened array', () => {
    expect(flatMap(array, duplicate)).toEqual(flatten(map(array, duplicate)));
  });

  it('should accept a falsey `collection`', () => {
    expect(flatMap(null, identity)).toEqual([]);
    expect(flatMap(undefined, identity)).toEqual([]);
  });

  it('should work with objects with non-number length properties', () => {
    expect(flatMap({ length: [1, 2] }, (a) => a)).toEqual([1, 2]);
  });
});
Example #2
Source File: uniq-by.spec.ts    From s-libs with MIT License 5 votes vote down vote up
function testLargeArray(...values: any[]): void {
  const largeArray = flatten(
    values.map((value) => times(Math.ceil(200 / values.length), () => value)),
  );
  expect(uniqBy(largeArray, identity)).toEqual(values);
}