lodash-es#identity TypeScript Examples

The following examples show how to use lodash-es#identity. 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: sum-by.spec.ts    From s-libs with MIT License 6 votes vote down vote up
describe('sumBy()', () => {
  //
  // stolen from https://github.com/lodash/lodash
  //

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

  it('should work with an `iteratee`', () => {
    expect(sumBy(objects, (object) => object.a)).toBe(6);
  });

  it('should provide correct `iteratee` arguments', () => {
    const logger = jasmine.createSpy();
    sumBy([6], logger);
    expectSingleCallAndReset(logger, 6);
  });

  it('should return the sum of an array of numbers', () => {
    expect(sumBy(array, identity)).toBe(12);
  });

  it('should return `0` when passing empty `array` values', () => {
    expect(sumBy([], identity)).toBe(0);
  });

  it('should not skip `NaN` values', () => {
    expect(sumBy([1, NaN], identity)).toBeNaN();
  });
});
Example #3
Source File: classnames.ts    From bext with MIT License 5 votes vote down vote up
classnames = (...cn: (string | undefined)[]) =>
  cn.filter(identity).join(' ')
Example #4
Source File: sorted-index.spec.ts    From s-libs with MIT License 5 votes vote down vote up
describe('sortedIndex()', () => {
  //
  // stolen from https://github.com/lodash/lodash
  //

  it('should return the insert index', () => {
    const array = [30, 50];
    expect(sortedIndex(array, 30)).toBe(0);
    expect(sortedIndex(array, 40)).toBe(1);
    expect(sortedIndex(array, 50)).toBe(1);
  });

  it('should work with an array of strings', () => {
    const array = ['a', 'c'];
    expect(sortedIndex(array, 'a')).toBe(0);
    expect(sortedIndex(array, 'b')).toBe(1);
    expect(sortedIndex(array, 'c')).toBe(1);
  });

  it('should accept a nullish `array` and a `value`', () => {
    expect(sortedIndex(null, 1)).toBe(0);
    expect(sortedIndex(null, undefined)).toBe(0);
    expect(sortedIndex(null, NaN)).toBe(0);
    expect(sortedIndex(undefined, 1)).toBe(0);
    expect(sortedIndex(undefined, undefined)).toBe(0);
    expect(sortedIndex(undefined, NaN)).toBe(0);
  });

  it('should align with `_.sortBy`', () => {
    const expected = [1, '2', null, undefined, NaN, NaN];

    // sanity check assumption that `expected` matches `sortBy`
    for (const array of [
      [NaN, null, 1, '2', NaN, undefined],
      ['2', null, 1, NaN, NaN, undefined],
    ]) {
      expect(sortBy(array, identity)).toEqual(expected);
    }

    expect(sortedIndex(expected, 3)).toBe(2);
    expect(sortedIndex(expected, null)).toBe(2);
    expect(sortedIndex(expected, undefined)).toBe(3);
    expect(sortedIndex(expected, NaN)).toBe(4);
  });

  it('should align with `_.sortBy` for nulls', () => {
    const array = [null, null];
    expect(sortedIndex(array, null)).toBe(0);
    expect(sortedIndex(array, 1)).toBe(0);
    expect(sortedIndex(array, 'a')).toBe(0);
  });
});
Example #5
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 #6
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 #7
Source File: sort-by.spec.ts    From s-libs with MIT License 5 votes vote down vote up
describe('sortBy()', () => {
  it('handles nil collections', () => {
    expect(sortBy(null, identity)).toEqual([]);
    expect(sortBy(undefined, identity)).toEqual([]);
  });

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

  const objects = [
    { a: 'x', b: 3 },
    { a: 'y', b: 4 },
    { a: 'x', b: 1 },
    { a: 'y', b: 2 },
  ];

  it('should sort in ascending order by `iteratee`', () => {
    const result = sortBy(objects, (e) => e.b);
    expect(result.map((e) => e.b)).toEqual([1, 2, 3, 4]);
  });

  it('should work with an object for `collection`', () => {
    const actual = sortBy({ a: 1, b: 2, c: 3 }, Math.sin);
    expect(actual).toEqual([3, 1, 2]);
  });

  it('should move `NaN` and nullish values to the end', () => {
    expect(
      sortBy(
        [NaN, undefined, null, 4, null, 1, undefined, 3, NaN, 2],
        identity,
      ),
    ).toEqual([1, 2, 3, 4, null, null, undefined, undefined, NaN, NaN]);

    expect(
      sortBy(
        [NaN, undefined, null, 'd', null, 'a', undefined, 'c', NaN, 'b'],
        identity,
      ),
    ).toEqual(['a', 'b', 'c', 'd', null, null, undefined, undefined, NaN, NaN]);
  });

  it('should coerce arrays returned from `iteratee`', () => {
    const actual = sortBy(objects, (object) => {
      const result = [object.a, object.b];
      result.toString = function (): string {
        return String(this[0]);
      };
      return result;
    });

    expect(actual).toEqual([objects[0], objects[2], objects[1], objects[3]]);
  });
});
Example #8
Source File: functions.spec.ts    From s-libs with MIT License 5 votes vote down vote up
describe('functions()', () => {
  it('does not include constructors', () => {
    class MyClass {
      a = 1;

      b(): number {
        return 2;
      }
    }
    expect(functions(MyClass.prototype)).toEqual(['b']);
  });

  it("doesn't call getters (production bug)", () => {
    class Gotcha {
      get error(): never {
        throw new Error('called getter');
      }
    }
    expect(() => {
      functions(Gotcha.prototype);
    }).not.toThrowError();
  });

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

  it('should return the function names of an object', () => {
    const object = { a: 'a', b: identity, c: /x/u, d: noop };
    expect(functions(object).sort()).toEqual(['b', 'd']);
  });

  it('should not include inherited functions', () => {
    function Foo(this: any): void {
      this.a = identity;
      this.b = 'b';
    }
    Foo.prototype.c = noop;

    expect(functions(new (Foo as any)())).toEqual(['a']);
  });

  it('should return an array', () => {
    const array = [1, 2, 3];
    const actual = functions(array);
    expect(isArray(actual)).toBe(true);
    expect(actual as any).not.toBe(array);
  });
});
Example #9
Source File: transform.spec.ts    From s-libs with MIT License 5 votes vote down vote up
describe('transform', () => {
  //
  // stolen from https://github.com/lodash/lodash
  //

  it('should support an `accumulator` value', () => {
    const array = [1, 2, 3];
    const object = { a: 1, b: 2, c: 3 };

    const squareAndPush = (result: number[], value: number): void => {
      result.push(value ** 2);
    };
    expect(transform(array, squareAndPush, [] as number[])).toEqual([1, 4, 9]);
    expect(transform(object, squareAndPush, [] as number[])).toEqual([1, 4, 9]);

    const squareAndSet = (
      result: ObjectWith<number>,
      value: number,
      key: number | string,
    ): void => {
      result[key] = value ** 2;
    };
    expect(transform(array, squareAndSet, {})).toEqual({ 0: 1, 1: 4, 2: 9 });
    expect(transform(object, squareAndSet, {})).toEqual({ a: 1, b: 4, c: 9 });

    testAccumulatorReturned([]);
    testAccumulatorReturned({});
    testAccumulatorReturned(null);

    function testAccumulatorReturned(accumulator: any): void {
      expect(transform(array, noop, accumulator)).toBe(accumulator);
      expect(transform(object, noop, accumulator)).toBe(accumulator);
      expect(transform(undefined, identity, accumulator)).toBe(accumulator);
    }
  });

  it('should create an empty object when given a falsey `object`', () => {
    expect(transform(undefined, identity)).toEqual({});
  });

  it('should provide correct `iteratee` arguments when transforming an array', () => {
    const spy = jasmine.createSpy();
    const object = [1, 2, 3];
    expect(transform(object, spy)).not.toBe(object);
    expect(spy.calls.first().args).toEqual([jasmine.anything(), 1, 0]);
  });

  it('should provide correct `iteratee` arguments when transforming an object', () => {
    const spy = jasmine.createSpy();
    const object = { a: 1, b: 2, c: 3 };
    expect(transform(object, spy)).not.toBe(object);
    expect(spy.calls.first().args).toEqual([jasmine.anything(), 1, 'a']);
  });

  it('can exit early when iterating arrays', () => {
    const array = [1, 2, 3];
    const values: number[] = [];

    transform(array, (_accumulator, value) => {
      values.push(value);
      return false;
    });

    expect(values).toEqual([1]);
  });

  it('can exit early when iterating arrays', () => {
    const object = { a: 1, b: 2, c: 3 };
    const values: number[] = [];

    transform(object, (_accumulator, value) => {
      values.push(value);
      return false;
    });

    expect(values).toEqual([1]);
  });
});
Example #10
Source File: group-by.spec.ts    From s-libs with MIT License 4 votes vote down vote up
describe('groupBy()', () => {
  it('works with an `undefined` collection', () => {
    expect(groupBy(undefined, identity)).toEqual({});
  });

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

  it('should transform keys by `iteratee`', () => {
    expect(groupBy([6.1, 4.2, 6.3], Math.floor)).toEqual({
      4: [4.2],
      6: [6.1, 6.3],
    });
  });

  it('should only add values to own, not inherited, properties', () => {
    const actual = groupBy([6.1, 4.2, 6.3], (n) =>
      Math.floor(n) > 4 ? 'hasOwnProperty' : 'constructor',
    );

    expect(actual.constructor).toEqual([4.2]);
    expect(actual.hasOwnProperty).toEqual([6.1, 6.3]);
  });

  it('should work with an object for `collection`', () => {
    expect(groupBy({ a: 6.1, b: 4.2, c: 6.3 }, Math.floor)).toEqual({
      4: [4.2],
      6: [6.1, 6.3],
    });
  });

  it('should provide correct iteratee arguments', () => {
    const spy = jasmine.createSpy();
    groupBy([1, 2, 3], spy);
    expect(spy.calls.first().args).toEqual([1]);
  });

  it('should treat sparse arrays as dense', () => {
    const array = [1];
    array[2] = 3;
    const spy = jasmine.createSpy().and.returnValue(true);

    groupBy(array, spy);

    expectCallsAndReset(spy, [1], [undefined], [3]);
  });

  it('should not iterate custom properties of arrays', () => {
    const array = [1];
    (array as any).a = 1;
    const spy = jasmine.createSpy().and.returnValue(true);

    groupBy(array, spy);

    expectCallsAndReset(spy, [1]);
  });

  it('iterates over own string keyed properties of objects', () => {
    const object = { a: 1 };
    const spy = jasmine.createSpy().and.returnValue(true);

    groupBy(object, spy);

    expectCallsAndReset(spy, [1]);
  });

  it('should ignore changes to `length`', () => {
    const array = [1];
    const spy = jasmine.createSpy().and.callFake(() => {
      array.push(2);
      return true;
    });

    groupBy(array, spy);

    expect(spy).toHaveBeenCalledTimes(1);
  });

  it('should ignore added `object` properties', () => {
    const object: any = { a: 1 };
    const spy = jasmine.createSpy().and.callFake(() => {
      object.b = 2;
      return true;
    });

    groupBy(object, spy);

    expect(spy).toHaveBeenCalledTimes(1);
  });
});
Example #11
Source File: partition.spec.ts    From s-libs with MIT License 4 votes vote down vote up
describe('partition', () => {
  it('can handle null/undefined collections', () => {
    expect(partition(null, identity)).toEqual([[], []]);
    expect(partition(undefined, identity)).toEqual([[], []]);
  });

  it('has fancy typing for arrays', () => {
    expect().nothing();

    type A = Array<number | string>;
    const a = [] as A;
    const aOrU = a as A | undefined;
    const aOrN = a as A | null;

    expectTypeOf(partition(a, () => true)).toEqualTypeOf<
      [Array<number | string>, Array<number | string>]
    >();
    expectTypeOf(partition(aOrU, () => true)).toEqualTypeOf<
      [Array<number | string>, Array<number | string>]
    >();
    expectTypeOf(partition(aOrN, () => true)).toEqualTypeOf<
      [Array<number | string>, Array<number | string>]
    >();

    // narrowing

    expectTypeOf(partition(a, isString)).toEqualTypeOf<[string[], number[]]>();
    expectTypeOf(partition(aOrU, isString)).toEqualTypeOf<
      [string[], number[]]
    >();
    expectTypeOf(partition(aOrN, isString)).toEqualTypeOf<
      [string[], number[]]
    >();

    expectTypeOf(partition(a, isDateOrString)).toEqualTypeOf<
      [string[], number[]]
    >();
    expectTypeOf(partition(aOrU, isDateOrString)).toEqualTypeOf<
      [string[], number[]]
    >();
    expectTypeOf(partition(aOrN, isDateOrString)).toEqualTypeOf<
      [string[], number[]]
    >();

    expectTypeOf(partition(a, isA)).toEqualTypeOf<
      [Array<'a'>, Array<number | string>]
    >();
    expectTypeOf(partition(aOrU, isA)).toEqualTypeOf<
      [Array<'a'>, Array<number | string>]
    >();
    expectTypeOf(partition(aOrN, isA)).toEqualTypeOf<
      [Array<'a'>, Array<number | string>]
    >();

    type AB = Array<'a' | 'b'>;
    const ab = [] as AB;
    const abOrU = ab as AB | undefined;
    const abOrN = ab as AB | null;

    expectTypeOf(partition(ab, isA)).toEqualTypeOf<[Array<'a'>, Array<'b'>]>();
    expectTypeOf(partition(abOrU, isA)).toEqualTypeOf<
      [Array<'a'>, Array<'b'>]
    >();
    expectTypeOf(partition(abOrN, isA)).toEqualTypeOf<
      [Array<'a'>, Array<'b'>]
    >();

    expectTypeOf(partition(ab, isString)).toEqualTypeOf<
      [Array<'a' | 'b'>, []]
    >();
    expectTypeOf(partition(abOrU, isString)).toEqualTypeOf<
      [Array<'a' | 'b'>, []]
    >();
    expectTypeOf(partition(abOrN, isString)).toEqualTypeOf<
      [Array<'a' | 'b'>, []]
    >();

    type AN = Array<number | 'a'>;
    const an = [] as AN;
    const anOrU = an as AN | undefined;
    const anOrN = an as AN | null;
    expectTypeOf(partition(an, isStringOr2)).toEqualTypeOf<
      [Array<'a' | 2>, number[]]
    >();
    expectTypeOf(partition(anOrU, isStringOr2)).toEqualTypeOf<
      [Array<'a' | 2>, number[]]
    >();
    expectTypeOf(partition(anOrN, isStringOr2)).toEqualTypeOf<
      [Array<'a' | 2>, number[]]
    >();
  });

  it('has fancy typing for objects', () => {
    expect().nothing();

    interface O {
      a: number;
      2: string;
      c: Date | Document;
    }
    const o = {} as O;
    const oOrN = o as O | null;
    const oOrU = o as O | undefined;
    type AllOTypes = Date | Document | number | string;
    expectTypeOf(partition(o, () => true)).toEqualTypeOf<
      [AllOTypes[], AllOTypes[]]
    >();
    expectTypeOf(partition(oOrN, () => true)).toEqualTypeOf<
      [AllOTypes[], AllOTypes[]]
    >();
    expectTypeOf(partition(oOrU, () => true)).toEqualTypeOf<
      [AllOTypes[], AllOTypes[]]
    >();

    // value narrowing

    expectTypeOf(partition(o, isString)).toEqualTypeOf<
      [string[], Array<Date | Document | number>]
    >();
    expectTypeOf(partition(oOrU, isString)).toEqualTypeOf<
      [string[], Array<Date | Document | number>]
    >();
    expectTypeOf(partition(oOrN, isString)).toEqualTypeOf<
      [string[], Array<Date | Document | number>]
    >();

    expectTypeOf(partition(o, isDate)).toEqualTypeOf<
      [Date[], Array<Document | number | string>]
    >();
    expectTypeOf(partition(oOrU, isDate)).toEqualTypeOf<
      [Date[], Array<Document | number | string>]
    >();
    expectTypeOf(partition(oOrN, isDate)).toEqualTypeOf<
      [Date[], Array<Document | number | string>]
    >();

    expectTypeOf(partition(o, isNumberOrString)).toEqualTypeOf<
      [Array<number | string>, Array<Date | Document>]
    >();
    expectTypeOf(partition(oOrU, isNumberOrString)).toEqualTypeOf<
      [Array<number | string>, Array<Date | Document>]
    >();
    expectTypeOf(partition(oOrN, isNumberOrString)).toEqualTypeOf<
      [Array<number | string>, Array<Date | Document>]
    >();

    expectTypeOf(partition(o, isDateOrString)).toEqualTypeOf<
      [Array<Date | string>, Array<Document | number>]
    >();
    expectTypeOf(partition(oOrU, isDateOrString)).toEqualTypeOf<
      [Array<Date | string>, Array<Document | number>]
    >();
    expectTypeOf(partition(oOrN, isDateOrString)).toEqualTypeOf<
      [Array<Date | string>, Array<Document | number>]
    >();

    expectTypeOf(partition(o, isMap)).toEqualTypeOf<[[], AllOTypes[]]>();
    expectTypeOf(partition(oOrU, isMap)).toEqualTypeOf<[[], AllOTypes[]]>();
    expectTypeOf(partition(oOrN, isMap)).toEqualTypeOf<[[], AllOTypes[]]>();

    expectTypeOf(partition(o, isMapOrString)).toEqualTypeOf<
      [string[], Array<Date | Document | number>]
    >();
    expectTypeOf(partition(oOrU, isMapOrString)).toEqualTypeOf<
      [string[], Array<Date | Document | number>]
    >();
    expectTypeOf(partition(oOrN, isMapOrString)).toEqualTypeOf<
      [string[], Array<Date | Document | number>]
    >();

    interface S2 {
      a: number | 'a';
    }
    const s2 = {} as S2;
    const s2OrN = s2 as S2 | null;
    const s2OrU = s2 as S2 | undefined;

    expectTypeOf(partition(s2, isA)).toEqualTypeOf<[Array<'a'>, number[]]>();
    expectTypeOf(partition(s2OrU, isA)).toEqualTypeOf<[Array<'a'>, number[]]>();
    expectTypeOf(partition(s2OrN, isA)).toEqualTypeOf<[Array<'a'>, number[]]>();

    expectTypeOf(partition(s2, isStringOr2)).toEqualTypeOf<
      [Array<'a' | 2>, number[]]
    >();
    expectTypeOf(partition(s2OrU, isStringOr2)).toEqualTypeOf<
      [Array<'a' | 2>, number[]]
    >();
    expectTypeOf(partition(s2OrN, isStringOr2)).toEqualTypeOf<
      [Array<'a' | 2>, number[]]
    >();
  });

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

  const array = [1, 0, 1];

  it('should split elements into two groups by `predicate`', () => {
    expect(partition([], identity)).toEqual([[], []]);
    expect(partition(array, stubTrue)).toEqual([array, []]);
    expect(partition(array, stubFalse)).toEqual([[], array]);
  });

  it('should work with an object for `collection`', () => {
    expect(partition({ a: 1.1, b: 0.2, c: 1.3 }, Math.floor)).toEqual([
      [1.1, 1.3],
      [0.2],
    ]);
  });

  it('should provide correct iteratee arguments', () => {
    const spy = jasmine.createSpy();
    partition([1, 2, 3], spy);
    expect(spy.calls.first().args).toEqual([1]);
  });

  it('iterates over own string keyed properties of objects', () => {
    const object = { a: 1 };
    const spy = jasmine.createSpy();

    partition(object, spy);

    expectCallsAndReset(spy, [1]);
  });

  it('should ignore changes to `length`', () => {
    const shortArray = [1];
    const spy = jasmine.createSpy().and.callFake(() => {
      shortArray.push(2);
      return true;
    });

    partition(shortArray, spy);

    expect(spy).toHaveBeenCalledTimes(1);
  });

  it('should ignore added `object` properties', () => {
    const object: any = { a: 1 };
    const spy = jasmine.createSpy().and.callFake(() => {
      object.b = 2;
      return true;
    });

    partition(object, spy);

    expect(spy).toHaveBeenCalledTimes(1);
  });
});
Example #12
Source File: some.spec.ts    From s-libs with MIT License 4 votes vote down vote up
describe('some()', () => {
  //
  // stolen from https://github.com/lodash/lodash
  //

  it('should return `true` if `predicate` returns truthy for any element', () => {
    expect(some([false, 1, ''], identity)).toBe(true);
    expect(some([null, 'a', 0], identity)).toBe(true);
  });

  it('should return `false` for empty collections', () => {
    for (const empty of [[], {}, null, undefined, false, 0, NaN, '']) {
      expect(some(empty, identity)).toBe(false);
    }
  });

  it('should return `true` as soon as `predicate` returns truthy', () => {
    let count = 0;

    const result = some([null, true, null], (value) => {
      ++count;
      return value;
    });

    expect(result).toBe(true);
    expect(count).toBe(2);
  });

  it('should return `false` if `predicate` returns falsey for all elements', () => {
    expect(some([false, false, false], identity)).toBe(false);
    expect(some([null, 0, ''], identity)).toBe(false);
  });

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

  it('should treat sparse arrays as dense', () => {
    const array = [1];
    array[2] = 3;
    const spy = jasmine.createSpy();

    some(array, spy);

    expectCallsAndReset(spy, [1, 0], [undefined, 1], [3, 2]);
  });

  it('should not iterate custom properties on arrays', () => {
    const array = [1];
    (array as any).a = 1;
    const spy = jasmine.createSpy();

    some(array, spy);

    expectCallsAndReset(spy, [1, 0]);
  });

  it('iterates over own string keyed properties of objects', () => {
    const object = { a: 1 };
    const spy = jasmine.createSpy();

    some(object, spy);

    expectCallsAndReset(spy, [1, 'a']);
  });

  it('should ignore changes to `length`', () => {
    const array = [1];
    const spy = jasmine.createSpy().and.callFake(() => {
      array.push(2);
      return false;
    });

    some(array, spy);

    expect(spy).toHaveBeenCalledTimes(1);
  });

  it('should ignore added `object` properties', () => {
    const object: any = { a: 1 };
    const spy = jasmine.createSpy().and.callFake(() => {
      object.b = 2;
      return false;
    });

    every(object, spy);

    expect(spy).toHaveBeenCalledTimes(1);
  });
});
Example #13
Source File: map-keys.spec.ts    From s-libs with MIT License 4 votes vote down vote up
describe('mapKeys()', () => {
  it('has fancy typing', () => {
    expect().nothing();

    type A = number[];
    const a = null as unknown as A;
    const aOrN = a as A | null;
    const aOrU = a as A | undefined;

    expectTypeOf(mapKeys(a, String)).toEqualTypeOf<Record<string, number>>();
    expectTypeOf(mapKeys(aOrN, String)).toEqualTypeOf<Record<string, number>>();
    expectTypeOf(mapKeys(aOrU, String)).toEqualTypeOf<Record<string, number>>();
    expectTypeOf(mapKeys(a, (_v, k) => k)).toEqualTypeOf<
      Record<number, number>
    >();
    expectTypeOf(mapKeys(aOrN, (_v, k) => k)).toEqualTypeOf<
      Record<number, number>
    >();
    expectTypeOf(mapKeys(aOrU, (_v, k) => k)).toEqualTypeOf<
      Record<number, number>
    >();

    interface O {
      a: number;
      b: number;
    }
    const o = null as unknown as O;
    const oOrN = o as O | null;
    const oOrU = o as O | undefined;

    expectTypeOf(mapKeys(o, Number)).toEqualTypeOf<Record<number, number>>();
    expectTypeOf(mapKeys(oOrN, Number)).toEqualTypeOf<Record<number, number>>();
    expectTypeOf(mapKeys(oOrU, Number)).toEqualTypeOf<Record<number, number>>();
    expectTypeOf(mapKeys(o, (_v, k) => k)).toEqualTypeOf<O>();
    expectTypeOf(mapKeys(oOrN, (_v, k) => k)).toEqualTypeOf<O | {}>();
    expectTypeOf(mapKeys(oOrU, (_v, k) => k)).toEqualTypeOf<O | {}>();
  });

  it('maps strings', () => {
    expect(mapKeys('12', String) as any).toEqual({ 1: '1', 2: '2' });
  });

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

  it('should map keys in `object` to a new object', () => {
    expect(mapKeys({ a: 1, b: 2 }, String)).toEqual({ 1: 1, 2: 2 });
  });

  it('should treat arrays like objects', () => {
    expect(mapKeys([1, 2], String)).toEqual({ 1: 1, 2: 2 });
  });

  it('should accept a falsey `object`', () => {
    expect(mapKeys(null, identity)).toEqual({});
    expect(mapKeys(undefined, identity)).toEqual({});
  });

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

  it('should treat sparse arrays as dense', () => {
    const array = [1];
    array[2] = 3;
    const spy = jasmine.createSpy();

    mapKeys(array, spy);

    expectCallsAndReset(spy, [1, '0'], [3, '2']);
  });

  it('should ignore changes to `length`', () => {
    const array = [1];
    const spy = jasmine.createSpy().and.callFake(() => {
      array.push(2);
      return true;
    });

    mapKeys(array, spy);

    expect(spy).toHaveBeenCalledTimes(1);
  });

  it('should ignore added `object` properties', () => {
    const object: any = { a: 1 };
    const spy = jasmine.createSpy().and.callFake(() => {
      object.b = 2;
      return true;
    });

    mapKeys(object, spy);

    expect(spy).toHaveBeenCalledTimes(1);
  });
});