lodash-es#times TypeScript Examples

The following examples show how to use lodash-es#times. 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: sample.spec.ts    From s-libs with MIT License 6 votes vote down vote up
describe('sample()', () => {
  it('gets the full universe of elements', () => {
    const array = [1, 2, 3];
    expect(new Set(times(30, () => sample(array)))).toEqual(new Set(array));

    const object = { a: 1, b: 2, c: 3 };
    expect(new Set(times(30, () => sample(object)))).toEqual(new Set(array));
  });

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

  it('should return a random element', () => {
    const array = [1, 2, 3];
    expect(array.includes(sample(array))).toBe(true);
  });

  it('should return `undefined` when sampling empty collections', () => {
    expect(sample([])).toBeUndefined();
    expect(sample({})).toBeUndefined();
  });

  it('should sample an object', () => {
    expect([1, 2, 3].includes(sample({ a: 1, b: 2, c: 3 }))).toBe(true);
  });
});
Example #3
Source File: shuffle.spec.ts    From s-libs with MIT License 6 votes vote down vote up
describe('shuffle()', () => {
  it('accepts nil values for the collection, returning an empty array', () => {
    expect(shuffle(null)).toEqual([]);
    expect(shuffle(undefined)).toEqual([]);
  });

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

  const array = [1, 2, 3];
  const object = { a: 1, b: 2, c: 3 };

  it('should return a new array', () => {
    const actual = shuffle(array);
    expect(actual).toEqual(jasmine.arrayWithExactContents(array));
    expect(actual).not.toBe(array);
  });

  it('should contain the same elements after a collection is shuffled', () => {
    expect(shuffle(array).sort()).toEqual(array);
    expect(shuffle(object).sort()).toEqual(array);
  });

  it('should shuffle small collections', () => {
    const actual = times(1000, () => shuffle([1, 2]));
    expect(sortBy(uniqBy(actual, String), '0')).toEqual([
      [1, 2],
      [2, 1],
    ]);
  });
});
Example #4
Source File: difference.spec.ts    From s-libs with MIT License 5 votes vote down vote up
describe('difference()', () => {
  //
  // stolen from https://github.com/lodash/lodash
  //

  it('should return the difference of two arrays', () => {
    expect(difference([2, 1], [2, 3])).toEqual([1]);
  });

  it('should return the difference of multiple arrays', () => {
    expect(difference([2, 1, 2, 3], [3, 4], [3, 2])).toEqual([1]);
  });

  it('should match `NaN`', () => {
    expect(difference([1, NaN, 3], [NaN, 5, NaN])).toEqual([1, 3]);
  });

  it('should work with large arrays', () => {
    const array1: any[] = range(201);
    const array2: any[] = range(200);
    const a = {};
    const b = {};
    const c = {};
    array1.push(a, b, c);
    array2.push(b, c, a);

    expect(difference(array1, array2)).toEqual([200]);
  });

  it('should work with large arrays of `NaN`', () => {
    const largeArray = times(200, () => NaN);
    expect(difference([1, NaN, 3], largeArray)).toEqual([1, 3]);
  });

  it('should work with large arrays of objects', () => {
    const object1 = {};
    const object2 = {};
    const largeArray = times(200, () => object1);

    expect(difference([object1, object2], largeArray)).toEqual([object2]);
  });

  it('should return an array', () => {
    const array = [1, 2, 3];
    const actual = difference(array);

    expect(actual).toEqual(jasmine.any(Array));
    expect(actual).not.toBe(array);
  });
});
Example #5
Source File: flatten.spec.ts    From s-libs with MIT License 5 votes vote down vote up
describe('flatten()', () => {
  it('can handle large arrays', () => {
    expect(() => {
      flatten(times(1000000, () => [1]));
    }).not.toThrowError();
  });

  //
  // stolen from https://github.com/healthiers/mini-dash
  //

  it('should return empty array', () => {
    expect(flatten([])).toEqual([]);
  });

  it('should flatten uniform length arrays', () => {
    expect(flatten([[1], [2], [3]])).toEqual([1, 2, 3]);
  });

  it('should return different lenth arrays', () => {
    expect(flatten([[1, 2, 3], [4], [5, 6]])).toEqual([1, 2, 3, 4, 5, 6]);
  });

  it('should not modify original array', () => {
    const array = [[1, 2, 3], [4], [5, 6]];

    const flattened = flatten(array);

    expect(flattened).toEqual([1, 2, 3, 4, 5, 6]);
    expect(array).toEqual([[1, 2, 3], [4], [5, 6]]);
  });

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

  it('should treat sparse arrays as dense', () => {
    const sparse = [4];
    sparse[2] = 6;

    expect(flatten([[1, 2, 3], sparse, Array(2)])).toEqual([
      1,
      2,
      3,
      4,
      undefined,
      6,
      undefined,
      undefined,
    ]);
  });

  it('should work with extremely large arrays', () => {
    const large = Array(5e5);

    expect(flatten([large])).toEqual(large);
  });

  it('should work with empty arrays', () => {
    expect(flatten([[], [[]], [[], [[[]]]]])).toEqual([[], [], [[[]]]]);
  });
});
Example #6
Source File: intersection.spec.ts    From s-libs with MIT License 5 votes vote down vote up
describe('intersection()', () => {
  it('works with null and undefined', () => {
    const array = [0, 1, null, 3];
    expect(intersection(array, null)).toEqual([]);
    expect(intersection(array, undefined)).toEqual([]);
    expect(intersection(null, array)).toEqual([]);
    expect(intersection(undefined, array)).toEqual([]);
    expect(intersection(null)).toEqual([]);
    expect(intersection(undefined)).toEqual([]);
  });

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

  it('should return the intersection of two arrays', () => {
    expect(intersection([2, 1], [2, 3])).toEqual([2]);
  });

  it('should return the intersection of multiple arrays', () => {
    expect(intersection([2, 1, 2, 3], [3, 4], [3, 2])).toEqual([3]);
  });

  it('should return an array of unique values', () => {
    expect(intersection([1, 1, 3, 2, 2], [5, 2, 2, 1, 4], [2, 1, 1])).toEqual([
      1, 2,
    ]);
  });

  it('should work with a single array', () => {
    expect(intersection([1, 1, 3, 2, 2])).toEqual([1, 3, 2]);
  });

  it('should match `NaN`', () => {
    expect(intersection([1, NaN, 3], [NaN, 5, NaN])).toEqual([NaN]);
  });

  it('should work with large arrays of `NaN`', () => {
    const largeArray = times(200, () => NaN);
    expect(intersection([1, NaN, 3], largeArray)).toEqual([NaN]);
  });

  it('should work with large arrays of objects', () => {
    const object = {};
    const largeArray = times(200, constant(object));

    expect(intersection([object], largeArray)).toEqual([object]);
    expect(intersection(range(200), [1])).toEqual([1]);
  });

  it('should return an array', () => {
    const array = [1, 2, 3];
    const actual = intersection(array);

    expect(actual).toEqual(jasmine.any(Array));
    expect(actual).not.toBe(array);
  });
});
Example #7
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);
}
Example #8
Source File: uniq-by.spec.ts    From s-libs with MIT License 5 votes vote down vote up
describe('uniqBy()', () => {
  //
  // stolen from https://github.com/lodash/lodash
  //

  const objects = [{ a: 2 }, { a: 3 }, { a: 1 }, { a: 2 }, { a: 3 }, { a: 1 }];

  it('should work with an `iteratee`', () => {
    expect(uniqBy(objects, (object) => object.a)).toEqual(objects.slice(0, 3));
  });

  it('should work with large arrays', () => {
    const largeArray = times(200, () => [1, 2]);

    const actual = uniqBy(largeArray, String);
    expect(actual[0]).toBe(largeArray[0]);
    expect(actual).toEqual([[1, 2]]);
  });

  it('should provide correct `iteratee` arguments', () => {
    const spy = jasmine.createSpy();
    uniqBy(objects, spy);
    expect(spy.calls.first().args).toEqual([objects[0]]);
  });

  it('should return unique values of an unsorted array', () => {
    expect(uniqBy([2, 1, 2], identity)).toEqual([2, 1]);
  });

  it('should return unique values of a sorted array', () => {
    expect(uniqBy([1, 2, 2], identity)).toEqual([1, 2]);
  });

  it('should treat object instances as unique', () => {
    expect(uniqBy(objects, identity)).toEqual(objects);
  });

  it('should match `NaN`', () => {
    expect(uniqBy([NaN, NaN], identity)).toEqual([NaN]);
  });

  it('should work with large arrays', () => {
    testLargeArray(0, {}, 'a');
  });

  it('should work with large arrays of boolean, `NaN`, and nullish values', () => {
    testLargeArray(null, undefined, false, true, NaN);
  });

  it('should work with large arrays of symbols', () => {
    const largeArray = times(200, Symbol);
    expect(uniqBy(largeArray, identity)).toEqual(largeArray);
  });

  it('should work with large arrays of well-known symbols', () => {
    // See http://www.ecma-international.org/ecma-262/6.0/#sec-well-known-symbols.
    testLargeArray(
      Symbol.hasInstance,
      Symbol.isConcatSpreadable,
      Symbol.iterator,
      Symbol.match,
      Symbol.replace,
      Symbol.search,
      Symbol.species,
      Symbol.split,
      Symbol.toPrimitive,
      Symbol.toStringTag,
      Symbol.unscopables,
    );
  });

  it('should distinguish between numbers and numeric strings', () => {
    testLargeArray('2', 2);
  });
});
Example #9
Source File: random.spec.ts    From s-libs with MIT License 5 votes vote down vote up
describe('random()', () => {
  const iterations = 1000;

  it('treats the min as 0 when only provided one integer', () => {
    expect(uniq(times(iterations, () => random(2))).sort()).toEqual([0, 1, 2]);
  });

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

  it('should return `0` or `1` when no arguments are given', () => {
    expect(uniq(times(iterations, () => random())).sort()).toEqual([0, 1]);
  });

  it('should support a `min` and `max`', () => {
    for (let i = iterations; --i >= 0; ) {
      const result = random(5, 10);
      expect(result).toBeGreaterThanOrEqual(5);
      expect(result).toBeLessThanOrEqual(10);
    }
  });

  it('should support not providing a `max`', () => {
    for (let i = iterations; --i >= 0; ) {
      const result = random(5);
      expect(result).toBeGreaterThanOrEqual(0);
      expect(result).toBeLessThanOrEqual(5);
    }
  });

  it('should swap `min` and `max` when `min` > `max`', () => {
    const result = uniq(times(iterations, () => random(4, 2))).sort();
    expect(result).toEqual([2, 3, 4]);
  });

  it('should support large integer values', () => {
    const min = 2 ** 31;
    const max = 2 ** 62;
    for (let i = iterations; --i >= 0; ) {
      const result = random(min, max);
      expect(result).toBeGreaterThanOrEqual(min);
      expect(result).toBeLessThanOrEqual(max);
    }
  });

  it('should support floats', () => {
    const result = random(1.5, 1.6);
    expect(result).toBeGreaterThanOrEqual(1.5);
    expect(result).toBeLessThanOrEqual(1.6);
  });

  it('should support providing a `floating`', () => {
    let result: number;

    result = random(true);
    expect(result).toBeGreaterThanOrEqual(0);
    expect(result).toBeLessThanOrEqual(1);
    expect(result % 1).not.toBe(0);

    result = random(2, true);
    expect(result).toBeGreaterThanOrEqual(0);
    expect(result).toBeLessThanOrEqual(2);
    expect(result % 1).not.toBe(0);

    result = random(2, 4, true);
    expect(result).toBeGreaterThanOrEqual(2);
    expect(result).toBeLessThanOrEqual(4);
    expect(result % 1).not.toBe(0);
  });
});