mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-03 08:37:49 +00:00
wip: code formatting nits continued
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
import assert from 'node:assert';
|
||||
import { args, toArgs, identity } from './utils';
|
||||
import difference from '../src/difference';
|
||||
import union from '../src/union';
|
||||
@@ -31,32 +30,32 @@ import zip from '../src/zip';
|
||||
import xor from '../src/xor';
|
||||
|
||||
describe('"Arrays" category methods', () => {
|
||||
const args = toArgs([1, null, [3], null, 5]),
|
||||
sortedArgs = toArgs([1, [3], 5, null, null]),
|
||||
array = [1, 2, 3, 4, 5, 6];
|
||||
const args = toArgs([1, null, [3], null, 5]);
|
||||
const sortedArgs = toArgs([1, [3], 5, null, null]);
|
||||
const array = [1, 2, 3, 4, 5, 6];
|
||||
|
||||
it('should work with `arguments` objects', () => {
|
||||
function message(methodName) {
|
||||
return `\`_.${methodName}\` should work with \`arguments\` objects`;
|
||||
}
|
||||
|
||||
assert.deepStrictEqual(difference(args, [null]), [1, [3], 5], message('difference'));
|
||||
expect(difference(args, [null]), [1, [3], 5]).toEqual(message('difference'));
|
||||
assert.deepStrictEqual(
|
||||
difference(array, args),
|
||||
[2, 3, 4, 6],
|
||||
'_.difference should work with `arguments` objects as secondary arguments',
|
||||
);
|
||||
|
||||
assert.deepStrictEqual(union(args, [null, 6]), [1, null, [3], 5, 6], message('union'));
|
||||
expect(union(args, [null, 6]), [1, null, [3], 5, 6]).toEqual(message('union'));
|
||||
assert.deepStrictEqual(
|
||||
union(array, args),
|
||||
array.concat([null, [3]]),
|
||||
'_.union should work with `arguments` objects as secondary arguments',
|
||||
);
|
||||
|
||||
assert.deepStrictEqual(compact(args), [1, [3], 5], message('compact'));
|
||||
assert.deepStrictEqual(drop(args, 3), [null, 5], message('drop'));
|
||||
assert.deepStrictEqual(dropRight(args, 3), [1, null], message('dropRight'));
|
||||
expect(compact(args), [1, [3], 5]).toEqual(message('compact'));
|
||||
expect(drop(args, 3), [null, 5]).toEqual(message('drop'));
|
||||
expect(dropRight(args, 3), [1, null]).toEqual(message('dropRight'));
|
||||
assert.deepStrictEqual(
|
||||
dropRightWhile(args, identity),
|
||||
[1, null, [3], null],
|
||||
@@ -67,26 +66,26 @@ describe('"Arrays" category methods', () => {
|
||||
[null, [3], null, 5],
|
||||
message('dropWhile'),
|
||||
);
|
||||
assert.deepStrictEqual(findIndex(args, identity), 0, message('findIndex'));
|
||||
assert.deepStrictEqual(findLastIndex(args, identity), 4, message('findLastIndex'));
|
||||
assert.deepStrictEqual(flatten(args), [1, null, 3, null, 5], message('flatten'));
|
||||
assert.deepStrictEqual(head(args), 1, message('head'));
|
||||
assert.deepStrictEqual(indexOf(args, 5), 4, message('indexOf'));
|
||||
assert.deepStrictEqual(initial(args), [1, null, [3], null], message('initial'));
|
||||
assert.deepStrictEqual(intersection(args, [1]), [1], message('intersection'));
|
||||
assert.deepStrictEqual(last(args), 5, message('last'));
|
||||
assert.deepStrictEqual(lastIndexOf(args, 1), 0, message('lastIndexOf'));
|
||||
assert.deepStrictEqual(sortedIndex(sortedArgs, 6), 3, message('sortedIndex'));
|
||||
assert.deepStrictEqual(sortedIndexOf(sortedArgs, 5), 2, message('sortedIndexOf'));
|
||||
assert.deepStrictEqual(sortedLastIndex(sortedArgs, 5), 3, message('sortedLastIndex'));
|
||||
assert.deepStrictEqual(sortedLastIndexOf(sortedArgs, 1), 0, message('sortedLastIndexOf'));
|
||||
assert.deepStrictEqual(tail(args, 4), [null, [3], null, 5], message('tail'));
|
||||
assert.deepStrictEqual(take(args, 2), [1, null], message('take'));
|
||||
assert.deepStrictEqual(takeRight(args, 1), [5], message('takeRight'));
|
||||
assert.deepStrictEqual(takeRightWhile(args, identity), [5], message('takeRightWhile'));
|
||||
assert.deepStrictEqual(takeWhile(args, identity), [1], message('takeWhile'));
|
||||
assert.deepStrictEqual(uniq(args), [1, null, [3], 5], message('uniq'));
|
||||
assert.deepStrictEqual(without(args, null), [1, [3], 5], message('without'));
|
||||
expect(findIndex(args, identity), 0).toEqual(message('findIndex'));
|
||||
expect(findLastIndex(args, identity), 4).toEqual(message('findLastIndex'));
|
||||
expect(flatten(args), [1, null, 3, null, 5]).toEqual(message('flatten'));
|
||||
expect(head(args), 1).toEqual(message('head'));
|
||||
expect(indexOf(args, 5), 4).toEqual(message('indexOf'));
|
||||
expect(initial(args), [1, null, [3], null]).toEqual(message('initial'));
|
||||
expect(intersection(args, [1]), [1]).toEqual(message('intersection'));
|
||||
expect(last(args), 5).toEqual(message('last'));
|
||||
expect(lastIndexOf(args, 1), 0).toEqual(message('lastIndexOf'));
|
||||
expect(sortedIndex(sortedArgs, 6), 3).toEqual(message('sortedIndex'));
|
||||
expect(sortedIndexOf(sortedArgs, 5), 2).toEqual(message('sortedIndexOf'));
|
||||
expect(sortedLastIndex(sortedArgs, 5), 3).toEqual(message('sortedLastIndex'));
|
||||
expect(sortedLastIndexOf(sortedArgs, 1), 0).toEqual(message('sortedLastIndexOf'));
|
||||
expect(tail(args, 4), [null, [3], null, 5]).toEqual(message('tail'));
|
||||
expect(take(args, 2), [1, null]).toEqual(message('take'));
|
||||
expect(takeRight(args, 1), [5]).toEqual(message('takeRight'));
|
||||
expect(takeRightWhile(args, identity), [5]).toEqual(message('takeRightWhile'));
|
||||
expect(takeWhile(args, identity), [1]).toEqual(message('takeWhile'));
|
||||
expect(uniq(args), [1, null, [3], 5]).toEqual(message('uniq'));
|
||||
expect(without(args, null), [1, [3], 5]).toEqual(message('without'));
|
||||
assert.deepStrictEqual(
|
||||
zip(args, args),
|
||||
[
|
||||
@@ -105,10 +104,10 @@ describe('"Arrays" category methods', () => {
|
||||
return `\`_.${methodName}\` should accept falsey primary arguments`;
|
||||
}
|
||||
|
||||
assert.deepStrictEqual(difference(null, array), [], message('difference'));
|
||||
assert.deepStrictEqual(intersection(null, array), [], message('intersection'));
|
||||
assert.deepStrictEqual(union(null, array), array, message('union'));
|
||||
assert.deepStrictEqual(xor(null, array), array, message('xor'));
|
||||
expect(difference(null, array), []).toEqual(message('difference'));
|
||||
expect(intersection(null, array), []).toEqual(message('intersection'));
|
||||
expect(union(null, array), array).toEqual(message('union'));
|
||||
expect(xor(null, array), array).toEqual(message('xor'));
|
||||
});
|
||||
|
||||
it('should accept falsey secondary arguments', () => {
|
||||
@@ -116,8 +115,8 @@ describe('"Arrays" category methods', () => {
|
||||
return `\`_.${methodName}\` should accept falsey secondary arguments`;
|
||||
}
|
||||
|
||||
assert.deepStrictEqual(difference(array, null), array, message('difference'));
|
||||
assert.deepStrictEqual(intersection(array, null), [], message('intersection'));
|
||||
assert.deepStrictEqual(union(array, null), array, message('union'));
|
||||
expect(difference(array, null), array).toEqual(message('difference'));
|
||||
expect(intersection(array, null), []).toEqual(message('intersection'));
|
||||
expect(union(array, null), array).toEqual(message('union'));
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,3 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { stubString } from './utils';
|
||||
|
||||
@@ -68,14 +67,14 @@ describe('"Strings" category methods', () => {
|
||||
const func = methods[methodName];
|
||||
|
||||
it(`\`_.${methodName}\` should return an empty string for empty values`, () => {
|
||||
const values = [, null, undefined, ''],
|
||||
expected = lodashStable.map(values, stubString);
|
||||
const values = [, null, undefined, ''];
|
||||
const expected = lodashStable.map(values, stubString);
|
||||
|
||||
const actual = lodashStable.map(values, (value, index) =>
|
||||
index ? func(value) : func(),
|
||||
);
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,3 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { LARGE_ARRAY_SIZE, isEven, _, create, stubFalse, objectProto, funcProto } from './utils';
|
||||
import difference from '../src/difference';
|
||||
@@ -10,18 +9,18 @@ import merge from '../src/merge';
|
||||
|
||||
describe('`__proto__` property bugs', () => {
|
||||
it('should work with the "__proto__" key in internal data objects', () => {
|
||||
const stringLiteral = '__proto__',
|
||||
stringObject = Object(stringLiteral),
|
||||
expected = [stringLiteral, stringObject];
|
||||
const stringLiteral = '__proto__';
|
||||
const stringObject = Object(stringLiteral);
|
||||
const expected = [stringLiteral, stringObject];
|
||||
|
||||
const largeArray = lodashStable.times(LARGE_ARRAY_SIZE, (count) =>
|
||||
isEven(count) ? stringLiteral : stringObject,
|
||||
);
|
||||
|
||||
assert.deepStrictEqual(difference(largeArray, largeArray), []);
|
||||
assert.deepStrictEqual(intersection(largeArray, largeArray), expected);
|
||||
assert.deepStrictEqual(uniq(largeArray), expected);
|
||||
assert.deepStrictEqual(without.apply(_, [largeArray].concat(largeArray)), []);
|
||||
expect(difference(largeArray, largeArray)).toEqual([]);
|
||||
expect(intersection(largeArray, largeArray)).toEqual(expected);
|
||||
expect(uniq(largeArray)).toEqual(expected);
|
||||
expect(without.apply(_, [largeArray].concat(largeArray))).toEqual([]);
|
||||
});
|
||||
|
||||
it('should treat "__proto__" as a regular key in assignments', () => {
|
||||
@@ -37,10 +36,10 @@ describe('`__proto__` property bugs', () => {
|
||||
return result instanceof Array;
|
||||
});
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
|
||||
actual = groupBy([{ a: '__proto__' }], 'a');
|
||||
assert.ok(!(actual instanceof Array));
|
||||
expect(actual instanceof Array).toBe(false);
|
||||
});
|
||||
|
||||
it('should not merge "__proto__" properties', () => {
|
||||
@@ -50,7 +49,7 @@ describe('`__proto__` property bugs', () => {
|
||||
const actual = 'a' in objectProto;
|
||||
delete objectProto.a;
|
||||
|
||||
assert.ok(!actual);
|
||||
expect(actual).toBe(false);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -60,14 +59,14 @@ describe('`__proto__` property bugs', () => {
|
||||
let actual = 'a' in funcProto;
|
||||
delete funcProto.a;
|
||||
|
||||
assert.ok(!actual);
|
||||
expect(actual).toBe(false);
|
||||
|
||||
merge({}, { constructor: { prototype: { a: 1 } } });
|
||||
|
||||
actual = 'a' in objectProto;
|
||||
delete objectProto.a;
|
||||
|
||||
assert.ok(!actual);
|
||||
expect(actual).toBe(false);
|
||||
});
|
||||
|
||||
it('should not indirectly merge `Object` properties', () => {
|
||||
@@ -76,6 +75,6 @@ describe('`__proto__` property bugs', () => {
|
||||
const actual = 'a' in Object;
|
||||
delete Object.a;
|
||||
|
||||
assert.ok(!actual);
|
||||
expect(actual).toBe(false);
|
||||
});
|
||||
});
|
||||
14
test/add.spec.js
Normal file
14
test/add.spec.js
Normal file
@@ -0,0 +1,14 @@
|
||||
import add from '../src/add';
|
||||
|
||||
describe('add', () => {
|
||||
it('should add two numbers', () => {
|
||||
expect(add(6, 4)).toBe(10);
|
||||
expect(add(-6, 4)).toBe(-2);
|
||||
expect(add(-6, -4)).toBe(-10);
|
||||
});
|
||||
|
||||
it('should not coerce arguments to numbers', () => {
|
||||
expect(add('6', '4')).toBe('64');
|
||||
expect(add('x', 'y')).toBe('xy');
|
||||
});
|
||||
});
|
||||
@@ -1,15 +0,0 @@
|
||||
import assert from 'node:assert';
|
||||
import add from '../src/add';
|
||||
|
||||
describe('add', () => {
|
||||
it('should add two numbers', () => {
|
||||
assert.strictEqual(add(6, 4), 10);
|
||||
assert.strictEqual(add(-6, 4), -2);
|
||||
assert.strictEqual(add(-6, -4), -10);
|
||||
});
|
||||
|
||||
it('should not coerce arguments to numbers', () => {
|
||||
assert.strictEqual(add('6', '4'), '64');
|
||||
assert.strictEqual(add('x', 'y'), 'xy');
|
||||
});
|
||||
});
|
||||
41
test/after.spec.js
Normal file
41
test/after.spec.js
Normal file
@@ -0,0 +1,41 @@
|
||||
import lodashStable from 'lodash';
|
||||
import after from '../src/after';
|
||||
|
||||
describe('after', () => {
|
||||
function testAfter(n, times) {
|
||||
let count = 0;
|
||||
lodashStable.times(
|
||||
times,
|
||||
after(n, () => {
|
||||
count++;
|
||||
}),
|
||||
);
|
||||
return count;
|
||||
}
|
||||
|
||||
it('should create a function that invokes `func` after `n` calls', () => {
|
||||
// 'after(n) should invoke `func` after being called `n` times'
|
||||
expect(testAfter(5, 5)).toBe(1);
|
||||
// 'after(n) should not invoke `func` before being called `n` times'
|
||||
expect(testAfter(5, 4)).toBe(0);
|
||||
// 'after(0) should not invoke `func` immediately'
|
||||
expect(testAfter(0, 0)).toBe(0);
|
||||
// 'after(0) should invoke `func` when called once'
|
||||
expect(testAfter(0, 1)).toBe(1);
|
||||
});
|
||||
|
||||
it('should coerce `n` values of `NaN` to `0`', () => {
|
||||
expect(testAfter(NaN, 1)).toBe(1);
|
||||
});
|
||||
|
||||
it('should use `this` binding of function', () => {
|
||||
const afterFn = after(1, function () {
|
||||
return ++this.count;
|
||||
});
|
||||
const object = { after: afterFn, count: 0 };
|
||||
|
||||
object.after();
|
||||
expect(object.after()).toBe(2);
|
||||
expect(object.count).toBe(2);
|
||||
});
|
||||
});
|
||||
@@ -1,46 +0,0 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import after from '../src/after';
|
||||
|
||||
describe('after', () => {
|
||||
function testAfter(n, times) {
|
||||
let count = 0;
|
||||
lodashStable.times(
|
||||
times,
|
||||
after(n, () => {
|
||||
count++;
|
||||
}),
|
||||
);
|
||||
return count;
|
||||
}
|
||||
|
||||
it('should create a function that invokes `func` after `n` calls', () => {
|
||||
assert.strictEqual(
|
||||
testAfter(5, 5),
|
||||
1,
|
||||
'after(n) should invoke `func` after being called `n` times',
|
||||
);
|
||||
assert.strictEqual(
|
||||
testAfter(5, 4),
|
||||
0,
|
||||
'after(n) should not invoke `func` before being called `n` times',
|
||||
);
|
||||
assert.strictEqual(testAfter(0, 0), 0, 'after(0) should not invoke `func` immediately');
|
||||
assert.strictEqual(testAfter(0, 1), 1, 'after(0) should invoke `func` when called once');
|
||||
});
|
||||
|
||||
it('should coerce `n` values of `NaN` to `0`', () => {
|
||||
assert.strictEqual(testAfter(NaN, 1), 1);
|
||||
});
|
||||
|
||||
it('should use `this` binding of function', () => {
|
||||
const afterFn = after(1, function () {
|
||||
return ++this.count;
|
||||
}),
|
||||
object = { after: afterFn, count: 0 };
|
||||
|
||||
object.after();
|
||||
assert.strictEqual(object.after(), 2);
|
||||
assert.strictEqual(object.count, 2);
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,3 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { slice, _ } from './utils';
|
||||
import ary from '../src/ary';
|
||||
@@ -12,15 +11,15 @@ describe('ary', () => {
|
||||
|
||||
it('should cap the number of arguments provided to `func`', () => {
|
||||
const actual = lodashStable.map(['6', '8', '10'], ary(parseInt, 1));
|
||||
assert.deepStrictEqual(actual, [6, 8, 10]);
|
||||
expect(actual, [6, 8).toEqual(10]);
|
||||
|
||||
const capped = ary(fn, 2);
|
||||
assert.deepStrictEqual(capped('a', 'b', 'c', 'd'), ['a', 'b']);
|
||||
expect(capped('a', 'b', 'c', 'd'), ['a').toEqual('b']);
|
||||
});
|
||||
|
||||
it('should use `func.length` if `n` is not given', () => {
|
||||
const capped = ary(fn);
|
||||
assert.deepStrictEqual(capped('a', 'b', 'c', 'd'), ['a', 'b', 'c']);
|
||||
expect(capped('a', 'b', 'c', 'd'), ['a', 'b').toEqual('c']);
|
||||
});
|
||||
|
||||
it('should treat a negative `n` as `0`', () => {
|
||||
@@ -30,60 +29,60 @@ describe('ary', () => {
|
||||
var actual = capped('a');
|
||||
} catch (e) {}
|
||||
|
||||
assert.deepStrictEqual(actual, []);
|
||||
expect(actual).toEqual([]);
|
||||
});
|
||||
|
||||
it('should coerce `n` to an integer', () => {
|
||||
const values = ['1', 1.6, 'xyz'],
|
||||
expected = [['a'], ['a'], []];
|
||||
const values = ['1', 1.6, 'xyz'];
|
||||
const expected = [['a'], ['a'], []];
|
||||
|
||||
const actual = lodashStable.map(values, (n) => {
|
||||
const capped = ary(fn, n);
|
||||
return capped('a', 'b');
|
||||
});
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should not force a minimum argument count', () => {
|
||||
const args = ['a', 'b', 'c'],
|
||||
capped = ary(fn, 3);
|
||||
const args = ['a', 'b', 'c'];
|
||||
const capped = ary(fn, 3);
|
||||
|
||||
const expected = lodashStable.map(args, (arg, index) => args.slice(0, index));
|
||||
|
||||
const actual = lodashStable.map(expected, (array) => capped.apply(undefined, array));
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should use `this` binding of function', () => {
|
||||
const capped = ary(function (a, b) {
|
||||
return this;
|
||||
}, 1),
|
||||
object = { capped: capped };
|
||||
return this;
|
||||
}, 1);
|
||||
const object = { capped: capped };
|
||||
|
||||
assert.strictEqual(object.capped(), object);
|
||||
expect(object.capped()).toBe(object);
|
||||
});
|
||||
|
||||
it('should use the existing `ary` if smaller', () => {
|
||||
const capped = ary(ary(fn, 1), 2);
|
||||
assert.deepStrictEqual(capped('a', 'b', 'c'), ['a']);
|
||||
expect(capped('a', 'b', 'c')).toEqual(['a']);
|
||||
});
|
||||
|
||||
it('should work as an iteratee for methods like `_.map`', () => {
|
||||
const funcs = lodashStable.map([fn], ary),
|
||||
actual = funcs[0]('a', 'b', 'c');
|
||||
const funcs = lodashStable.map([fn], ary);
|
||||
const actual = funcs[0]('a', 'b', 'c');
|
||||
|
||||
assert.deepStrictEqual(actual, ['a', 'b', 'c']);
|
||||
expect(actual, ['a', 'b').toEqual('c']);
|
||||
});
|
||||
|
||||
it('should work when combined with other methods that use metadata', () => {
|
||||
let array = ['a', 'b', 'c'],
|
||||
includes = curry(rearg(ary(_.includes, 2), 1, 0), 2);
|
||||
const array = ['a', 'b', 'c'];
|
||||
let includes = curry(rearg(ary(_.includes, 2), 1, 0), 2);
|
||||
|
||||
assert.strictEqual(includes('b')(array, 2), true);
|
||||
expect(includes('b')(array, 2)).toBe(true);
|
||||
|
||||
includes = _(_.includes).ary(2).rearg(1, 0).curry(2).value();
|
||||
assert.strictEqual(includes('b')(array, 2), true);
|
||||
expect(includes('b')(array, 2)).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,3 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { _, defineProperty, stubOne, noop, stubNaN } from './utils';
|
||||
|
||||
@@ -7,23 +6,23 @@ describe('assign and assignIn', () => {
|
||||
const func = _[methodName];
|
||||
|
||||
it(`\`_.${methodName}\` should assign source properties to \`object\``, () => {
|
||||
assert.deepStrictEqual(func({ a: 1 }, { b: 2 }), { a: 1, b: 2 });
|
||||
expect(func({ a: 1 }, { b: 2 }), { a: 1).toEqual(b: 2 });
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should accept multiple sources`, () => {
|
||||
const expected = { a: 1, b: 2, c: 3 };
|
||||
assert.deepStrictEqual(func({ a: 1 }, { b: 2 }, { c: 3 }), expected);
|
||||
assert.deepStrictEqual(func({ a: 1 }, { b: 2, c: 2 }, { c: 3 }), expected);
|
||||
expect(func({ a: 1 }, { b: 2 }, { c: 3 })).toEqual(expected);
|
||||
expect(func({ a: 1 }, { b: 2, c: 2 }, { c: 3 })).toEqual(expected);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should overwrite destination properties`, () => {
|
||||
const expected = { a: 3, b: 2, c: 1 };
|
||||
assert.deepStrictEqual(func({ a: 1, b: 2 }, expected), expected);
|
||||
expect(func({ a: 1, b: 2 }, expected)).toEqual(expected);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should assign source properties with nullish values`, () => {
|
||||
const expected = { a: null, b: undefined, c: null };
|
||||
assert.deepStrictEqual(func({ a: 1, b: 2 }, expected), expected);
|
||||
expect(func({ a: 1, b: 2 }, expected)).toEqual(expected);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should skip assignments if values are the same`, () => {
|
||||
@@ -82,25 +81,25 @@ describe('assign and assignIn', () => {
|
||||
var actual = func(object, source);
|
||||
} catch (e) {}
|
||||
|
||||
assert.deepStrictEqual(actual, source);
|
||||
expect(actual).toEqual(source);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should treat sparse array sources as dense`, () => {
|
||||
const array = [1];
|
||||
array[2] = 3;
|
||||
|
||||
assert.deepStrictEqual(func({}, array), { '0': 1, '1': undefined, '2': 3 });
|
||||
expect(func({}, array), { 0: 1, 1: undefined).toEqual(2: 3 });
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should assign values of prototype objects`, () => {
|
||||
function Foo() {}
|
||||
Foo.prototype.a = 1;
|
||||
|
||||
assert.deepStrictEqual(func({}, Foo.prototype), { a: 1 });
|
||||
expect(func({}, Foo.prototype)).toEqual({ a: 1 });
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should coerce string sources to objects`, () => {
|
||||
assert.deepStrictEqual(func({}, 'a'), { '0': 'a' });
|
||||
expect(func({}, 'a')).toEqual({ 0: 'a' });
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,9 +1,8 @@
|
||||
import assert from 'node:assert';
|
||||
import extend from '../src/extend';
|
||||
import assignIn from '../src/assignIn';
|
||||
|
||||
describe('assignIn', () => {
|
||||
it('should be aliased', () => {
|
||||
assert.strictEqual(extend, assignIn);
|
||||
expect(extend).toBe(assignIn);
|
||||
});
|
||||
});
|
||||
@@ -1,9 +1,8 @@
|
||||
import assert from 'node:assert';
|
||||
import extendWith from '../src/extendWith';
|
||||
import assignInWith from '../src/assignInWith';
|
||||
|
||||
describe('assignInWith', () => {
|
||||
it('should be aliased', () => {
|
||||
assert.strictEqual(extendWith, assignInWith);
|
||||
expect(extendWith).toBe(assignInWith);
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,3 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { _, noop } from './utils';
|
||||
|
||||
@@ -11,12 +10,12 @@ describe('assignWith and assignInWith', () => {
|
||||
a === undefined ? b : a,
|
||||
);
|
||||
|
||||
assert.deepStrictEqual(actual, { a: 1, b: 2, c: 3 });
|
||||
expect(actual, { a: 1, b: 2).toEqual(c: 3 });
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should work with a \`customizer\` that returns \`undefined\``, () => {
|
||||
const expected = { a: 1 };
|
||||
assert.deepStrictEqual(func({}, expected, noop), expected);
|
||||
expect(func({}, expected, noop)).toEqual(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,20 +1,19 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { empties, stubOne, falsey, args, LARGE_ARRAY_SIZE, square, identity } from './utils';
|
||||
import at from '../src/at';
|
||||
|
||||
describe('at', () => {
|
||||
const array = ['a', 'b', 'c'],
|
||||
object = { a: [{ b: { c: 3 } }, 4] };
|
||||
const array = ['a', 'b', 'c'];
|
||||
const object = { a: [{ b: { c: 3 } }, 4] };
|
||||
|
||||
it('should return the elements corresponding to the specified keys', () => {
|
||||
const actual = at(array, [0, 2]);
|
||||
assert.deepStrictEqual(actual, ['a', 'c']);
|
||||
expect(actual, ['a').toEqual('c']);
|
||||
});
|
||||
|
||||
it('should return `undefined` for nonexistent keys', () => {
|
||||
const actual = at(array, [2, 4, 0]);
|
||||
assert.deepStrictEqual(actual, ['c', undefined, 'a']);
|
||||
expect(actual, ['c', undefined).toEqual('a']);
|
||||
});
|
||||
|
||||
it('should work with non-index keys on array values', () => {
|
||||
@@ -30,20 +29,20 @@ describe('at', () => {
|
||||
[],
|
||||
);
|
||||
|
||||
const expected = lodashStable.map(values, stubOne),
|
||||
actual = at(array, values);
|
||||
const expected = lodashStable.map(values, stubOne);
|
||||
const actual = at(array, values);
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should return an empty array when no keys are given', () => {
|
||||
assert.deepStrictEqual(at(array), []);
|
||||
assert.deepStrictEqual(at(array, [], []), []);
|
||||
expect(at(array)).toEqual([]);
|
||||
expect(at(array, [], [])).toEqual([]);
|
||||
});
|
||||
|
||||
it('should accept multiple key arguments', () => {
|
||||
const actual = at(['a', 'b', 'c', 'd'], 3, 0, 2);
|
||||
assert.deepStrictEqual(actual, ['d', 'a', 'c']);
|
||||
expect(actual, ['d', 'a').toEqual('c']);
|
||||
});
|
||||
|
||||
it('should work with a falsey `object` when keys are given', () => {
|
||||
@@ -55,22 +54,22 @@ describe('at', () => {
|
||||
} catch (e) {}
|
||||
});
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should work with an `arguments` object for `object`', () => {
|
||||
const actual = at(args, [2, 0]);
|
||||
assert.deepStrictEqual(actual, [3, 1]);
|
||||
expect(actual, [3).toEqual(1]);
|
||||
});
|
||||
|
||||
it('should work with `arguments` object as secondary arguments', () => {
|
||||
const actual = at([1, 2, 3, 4, 5], args);
|
||||
assert.deepStrictEqual(actual, [2, 3, 4]);
|
||||
expect(actual, [2, 3).toEqual(4]);
|
||||
});
|
||||
|
||||
it('should work with an object for `object`', () => {
|
||||
const actual = at(object, ['a[0].b.c', 'a[1]']);
|
||||
assert.deepStrictEqual(actual, [3, 4]);
|
||||
expect(actual, [3).toEqual(4]);
|
||||
});
|
||||
|
||||
it('should pluck inherited property values', () => {
|
||||
@@ -80,52 +79,52 @@ describe('at', () => {
|
||||
Foo.prototype.b = 2;
|
||||
|
||||
const actual = at(new Foo(), 'b');
|
||||
assert.deepStrictEqual(actual, [2]);
|
||||
expect(actual).toEqual([2]);
|
||||
});
|
||||
|
||||
it('should work in a lazy sequence', () => {
|
||||
const largeArray = lodashStable.range(LARGE_ARRAY_SIZE),
|
||||
smallArray = array;
|
||||
const largeArray = lodashStable.range(LARGE_ARRAY_SIZE);
|
||||
const smallArray = array;
|
||||
|
||||
lodashStable.each([[2], ['2'], [2, 1]], (paths) => {
|
||||
lodashStable.times(2, (index) => {
|
||||
const array = index ? largeArray : smallArray,
|
||||
wrapped = _(array).map(identity).at(paths);
|
||||
const array = index ? largeArray : smallArray;
|
||||
const wrapped = _(array).map(identity).at(paths);
|
||||
|
||||
assert.deepEqual(wrapped.value(), at(_.map(array, identity), paths));
|
||||
expect(wrapped.value(), at(_.map(array, identity)).toEqual(paths));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should support shortcut fusion', () => {
|
||||
let array = lodashStable.range(LARGE_ARRAY_SIZE),
|
||||
count = 0,
|
||||
iteratee = function (value) {
|
||||
count++;
|
||||
return square(value);
|
||||
},
|
||||
lastIndex = LARGE_ARRAY_SIZE - 1;
|
||||
const array = lodashStable.range(LARGE_ARRAY_SIZE);
|
||||
let count = 0;
|
||||
const iteratee = function (value) {
|
||||
count++;
|
||||
return square(value);
|
||||
};
|
||||
const lastIndex = LARGE_ARRAY_SIZE - 1;
|
||||
|
||||
lodashStable.each([lastIndex, `${lastIndex}`, LARGE_ARRAY_SIZE, []], (n, index) => {
|
||||
count = 0;
|
||||
let actual = _(array).map(iteratee).at(n).value(),
|
||||
expected = index < 2 ? 1 : 0;
|
||||
const actual = _(array).map(iteratee).at(n).value();
|
||||
let expected = index < 2 ? 1 : 0;
|
||||
|
||||
assert.strictEqual(count, expected);
|
||||
expect(count).toBe(expected);
|
||||
|
||||
expected = index === 3 ? [] : [index === 2 ? undefined : square(lastIndex)];
|
||||
assert.deepEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
});
|
||||
|
||||
it('work with an object for `object` when chaining', () => {
|
||||
let paths = ['a[0].b.c', 'a[1]'],
|
||||
actual = _(object).map(identity).at(paths).value();
|
||||
const paths = ['a[0].b.c', 'a[1]'];
|
||||
let actual = _(object).map(identity).at(paths).value();
|
||||
|
||||
assert.deepEqual(actual, at(_.map(object, identity), paths));
|
||||
expect(actual, at(_.map(object, identity)).toEqual(paths));
|
||||
|
||||
const indexObject = { '0': 1 };
|
||||
const indexObject = { 0: 1 };
|
||||
actual = _(indexObject).at(0).value();
|
||||
assert.deepEqual(actual, at(indexObject, 0));
|
||||
expect(actual, at(indexObject).toEqual(0));
|
||||
});
|
||||
});
|
||||
@@ -1,11 +1,10 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { slice, errors, stubTrue, CustomError, realm } from './utils';
|
||||
import attempt from '../src/attempt';
|
||||
|
||||
describe('attempt', () => {
|
||||
it('should return the result of `func`', () => {
|
||||
assert.strictEqual(attempt(lodashStable.constant('x')), 'x');
|
||||
expect(attempt(lodashStable.constant('x'))).toBe('x');
|
||||
});
|
||||
|
||||
it('should provide additional arguments to `func`', () => {
|
||||
@@ -16,7 +15,7 @@ describe('attempt', () => {
|
||||
1,
|
||||
2,
|
||||
);
|
||||
assert.deepStrictEqual(actual, [1, 2]);
|
||||
expect(actual, [1).toEqual(2]);
|
||||
});
|
||||
|
||||
it('should return the caught error', () => {
|
||||
@@ -30,21 +29,21 @@ describe('attempt', () => {
|
||||
}) === error,
|
||||
);
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should coerce errors to error objects', () => {
|
||||
const actual = attempt(() => {
|
||||
throw 'x';
|
||||
});
|
||||
assert.ok(lodashStable.isEqual(actual, Error('x')));
|
||||
expect(lodashStable.isEqual(actual, Error('x')))
|
||||
});
|
||||
|
||||
it('should preserve custom errors', () => {
|
||||
const actual = attempt(() => {
|
||||
throw new CustomError('x');
|
||||
});
|
||||
assert.ok(actual instanceof CustomError);
|
||||
expect(actual instanceof CustomError)
|
||||
});
|
||||
|
||||
it('should work with an error object from another realm', () => {
|
||||
@@ -59,15 +58,15 @@ describe('attempt', () => {
|
||||
}) === error,
|
||||
);
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
}
|
||||
});
|
||||
|
||||
it('should return an unwrapped value when implicitly chaining', () => {
|
||||
assert.strictEqual(_(lodashStable.constant('x')).attempt(), 'x');
|
||||
expect(_(lodashStable.constant('x')).attempt()).toBe('x');
|
||||
});
|
||||
|
||||
it('should return a wrapped value when explicitly chaining', () => {
|
||||
assert.ok(_(lodashStable.constant('x')).chain().attempt() instanceof _);
|
||||
expect(_(lodashStable.constant('x')).chain().attempt() instanceof _)
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,3 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
|
||||
import {
|
||||
@@ -19,26 +18,26 @@ import VERSION from '../src/VERSION';
|
||||
describe(basename, () => {
|
||||
it(`should support loading ${basename} as the "lodash" module`, () => {
|
||||
if (amd) {
|
||||
assert.strictEqual((lodashModule || {}).moduleName, 'lodash');
|
||||
expect((lodashModule || {}).moduleName).toBe('lodash');
|
||||
}
|
||||
});
|
||||
|
||||
it(`should support loading ${basename} with the Require.js "shim" configuration option`, () => {
|
||||
if (amd && lodashStable.includes(ui.loaderPath, 'requirejs')) {
|
||||
assert.strictEqual((shimmedModule || {}).moduleName, 'shimmed');
|
||||
expect((shimmedModule || {}).moduleName).toBe('shimmed');
|
||||
}
|
||||
});
|
||||
|
||||
it(`should support loading ${basename} as the "underscore" module`, () => {
|
||||
if (amd) {
|
||||
assert.strictEqual((underscoreModule || {}).moduleName, 'underscore');
|
||||
expect((underscoreModule || {}).moduleName).toBe('underscore');
|
||||
}
|
||||
});
|
||||
|
||||
it(`should support loading ${basename} in a web worker`, (done) => {
|
||||
if (Worker) {
|
||||
const limit = 30000 / QUnit.config.asyncRetries,
|
||||
start = +new Date();
|
||||
const limit = 30000 / QUnit.config.asyncRetries;
|
||||
const start = +new Date();
|
||||
|
||||
const attempt = function () {
|
||||
const actual = _VERSION;
|
||||
@@ -46,7 +45,7 @@ describe(basename, () => {
|
||||
setTimeout(attempt, 16);
|
||||
return;
|
||||
}
|
||||
assert.strictEqual(actual, VERSION);
|
||||
expect(actual).toBe(VERSION);
|
||||
done();
|
||||
};
|
||||
|
||||
@@ -58,7 +57,7 @@ describe(basename, () => {
|
||||
|
||||
it('should not add `Function.prototype` extensions to lodash', () => {
|
||||
if (lodashBizarro) {
|
||||
assert.ok(!('_method' in lodashBizarro));
|
||||
expect('_method' in lodashBizarro).toBe(false);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -72,9 +71,9 @@ describe(basename, () => {
|
||||
}
|
||||
Foo.prototype.b = 2;
|
||||
|
||||
const object = { a: 1 },
|
||||
otherObject = { b: 2 },
|
||||
largeArray = lodashStable.times(LARGE_ARRAY_SIZE, lodashStable.constant(object));
|
||||
const object = { a: 1 };
|
||||
const otherObject = { b: 2 };
|
||||
const largeArray = lodashStable.times(LARGE_ARRAY_SIZE, lodashStable.constant(object));
|
||||
|
||||
if (lodashBizarro) {
|
||||
try {
|
||||
@@ -83,7 +82,7 @@ describe(basename, () => {
|
||||
actual = null;
|
||||
}
|
||||
let label = message('_.create', 'Object.create');
|
||||
assert.ok(actual instanceof Foo, label);
|
||||
expect(actual instanceof Foo, label);
|
||||
|
||||
try {
|
||||
actual = [
|
||||
@@ -95,7 +94,7 @@ describe(basename, () => {
|
||||
actual = null;
|
||||
}
|
||||
label = message('_.difference`, `_.intersection`, and `_.uniq', 'Map');
|
||||
assert.deepStrictEqual(actual, [[otherObject], [object], [object]], label);
|
||||
expect(actual, [[otherObject], [object], [object]]).toEqual(label);
|
||||
|
||||
try {
|
||||
if (Symbol) {
|
||||
@@ -106,7 +105,7 @@ describe(basename, () => {
|
||||
actual = null;
|
||||
}
|
||||
label = message('_.clone` and `_.cloneDeep', 'Object.getOwnPropertySymbols');
|
||||
assert.deepStrictEqual(actual, [object, object], label);
|
||||
expect(actual, [object, object]).toEqual(label);
|
||||
|
||||
try {
|
||||
// Avoid buggy symbol detection in Babel's `_typeof` helper.
|
||||
@@ -120,7 +119,7 @@ describe(basename, () => {
|
||||
actual = null;
|
||||
}
|
||||
label = message('_.clone`, `_.isEqual`, and `_.toString', 'Symbol');
|
||||
assert.deepStrictEqual(actual, [{}, false, ''], label);
|
||||
expect(actual, [{}, false, '']).toEqual(label);
|
||||
|
||||
try {
|
||||
var map = new lodashBizarro.memoize.Cache();
|
||||
@@ -129,7 +128,7 @@ describe(basename, () => {
|
||||
actual = null;
|
||||
}
|
||||
label = message('_.memoize.Cache', 'Map');
|
||||
assert.deepStrictEqual(actual, 1, label);
|
||||
expect(actual, 1).toEqual(label);
|
||||
|
||||
try {
|
||||
map = new (Map || Object)();
|
||||
@@ -141,7 +140,7 @@ describe(basename, () => {
|
||||
actual = null;
|
||||
}
|
||||
label = message('_.toArray', 'Map');
|
||||
assert.deepStrictEqual(actual, [], label);
|
||||
expect(actual, []).toEqual(label);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,3 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { _ } from './utils';
|
||||
|
||||
@@ -25,22 +24,22 @@ describe('before', () => {
|
||||
4,
|
||||
'before(n) should not invoke `func` after being called `n - 1` times',
|
||||
);
|
||||
assert.strictEqual(before(0, 0), 0, 'before(0) should not invoke `func` immediately');
|
||||
assert.strictEqual(before(0, 1), 0, 'before(0) should not invoke `func` when called');
|
||||
expect(before(0, 0), 0).toBe('before(0) should not invoke `func` immediately');
|
||||
expect(before(0, 1), 0).toBe('before(0) should not invoke `func` when called');
|
||||
});
|
||||
|
||||
it('should coerce `n` values of `NaN` to `0`', () => {
|
||||
assert.strictEqual(before(NaN, 1), 0);
|
||||
expect(before(NaN, 1)).toBe(0);
|
||||
});
|
||||
|
||||
it('should use `this` binding of function', () => {
|
||||
const before = _.before(2, function () {
|
||||
return ++this.count;
|
||||
}),
|
||||
object = { before: before, count: 0 };
|
||||
return ++this.count;
|
||||
});
|
||||
const object = { before: before, count: 0 };
|
||||
|
||||
object.before();
|
||||
assert.strictEqual(object.before(), 1);
|
||||
assert.strictEqual(object.count, 1);
|
||||
expect(object.before()).toBe(1);
|
||||
expect(object.count).toBe(1);
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,3 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { push, falsey, stubTrue } from './utils';
|
||||
import bind from '../src/bind';
|
||||
@@ -15,7 +14,7 @@ describe('bind', () => {
|
||||
const object = {},
|
||||
bound = bind(fn, object);
|
||||
|
||||
assert.deepStrictEqual(bound('a'), [object, 'a']);
|
||||
expect(bound('a'), [object).toEqual('a']);
|
||||
});
|
||||
|
||||
it('should accept a falsey `thisArg`', () => {
|
||||
@@ -40,15 +39,15 @@ describe('bind', () => {
|
||||
let bound = bind(fn, null),
|
||||
actual = bound('a');
|
||||
|
||||
assert.ok(actual[0] === null || (actual[0] && actual[0].Array));
|
||||
assert.strictEqual(actual[1], 'a');
|
||||
expect(actual[0] === null || (actual[0] && actual[0].Array))
|
||||
expect(actual[1]).toBe('a');
|
||||
|
||||
lodashStable.times(2, (index) => {
|
||||
bound = index ? bind(fn, undefined) : bind(fn);
|
||||
actual = bound('b');
|
||||
|
||||
assert.ok(actual[0] === undefined || (actual[0] && actual[0].Array));
|
||||
assert.strictEqual(actual[1], 'b');
|
||||
expect(actual[0] === undefined || (actual[0] && actual[0].Array))
|
||||
expect(actual[1]).toBe('b');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -56,14 +55,14 @@ describe('bind', () => {
|
||||
let object = {},
|
||||
bound = bind(fn, object, 'a');
|
||||
|
||||
assert.deepStrictEqual(bound(), [object, 'a']);
|
||||
expect(bound(), [object).toEqual('a']);
|
||||
|
||||
bound = bind(fn, object, 'a');
|
||||
assert.deepStrictEqual(bound('b'), [object, 'a', 'b']);
|
||||
expect(bound('b'), [object, 'a').toEqual('b']);
|
||||
|
||||
bound = bind(fn, object, 'a', 'b');
|
||||
assert.deepStrictEqual(bound(), [object, 'a', 'b']);
|
||||
assert.deepStrictEqual(bound('c', 'd'), [object, 'a', 'b', 'c', 'd']);
|
||||
expect(bound(), [object, 'a').toEqual('b']);
|
||||
expect(bound('c', 'd'), [object, 'a', 'b', 'c').toEqual('d']);
|
||||
});
|
||||
|
||||
it('should support placeholders', () => {
|
||||
@@ -71,10 +70,10 @@ describe('bind', () => {
|
||||
ph = bind.placeholder,
|
||||
bound = bind(fn, object, ph, 'b', ph);
|
||||
|
||||
assert.deepStrictEqual(bound('a', 'c'), [object, 'a', 'b', 'c']);
|
||||
assert.deepStrictEqual(bound('a'), [object, 'a', 'b', undefined]);
|
||||
assert.deepStrictEqual(bound('a', 'c', 'd'), [object, 'a', 'b', 'c', 'd']);
|
||||
assert.deepStrictEqual(bound(), [object, undefined, 'b', undefined]);
|
||||
expect(bound('a', 'c'), [object, 'a', 'b').toEqual('c']);
|
||||
expect(bound('a'), [object, 'a', 'b').toEqual(undefined]);
|
||||
expect(bound('a', 'c', 'd'), [object, 'a', 'b', 'c').toEqual('d']);
|
||||
expect(bound(), [object, undefined, 'b').toEqual(undefined]);
|
||||
});
|
||||
|
||||
it('should use `_.placeholder` when set', () => {
|
||||
@@ -83,7 +82,7 @@ describe('bind', () => {
|
||||
object = {},
|
||||
bound = bind(fn, object, _ph, 'b', ph);
|
||||
|
||||
assert.deepEqual(bound('a', 'c'), [object, 'a', 'b', ph, 'c']);
|
||||
expect(bound('a', 'c'), [object, 'a', 'b', ph).toEqual('c']);
|
||||
delete placeholder;
|
||||
});
|
||||
|
||||
@@ -91,10 +90,10 @@ describe('bind', () => {
|
||||
let fn = function (a, b, c) {},
|
||||
bound = bind(fn, {});
|
||||
|
||||
assert.strictEqual(bound.length, 0);
|
||||
expect(bound.length).toBe(0);
|
||||
|
||||
bound = bind(fn, {}, 1);
|
||||
assert.strictEqual(bound.length, 0);
|
||||
expect(bound.length).toBe(0);
|
||||
});
|
||||
|
||||
it('should ignore binding when called with the `new` operator', () => {
|
||||
@@ -105,9 +104,9 @@ describe('bind', () => {
|
||||
const bound = bind(Foo, { a: 1 }),
|
||||
newBound = new bound();
|
||||
|
||||
assert.strictEqual(bound().a, 1);
|
||||
assert.strictEqual(newBound.a, undefined);
|
||||
assert.ok(newBound instanceof Foo);
|
||||
expect(bound().a).toBe(1);
|
||||
expect(newBound.a).toBe(undefined);
|
||||
expect(newBound instanceof Foo)
|
||||
});
|
||||
|
||||
it('should handle a number of arguments when called with the `new` operator', () => {
|
||||
@@ -154,7 +153,7 @@ describe('bind', () => {
|
||||
} catch (e) {}
|
||||
});
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should ensure `new bound` is an instance of `func`', () => {
|
||||
@@ -165,15 +164,15 @@ describe('bind', () => {
|
||||
var bound = bind(Foo),
|
||||
object = {};
|
||||
|
||||
assert.ok(new bound() instanceof Foo);
|
||||
assert.strictEqual(new bound(true), object);
|
||||
expect(new bound() instanceof Foo)
|
||||
expect(new bound(true)).toBe(object);
|
||||
});
|
||||
|
||||
it('should append array arguments to partially applied arguments', () => {
|
||||
const object = {},
|
||||
bound = bind(fn, object, 'a');
|
||||
|
||||
assert.deepStrictEqual(bound(['b'], 'c'), [object, 'a', ['b'], 'c']);
|
||||
expect(bound(['b'], 'c'), [object, 'a', ['b']).toEqual('c']);
|
||||
});
|
||||
|
||||
it('should not rebind functions', () => {
|
||||
@@ -185,9 +184,9 @@ describe('bind', () => {
|
||||
bound2 = bind(bound1, object2, 'a'),
|
||||
bound3 = bind(bound1, object3, 'b');
|
||||
|
||||
assert.deepStrictEqual(bound1(), [object1]);
|
||||
assert.deepStrictEqual(bound2(), [object1, 'a']);
|
||||
assert.deepStrictEqual(bound3(), [object1, 'b']);
|
||||
expect(bound1()).toEqual([object1]);
|
||||
expect(bound2(), [object1).toEqual('a']);
|
||||
expect(bound3(), [object1).toEqual('b']);
|
||||
});
|
||||
|
||||
it('should not error when instantiating bound built-ins', () => {
|
||||
@@ -198,7 +197,7 @@ describe('bind', () => {
|
||||
var actual = new Ctor(2012, 4, 23, 0, 0, 0, 0);
|
||||
} catch (e) {}
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
|
||||
Ctor = bind(Date, null, 2012, 4, 23);
|
||||
|
||||
@@ -206,7 +205,7 @@ describe('bind', () => {
|
||||
actual = new Ctor(0, 0, 0, 0);
|
||||
} catch (e) {}
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should not error when calling bound class constructors with the `new` operator', () => {
|
||||
@@ -240,7 +239,7 @@ describe('bind', () => {
|
||||
} catch (e) {}
|
||||
});
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -248,9 +247,9 @@ describe('bind', () => {
|
||||
const object = {},
|
||||
bound = _(fn).bind({}, 'a', 'b');
|
||||
|
||||
assert.ok(bound instanceof _);
|
||||
expect(bound instanceof _)
|
||||
|
||||
const actual = bound.value()('c');
|
||||
assert.deepEqual(actual, [object, 'a', 'b', 'c']);
|
||||
expect(actual, [object, 'a', 'b').toEqual('c']);
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,3 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { args, toArgs, arrayProto } from './utils';
|
||||
import bindAll from '../src/bindAll';
|
||||
@@ -16,7 +15,7 @@ describe('bindAll', () => {
|
||||
'-0': function () {
|
||||
return this._n0;
|
||||
},
|
||||
'0': function () {
|
||||
0: function () {
|
||||
return this._p0;
|
||||
},
|
||||
a: function () {
|
||||
@@ -39,7 +38,7 @@ describe('bindAll', () => {
|
||||
|
||||
const actual = lodashStable.map(['a', 'b', 'c'], (key) => object[key].call({}));
|
||||
|
||||
assert.deepStrictEqual(actual, [1, 2, undefined]);
|
||||
expect(actual, [1, 2).toEqual(undefined]);
|
||||
});
|
||||
|
||||
it('should accept arrays of method names', () => {
|
||||
@@ -48,7 +47,7 @@ describe('bindAll', () => {
|
||||
|
||||
const actual = lodashStable.map(['a', 'b', 'c', 'd'], (key) => object[key].call({}));
|
||||
|
||||
assert.deepStrictEqual(actual, [1, 2, 3, undefined]);
|
||||
expect(actual, [1, 2, 3).toEqual(undefined]);
|
||||
});
|
||||
|
||||
it('should preserve the sign of `0`', () => {
|
||||
@@ -60,13 +59,13 @@ describe('bindAll', () => {
|
||||
return object[lodashStable.toString(key)].call({});
|
||||
});
|
||||
|
||||
assert.deepStrictEqual(actual, [-2, -2, -1, -1]);
|
||||
expect(actual, [-2, -2, -1).toEqual(-1]);
|
||||
});
|
||||
|
||||
it('should work with an array `object`', () => {
|
||||
const array = ['push', 'pop'];
|
||||
bindAll(array);
|
||||
assert.strictEqual(array.pop, arrayProto.pop);
|
||||
expect(array.pop).toBe(arrayProto.pop);
|
||||
});
|
||||
|
||||
it('should work with `arguments` objects as secondary arguments', () => {
|
||||
@@ -75,6 +74,6 @@ describe('bindAll', () => {
|
||||
|
||||
const actual = lodashStable.map(args, (key) => object[key].call({}));
|
||||
|
||||
assert.deepStrictEqual(actual, [1]);
|
||||
expect(actual).toEqual([1]);
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,3 @@
|
||||
import assert from 'node:assert';
|
||||
import { slice } from './utils';
|
||||
import bindKey from '../src/bindKey';
|
||||
|
||||
@@ -12,13 +11,13 @@ describe('bindKey', () => {
|
||||
};
|
||||
|
||||
const bound = bindKey(object, 'greet', 'hi');
|
||||
assert.strictEqual(bound(), 'fred says: hi');
|
||||
expect(bound()).toBe('fred says: hi');
|
||||
|
||||
object.greet = function (greeting) {
|
||||
return `${this.user} says: ${greeting}!`;
|
||||
};
|
||||
|
||||
assert.strictEqual(bound(), 'fred says: hi!');
|
||||
expect(bound()).toBe('fred says: hi!');
|
||||
});
|
||||
|
||||
it('should support placeholders', () => {
|
||||
@@ -28,13 +27,13 @@ describe('bindKey', () => {
|
||||
},
|
||||
};
|
||||
|
||||
const ph = bindKey.placeholder,
|
||||
bound = bindKey(object, 'fn', ph, 'b', ph);
|
||||
const ph = bindKey.placeholder;
|
||||
const bound = bindKey(object, 'fn', ph, 'b', ph);
|
||||
|
||||
assert.deepStrictEqual(bound('a', 'c'), ['a', 'b', 'c']);
|
||||
assert.deepStrictEqual(bound('a'), ['a', 'b', undefined]);
|
||||
assert.deepStrictEqual(bound('a', 'c', 'd'), ['a', 'b', 'c', 'd']);
|
||||
assert.deepStrictEqual(bound(), [undefined, 'b', undefined]);
|
||||
expect(bound('a', 'c'), ['a', 'b').toEqual('c']);
|
||||
expect(bound('a'), ['a', 'b').toEqual(undefined]);
|
||||
expect(bound('a', 'c', 'd'), ['a', 'b', 'c').toEqual('d']);
|
||||
expect(bound(), [undefined, 'b').toEqual(undefined]);
|
||||
});
|
||||
|
||||
it('should use `_.placeholder` when set', () => {
|
||||
@@ -44,11 +43,11 @@ describe('bindKey', () => {
|
||||
},
|
||||
};
|
||||
|
||||
const _ph = (_.placeholder = {}),
|
||||
ph = bindKey.placeholder,
|
||||
bound = bindKey(object, 'fn', _ph, 'b', ph);
|
||||
const _ph = (_.placeholder = {});
|
||||
const ph = bindKey.placeholder;
|
||||
const bound = bindKey(object, 'fn', _ph, 'b', ph);
|
||||
|
||||
assert.deepEqual(bound('a', 'c'), ['a', 'b', ph, 'c']);
|
||||
expect(bound('a', 'c'), ['a', 'b', ph).toEqual('c']);
|
||||
delete _.placeholder;
|
||||
});
|
||||
|
||||
@@ -57,10 +56,10 @@ describe('bindKey', () => {
|
||||
return value && object;
|
||||
}
|
||||
|
||||
var object = { Foo: Foo },
|
||||
bound = bindKey(object, 'Foo');
|
||||
var object = { Foo: Foo };
|
||||
const bound = bindKey(object, 'Foo');
|
||||
|
||||
assert.ok(new bound() instanceof Foo);
|
||||
assert.strictEqual(new bound(true), object);
|
||||
expect(new bound() instanceof Foo)
|
||||
expect(new bound(true)).toBe(object);
|
||||
});
|
||||
});
|
||||
27
test/camelCase.spec.js
Normal file
27
test/camelCase.spec.js
Normal file
@@ -0,0 +1,27 @@
|
||||
import lodashStable from 'lodash';
|
||||
import camelCase from '../src/camelCase';
|
||||
|
||||
describe('camelCase', () => {
|
||||
it('should work with numbers', () => {
|
||||
expect(camelCase('12 feet')).toBe('12Feet');
|
||||
expect(camelCase('enable 6h format')).toBe('enable6HFormat');
|
||||
expect(camelCase('enable 24H format')).toBe('enable24HFormat');
|
||||
expect(camelCase('too legit 2 quit')).toBe('tooLegit2Quit');
|
||||
expect(camelCase('walk 500 miles')).toBe('walk500Miles');
|
||||
expect(camelCase('xhr2 request')).toBe('xhr2Request');
|
||||
});
|
||||
|
||||
it('should handle acronyms', () => {
|
||||
lodashStable.each(['safe HTML', 'safeHTML'], (string) => {
|
||||
expect(camelCase(string)).toBe('safeHtml');
|
||||
});
|
||||
|
||||
lodashStable.each(['escape HTML entities', 'escapeHTMLEntities'], (string) => {
|
||||
expect(camelCase(string)).toBe('escapeHtmlEntities');
|
||||
});
|
||||
|
||||
lodashStable.each(['XMLHttpRequest', 'XmlHTTPRequest'], (string) => {
|
||||
expect(camelCase(string)).toBe('xmlHttpRequest');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,28 +0,0 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import camelCase from '../src/camelCase';
|
||||
|
||||
describe('camelCase', () => {
|
||||
it('should work with numbers', () => {
|
||||
assert.strictEqual(camelCase('12 feet'), '12Feet');
|
||||
assert.strictEqual(camelCase('enable 6h format'), 'enable6HFormat');
|
||||
assert.strictEqual(camelCase('enable 24H format'), 'enable24HFormat');
|
||||
assert.strictEqual(camelCase('too legit 2 quit'), 'tooLegit2Quit');
|
||||
assert.strictEqual(camelCase('walk 500 miles'), 'walk500Miles');
|
||||
assert.strictEqual(camelCase('xhr2 request'), 'xhr2Request');
|
||||
});
|
||||
|
||||
it('should handle acronyms', () => {
|
||||
lodashStable.each(['safe HTML', 'safeHTML'], (string) => {
|
||||
assert.strictEqual(camelCase(string), 'safeHtml');
|
||||
});
|
||||
|
||||
lodashStable.each(['escape HTML entities', 'escapeHTMLEntities'], (string) => {
|
||||
assert.strictEqual(camelCase(string), 'escapeHtmlEntities');
|
||||
});
|
||||
|
||||
lodashStable.each(['XMLHttpRequest', 'XmlHTTPRequest'], (string) => {
|
||||
assert.strictEqual(camelCase(string), 'xmlHttpRequest');
|
||||
});
|
||||
});
|
||||
});
|
||||
9
test/capitalize.spec.js
Normal file
9
test/capitalize.spec.js
Normal file
@@ -0,0 +1,9 @@
|
||||
import capitalize from '../src/capitalize';
|
||||
|
||||
describe('capitalize', () => {
|
||||
it('should capitalize the first character of a string', () => {
|
||||
expect(capitalize('fred')).toBe('Fred');
|
||||
expect(capitalize('Fred')).toBe('Fred');
|
||||
expect(capitalize(' fred')).toBe(' fred');
|
||||
});
|
||||
});
|
||||
@@ -1,10 +0,0 @@
|
||||
import assert from 'node:assert';
|
||||
import capitalize from '../src/capitalize';
|
||||
|
||||
describe('capitalize', () => {
|
||||
it('should capitalize the first character of a string', () => {
|
||||
assert.strictEqual(capitalize('fred'), 'Fred');
|
||||
assert.strictEqual(capitalize('Fred'), 'Fred');
|
||||
assert.strictEqual(capitalize(' fred'), ' fred');
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,3 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { stubTrue, burredLetters, deburredLetters } from './utils';
|
||||
import camelCase from '../src/camelCase';
|
||||
@@ -19,8 +18,8 @@ const caseMethods = {
|
||||
|
||||
describe('case methods', () => {
|
||||
lodashStable.each(['camel', 'kebab', 'lower', 'snake', 'start', 'upper'], (caseName) => {
|
||||
const methodName = `${caseName}Case`,
|
||||
func = caseMethods[methodName];
|
||||
const methodName = `${caseName}Case`;
|
||||
const func = caseMethods[methodName];
|
||||
|
||||
const strings = [
|
||||
'foo bar',
|
||||
@@ -56,7 +55,7 @@ describe('case methods', () => {
|
||||
return func(string) === expected;
|
||||
});
|
||||
|
||||
assert.deepStrictEqual(actual, lodashStable.map(strings, stubTrue));
|
||||
expect(actual, lodashStable.map(strings).toEqual(stubTrue));
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should handle double-converting strings`, () => {
|
||||
@@ -65,7 +64,7 @@ describe('case methods', () => {
|
||||
return func(func(string)) === expected;
|
||||
});
|
||||
|
||||
assert.deepStrictEqual(actual, lodashStable.map(strings, stubTrue));
|
||||
expect(actual, lodashStable.map(strings).toEqual(stubTrue));
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should remove contraction apostrophes`, () => {
|
||||
@@ -93,19 +92,19 @@ describe('case methods', () => {
|
||||
}
|
||||
});
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should remove Latin mathematical operators`, () => {
|
||||
const actual = lodashStable.map(['\xd7', '\xf7'], func);
|
||||
assert.deepStrictEqual(actual, ['', '']);
|
||||
expect(actual, ['').toEqual('']);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should coerce \`string\` to a string`, () => {
|
||||
const string = 'foo bar';
|
||||
assert.strictEqual(func(Object(string)), converted);
|
||||
assert.strictEqual(func({ toString: lodashStable.constant(string) }), converted);
|
||||
expect(func(Object(string))).toBe(converted);
|
||||
expect(func({ toString: lodashStable.constant(string) })).toBe(converted);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -127,7 +126,7 @@ describe('case methods', () => {
|
||||
'enable 6h format',
|
||||
);
|
||||
|
||||
assert.strictEqual(actual, 'enable6HFormat');
|
||||
expect(actual).toBe('enable6HFormat');
|
||||
});
|
||||
})();
|
||||
});
|
||||
@@ -1,23 +1,22 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { falsey } from './utils';
|
||||
import castArray from '../src/castArray';
|
||||
|
||||
describe('castArray', () => {
|
||||
it('should wrap non-array items in an array', () => {
|
||||
const values = falsey.concat(true, 1, 'a', { a: 1 }),
|
||||
expected = lodashStable.map(values, (value) => [value]),
|
||||
actual = lodashStable.map(values, castArray);
|
||||
const values = falsey.concat(true, 1, 'a', { a: 1 });
|
||||
const expected = lodashStable.map(values, (value) => [value]);
|
||||
const actual = lodashStable.map(values, castArray);
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should return array values by reference', () => {
|
||||
const array = [1];
|
||||
assert.strictEqual(castArray(array), array);
|
||||
expect(castArray(array)).toBe(array);
|
||||
});
|
||||
|
||||
it('should return an empty array when no arguments are given', () => {
|
||||
assert.deepStrictEqual(castArray(), []);
|
||||
expect(castArray()).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -1,93 +0,0 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { square } from './utils';
|
||||
import chain from '../src/chain';
|
||||
|
||||
describe('chain', () => {
|
||||
it('should return a wrapped value', () => {
|
||||
const actual = chain({ a: 0 });
|
||||
assert.ok(actual instanceof _);
|
||||
});
|
||||
|
||||
it('should return existing wrapped values', () => {
|
||||
const wrapped = _({ a: 0 });
|
||||
assert.strictEqual(chain(wrapped), wrapped);
|
||||
assert.strictEqual(wrapped.chain(), wrapped);
|
||||
});
|
||||
|
||||
it('should enable chaining for methods that return unwrapped values', () => {
|
||||
const array = ['c', 'b', 'a'];
|
||||
|
||||
assert.ok(chain(array).head() instanceof _);
|
||||
assert.ok(_(array).chain().head() instanceof _);
|
||||
|
||||
assert.ok(chain(array).isArray() instanceof _);
|
||||
assert.ok(_(array).chain().isArray() instanceof _);
|
||||
|
||||
assert.ok(chain(array).sortBy().head() instanceof _);
|
||||
assert.ok(_(array).chain().sortBy().head() instanceof _);
|
||||
});
|
||||
|
||||
it('should chain multiple methods', () => {
|
||||
lodashStable.times(2, (index) => {
|
||||
let array = ['one two three four', 'five six seven eight', 'nine ten eleven twelve'],
|
||||
expected = {
|
||||
' ': 9,
|
||||
e: 14,
|
||||
f: 2,
|
||||
g: 1,
|
||||
h: 2,
|
||||
i: 4,
|
||||
l: 2,
|
||||
n: 6,
|
||||
o: 3,
|
||||
r: 2,
|
||||
s: 2,
|
||||
t: 5,
|
||||
u: 1,
|
||||
v: 4,
|
||||
w: 2,
|
||||
x: 1,
|
||||
},
|
||||
wrapped = index ? _(array).chain() : chain(array);
|
||||
|
||||
let actual = wrapped
|
||||
.chain()
|
||||
.map((value) => value.split(''))
|
||||
.flatten()
|
||||
.reduce((object, chr) => {
|
||||
object[chr] || (object[chr] = 0);
|
||||
object[chr]++;
|
||||
return object;
|
||||
}, {})
|
||||
.value();
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
|
||||
array = [1, 2, 3, 4, 5, 6];
|
||||
wrapped = index ? _(array).chain() : chain(array);
|
||||
actual = wrapped
|
||||
.chain()
|
||||
.filter((n) => n % 2 != 0)
|
||||
.reject((n) => n % 3 === 0)
|
||||
.sortBy((n) => -n)
|
||||
.value();
|
||||
|
||||
assert.deepStrictEqual(actual, [5, 1]);
|
||||
|
||||
array = [3, 4];
|
||||
wrapped = index ? _(array).chain() : chain(array);
|
||||
actual = wrapped
|
||||
.reverse()
|
||||
.concat([2, 1])
|
||||
.unshift(5)
|
||||
.tap((value) => {
|
||||
value.pop();
|
||||
})
|
||||
.map(square)
|
||||
.value();
|
||||
|
||||
assert.deepStrictEqual(actual, [25, 16, 9, 4]);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,3 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { falsey, stubArray } from './utils';
|
||||
import chunk from '../src/chunk';
|
||||
@@ -31,19 +30,19 @@ describe('chunk', () => {
|
||||
index ? chunk(array, size) : chunk(array),
|
||||
);
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should ensure the minimum `size` is `0`', () => {
|
||||
const values = lodashStable.reject(falsey, lodashStable.isUndefined).concat(-1, -Infinity),
|
||||
expected = lodashStable.map(values, stubArray);
|
||||
const values = lodashStable.reject(falsey, lodashStable.isUndefined).concat(-1, -Infinity);
|
||||
const expected = lodashStable.map(values, stubArray);
|
||||
|
||||
const actual = lodashStable.map(values, (n) => chunk(array, n));
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should coerce `size` to an integer', () => {
|
||||
assert.deepStrictEqual(chunk(array, array.length / 4), [[0], [1], [2], [3], [4], [5]]);
|
||||
expect(chunk(array, array.length / 4), [[0], [1], [2], [3], [4]).toEqual([5]]);
|
||||
});
|
||||
});
|
||||
57
test/clamp.spec.js
Normal file
57
test/clamp.spec.js
Normal file
@@ -0,0 +1,57 @@
|
||||
import clamp from '../src/clamp';
|
||||
|
||||
describe('clamp', () => {
|
||||
it('should work with a `max`', () => {
|
||||
expect(clamp(5, 3)).toBe(3);
|
||||
expect(clamp(1, 3)).toBe(1);
|
||||
});
|
||||
|
||||
it('should clamp negative numbers', () => {
|
||||
expect(clamp(-10, -5, 5)).toBe(-5);
|
||||
expect(clamp(-10.2, -5.5, 5.5)).toBe(-5.5);
|
||||
expect(clamp(-Infinity, -5, 5)).toBe(-5);
|
||||
});
|
||||
|
||||
it('should clamp positive numbers', () => {
|
||||
expect(clamp(10, -5, 5)).toBe(5);
|
||||
expect(clamp(10.6, -5.6, 5.4)).toBe(5.4);
|
||||
expect(clamp(Infinity, -5, 5)).toBe(5);
|
||||
});
|
||||
|
||||
it('should not alter negative numbers in range', () => {
|
||||
expect(clamp(-4, -5, 5)).toBe(-4);
|
||||
expect(clamp(-5, -5, 5)).toBe(-5);
|
||||
expect(clamp(-5.5, -5.6, 5.6)).toBe(-5.5);
|
||||
});
|
||||
|
||||
it('should not alter positive numbers in range', () => {
|
||||
expect(clamp(4, -5, 5)).toBe(4);
|
||||
expect(clamp(5, -5, 5)).toBe(5);
|
||||
expect(clamp(4.5, -5.1, 5.2)).toBe(4.5);
|
||||
});
|
||||
|
||||
it('should not alter `0` in range', () => {
|
||||
expect(1 / clamp(0, -5, 5)).toBe(Infinity);
|
||||
});
|
||||
|
||||
it('should clamp to `0`', () => {
|
||||
expect(1 / clamp(-10, 0, 5)).toBe(Infinity);
|
||||
});
|
||||
|
||||
it('should not alter `-0` in range', () => {
|
||||
expect(1 / clamp(-0, -5, 5)).toBe(-Infinity);
|
||||
});
|
||||
|
||||
it('should clamp to `-0`', () => {
|
||||
expect(1 / clamp(-10, -0, 5)).toBe(-Infinity);
|
||||
});
|
||||
|
||||
it('should return `NaN` when `number` is `NaN`', () => {
|
||||
expect(clamp(NaN, -5, 5)).toEqual(NaN);
|
||||
});
|
||||
|
||||
it('should coerce `min` and `max` of `NaN` to `0`', () => {
|
||||
expect(clamp(1, -5, NaN)).toEqual(0);
|
||||
expect(clamp(-1, NaN, 5)).toEqual(0);
|
||||
});
|
||||
});
|
||||
@@ -1,58 +0,0 @@
|
||||
import assert from 'node:assert';
|
||||
import clamp from '../src/clamp';
|
||||
|
||||
describe('clamp', () => {
|
||||
it('should work with a `max`', () => {
|
||||
assert.strictEqual(clamp(5, 3), 3);
|
||||
assert.strictEqual(clamp(1, 3), 1);
|
||||
});
|
||||
|
||||
it('should clamp negative numbers', () => {
|
||||
assert.strictEqual(clamp(-10, -5, 5), -5);
|
||||
assert.strictEqual(clamp(-10.2, -5.5, 5.5), -5.5);
|
||||
assert.strictEqual(clamp(-Infinity, -5, 5), -5);
|
||||
});
|
||||
|
||||
it('should clamp positive numbers', () => {
|
||||
assert.strictEqual(clamp(10, -5, 5), 5);
|
||||
assert.strictEqual(clamp(10.6, -5.6, 5.4), 5.4);
|
||||
assert.strictEqual(clamp(Infinity, -5, 5), 5);
|
||||
});
|
||||
|
||||
it('should not alter negative numbers in range', () => {
|
||||
assert.strictEqual(clamp(-4, -5, 5), -4);
|
||||
assert.strictEqual(clamp(-5, -5, 5), -5);
|
||||
assert.strictEqual(clamp(-5.5, -5.6, 5.6), -5.5);
|
||||
});
|
||||
|
||||
it('should not alter positive numbers in range', () => {
|
||||
assert.strictEqual(clamp(4, -5, 5), 4);
|
||||
assert.strictEqual(clamp(5, -5, 5), 5);
|
||||
assert.strictEqual(clamp(4.5, -5.1, 5.2), 4.5);
|
||||
});
|
||||
|
||||
it('should not alter `0` in range', () => {
|
||||
assert.strictEqual(1 / clamp(0, -5, 5), Infinity);
|
||||
});
|
||||
|
||||
it('should clamp to `0`', () => {
|
||||
assert.strictEqual(1 / clamp(-10, 0, 5), Infinity);
|
||||
});
|
||||
|
||||
it('should not alter `-0` in range', () => {
|
||||
assert.strictEqual(1 / clamp(-0, -5, 5), -Infinity);
|
||||
});
|
||||
|
||||
it('should clamp to `-0`', () => {
|
||||
assert.strictEqual(1 / clamp(-10, -0, 5), -Infinity);
|
||||
});
|
||||
|
||||
it('should return `NaN` when `number` is `NaN`', () => {
|
||||
assert.deepStrictEqual(clamp(NaN, -5, 5), NaN);
|
||||
});
|
||||
|
||||
it('should coerce `min` and `max` of `NaN` to `0`', () => {
|
||||
assert.deepStrictEqual(clamp(1, -5, NaN), 0);
|
||||
assert.deepStrictEqual(clamp(-1, NaN, 5), 0);
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,3 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
|
||||
import {
|
||||
@@ -49,7 +48,7 @@ xdescribe('clone methods', function () {
|
||||
const objects = {
|
||||
'`arguments` objects': arguments,
|
||||
arrays: ['a', ''],
|
||||
'array-like objects': { '0': 'a', length: 1 },
|
||||
'array-like objects': { 0: 'a', length: 1 },
|
||||
booleans: false,
|
||||
'boolean objects': Object(false),
|
||||
'date objects': new Date(),
|
||||
@@ -83,11 +82,11 @@ xdescribe('clone methods', function () {
|
||||
});
|
||||
|
||||
it('`_.clone` should perform a shallow clone', () => {
|
||||
const array = [{ a: 0 }, { b: 1 }],
|
||||
actual = _.clone(array);
|
||||
const array = [{ a: 0 }, { b: 1 }];
|
||||
const actual = _.clone(array);
|
||||
|
||||
assert.deepStrictEqual(actual, array);
|
||||
assert.ok(actual !== array && actual[0] === array[0]);
|
||||
expect(actual).toEqual(array);
|
||||
expect(actual !== array && actual[0] === array[0])
|
||||
});
|
||||
|
||||
it('`_.cloneDeep` should deep clone objects with circular references', () => {
|
||||
@@ -111,10 +110,10 @@ xdescribe('clone methods', function () {
|
||||
cyclical[`v${index}`] = [index ? cyclical[`v${index - 1}`] : cyclical];
|
||||
});
|
||||
|
||||
const clone = cloneDeep(cyclical),
|
||||
actual = clone[`v${LARGE_ARRAY_SIZE}`][0];
|
||||
const clone = cloneDeep(cyclical);
|
||||
const actual = clone[`v${LARGE_ARRAY_SIZE}`][0];
|
||||
|
||||
assert.strictEqual(actual, clone[`v${LARGE_ARRAY_SIZE - 1}`]);
|
||||
expect(actual).toBe(clone[`v${LARGE_ARRAY_SIZE - 1}`]);
|
||||
assert.notStrictEqual(actual, cyclical[`v${LARGE_ARRAY_SIZE - 1}`]);
|
||||
});
|
||||
|
||||
@@ -125,22 +124,22 @@ xdescribe('clone methods', function () {
|
||||
actual = last(arguments);
|
||||
});
|
||||
|
||||
assert.ok(isNpm ? actual.constructor.name === 'Stack' : actual instanceof mapCaches.Stack);
|
||||
expect(isNpm ? actual.constructor.name === 'Stack' : actual instanceof mapCaches.Stack)
|
||||
});
|
||||
|
||||
lodashStable.each(['clone', 'cloneDeep'], (methodName) => {
|
||||
const func = _[methodName],
|
||||
isDeep = methodName === 'cloneDeep';
|
||||
const func = _[methodName];
|
||||
const isDeep = methodName === 'cloneDeep';
|
||||
|
||||
lodashStable.forOwn(objects, (object, kind) => {
|
||||
it(`\`_.${methodName}\` should clone ${kind}`, () => {
|
||||
const actual = func(object);
|
||||
assert.ok(lodashStable.isEqual(actual, object));
|
||||
expect(lodashStable.isEqual(actual, object))
|
||||
|
||||
if (lodashStable.isObject(object)) {
|
||||
assert.notStrictEqual(actual, object);
|
||||
} else {
|
||||
assert.strictEqual(actual, object);
|
||||
expect(actual).toBe(object);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -148,38 +147,38 @@ xdescribe('clone methods', function () {
|
||||
it(`\`_.${methodName}\` should clone array buffers`, () => {
|
||||
if (ArrayBuffer) {
|
||||
const actual = func(arrayBuffer);
|
||||
assert.strictEqual(actual.byteLength, arrayBuffer.byteLength);
|
||||
expect(actual.byteLength).toBe(arrayBuffer.byteLength);
|
||||
assert.notStrictEqual(actual, arrayBuffer);
|
||||
}
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should clone buffers`, () => {
|
||||
if (Buffer) {
|
||||
const buffer = new Buffer([1, 2]),
|
||||
actual = func(buffer);
|
||||
const buffer = Buffer.from([1, 2]);
|
||||
const actual = func(buffer);
|
||||
|
||||
assert.strictEqual(actual.byteLength, buffer.byteLength);
|
||||
assert.strictEqual(actual.inspect(), buffer.inspect());
|
||||
expect(actual.byteLength).toBe(buffer.byteLength);
|
||||
expect(actual.inspect()).toBe(buffer.inspect());
|
||||
assert.notStrictEqual(actual, buffer);
|
||||
|
||||
buffer[0] = 2;
|
||||
assert.strictEqual(actual[0], isDeep ? 2 : 1);
|
||||
expect(actual[0]).toBe(isDeep ? 2 : 1);
|
||||
}
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should clone \`index\` and \`input\` array properties`, () => {
|
||||
const array = /c/.exec('abcde'),
|
||||
actual = func(array);
|
||||
const array = /c/.exec('abcde');
|
||||
const actual = func(array);
|
||||
|
||||
assert.strictEqual(actual.index, 2);
|
||||
assert.strictEqual(actual.input, 'abcde');
|
||||
expect(actual.index).toBe(2);
|
||||
expect(actual.input).toBe('abcde');
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should clone \`lastIndex\` regexp property`, () => {
|
||||
const regexp = /c/g;
|
||||
regexp.exec('abcde');
|
||||
|
||||
assert.strictEqual(func(regexp).lastIndex, 3);
|
||||
expect(func(regexp).lastIndex).toBe(3);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should clone expando properties`, () => {
|
||||
@@ -193,29 +192,29 @@ xdescribe('clone methods', function () {
|
||||
|
||||
const actual = lodashStable.map(values, (value) => func(value).a === 1);
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should clone prototype objects`, () => {
|
||||
const actual = func(Foo.prototype);
|
||||
|
||||
assert.ok(!(actual instanceof Foo));
|
||||
assert.deepStrictEqual(actual, { b: 1 });
|
||||
expect((actual instanceof Foo)).toBe(false)
|
||||
expect(actual).toEqual({ b: 1 });
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should set the \`[[Prototype]]\` of a clone`, () => {
|
||||
assert.ok(func(new Foo()) instanceof Foo);
|
||||
expect(func(new Foo()) instanceof Foo)
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should set the \`[[Prototype]]\` of a clone even when the \`constructor\` is incorrect`, () => {
|
||||
Foo.prototype.constructor = Object;
|
||||
assert.ok(func(new Foo()) instanceof Foo);
|
||||
expect(func(new Foo()) instanceof Foo)
|
||||
Foo.prototype.constructor = Foo;
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should ensure \`value\` constructor is a function before using its \`[[Prototype]]\``, () => {
|
||||
Foo.prototype.constructor = null;
|
||||
assert.ok(!(func(new Foo()) instanceof Foo));
|
||||
expect((func(new Foo()) instanceof Foo)).toBe(false)
|
||||
Foo.prototype.constructor = Foo;
|
||||
});
|
||||
|
||||
@@ -232,7 +231,7 @@ xdescribe('clone methods', function () {
|
||||
|
||||
const actual = func(object);
|
||||
|
||||
assert.deepStrictEqual(actual, object);
|
||||
expect(actual).toEqual(object);
|
||||
assert.notStrictEqual(actual, object);
|
||||
});
|
||||
|
||||
@@ -261,33 +260,33 @@ xdescribe('clone methods', function () {
|
||||
assert.notStrictEqual(actual[symbol], object[symbol]);
|
||||
assert.notStrictEqual(actual.a, object.a);
|
||||
} else {
|
||||
assert.strictEqual(actual[symbol], object[symbol]);
|
||||
assert.strictEqual(actual.a, object.a);
|
||||
expect(actual[symbol]).toBe(object[symbol]);
|
||||
expect(actual.a).toBe(object.a);
|
||||
}
|
||||
assert.deepStrictEqual(actual[symbol], object[symbol]);
|
||||
assert.deepStrictEqual(getSymbols(actual.a.b), [symbol]);
|
||||
assert.deepStrictEqual(actual.a.b[symbol], object.a.b[symbol]);
|
||||
assert.deepStrictEqual(actual.a.b[symbol2], object.a.b[symbol2]);
|
||||
assert.deepStrictEqual(actual.a.b[symbol3], object.a.b[symbol3]);
|
||||
expect(actual[symbol]).toEqual(object[symbol]);
|
||||
expect(getSymbols(actual.a.b)).toEqual([symbol]);
|
||||
expect(actual.a.b[symbol]).toEqual(object.a.b[symbol]);
|
||||
expect(actual.a.b[symbol2]).toEqual(object.a.b[symbol2]);
|
||||
expect(actual.a.b[symbol3]).toEqual(object.a.b[symbol3]);
|
||||
}
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should clone symbol objects`, () => {
|
||||
if (Symbol) {
|
||||
assert.strictEqual(func(symbol), symbol);
|
||||
expect(func(symbol)).toBe(symbol);
|
||||
|
||||
const object = Object(symbol),
|
||||
actual = func(object);
|
||||
const object = Object(symbol);
|
||||
const actual = func(object);
|
||||
|
||||
assert.strictEqual(typeof actual, 'object');
|
||||
assert.strictEqual(typeof actual.valueOf(), 'symbol');
|
||||
expect(typeof actual).toBe('object');
|
||||
expect(typeof actual.valueOf()).toBe('symbol');
|
||||
assert.notStrictEqual(actual, object);
|
||||
}
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should not clone symbol primitives`, () => {
|
||||
if (Symbol) {
|
||||
assert.strictEqual(func(symbol), symbol);
|
||||
expect(func(symbol)).toBe(symbol);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -296,9 +295,9 @@ xdescribe('clone methods', function () {
|
||||
const element = document.createElement('div');
|
||||
|
||||
try {
|
||||
assert.deepStrictEqual(func(element), {});
|
||||
expect(func(element)).toEqual({});
|
||||
} catch (e) {
|
||||
assert.ok(false, e.message);
|
||||
expect(false, e.message)
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -326,24 +325,24 @@ xdescribe('clone methods', function () {
|
||||
const expected = lodashStable.map(objects, stubTrue);
|
||||
|
||||
const actual = lodashStable.map(objects, (object) => {
|
||||
const Ctor = object.constructor,
|
||||
result = func(object);
|
||||
const Ctor = object.constructor;
|
||||
const result = func(object);
|
||||
|
||||
return (
|
||||
result !== object && (result instanceof Ctor || !(new Ctor() instanceof Ctor))
|
||||
);
|
||||
});
|
||||
|
||||
assert.deepStrictEqual(actual, expected, props.join(', '));
|
||||
expect(actual, expected, props.join(').toEqual('));
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should perform a ${
|
||||
isDeep ? 'deep' : 'shallow'
|
||||
} clone when used as an iteratee for methods like \`_.map\``, () => {
|
||||
const expected = [{ a: [0] }, { b: [1] }],
|
||||
actual = lodashStable.map(expected, func);
|
||||
const expected = [{ a: [0] }, { b: [1] }];
|
||||
const actual = lodashStable.map(expected, func);
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
|
||||
if (isDeep) {
|
||||
assert.ok(
|
||||
@@ -361,10 +360,10 @@ xdescribe('clone methods', function () {
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should return a unwrapped value when chaining`, () => {
|
||||
const object = objects.objects,
|
||||
actual = _(object)[methodName]();
|
||||
const object = objects.objects;
|
||||
const actual = _(object)[methodName]();
|
||||
|
||||
assert.deepEqual(actual, object);
|
||||
expect(actual).toEqual(object);
|
||||
assert.notStrictEqual(actual, object);
|
||||
});
|
||||
|
||||
@@ -374,15 +373,15 @@ xdescribe('clone methods', function () {
|
||||
|
||||
lodashStable.times(2, (index) => {
|
||||
if (Ctor) {
|
||||
const buffer = new ArrayBuffer(24),
|
||||
view = index ? new Ctor(buffer, 8, 1) : new Ctor(buffer),
|
||||
actual = func(view);
|
||||
const buffer = new ArrayBuffer(24);
|
||||
const view = index ? new Ctor(buffer, 8, 1) : new Ctor(buffer);
|
||||
const actual = func(view);
|
||||
|
||||
assert.deepStrictEqual(actual, view);
|
||||
expect(actual).toEqual(view);
|
||||
assert.notStrictEqual(actual, view);
|
||||
assert.strictEqual(actual.buffer === view.buffer, !isDeep);
|
||||
assert.strictEqual(actual.byteOffset, view.byteOffset);
|
||||
assert.strictEqual(actual.length, view.length);
|
||||
expect(actual.buffer === view.buffer).toBe(!isDeep);
|
||||
expect(actual.byteOffset).toBe(view.byteOffset);
|
||||
expect(actual.length).toBe(view.length);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -391,39 +390,39 @@ xdescribe('clone methods', function () {
|
||||
lodashStable.forOwn(uncloneable, (value, key) => {
|
||||
it(`\`_.${methodName}\` should not clone ${key}`, () => {
|
||||
if (value) {
|
||||
const object = { a: value, b: { c: value } },
|
||||
actual = func(object),
|
||||
expected = value === Foo ? { c: Foo.c } : {};
|
||||
const object = { a: value, b: { c: value } };
|
||||
const actual = func(object);
|
||||
const expected = value === Foo ? { c: Foo.c } : {};
|
||||
|
||||
assert.deepStrictEqual(actual, object);
|
||||
expect(actual).toEqual(object);
|
||||
assert.notStrictEqual(actual, object);
|
||||
assert.deepStrictEqual(func(value), expected);
|
||||
expect(func(value)).toEqual(expected);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
lodashStable.each(['cloneWith', 'cloneDeepWith'], (methodName) => {
|
||||
const func = _[methodName],
|
||||
isDeep = methodName === 'cloneDeepWith';
|
||||
const func = _[methodName];
|
||||
const isDeep = methodName === 'cloneDeepWith';
|
||||
|
||||
it(`\`_.${methodName}\` should provide correct \`customizer\` arguments`, () => {
|
||||
const argsList = [],
|
||||
object = new Foo();
|
||||
const argsList = [];
|
||||
const object = new Foo();
|
||||
|
||||
func(object, function () {
|
||||
const length = arguments.length,
|
||||
args = slice.call(arguments, 0, length - (length > 1 ? 1 : 0));
|
||||
const length = arguments.length;
|
||||
const args = slice.call(arguments, 0, length - (length > 1 ? 1 : 0));
|
||||
|
||||
argsList.push(args);
|
||||
});
|
||||
|
||||
assert.deepStrictEqual(argsList, isDeep ? [[object], [1, 'a', object]] : [[object]]);
|
||||
expect(argsList, isDeep ? [[object], [1, 'a').toEqual(object]] : [[object]]);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should handle cloning when \`customizer\` returns \`undefined\``, () => {
|
||||
const actual = func({ a: { b: 'c' } }, noop);
|
||||
assert.deepStrictEqual(actual, { a: { b: 'c' } });
|
||||
expect(actual).toEqual({ a: { b: 'c' } });
|
||||
});
|
||||
|
||||
lodashStable.forOwn(uncloneable, (value, key) => {
|
||||
@@ -433,12 +432,12 @@ xdescribe('clone methods', function () {
|
||||
};
|
||||
|
||||
let actual = func(value, customizer);
|
||||
assert.strictEqual(actual, value);
|
||||
expect(actual).toBe(value);
|
||||
|
||||
const object = { a: value, b: { c: value } };
|
||||
actual = func(object, customizer);
|
||||
|
||||
assert.deepStrictEqual(actual, object);
|
||||
expect(actual).toEqual(object);
|
||||
assert.notStrictEqual(actual, object);
|
||||
});
|
||||
});
|
||||
9
test/compact.spec.js
Normal file
9
test/compact.spec.js
Normal file
@@ -0,0 +1,9 @@
|
||||
import { falsey } from './utils';
|
||||
import compact from '../src/compact';
|
||||
|
||||
describe('compact', () => {
|
||||
it('should filter falsey values', () => {
|
||||
const array = ['0', '1', '2'];
|
||||
expect(compact(falsey.concat(array))).toEqual(array);
|
||||
});
|
||||
});
|
||||
@@ -1,44 +0,0 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { LARGE_ARRAY_SIZE, _, falsey } from './utils';
|
||||
import compact from '../src/compact';
|
||||
import slice from '../src/slice';
|
||||
|
||||
describe('compact', () => {
|
||||
const largeArray = lodashStable.range(LARGE_ARRAY_SIZE).concat(null);
|
||||
|
||||
it('should filter falsey values', () => {
|
||||
const array = ['0', '1', '2'];
|
||||
assert.deepStrictEqual(compact(falsey.concat(array)), array);
|
||||
});
|
||||
|
||||
it('should work when in-between lazy operators', () => {
|
||||
let actual = _(falsey).thru(slice).compact().thru(slice).value();
|
||||
assert.deepEqual(actual, []);
|
||||
|
||||
actual = _(falsey).thru(slice).push(true, 1).compact().push('a').value();
|
||||
assert.deepEqual(actual, [true, 1, 'a']);
|
||||
});
|
||||
|
||||
it('should work in a lazy sequence', () => {
|
||||
const actual = _(largeArray).slice(1).compact().reverse().take().value();
|
||||
assert.deepEqual(actual, _.take(compact(slice(largeArray, 1)).reverse()));
|
||||
});
|
||||
|
||||
it('should work in a lazy sequence with a custom `_.iteratee`', () => {
|
||||
let iteratee = _.iteratee,
|
||||
pass = false;
|
||||
|
||||
_.iteratee = identity;
|
||||
|
||||
try {
|
||||
const actual = _(largeArray).slice(1).compact().value();
|
||||
pass = lodashStable.isEqual(actual, compact(slice(largeArray, 1)));
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
}
|
||||
|
||||
assert.ok(pass);
|
||||
_.iteratee = iteratee;
|
||||
});
|
||||
});
|
||||
@@ -1,22 +1,21 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import concat from '../src/concat';
|
||||
|
||||
describe('concat', () => {
|
||||
it('should shallow clone `array`', () => {
|
||||
const array = [1, 2, 3],
|
||||
actual = concat(array);
|
||||
const array = [1, 2, 3];
|
||||
const actual = concat(array);
|
||||
|
||||
assert.deepStrictEqual(actual, array);
|
||||
expect(actual).toEqual(array);
|
||||
assert.notStrictEqual(actual, array);
|
||||
});
|
||||
|
||||
it('should concat arrays and values', () => {
|
||||
const array = [1],
|
||||
actual = concat(array, 2, [3], [[4]]);
|
||||
const array = [1];
|
||||
const actual = concat(array, 2, [3], [[4]]);
|
||||
|
||||
assert.deepStrictEqual(actual, [1, 2, 3, [4]]);
|
||||
assert.deepStrictEqual(array, [1]);
|
||||
expect(actual).toEqual([1, 2, 3, [4]]);
|
||||
expect(array).toEqual([1]);
|
||||
});
|
||||
|
||||
it('should cast non-array `array` values to arrays', () => {
|
||||
@@ -26,32 +25,32 @@ describe('concat', () => {
|
||||
|
||||
let actual = lodashStable.map(values, (value, index) => (index ? concat(value) : concat()));
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
|
||||
expected = lodashStable.map(values, (value) => [value, 2, [3]]);
|
||||
|
||||
actual = lodashStable.map(values, (value) => concat(value, [2], [[3]]));
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should treat sparse arrays as dense', () => {
|
||||
const expected = [],
|
||||
actual = concat(Array(1), Array(1));
|
||||
const expected = [];
|
||||
const actual = concat(Array(1), Array(1));
|
||||
|
||||
expected.push(undefined, undefined);
|
||||
|
||||
assert.ok('0' in actual);
|
||||
assert.ok('1' in actual);
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect('0' in actual)
|
||||
expect('1' in actual)
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should return a new wrapped array', () => {
|
||||
const array = [1],
|
||||
wrapped = _(array).concat([2, 3]),
|
||||
actual = wrapped.value();
|
||||
const array = [1];
|
||||
const wrapped = _(array).concat([2, 3]);
|
||||
const actual = wrapped.value();
|
||||
|
||||
assert.deepEqual(array, [1]);
|
||||
assert.deepEqual(actual, [1, 2, 3]);
|
||||
expect(array).toEqual([1]);
|
||||
expect(actual, [1, 2).toEqual(3]);
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,3 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { _, stubA, stubB, stubC, slice, stubFalse, stubTrue } from './utils';
|
||||
|
||||
@@ -10,15 +9,15 @@ describe('cond', () => {
|
||||
[lodashStable.property('c'), stubC],
|
||||
]);
|
||||
|
||||
assert.strictEqual(cond({ a: 1, b: 2, c: 3 }), 'a');
|
||||
assert.strictEqual(cond({ a: 0, b: 1, c: 2 }), 'b');
|
||||
assert.strictEqual(cond({ a: -1, b: 0, c: 1 }), 'c');
|
||||
expect(cond({ a: 1, b: 2, c: 3 })).toBe('a');
|
||||
expect(cond({ a: 0, b: 1, c: 2 })).toBe('b');
|
||||
expect(cond({ a: -1, b: 0, c: 1 })).toBe('c');
|
||||
});
|
||||
|
||||
it('should provide arguments to functions', () => {
|
||||
let args1,
|
||||
args2,
|
||||
expected = ['a', 'b', 'c'];
|
||||
let args1;
|
||||
let args2;
|
||||
const expected = ['a', 'b', 'c'];
|
||||
|
||||
const cond = _.cond([
|
||||
[
|
||||
@@ -34,8 +33,8 @@ describe('cond', () => {
|
||||
|
||||
cond('a', 'b', 'c');
|
||||
|
||||
assert.deepStrictEqual(args1, expected);
|
||||
assert.deepStrictEqual(args2, expected);
|
||||
expect(args1).toEqual(expected);
|
||||
expect(args2).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should work with predicate shorthands', () => {
|
||||
@@ -45,14 +44,14 @@ describe('cond', () => {
|
||||
['c', stubC],
|
||||
]);
|
||||
|
||||
assert.strictEqual(cond({ a: 1, b: 2, c: 3 }), 'a');
|
||||
assert.strictEqual(cond({ a: 0, b: 1, c: 2 }), 'b');
|
||||
assert.strictEqual(cond({ a: -1, b: 0, c: 1 }), 'c');
|
||||
expect(cond({ a: 1, b: 2, c: 3 })).toBe('a');
|
||||
expect(cond({ a: 0, b: 1, c: 2 })).toBe('b');
|
||||
expect(cond({ a: -1, b: 0, c: 1 })).toBe('c');
|
||||
});
|
||||
|
||||
it('should return `undefined` when no condition is met', () => {
|
||||
const cond = _.cond([[stubFalse, stubA]]);
|
||||
assert.strictEqual(cond({ a: 1 }), undefined);
|
||||
expect(cond({ a: 1 })).toBe(undefined);
|
||||
});
|
||||
|
||||
it('should throw a TypeError if `pairs` is not composed of functions', () => {
|
||||
@@ -76,6 +75,6 @@ describe('cond', () => {
|
||||
]);
|
||||
|
||||
const object = { cond: cond, a: 1, b: 2 };
|
||||
assert.strictEqual(object.cond('a', 'b'), 2);
|
||||
expect(object.cond('a', 'b')).toBe(2);
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,3 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { _, stubFalse, stubTrue, empties } from './utils';
|
||||
import conformsTo from '../src/conformsTo';
|
||||
@@ -29,7 +28,7 @@ describe('conforms methods', () => {
|
||||
});
|
||||
|
||||
let actual = lodashStable.filter(objects, par);
|
||||
assert.deepStrictEqual(actual, [objects[0], objects[2]]);
|
||||
expect(actual, [objects[0]).toEqual(objects[2]]);
|
||||
|
||||
par = conforms({
|
||||
b: function (value) {
|
||||
@@ -41,7 +40,7 @@ describe('conforms methods', () => {
|
||||
});
|
||||
|
||||
actual = lodashStable.filter(objects, par);
|
||||
assert.deepStrictEqual(actual, [objects[2]]);
|
||||
expect(actual).toEqual([objects[2]]);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should not match by inherited \`source\` properties`, () => {
|
||||
@@ -60,10 +59,10 @@ describe('conforms methods', () => {
|
||||
{ a: 3, b: 16 },
|
||||
];
|
||||
|
||||
const par = conforms(new Foo()),
|
||||
actual = lodashStable.filter(objects, par);
|
||||
const par = conforms(new Foo());
|
||||
const actual = lodashStable.filter(objects, par);
|
||||
|
||||
assert.deepStrictEqual(actual, [objects[1], objects[2]]);
|
||||
expect(actual, [objects[1]).toEqual(objects[2]]);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should not invoke \`source\` predicates for missing \`object\` properties`, () => {
|
||||
@@ -76,8 +75,8 @@ describe('conforms methods', () => {
|
||||
},
|
||||
});
|
||||
|
||||
assert.strictEqual(par({}), false);
|
||||
assert.strictEqual(count, 0);
|
||||
expect(par({})).toBe(false);
|
||||
expect(count).toBe(0);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should work with a function for \`object\``, () => {
|
||||
@@ -93,8 +92,8 @@ describe('conforms methods', () => {
|
||||
},
|
||||
});
|
||||
|
||||
assert.strictEqual(par(Foo), false);
|
||||
assert.strictEqual(par(Bar), true);
|
||||
expect(par(Foo)).toBe(false);
|
||||
expect(par(Bar)).toBe(true);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should work with a function for \`source\``, () => {
|
||||
@@ -103,10 +102,10 @@ describe('conforms methods', () => {
|
||||
return value > 1;
|
||||
};
|
||||
|
||||
const objects = [{ a: 1 }, { a: 2 }],
|
||||
actual = lodashStable.filter(objects, conforms(Foo));
|
||||
const objects = [{ a: 1 }, { a: 2 }];
|
||||
const actual = lodashStable.filter(objects, conforms(Foo));
|
||||
|
||||
assert.deepStrictEqual(actual, [objects[1]]);
|
||||
expect(actual).toEqual([objects[1]]);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should work with a non-plain \`object\``, () => {
|
||||
@@ -121,12 +120,12 @@ describe('conforms methods', () => {
|
||||
},
|
||||
});
|
||||
|
||||
assert.strictEqual(par(new Foo()), true);
|
||||
expect(par(new Foo())).toBe(true);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should return \`false\` when \`object\` is nullish`, () => {
|
||||
const values = [, null, undefined],
|
||||
expected = lodashStable.map(values, stubFalse);
|
||||
const values = [, null, undefined];
|
||||
const expected = lodashStable.map(values, stubFalse);
|
||||
|
||||
const par = conforms({
|
||||
a: function (value) {
|
||||
@@ -140,13 +139,13 @@ describe('conforms methods', () => {
|
||||
} catch (e) {}
|
||||
});
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should return \`true\` when comparing an empty \`source\` to a nullish \`object\``, () => {
|
||||
const values = [, null, undefined],
|
||||
expected = lodashStable.map(values, stubTrue),
|
||||
par = conforms({});
|
||||
const values = [, null, undefined];
|
||||
const expected = lodashStable.map(values, stubTrue);
|
||||
const par = conforms({});
|
||||
|
||||
const actual = lodashStable.map(values, (value, index) => {
|
||||
try {
|
||||
@@ -154,19 +153,19 @@ describe('conforms methods', () => {
|
||||
} catch (e) {}
|
||||
});
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should return \`true\` when comparing an empty \`source\``, () => {
|
||||
const object = { a: 1 },
|
||||
expected = lodashStable.map(empties, stubTrue);
|
||||
const object = { a: 1 };
|
||||
const expected = lodashStable.map(empties, stubTrue);
|
||||
|
||||
const actual = lodashStable.map(empties, (value) => {
|
||||
const par = conforms(value);
|
||||
return par(object);
|
||||
});
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
20
test/conforms.spec.js
Normal file
20
test/conforms.spec.js
Normal file
@@ -0,0 +1,20 @@
|
||||
import conforms from '../src/conforms';
|
||||
|
||||
describe('conforms', () => {
|
||||
it('should not change behavior if `source` is modified', () => {
|
||||
const object = { a: 2 };
|
||||
const source = {
|
||||
a: function (value) {
|
||||
return value > 1;
|
||||
},
|
||||
};
|
||||
const par = conforms(source);
|
||||
|
||||
expect(par(object)).toBe(true);
|
||||
|
||||
source.a = function (value) {
|
||||
return value < 2;
|
||||
};
|
||||
expect(par(object)).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -1,21 +0,0 @@
|
||||
import assert from 'node:assert';
|
||||
import conforms from '../src/conforms';
|
||||
|
||||
describe('conforms', () => {
|
||||
it('should not change behavior if `source` is modified', () => {
|
||||
const object = { a: 2 },
|
||||
source = {
|
||||
a: function (value) {
|
||||
return value > 1;
|
||||
},
|
||||
},
|
||||
par = conforms(source);
|
||||
|
||||
assert.strictEqual(par(object), true);
|
||||
|
||||
source.a = function (value) {
|
||||
return value < 2;
|
||||
};
|
||||
assert.strictEqual(par(object), true);
|
||||
});
|
||||
});
|
||||
@@ -1,12 +1,11 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { empties, _, falsey, stubTrue } from './utils';
|
||||
|
||||
describe('constant', () => {
|
||||
it('should create a function that returns `value`', () => {
|
||||
const object = { a: 1 },
|
||||
values = Array(2).concat(empties, true, 1, 'a'),
|
||||
constant = _.constant(object);
|
||||
const object = { a: 1 };
|
||||
const values = Array(2).concat(empties, true, 1, 'a');
|
||||
const constant = _.constant(object);
|
||||
|
||||
const results = lodashStable.map(values, (value, index) => {
|
||||
if (index < 2) {
|
||||
@@ -15,24 +14,24 @@ describe('constant', () => {
|
||||
return constant(value);
|
||||
});
|
||||
|
||||
assert.ok(lodashStable.every(results, (result) => result === object));
|
||||
expect(lodashStable.every(results, (result) => result === object));
|
||||
});
|
||||
|
||||
it('should work with falsey values', () => {
|
||||
const expected = lodashStable.map(falsey, stubTrue);
|
||||
|
||||
const actual = lodashStable.map(falsey, (value, index) => {
|
||||
const constant = index ? _.constant(value) : _.constant(),
|
||||
result = constant();
|
||||
const constant = index ? _.constant(value) : _.constant();
|
||||
const result = constant();
|
||||
|
||||
return result === value || (result !== result && value !== value);
|
||||
});
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should return a wrapped value when chaining', () => {
|
||||
const wrapped = _(true).constant();
|
||||
assert.ok(wrapped instanceof _);
|
||||
expect(wrapped instanceof _);
|
||||
});
|
||||
});
|
||||
53
test/countBy.spec.js
Normal file
53
test/countBy.spec.js
Normal file
@@ -0,0 +1,53 @@
|
||||
import lodashStable from 'lodash';
|
||||
import countBy from '../src/countBy';
|
||||
|
||||
describe('countBy', () => {
|
||||
const array = [6.1, 4.2, 6.3];
|
||||
|
||||
it('should transform keys by `iteratee`', () => {
|
||||
const actual = countBy(array, Math.floor);
|
||||
expect(actual).toEqual({ 4: 1, 6: 2 });
|
||||
});
|
||||
|
||||
it('should use `_.identity` when `iteratee` is nullish', () => {
|
||||
const array = [4, 6, 6];
|
||||
const values = [, null, undefined];
|
||||
const expected = lodashStable.map(values, lodashStable.constant({ 4: 1, 6: 2 }));
|
||||
|
||||
const actual = lodashStable.map(values, (value, index) =>
|
||||
index ? countBy(array, value) : countBy(array),
|
||||
);
|
||||
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should work with `_.property` shorthands', () => {
|
||||
const actual = countBy(['one', 'two', 'three'], 'length');
|
||||
expect(actual).toEqual({ 3: 2, 5: 1 });
|
||||
});
|
||||
|
||||
it('should only add values to own, not inherited, properties', () => {
|
||||
const actual = countBy(array, (n) =>
|
||||
Math.floor(n) > 4 ? 'hasOwnProperty' : 'constructor',
|
||||
);
|
||||
|
||||
expect(actual.constructor).toEqual(1);
|
||||
expect(actual.hasOwnProperty).toEqual(2);
|
||||
});
|
||||
|
||||
it('should work with a number for `iteratee`', () => {
|
||||
const array = [
|
||||
[1, 'a'],
|
||||
[2, 'a'],
|
||||
[2, 'b'],
|
||||
];
|
||||
|
||||
expect(countBy(array, 0)).toEqual({ 1: 1, 2: 2 });
|
||||
expect(countBy(array, 1)).toEqual({ a: 2, b: 1 });
|
||||
});
|
||||
|
||||
it('should work with an object for `collection`', () => {
|
||||
const actual = countBy({ a: 6.1, b: 4.2, c: 6.3 }, Math.floor);
|
||||
expect(actual).toEqual({ 4: 1, 6: 2 });
|
||||
});
|
||||
});
|
||||
@@ -1,68 +0,0 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { LARGE_ARRAY_SIZE } from './utils';
|
||||
import countBy from '../src/countBy';
|
||||
|
||||
describe('countBy', () => {
|
||||
const array = [6.1, 4.2, 6.3];
|
||||
|
||||
it('should transform keys by `iteratee`', () => {
|
||||
const actual = countBy(array, Math.floor);
|
||||
assert.deepStrictEqual(actual, { '4': 1, '6': 2 });
|
||||
});
|
||||
|
||||
it('should use `_.identity` when `iteratee` is nullish', () => {
|
||||
const array = [4, 6, 6],
|
||||
values = [, null, undefined],
|
||||
expected = lodashStable.map(values, lodashStable.constant({ '4': 1, '6': 2 }));
|
||||
|
||||
const actual = lodashStable.map(values, (value, index) =>
|
||||
index ? countBy(array, value) : countBy(array),
|
||||
);
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
});
|
||||
|
||||
it('should work with `_.property` shorthands', () => {
|
||||
const actual = countBy(['one', 'two', 'three'], 'length');
|
||||
assert.deepStrictEqual(actual, { '3': 2, '5': 1 });
|
||||
});
|
||||
|
||||
it('should only add values to own, not inherited, properties', () => {
|
||||
const actual = countBy(array, (n) =>
|
||||
Math.floor(n) > 4 ? 'hasOwnProperty' : 'constructor',
|
||||
);
|
||||
|
||||
assert.deepStrictEqual(actual.constructor, 1);
|
||||
assert.deepStrictEqual(actual.hasOwnProperty, 2);
|
||||
});
|
||||
|
||||
it('should work with a number for `iteratee`', () => {
|
||||
const array = [
|
||||
[1, 'a'],
|
||||
[2, 'a'],
|
||||
[2, 'b'],
|
||||
];
|
||||
|
||||
assert.deepStrictEqual(countBy(array, 0), { '1': 1, '2': 2 });
|
||||
assert.deepStrictEqual(countBy(array, 1), { a: 2, b: 1 });
|
||||
});
|
||||
|
||||
it('should work with an object for `collection`', () => {
|
||||
const actual = countBy({ a: 6.1, b: 4.2, c: 6.3 }, Math.floor);
|
||||
assert.deepStrictEqual(actual, { '4': 1, '6': 2 });
|
||||
});
|
||||
|
||||
it('should work in a lazy sequence', () => {
|
||||
const array = lodashStable
|
||||
.range(LARGE_ARRAY_SIZE)
|
||||
.concat(
|
||||
lodashStable.range(Math.floor(LARGE_ARRAY_SIZE / 2), LARGE_ARRAY_SIZE),
|
||||
lodashStable.range(Math.floor(LARGE_ARRAY_SIZE / 1.5), LARGE_ARRAY_SIZE),
|
||||
);
|
||||
|
||||
const actual = _(array).countBy().map(square).filter(isEven).take().value();
|
||||
|
||||
assert.deepEqual(actual, _.take(_.filter(_.map(countBy(array), square), isEven)));
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,3 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { falsey, primitives, stubTrue } from './utils';
|
||||
import create from '../src/create';
|
||||
@@ -20,8 +19,8 @@ describe('create', () => {
|
||||
|
||||
const actual = new Circle();
|
||||
|
||||
assert.ok(actual instanceof Circle);
|
||||
assert.ok(actual instanceof Shape);
|
||||
expect(actual instanceof Circle);
|
||||
expect(actual instanceof Shape);
|
||||
assert.notStrictEqual(Circle.prototype, Shape.prototype);
|
||||
});
|
||||
|
||||
@@ -32,11 +31,11 @@ describe('create', () => {
|
||||
|
||||
const actual = new Circle();
|
||||
|
||||
assert.ok(actual instanceof Circle);
|
||||
assert.ok(actual instanceof Shape);
|
||||
assert.deepStrictEqual(Object.keys(Circle.prototype), properties);
|
||||
expect(actual instanceof Circle);
|
||||
expect(actual instanceof Shape);
|
||||
expect(Object.keys(Circle.prototype)).toEqual(properties);
|
||||
properties.forEach((property) => {
|
||||
assert.strictEqual(Circle.prototype[property], expected[property]);
|
||||
expect(Circle.prototype[property]).toBe(expected[property]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -51,9 +50,9 @@ describe('create', () => {
|
||||
const expected = { a: 1, c: 3 };
|
||||
const properties = Object.keys(expected);
|
||||
|
||||
assert.deepStrictEqual(Object.keys(actual), properties);
|
||||
expect(Object.keys(actual)).toEqual(properties);
|
||||
properties.forEach((property) => {
|
||||
assert.strictEqual(actual[property], expected[property]);
|
||||
expect(actual[property]).toBe(expected[property]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -62,7 +61,7 @@ describe('create', () => {
|
||||
this.a = 1;
|
||||
}
|
||||
const object = create(new Foo(), { a: 1 });
|
||||
assert.deepStrictEqual(lodashStable.keys(object), ['a']);
|
||||
expect(lodashStable.keys(object)).toEqual(['a']);
|
||||
});
|
||||
|
||||
it('should accept a falsey `prototype`', () => {
|
||||
@@ -71,7 +70,7 @@ describe('create', () => {
|
||||
);
|
||||
|
||||
actual.forEach((value) => {
|
||||
assert.ok(lodashStable.isObject(value));
|
||||
expect(lodashStable.isObject(value));
|
||||
});
|
||||
});
|
||||
|
||||
@@ -81,20 +80,20 @@ describe('create', () => {
|
||||
);
|
||||
|
||||
actual.forEach((value) => {
|
||||
assert.ok(lodashStable.isObject(value));
|
||||
expect(lodashStable.isObject(value));
|
||||
});
|
||||
});
|
||||
|
||||
it('should work as an iteratee for methods like `_.map`', () => {
|
||||
const array = [{ a: 1 }, { a: 1 }, { a: 1 }],
|
||||
expected = lodashStable.map(array, stubTrue),
|
||||
objects = lodashStable.map(array, create);
|
||||
const array = [{ a: 1 }, { a: 1 }, { a: 1 }];
|
||||
const expected = lodashStable.map(array, stubTrue);
|
||||
const objects = lodashStable.map(array, create);
|
||||
|
||||
const actual = lodashStable.map(
|
||||
objects,
|
||||
(object) => object.a === 1 && !keys(object).length,
|
||||
);
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
});
|
||||
@@ -1,15 +1,14 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { _, slice } from './utils';
|
||||
import curry from '../src/curry';
|
||||
|
||||
describe('curry methods', () => {
|
||||
lodashStable.each(['curry', 'curryRight'], (methodName) => {
|
||||
const func = _[methodName],
|
||||
fn = function (a, b) {
|
||||
return slice.call(arguments);
|
||||
},
|
||||
isCurry = methodName === 'curry';
|
||||
const func = _[methodName];
|
||||
const fn = function (a, b) {
|
||||
return slice.call(arguments);
|
||||
};
|
||||
const isCurry = methodName === 'curry';
|
||||
|
||||
it(`\`_.${methodName}\` should not error on functions with the same name as lodash methods`, () => {
|
||||
function run(a, b) {
|
||||
@@ -22,7 +21,7 @@ describe('curry methods', () => {
|
||||
var actual = curried(1)(2);
|
||||
} catch (e) {}
|
||||
|
||||
assert.strictEqual(actual, 3);
|
||||
expect(actual).toBe(3);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should work for function names that shadow those on \`Object.prototype\``, () => {
|
||||
@@ -30,23 +29,23 @@ describe('curry methods', () => {
|
||||
|
||||
const expected = [1, 2, 3];
|
||||
|
||||
assert.deepStrictEqual(curried(1)(2)(3), expected);
|
||||
expect(curried(1)(2)(3)).toEqual(expected);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should work as an iteratee for methods like \`_.map\``, () => {
|
||||
const array = [fn, fn, fn],
|
||||
object = { a: fn, b: fn, c: fn };
|
||||
const array = [fn, fn, fn];
|
||||
const object = { a: fn, b: fn, c: fn };
|
||||
|
||||
lodashStable.each([array, object], (collection) => {
|
||||
const curries = lodashStable.map(collection, func),
|
||||
expected = lodashStable.map(
|
||||
collection,
|
||||
lodashStable.constant(isCurry ? ['a', 'b'] : ['b', 'a']),
|
||||
);
|
||||
const curries = lodashStable.map(collection, func);
|
||||
const expected = lodashStable.map(
|
||||
collection,
|
||||
lodashStable.constant(isCurry ? ['a', 'b'] : ['b', 'a']),
|
||||
);
|
||||
|
||||
const actual = lodashStable.map(curries, (curried) => curried('a')('b'));
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,3 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { slice, stubArray } from './utils';
|
||||
import curry from '../src/curry';
|
||||
@@ -16,18 +15,18 @@ describe('curry', () => {
|
||||
const curried = curry(fn),
|
||||
expected = [1, 2, 3, 4];
|
||||
|
||||
assert.deepStrictEqual(curried(1)(2)(3)(4), expected);
|
||||
assert.deepStrictEqual(curried(1, 2)(3, 4), expected);
|
||||
assert.deepStrictEqual(curried(1, 2, 3, 4), expected);
|
||||
expect(curried(1)(2)(3)(4)).toEqual(expected);
|
||||
expect(curried(1, 2)(3, 4)).toEqual(expected);
|
||||
expect(curried(1, 2, 3, 4)).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should allow specifying `arity`', () => {
|
||||
const curried = curry(fn, 3),
|
||||
expected = [1, 2, 3];
|
||||
|
||||
assert.deepStrictEqual(curried(1)(2, 3), expected);
|
||||
assert.deepStrictEqual(curried(1, 2)(3), expected);
|
||||
assert.deepStrictEqual(curried(1, 2, 3), expected);
|
||||
expect(curried(1)(2, 3)).toEqual(expected);
|
||||
expect(curried(1, 2)(3)).toEqual(expected);
|
||||
expect(curried(1, 2, 3)).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should coerce `arity` to an integer', () => {
|
||||
@@ -36,18 +35,18 @@ describe('curry', () => {
|
||||
|
||||
const actual = lodashStable.map(values, (arity) => curry(fn, arity)());
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
assert.deepStrictEqual(curry(fn, '2')(1)(2), [1, 2]);
|
||||
expect(actual).toEqual(expected);
|
||||
expect(curry(fn, '2')(1)(2), [1).toEqual(2]);
|
||||
});
|
||||
|
||||
it('should support placeholders', () => {
|
||||
const curried = curry(fn),
|
||||
ph = curried.placeholder;
|
||||
|
||||
assert.deepStrictEqual(curried(1)(ph, 3)(ph, 4)(2), [1, 2, 3, 4]);
|
||||
assert.deepStrictEqual(curried(ph, 2)(1)(ph, 4)(3), [1, 2, 3, 4]);
|
||||
assert.deepStrictEqual(curried(ph, ph, 3)(ph, 2)(ph, 4)(1), [1, 2, 3, 4]);
|
||||
assert.deepStrictEqual(curried(ph, ph, ph, 4)(ph, ph, 3)(ph, 2)(1), [1, 2, 3, 4]);
|
||||
expect(curried(1)(ph, 3)(ph, 4)(2), [1, 2, 3).toEqual(4]);
|
||||
expect(curried(ph, 2)(1)(ph, 4)(3), [1, 2, 3).toEqual(4]);
|
||||
expect(curried(ph, ph, 3)(ph, 2)(ph, 4)(1), [1, 2, 3).toEqual(4]);
|
||||
expect(curried(ph, ph, ph, 4)(ph, ph, 3)(ph, 2)(1), [1, 2, 3).toEqual(4]);
|
||||
});
|
||||
|
||||
it('should persist placeholders', () => {
|
||||
@@ -55,7 +54,7 @@ describe('curry', () => {
|
||||
ph = curried.placeholder,
|
||||
actual = curried(ph, ph, ph, 'd')('a')(ph)('b')('c');
|
||||
|
||||
assert.deepStrictEqual(actual, ['a', 'b', 'c', 'd']);
|
||||
expect(actual, ['a', 'b', 'c').toEqual('d']);
|
||||
});
|
||||
|
||||
it('should use `_.placeholder` when set', () => {
|
||||
@@ -63,23 +62,23 @@ describe('curry', () => {
|
||||
_ph = (placeholder = {}),
|
||||
ph = curried.placeholder;
|
||||
|
||||
assert.deepEqual(curried(1)(_ph, 3)(ph, 4), [1, ph, 3, 4]);
|
||||
expect(curried(1)(_ph, 3)(ph, 4), [1, ph, 3).toEqual(4]);
|
||||
delete placeholder;
|
||||
});
|
||||
|
||||
it('should provide additional arguments after reaching the target arity', () => {
|
||||
const curried = curry(fn, 3);
|
||||
assert.deepStrictEqual(curried(1)(2, 3, 4), [1, 2, 3, 4]);
|
||||
assert.deepStrictEqual(curried(1, 2)(3, 4, 5), [1, 2, 3, 4, 5]);
|
||||
assert.deepStrictEqual(curried(1, 2, 3, 4, 5, 6), [1, 2, 3, 4, 5, 6]);
|
||||
expect(curried(1)(2, 3, 4), [1, 2, 3).toEqual(4]);
|
||||
expect(curried(1, 2)(3, 4, 5), [1, 2, 3, 4).toEqual(5]);
|
||||
expect(curried(1, 2, 3, 4, 5, 6), [1, 2, 3, 4, 5).toEqual(6]);
|
||||
});
|
||||
|
||||
it('should create a function with a `length` of `0`', () => {
|
||||
lodashStable.times(2, (index) => {
|
||||
const curried = index ? curry(fn, 4) : curry(fn);
|
||||
assert.strictEqual(curried.length, 0);
|
||||
assert.strictEqual(curried(1).length, 0);
|
||||
assert.strictEqual(curried(1, 2).length, 0);
|
||||
expect(curried.length).toBe(0);
|
||||
expect(curried(1).length).toBe(0);
|
||||
expect(curried(1, 2).length).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -91,8 +90,8 @@ describe('curry', () => {
|
||||
var curried = curry(Foo),
|
||||
object = {};
|
||||
|
||||
assert.ok(new curried(false) instanceof Foo);
|
||||
assert.strictEqual(new curried(true), object);
|
||||
expect(new curried(false) instanceof Foo)
|
||||
expect(new curried(true)).toBe(object);
|
||||
});
|
||||
|
||||
it('should use `this` binding of function', () => {
|
||||
@@ -104,18 +103,18 @@ describe('curry', () => {
|
||||
const object = { a: 1, b: 2, c: 3 },
|
||||
expected = [1, 2, 3];
|
||||
|
||||
assert.deepStrictEqual(curry(bind(fn, object), 3)('a')('b')('c'), expected);
|
||||
assert.deepStrictEqual(curry(bind(fn, object), 3)('a', 'b')('c'), expected);
|
||||
assert.deepStrictEqual(curry(bind(fn, object), 3)('a', 'b', 'c'), expected);
|
||||
expect(curry(bind(fn, object), 3)('a')('b')('c')).toEqual(expected);
|
||||
expect(curry(bind(fn, object), 3)('a', 'b')('c')).toEqual(expected);
|
||||
expect(curry(bind(fn, object), 3)('a', 'b', 'c')).toEqual(expected);
|
||||
|
||||
assert.deepStrictEqual(bind(curry(fn), object)('a')('b')('c'), Array(3));
|
||||
assert.deepStrictEqual(bind(curry(fn), object)('a', 'b')('c'), Array(3));
|
||||
assert.deepStrictEqual(bind(curry(fn), object)('a', 'b', 'c'), expected);
|
||||
expect(bind(curry(fn), object)('a')('b')('c')).toEqual(Array(3));
|
||||
expect(bind(curry(fn), object)('a', 'b')('c')).toEqual(Array(3));
|
||||
expect(bind(curry(fn), object)('a', 'b', 'c')).toEqual(expected);
|
||||
|
||||
object.curried = curry(fn);
|
||||
assert.deepStrictEqual(object.curried('a')('b')('c'), Array(3));
|
||||
assert.deepStrictEqual(object.curried('a', 'b')('c'), Array(3));
|
||||
assert.deepStrictEqual(object.curried('a', 'b', 'c'), expected);
|
||||
expect(object.curried('a')('b')('c')).toEqual(Array(3));
|
||||
expect(object.curried('a', 'b')('c')).toEqual(Array(3));
|
||||
expect(object.curried('a', 'b', 'c')).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should work with partialed methods', () => {
|
||||
@@ -127,7 +126,7 @@ describe('curry', () => {
|
||||
c = partialRight(b, 4),
|
||||
d = partialRight(b(3), 4);
|
||||
|
||||
assert.deepStrictEqual(c(3), expected);
|
||||
assert.deepStrictEqual(d(), expected);
|
||||
expect(c(3)).toEqual(expected);
|
||||
expect(d()).toEqual(expected);
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,3 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { slice, stubArray } from './utils';
|
||||
import curryRight from '../src/curryRight';
|
||||
@@ -16,18 +15,18 @@ describe('curryRight', () => {
|
||||
const curried = curryRight(fn),
|
||||
expected = [1, 2, 3, 4];
|
||||
|
||||
assert.deepStrictEqual(curried(4)(3)(2)(1), expected);
|
||||
assert.deepStrictEqual(curried(3, 4)(1, 2), expected);
|
||||
assert.deepStrictEqual(curried(1, 2, 3, 4), expected);
|
||||
expect(curried(4)(3)(2)(1)).toEqual(expected);
|
||||
expect(curried(3, 4)(1, 2)).toEqual(expected);
|
||||
expect(curried(1, 2, 3, 4)).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should allow specifying `arity`', () => {
|
||||
const curried = curryRight(fn, 3),
|
||||
expected = [1, 2, 3];
|
||||
|
||||
assert.deepStrictEqual(curried(3)(1, 2), expected);
|
||||
assert.deepStrictEqual(curried(2, 3)(1), expected);
|
||||
assert.deepStrictEqual(curried(1, 2, 3), expected);
|
||||
expect(curried(3)(1, 2)).toEqual(expected);
|
||||
expect(curried(2, 3)(1)).toEqual(expected);
|
||||
expect(curried(1, 2, 3)).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should coerce `arity` to an integer', () => {
|
||||
@@ -36,8 +35,8 @@ describe('curryRight', () => {
|
||||
|
||||
const actual = lodashStable.map(values, (arity) => curryRight(fn, arity)());
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
assert.deepStrictEqual(curryRight(fn, '2')(1)(2), [2, 1]);
|
||||
expect(actual).toEqual(expected);
|
||||
expect(curryRight(fn, '2')(1)(2), [2).toEqual(1]);
|
||||
});
|
||||
|
||||
it('should support placeholders', () => {
|
||||
@@ -45,10 +44,10 @@ describe('curryRight', () => {
|
||||
expected = [1, 2, 3, 4],
|
||||
ph = curried.placeholder;
|
||||
|
||||
assert.deepStrictEqual(curried(4)(2, ph)(1, ph)(3), expected);
|
||||
assert.deepStrictEqual(curried(3, ph)(4)(1, ph)(2), expected);
|
||||
assert.deepStrictEqual(curried(ph, ph, 4)(ph, 3)(ph, 2)(1), expected);
|
||||
assert.deepStrictEqual(curried(ph, ph, ph, 4)(ph, ph, 3)(ph, 2)(1), expected);
|
||||
expect(curried(4)(2, ph)(1, ph)(3)).toEqual(expected);
|
||||
expect(curried(3, ph)(4)(1, ph)(2)).toEqual(expected);
|
||||
expect(curried(ph, ph, 4)(ph, 3)(ph, 2)(1)).toEqual(expected);
|
||||
expect(curried(ph, ph, ph, 4)(ph, ph, 3)(ph, 2)(1)).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should persist placeholders', () => {
|
||||
@@ -56,7 +55,7 @@ describe('curryRight', () => {
|
||||
ph = curried.placeholder,
|
||||
actual = curried('a', ph, ph, ph)('b')(ph)('c')('d');
|
||||
|
||||
assert.deepStrictEqual(actual, ['a', 'b', 'c', 'd']);
|
||||
expect(actual, ['a', 'b', 'c').toEqual('d']);
|
||||
});
|
||||
|
||||
it('should use `_.placeholder` when set', () => {
|
||||
@@ -64,23 +63,23 @@ describe('curryRight', () => {
|
||||
_ph = (placeholder = {}),
|
||||
ph = curried.placeholder;
|
||||
|
||||
assert.deepEqual(curried(4)(2, _ph)(1, ph), [1, 2, ph, 4]);
|
||||
expect(curried(4)(2, _ph)(1, ph), [1, 2, ph).toEqual(4]);
|
||||
delete placeholder;
|
||||
});
|
||||
|
||||
it('should provide additional arguments after reaching the target arity', () => {
|
||||
const curried = curryRight(fn, 3);
|
||||
assert.deepStrictEqual(curried(4)(1, 2, 3), [1, 2, 3, 4]);
|
||||
assert.deepStrictEqual(curried(4, 5)(1, 2, 3), [1, 2, 3, 4, 5]);
|
||||
assert.deepStrictEqual(curried(1, 2, 3, 4, 5, 6), [1, 2, 3, 4, 5, 6]);
|
||||
expect(curried(4)(1, 2, 3), [1, 2, 3).toEqual(4]);
|
||||
expect(curried(4, 5)(1, 2, 3), [1, 2, 3, 4).toEqual(5]);
|
||||
expect(curried(1, 2, 3, 4, 5, 6), [1, 2, 3, 4, 5).toEqual(6]);
|
||||
});
|
||||
|
||||
it('should create a function with a `length` of `0`', () => {
|
||||
lodashStable.times(2, (index) => {
|
||||
const curried = index ? curryRight(fn, 4) : curryRight(fn);
|
||||
assert.strictEqual(curried.length, 0);
|
||||
assert.strictEqual(curried(4).length, 0);
|
||||
assert.strictEqual(curried(3, 4).length, 0);
|
||||
expect(curried.length).toBe(0);
|
||||
expect(curried(4).length).toBe(0);
|
||||
expect(curried(3, 4).length).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -92,8 +91,8 @@ describe('curryRight', () => {
|
||||
var curried = curryRight(Foo),
|
||||
object = {};
|
||||
|
||||
assert.ok(new curried(false) instanceof Foo);
|
||||
assert.strictEqual(new curried(true), object);
|
||||
expect(new curried(false) instanceof Foo)
|
||||
expect(new curried(true)).toBe(object);
|
||||
});
|
||||
|
||||
it('should use `this` binding of function', () => {
|
||||
@@ -105,18 +104,18 @@ describe('curryRight', () => {
|
||||
const object = { a: 1, b: 2, c: 3 },
|
||||
expected = [1, 2, 3];
|
||||
|
||||
assert.deepStrictEqual(curryRight(bind(fn, object), 3)('c')('b')('a'), expected);
|
||||
assert.deepStrictEqual(curryRight(bind(fn, object), 3)('b', 'c')('a'), expected);
|
||||
assert.deepStrictEqual(curryRight(bind(fn, object), 3)('a', 'b', 'c'), expected);
|
||||
expect(curryRight(bind(fn, object), 3)('c')('b')('a')).toEqual(expected);
|
||||
expect(curryRight(bind(fn, object), 3)('b', 'c')('a')).toEqual(expected);
|
||||
expect(curryRight(bind(fn, object), 3)('a', 'b', 'c')).toEqual(expected);
|
||||
|
||||
assert.deepStrictEqual(bind(curryRight(fn), object)('c')('b')('a'), Array(3));
|
||||
assert.deepStrictEqual(bind(curryRight(fn), object)('b', 'c')('a'), Array(3));
|
||||
assert.deepStrictEqual(bind(curryRight(fn), object)('a', 'b', 'c'), expected);
|
||||
expect(bind(curryRight(fn), object)('c')('b')('a')).toEqual(Array(3));
|
||||
expect(bind(curryRight(fn), object)('b', 'c')('a')).toEqual(Array(3));
|
||||
expect(bind(curryRight(fn), object)('a', 'b', 'c')).toEqual(expected);
|
||||
|
||||
object.curried = curryRight(fn);
|
||||
assert.deepStrictEqual(object.curried('c')('b')('a'), Array(3));
|
||||
assert.deepStrictEqual(object.curried('b', 'c')('a'), Array(3));
|
||||
assert.deepStrictEqual(object.curried('a', 'b', 'c'), expected);
|
||||
expect(object.curried('c')('b')('a')).toEqual(Array(3));
|
||||
expect(object.curried('b', 'c')('a')).toEqual(Array(3));
|
||||
expect(object.curried('a', 'b', 'c')).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should work with partialed methods', () => {
|
||||
@@ -128,7 +127,7 @@ describe('curryRight', () => {
|
||||
c = bind(b, null, 1),
|
||||
d = partial(b(2), 1);
|
||||
|
||||
assert.deepStrictEqual(c(2), expected);
|
||||
assert.deepStrictEqual(d(), expected);
|
||||
expect(c(2)).toEqual(expected);
|
||||
expect(d()).toEqual(expected);
|
||||
});
|
||||
});
|
||||
@@ -1,14 +1,13 @@
|
||||
import assert from 'node:assert';
|
||||
import partial from '../src/partial';
|
||||
import property from '../src/property';
|
||||
import iteratee from '../src/iteratee';
|
||||
|
||||
describe('custom `_.iteratee` methods', () => {
|
||||
var array = ['one', 'two', 'three'],
|
||||
getPropA = partial(property, 'a'),
|
||||
getPropB = partial(property, 'b'),
|
||||
getLength = partial(property, 'length'),
|
||||
iteratee = iteratee;
|
||||
const array = ['one', 'two', 'three'];
|
||||
const getPropA = partial(property, 'a');
|
||||
const getPropB = partial(property, 'b');
|
||||
const getLength = partial(property, 'length');
|
||||
var iteratee = iteratee;
|
||||
|
||||
const getSum = function () {
|
||||
return function (result, object) {
|
||||
@@ -24,31 +23,31 @@ describe('custom `_.iteratee` methods', () => {
|
||||
|
||||
it('`_.countBy` should use `_.iteratee` internally', () => {
|
||||
iteratee = getLength;
|
||||
assert.deepEqual(_.countBy(array), { '3': 2, '5': 1 });
|
||||
expect(_.countBy(array), { 3: 2).toEqual(5: 1 });
|
||||
iteratee = iteratee;
|
||||
});
|
||||
|
||||
it('`_.differenceBy` should use `_.iteratee` internally', () => {
|
||||
iteratee = getPropA;
|
||||
assert.deepEqual(_.differenceBy(objects, [objects[1]]), [objects[0]]);
|
||||
expect(_.differenceBy(objects, [objects[1]])).toEqual([objects[0]]);
|
||||
iteratee = iteratee;
|
||||
});
|
||||
|
||||
it('`_.dropRightWhile` should use `_.iteratee` internally', () => {
|
||||
iteratee = getPropB;
|
||||
assert.deepEqual(_.dropRightWhile(objects), objects.slice(0, 2));
|
||||
expect(_.dropRightWhile(objects), objects.slice(0).toEqual(2));
|
||||
iteratee = iteratee;
|
||||
});
|
||||
|
||||
it('`_.dropWhile` should use `_.iteratee` internally', () => {
|
||||
iteratee = getPropB;
|
||||
assert.deepEqual(_.dropWhile(objects.reverse()).reverse(), objects.reverse().slice(0, 2));
|
||||
expect(_.dropWhile(objects.reverse()).reverse(), objects.reverse().slice(0).toEqual(2));
|
||||
iteratee = iteratee;
|
||||
});
|
||||
|
||||
it('`_.every` should use `_.iteratee` internally', () => {
|
||||
iteratee = getPropA;
|
||||
assert.strictEqual(_.every(objects.slice(1)), true);
|
||||
expect(_.every(objects.slice(1))).toBe(true);
|
||||
iteratee = iteratee;
|
||||
});
|
||||
|
||||
@@ -56,97 +55,97 @@ describe('custom `_.iteratee` methods', () => {
|
||||
const objects = [{ a: 0 }, { a: 1 }];
|
||||
|
||||
iteratee = getPropA;
|
||||
assert.deepEqual(_.filter(objects), [objects[1]]);
|
||||
expect(_.filter(objects)).toEqual([objects[1]]);
|
||||
iteratee = iteratee;
|
||||
});
|
||||
|
||||
it('`_.find` should use `_.iteratee` internally', () => {
|
||||
iteratee = getPropA;
|
||||
assert.strictEqual(_.find(objects), objects[1]);
|
||||
expect(_.find(objects)).toBe(objects[1]);
|
||||
iteratee = iteratee;
|
||||
});
|
||||
|
||||
it('`_.findIndex` should use `_.iteratee` internally', () => {
|
||||
iteratee = getPropA;
|
||||
assert.strictEqual(_.findIndex(objects), 1);
|
||||
expect(_.findIndex(objects)).toBe(1);
|
||||
iteratee = iteratee;
|
||||
});
|
||||
|
||||
it('`_.findLast` should use `_.iteratee` internally', () => {
|
||||
iteratee = getPropA;
|
||||
assert.strictEqual(_.findLast(objects), objects[2]);
|
||||
expect(_.findLast(objects)).toBe(objects[2]);
|
||||
iteratee = iteratee;
|
||||
});
|
||||
|
||||
it('`_.findLastIndex` should use `_.iteratee` internally', () => {
|
||||
iteratee = getPropA;
|
||||
assert.strictEqual(_.findLastIndex(objects), 2);
|
||||
expect(_.findLastIndex(objects)).toBe(2);
|
||||
iteratee = iteratee;
|
||||
});
|
||||
|
||||
it('`_.findKey` should use `_.iteratee` internally', () => {
|
||||
iteratee = getPropB;
|
||||
assert.strictEqual(_.findKey(objects), '2');
|
||||
expect(_.findKey(objects)).toBe('2');
|
||||
iteratee = iteratee;
|
||||
});
|
||||
|
||||
it('`_.findLastKey` should use `_.iteratee` internally', () => {
|
||||
iteratee = getPropB;
|
||||
assert.strictEqual(_.findLastKey(objects), '2');
|
||||
expect(_.findLastKey(objects)).toBe('2');
|
||||
iteratee = iteratee;
|
||||
});
|
||||
|
||||
it('`_.groupBy` should use `_.iteratee` internally', () => {
|
||||
iteratee = getLength;
|
||||
assert.deepEqual(_.groupBy(array), { '3': ['one', 'two'], '5': ['three'] });
|
||||
expect(_.groupBy(array), { 3: ['one', 'two']).toEqual(5: ['three'] });
|
||||
iteratee = iteratee;
|
||||
});
|
||||
|
||||
it('`_.intersectionBy` should use `_.iteratee` internally', () => {
|
||||
iteratee = getPropA;
|
||||
assert.deepEqual(_.intersectionBy(objects, [objects[2]]), [objects[1]]);
|
||||
expect(_.intersectionBy(objects, [objects[2]])).toEqual([objects[1]]);
|
||||
iteratee = iteratee;
|
||||
});
|
||||
|
||||
it('`_.keyBy` should use `_.iteratee` internally', () => {
|
||||
iteratee = getLength;
|
||||
assert.deepEqual(_.keyBy(array), { '3': 'two', '5': 'three' });
|
||||
expect(_.keyBy(array), { 3: 'two').toEqual(5: 'three' });
|
||||
iteratee = iteratee;
|
||||
});
|
||||
|
||||
it('`_.map` should use `_.iteratee` internally', () => {
|
||||
iteratee = getPropA;
|
||||
assert.deepEqual(_.map(objects), [0, 1, 1]);
|
||||
expect(_.map(objects), [0, 1).toEqual(1]);
|
||||
iteratee = iteratee;
|
||||
});
|
||||
|
||||
it('`_.mapKeys` should use `_.iteratee` internally', () => {
|
||||
iteratee = getPropB;
|
||||
assert.deepEqual(_.mapKeys({ a: { b: 2 } }), { '2': { b: 2 } });
|
||||
expect(_.mapKeys({ a: { b: 2 } })).toEqual({ 2: { b: 2 } });
|
||||
iteratee = iteratee;
|
||||
});
|
||||
|
||||
it('`_.mapValues` should use `_.iteratee` internally', () => {
|
||||
iteratee = getPropB;
|
||||
assert.deepEqual(_.mapValues({ a: { b: 2 } }), { a: 2 });
|
||||
expect(_.mapValues({ a: { b: 2 } })).toEqual({ a: 2 });
|
||||
iteratee = iteratee;
|
||||
});
|
||||
|
||||
it('`_.maxBy` should use `_.iteratee` internally', () => {
|
||||
iteratee = getPropB;
|
||||
assert.deepEqual(_.maxBy(objects), objects[2]);
|
||||
expect(_.maxBy(objects)).toEqual(objects[2]);
|
||||
iteratee = iteratee;
|
||||
});
|
||||
|
||||
it('`_.meanBy` should use `_.iteratee` internally', () => {
|
||||
iteratee = getPropA;
|
||||
assert.strictEqual(_.meanBy(objects), 2 / 3);
|
||||
expect(_.meanBy(objects)).toBe(2 / 3);
|
||||
iteratee = iteratee;
|
||||
});
|
||||
|
||||
it('`_.minBy` should use `_.iteratee` internally', () => {
|
||||
iteratee = getPropB;
|
||||
assert.deepEqual(_.minBy(objects), objects[0]);
|
||||
expect(_.minBy(objects)).toEqual(objects[0]);
|
||||
iteratee = iteratee;
|
||||
});
|
||||
|
||||
@@ -154,25 +153,25 @@ describe('custom `_.iteratee` methods', () => {
|
||||
const objects = [{ a: 1 }, { a: 1 }, { b: 2 }];
|
||||
|
||||
iteratee = getPropA;
|
||||
assert.deepEqual(_.partition(objects), [objects.slice(0, 2), objects.slice(2)]);
|
||||
expect(_.partition(objects), [objects.slice(0, 2)).toEqual(objects.slice(2)]);
|
||||
iteratee = iteratee;
|
||||
});
|
||||
|
||||
it('`_.pullAllBy` should use `_.iteratee` internally', () => {
|
||||
iteratee = getPropA;
|
||||
assert.deepEqual(_.pullAllBy(objects.slice(), [{ a: 1, b: 0 }]), [objects[0]]);
|
||||
expect(_.pullAllBy(objects.slice(), [{ a: 1, b: 0 }])).toEqual([objects[0]]);
|
||||
iteratee = iteratee;
|
||||
});
|
||||
|
||||
it('`_.reduce` should use `_.iteratee` internally', () => {
|
||||
iteratee = getSum;
|
||||
assert.strictEqual(_.reduce(objects, undefined, 0), 2);
|
||||
expect(_.reduce(objects, undefined, 0)).toBe(2);
|
||||
iteratee = iteratee;
|
||||
});
|
||||
|
||||
it('`_.reduceRight` should use `_.iteratee` internally', () => {
|
||||
iteratee = getSum;
|
||||
assert.strictEqual(_.reduceRight(objects, undefined, 0), 2);
|
||||
expect(_.reduceRight(objects, undefined, 0)).toBe(2);
|
||||
iteratee = iteratee;
|
||||
});
|
||||
|
||||
@@ -180,7 +179,7 @@ describe('custom `_.iteratee` methods', () => {
|
||||
const objects = [{ a: 0 }, { a: 1 }];
|
||||
|
||||
iteratee = getPropA;
|
||||
assert.deepEqual(_.reject(objects), [objects[0]]);
|
||||
expect(_.reject(objects)).toEqual([objects[0]]);
|
||||
iteratee = iteratee;
|
||||
});
|
||||
|
||||
@@ -189,19 +188,19 @@ describe('custom `_.iteratee` methods', () => {
|
||||
|
||||
iteratee = getPropA;
|
||||
_.remove(objects);
|
||||
assert.deepEqual(objects, [{ a: 0 }]);
|
||||
expect(objects).toEqual([{ a: 0 }]);
|
||||
iteratee = iteratee;
|
||||
});
|
||||
|
||||
it('`_.some` should use `_.iteratee` internally', () => {
|
||||
iteratee = getPropB;
|
||||
assert.strictEqual(_.some(objects), true);
|
||||
expect(_.some(objects)).toBe(true);
|
||||
iteratee = iteratee;
|
||||
});
|
||||
|
||||
it('`_.sortBy` should use `_.iteratee` internally', () => {
|
||||
iteratee = getPropA;
|
||||
assert.deepEqual(_.sortBy(objects.slice().reverse()), [objects[0], objects[2], objects[1]]);
|
||||
expect(_.sortBy(objects.slice().reverse()), [objects[0], objects[2]).toEqual(objects[1]]);
|
||||
iteratee = iteratee;
|
||||
});
|
||||
|
||||
@@ -209,7 +208,7 @@ describe('custom `_.iteratee` methods', () => {
|
||||
const objects = [{ a: 30 }, { a: 50 }];
|
||||
|
||||
iteratee = getPropA;
|
||||
assert.strictEqual(_.sortedIndexBy(objects, { a: 40 }), 1);
|
||||
expect(_.sortedIndexBy(objects, { a: 40 })).toBe(1);
|
||||
iteratee = iteratee;
|
||||
});
|
||||
|
||||
@@ -217,25 +216,25 @@ describe('custom `_.iteratee` methods', () => {
|
||||
const objects = [{ a: 30 }, { a: 50 }];
|
||||
|
||||
iteratee = getPropA;
|
||||
assert.strictEqual(_.sortedLastIndexBy(objects, { a: 40 }), 1);
|
||||
expect(_.sortedLastIndexBy(objects, { a: 40 })).toBe(1);
|
||||
iteratee = iteratee;
|
||||
});
|
||||
|
||||
it('`_.sumBy` should use `_.iteratee` internally', () => {
|
||||
iteratee = getPropB;
|
||||
assert.strictEqual(_.sumBy(objects), 1);
|
||||
expect(_.sumBy(objects)).toBe(1);
|
||||
iteratee = iteratee;
|
||||
});
|
||||
|
||||
it('`_.takeRightWhile` should use `_.iteratee` internally', () => {
|
||||
iteratee = getPropB;
|
||||
assert.deepEqual(_.takeRightWhile(objects), objects.slice(2));
|
||||
expect(_.takeRightWhile(objects)).toEqual(objects.slice(2));
|
||||
iteratee = iteratee;
|
||||
});
|
||||
|
||||
it('`_.takeWhile` should use `_.iteratee` internally', () => {
|
||||
iteratee = getPropB;
|
||||
assert.deepEqual(_.takeWhile(objects.reverse()), objects.reverse().slice(2));
|
||||
expect(_.takeWhile(objects.reverse())).toEqual(objects.reverse().slice(2));
|
||||
iteratee = iteratee;
|
||||
});
|
||||
|
||||
@@ -246,25 +245,25 @@ describe('custom `_.iteratee` methods', () => {
|
||||
};
|
||||
};
|
||||
|
||||
assert.deepEqual(_.transform(objects, undefined, { sum: 0 }), { sum: 2 });
|
||||
expect(_.transform(objects, undefined, { sum: 0 })).toEqual({ sum: 2 });
|
||||
iteratee = iteratee;
|
||||
});
|
||||
|
||||
it('`_.uniqBy` should use `_.iteratee` internally', () => {
|
||||
iteratee = getPropB;
|
||||
assert.deepEqual(_.uniqBy(objects), [objects[0], objects[2]]);
|
||||
expect(_.uniqBy(objects), [objects[0]).toEqual(objects[2]]);
|
||||
iteratee = iteratee;
|
||||
});
|
||||
|
||||
it('`_.unionBy` should use `_.iteratee` internally', () => {
|
||||
iteratee = getPropB;
|
||||
assert.deepEqual(_.unionBy(objects.slice(0, 1), [objects[2]]), [objects[0], objects[2]]);
|
||||
expect(_.unionBy(objects.slice(0, 1), [objects[2]]), [objects[0]).toEqual(objects[2]]);
|
||||
iteratee = iteratee;
|
||||
});
|
||||
|
||||
it('`_.xorBy` should use `_.iteratee` internally', () => {
|
||||
iteratee = getPropA;
|
||||
assert.deepEqual(_.xorBy(objects, objects.slice(1)), [objects[0]]);
|
||||
expect(_.xorBy(objects, objects.slice(1))).toEqual([objects[0]]);
|
||||
iteratee = iteratee;
|
||||
});
|
||||
});
|
||||
@@ -1,57 +1,55 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { _, noop, push, isModularize } from './utils';
|
||||
import runInContext from '../src/runInContext';
|
||||
|
||||
describe('debounce and throttle', () => {
|
||||
lodashStable.each(['debounce', 'throttle'], (methodName) => {
|
||||
const func = _[methodName],
|
||||
isDebounce = methodName === 'debounce';
|
||||
const func = _[methodName];
|
||||
const isDebounce = methodName === 'debounce';
|
||||
|
||||
it(`\`_.${methodName}\` should not error for non-object \`options\` values`, () => {
|
||||
func(noop, 32, 1);
|
||||
assert.ok(true);
|
||||
expect(true);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should use a default \`wait\` of \`0\``, (done) => {
|
||||
let callCount = 0,
|
||||
funced = func(() => {
|
||||
callCount++;
|
||||
});
|
||||
let callCount = 0;
|
||||
const funced = func(() => {
|
||||
callCount++;
|
||||
});
|
||||
|
||||
funced();
|
||||
|
||||
setTimeout(() => {
|
||||
funced();
|
||||
assert.strictEqual(callCount, isDebounce ? 1 : 2);
|
||||
expect(callCount).toBe(isDebounce ? 1 : 2);
|
||||
done();
|
||||
}, 32);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should invoke \`func\` with the correct \`this\` binding`, (done) => {
|
||||
const actual = [],
|
||||
object = {
|
||||
funced: func(function () {
|
||||
actual.push(this);
|
||||
}, 32),
|
||||
},
|
||||
expected = lodashStable.times(isDebounce ? 1 : 2, lodashStable.constant(object));
|
||||
const actual = [];
|
||||
const object = {
|
||||
funced: func(function () {
|
||||
actual.push(this);
|
||||
}, 32),
|
||||
};
|
||||
const expected = lodashStable.times(isDebounce ? 1 : 2, lodashStable.constant(object));
|
||||
|
||||
object.funced();
|
||||
if (!isDebounce) {
|
||||
object.funced();
|
||||
}
|
||||
setTimeout(() => {
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
done();
|
||||
}, 64);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` supports recursive calls`, (done) => {
|
||||
const actual = [],
|
||||
args = lodashStable.map(['a', 'b', 'c'], (chr) => [{}, chr]),
|
||||
expected = args.slice(),
|
||||
queue = args.slice();
|
||||
const actual = [];
|
||||
const args = lodashStable.map(['a', 'b', 'c'], (chr) => [{}, chr]);
|
||||
const expected = args.slice();
|
||||
const queue = args.slice();
|
||||
|
||||
var funced = func(function () {
|
||||
const current = [this];
|
||||
@@ -66,18 +64,18 @@ describe('debounce and throttle', () => {
|
||||
|
||||
const next = queue.shift();
|
||||
funced.call(next[0], next[1]);
|
||||
assert.deepStrictEqual(actual, expected.slice(0, isDebounce ? 0 : 1));
|
||||
expect(actual, expected.slice(0).toEqual(isDebounce ? 0 : 1));
|
||||
|
||||
setTimeout(() => {
|
||||
assert.deepStrictEqual(actual, expected.slice(0, actual.length));
|
||||
expect(actual, expected.slice(0).toEqual(actual.length));
|
||||
done();
|
||||
}, 256);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should work if the system time is set backwards`, (done) => {
|
||||
xit(`\`_.${methodName}\` should work if the system time is set backwards`, (done) => {
|
||||
if (!isModularize) {
|
||||
let callCount = 0,
|
||||
dateCount = 0;
|
||||
let callCount = 0;
|
||||
let dateCount = 0;
|
||||
|
||||
const lodash = runInContext({
|
||||
Date: {
|
||||
@@ -97,7 +95,7 @@ describe('debounce and throttle', () => {
|
||||
|
||||
setTimeout(() => {
|
||||
funced();
|
||||
assert.strictEqual(callCount, isDebounce ? 1 : 2);
|
||||
expect(callCount).toBe(isDebounce ? 1 : 2);
|
||||
done();
|
||||
}, 64);
|
||||
} else {
|
||||
@@ -120,7 +118,7 @@ describe('debounce and throttle', () => {
|
||||
funced.cancel();
|
||||
|
||||
setTimeout(() => {
|
||||
assert.strictEqual(callCount, 0);
|
||||
expect(callCount).toBe(0);
|
||||
done();
|
||||
}, 64);
|
||||
});
|
||||
@@ -130,14 +128,14 @@ describe('debounce and throttle', () => {
|
||||
|
||||
const funced = func(() => ++callCount, 32, { leading: true });
|
||||
|
||||
assert.strictEqual(funced(), 1);
|
||||
expect(funced()).toBe(1);
|
||||
funced.cancel();
|
||||
|
||||
assert.strictEqual(funced(), 2);
|
||||
expect(funced()).toBe(2);
|
||||
funced();
|
||||
|
||||
setTimeout(() => {
|
||||
assert.strictEqual(callCount, 3);
|
||||
expect(callCount).toBe(3);
|
||||
done();
|
||||
}, 64);
|
||||
});
|
||||
@@ -148,25 +146,25 @@ describe('debounce and throttle', () => {
|
||||
const funced = func(() => ++callCount, 32, { leading: false });
|
||||
|
||||
funced();
|
||||
assert.strictEqual(funced.flush(), 1);
|
||||
expect(funced.flush()).toBe(1);
|
||||
|
||||
setTimeout(() => {
|
||||
assert.strictEqual(callCount, 1);
|
||||
expect(callCount).toBe(1);
|
||||
done();
|
||||
}, 64);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should noop \`cancel\` and \`flush\` when nothing is queued`, (done) => {
|
||||
let callCount = 0,
|
||||
funced = func(() => {
|
||||
callCount++;
|
||||
}, 32);
|
||||
let callCount = 0;
|
||||
const funced = func(() => {
|
||||
callCount++;
|
||||
}, 32);
|
||||
|
||||
funced.cancel();
|
||||
assert.strictEqual(funced.flush(), undefined);
|
||||
expect(funced.flush()).toBe(undefined);
|
||||
|
||||
setTimeout(() => {
|
||||
assert.strictEqual(callCount, 0);
|
||||
expect(callCount).toBe(0);
|
||||
done();
|
||||
}, 64);
|
||||
});
|
||||
@@ -1,4 +1,3 @@
|
||||
import assert from 'node:assert';
|
||||
import { identity, argv, isPhantom, push } from './utils';
|
||||
import debounce from '../src/debounce';
|
||||
|
||||
@@ -12,19 +11,19 @@ describe('debounce', () => {
|
||||
}, 32);
|
||||
|
||||
const results = [debounced('a'), debounced('b'), debounced('c')];
|
||||
assert.deepStrictEqual(results, [undefined, undefined, undefined]);
|
||||
assert.strictEqual(callCount, 0);
|
||||
expect(results, [undefined, undefined).toEqual(undefined]);
|
||||
expect(callCount).toBe(0);
|
||||
|
||||
setTimeout(() => {
|
||||
assert.strictEqual(callCount, 1);
|
||||
expect(callCount).toBe(1);
|
||||
|
||||
const results = [debounced('d'), debounced('e'), debounced('f')];
|
||||
assert.deepStrictEqual(results, ['c', 'c', 'c']);
|
||||
assert.strictEqual(callCount, 1);
|
||||
expect(results, ['c', 'c').toEqual('c']);
|
||||
expect(callCount).toBe(1);
|
||||
}, 128);
|
||||
|
||||
setTimeout(() => {
|
||||
assert.strictEqual(callCount, 2);
|
||||
expect(callCount).toBe(2);
|
||||
done();
|
||||
}, 256);
|
||||
});
|
||||
@@ -44,36 +43,36 @@ describe('debounce', () => {
|
||||
});
|
||||
|
||||
it('should not immediately call `func` when `wait` is `0`', (done) => {
|
||||
let callCount = 0,
|
||||
debounced = debounce(() => {
|
||||
++callCount;
|
||||
}, 0);
|
||||
let callCount = 0;
|
||||
const debounced = debounce(() => {
|
||||
++callCount;
|
||||
}, 0);
|
||||
|
||||
debounced();
|
||||
debounced();
|
||||
assert.strictEqual(callCount, 0);
|
||||
expect(callCount).toBe(0);
|
||||
|
||||
setTimeout(() => {
|
||||
assert.strictEqual(callCount, 1);
|
||||
expect(callCount).toBe(1);
|
||||
done();
|
||||
}, 5);
|
||||
});
|
||||
|
||||
it('should apply default options', (done) => {
|
||||
let callCount = 0,
|
||||
debounced = debounce(
|
||||
() => {
|
||||
callCount++;
|
||||
},
|
||||
32,
|
||||
{},
|
||||
);
|
||||
let callCount = 0;
|
||||
const debounced = debounce(
|
||||
() => {
|
||||
callCount++;
|
||||
},
|
||||
32,
|
||||
{},
|
||||
);
|
||||
|
||||
debounced();
|
||||
assert.strictEqual(callCount, 0);
|
||||
expect(callCount).toBe(0);
|
||||
|
||||
setTimeout(() => {
|
||||
assert.strictEqual(callCount, 1);
|
||||
expect(callCount).toBe(1);
|
||||
done();
|
||||
}, 64);
|
||||
});
|
||||
@@ -98,38 +97,38 @@ describe('debounce', () => {
|
||||
);
|
||||
|
||||
withLeading();
|
||||
assert.strictEqual(callCounts[0], 1);
|
||||
expect(callCounts[0]).toBe(1);
|
||||
|
||||
withLeadingAndTrailing();
|
||||
withLeadingAndTrailing();
|
||||
assert.strictEqual(callCounts[1], 1);
|
||||
expect(callCounts[1]).toBe(1);
|
||||
|
||||
setTimeout(() => {
|
||||
assert.deepStrictEqual(callCounts, [1, 2]);
|
||||
expect(callCounts, [1).toEqual(2]);
|
||||
|
||||
withLeading();
|
||||
assert.strictEqual(callCounts[0], 2);
|
||||
expect(callCounts[0]).toBe(2);
|
||||
|
||||
done();
|
||||
}, 64);
|
||||
});
|
||||
|
||||
it('subsequent leading debounced calls return the last `func` result', (done) => {
|
||||
const debounced = debounce(identity, 32, { leading: true, trailing: false }),
|
||||
results = [debounced('a'), debounced('b')];
|
||||
const debounced = debounce(identity, 32, { leading: true, trailing: false });
|
||||
const results = [debounced('a'), debounced('b')];
|
||||
|
||||
assert.deepStrictEqual(results, ['a', 'a']);
|
||||
expect(results, ['a').toEqual('a']);
|
||||
|
||||
setTimeout(() => {
|
||||
const results = [debounced('c'), debounced('d')];
|
||||
assert.deepStrictEqual(results, ['c', 'c']);
|
||||
expect(results, ['c').toEqual('c']);
|
||||
done();
|
||||
}, 64);
|
||||
});
|
||||
|
||||
it('should support a `trailing` option', (done) => {
|
||||
let withCount = 0,
|
||||
withoutCount = 0;
|
||||
let withCount = 0;
|
||||
let withoutCount = 0;
|
||||
|
||||
const withTrailing = debounce(
|
||||
() => {
|
||||
@@ -148,14 +147,14 @@ describe('debounce', () => {
|
||||
);
|
||||
|
||||
withTrailing();
|
||||
assert.strictEqual(withCount, 0);
|
||||
expect(withCount).toBe(0);
|
||||
|
||||
withoutTrailing();
|
||||
assert.strictEqual(withoutCount, 0);
|
||||
expect(withoutCount).toBe(0);
|
||||
|
||||
setTimeout(() => {
|
||||
assert.strictEqual(withCount, 1);
|
||||
assert.strictEqual(withoutCount, 0);
|
||||
expect(withCount).toBe(1);
|
||||
expect(withoutCount).toBe(0);
|
||||
done();
|
||||
}, 64);
|
||||
});
|
||||
@@ -174,25 +173,25 @@ describe('debounce', () => {
|
||||
|
||||
debounced();
|
||||
debounced();
|
||||
assert.strictEqual(callCount, 0);
|
||||
expect(callCount).toBe(0);
|
||||
|
||||
setTimeout(() => {
|
||||
assert.strictEqual(callCount, 1);
|
||||
expect(callCount).toBe(1);
|
||||
debounced();
|
||||
debounced();
|
||||
assert.strictEqual(callCount, 1);
|
||||
expect(callCount).toBe(1);
|
||||
}, 128);
|
||||
|
||||
setTimeout(() => {
|
||||
assert.strictEqual(callCount, 2);
|
||||
expect(callCount).toBe(2);
|
||||
done();
|
||||
}, 256);
|
||||
});
|
||||
|
||||
it('should support `maxWait` in a tight loop', (done) => {
|
||||
let limit = argv || isPhantom ? 1000 : 320,
|
||||
withCount = 0,
|
||||
withoutCount = 0;
|
||||
const limit = argv || isPhantom ? 1000 : 320;
|
||||
let withCount = 0;
|
||||
let withoutCount = 0;
|
||||
|
||||
const withMaxWait = debounce(
|
||||
() => {
|
||||
@@ -213,7 +212,7 @@ describe('debounce', () => {
|
||||
}
|
||||
const actual = [Boolean(withoutCount), Boolean(withCount)];
|
||||
setTimeout(() => {
|
||||
assert.deepStrictEqual(actual, [false, true]);
|
||||
expect(actual, [false).toEqual(true]);
|
||||
done();
|
||||
}, 1);
|
||||
});
|
||||
@@ -236,7 +235,7 @@ describe('debounce', () => {
|
||||
setTimeout(debounced, 210);
|
||||
|
||||
setTimeout(() => {
|
||||
assert.strictEqual(callCount, 2);
|
||||
expect(callCount).toBe(2);
|
||||
done();
|
||||
}, 500);
|
||||
});
|
||||
@@ -256,19 +255,19 @@ describe('debounce', () => {
|
||||
|
||||
setTimeout(() => {
|
||||
debounced();
|
||||
assert.strictEqual(callCount, 1);
|
||||
expect(callCount).toBe(1);
|
||||
}, 128);
|
||||
|
||||
setTimeout(() => {
|
||||
assert.strictEqual(callCount, 2);
|
||||
expect(callCount).toBe(2);
|
||||
done();
|
||||
}, 192);
|
||||
});
|
||||
|
||||
it('should invoke the trailing call with the correct arguments and `this` binding', (done) => {
|
||||
let actual,
|
||||
callCount = 0,
|
||||
object = {};
|
||||
let actual;
|
||||
let callCount = 0;
|
||||
const object = {};
|
||||
|
||||
const debounced = debounce(
|
||||
function (value) {
|
||||
@@ -286,8 +285,8 @@ describe('debounce', () => {
|
||||
}
|
||||
}
|
||||
setTimeout(() => {
|
||||
assert.strictEqual(callCount, 2);
|
||||
assert.deepStrictEqual(actual, [object, 'a']);
|
||||
expect(callCount).toBe(2);
|
||||
expect(actual, [object).toEqual('a']);
|
||||
done();
|
||||
}, 64);
|
||||
});
|
||||
@@ -1,4 +1,3 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { burredLetters, deburredLetters, comboMarks } from './utils';
|
||||
import deburr from '../src/deburr';
|
||||
@@ -6,14 +5,14 @@ import deburr from '../src/deburr';
|
||||
describe('deburr', () => {
|
||||
it('should convert Latin Unicode letters to basic Latin', () => {
|
||||
const actual = lodashStable.map(burredLetters, deburr);
|
||||
assert.deepStrictEqual(actual, deburredLetters);
|
||||
expect(actual).toEqual(deburredLetters);
|
||||
});
|
||||
|
||||
it('should not deburr Latin mathematical operators', () => {
|
||||
const operators = ['\xd7', '\xf7'],
|
||||
actual = lodashStable.map(operators, deburr);
|
||||
const operators = ['\xd7', '\xf7'];
|
||||
const actual = lodashStable.map(operators, deburr);
|
||||
|
||||
assert.deepStrictEqual(actual, operators);
|
||||
expect(actual).toEqual(operators);
|
||||
});
|
||||
|
||||
it('should deburr combining diacritical marks', () => {
|
||||
@@ -21,6 +20,6 @@ describe('deburr', () => {
|
||||
|
||||
const actual = lodashStable.map(comboMarks, (chr) => deburr(`e${chr}i`));
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,3 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { falsey } from './utils';
|
||||
import defaultTo from '../src/defaultTo';
|
||||
@@ -11,6 +10,6 @@ describe('defaultTo', () => {
|
||||
|
||||
const actual = lodashStable.map(falsey, (value) => defaultTo(value, 1));
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,3 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { objectProto } from './utils';
|
||||
import defaults from '../src/defaults';
|
||||
@@ -6,34 +5,34 @@ import defaults from '../src/defaults';
|
||||
describe('defaults', () => {
|
||||
it('should assign source properties if missing on `object`', () => {
|
||||
const actual = defaults({ a: 1 }, { a: 2, b: 2 });
|
||||
assert.deepStrictEqual(actual, { a: 1, b: 2 });
|
||||
expect(actual).toEqual({ a: 1, b: 2 });
|
||||
});
|
||||
|
||||
it('should accept multiple sources', () => {
|
||||
let expected = { a: 1, b: 2, c: 3 },
|
||||
actual = defaults({ a: 1, b: 2 }, { b: 3 }, { c: 3 });
|
||||
const expected = { a: 1, b: 2, c: 3 };
|
||||
let actual = defaults({ a: 1, b: 2 }, { b: 3 }, { c: 3 });
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
|
||||
actual = defaults({ a: 1, b: 2 }, { b: 3, c: 3 }, { c: 2 });
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should not overwrite `null` values', () => {
|
||||
const actual = defaults({ a: null }, { a: 1 });
|
||||
assert.strictEqual(actual.a, null);
|
||||
expect(actual.a).toBe(null);
|
||||
});
|
||||
|
||||
it('should overwrite `undefined` values', () => {
|
||||
const actual = defaults({ a: undefined }, { a: 1 });
|
||||
assert.strictEqual(actual.a, 1);
|
||||
expect(actual.a).toBe(1);
|
||||
});
|
||||
|
||||
it('should assign `undefined` values', () => {
|
||||
const source = { a: undefined, b: 1 },
|
||||
actual = defaults({}, source);
|
||||
const source = { a: undefined, b: 1 };
|
||||
const actual = defaults({}, source);
|
||||
|
||||
assert.deepStrictEqual(actual, { a: undefined, b: 1 });
|
||||
expect(actual).toEqual({ a: undefined, b: 1 });
|
||||
});
|
||||
|
||||
it('should assign properties that shadow those on `Object.prototype`', () => {
|
||||
@@ -58,9 +57,9 @@ describe('defaults', () => {
|
||||
};
|
||||
|
||||
let expected = lodashStable.clone(source);
|
||||
assert.deepStrictEqual(defaults({}, source), expected);
|
||||
expect(defaults({}, source)).toEqual(expected);
|
||||
|
||||
expected = lodashStable.clone(object);
|
||||
assert.deepStrictEqual(defaults({}, object, source), expected);
|
||||
expect(defaults({}, object, source)).toEqual(expected);
|
||||
});
|
||||
});
|
||||
100
test/defaultsDeep.spec.js
Normal file
100
test/defaultsDeep.spec.js
Normal file
@@ -0,0 +1,100 @@
|
||||
import lodashStable from 'lodash';
|
||||
import { noop } from './utils';
|
||||
import defaultsDeep from '../src/defaultsDeep';
|
||||
|
||||
describe('defaultsDeep', () => {
|
||||
it('should deep assign source properties if missing on `object`', () => {
|
||||
const object = { a: { b: 2 }, d: 4 };
|
||||
const source = { a: { b: 3, c: 3 }, e: 5 };
|
||||
const expected = { a: { b: 2, c: 3 }, d: 4, e: 5 };
|
||||
|
||||
expect(defaultsDeep(object, source)).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should accept multiple sources', () => {
|
||||
const source1 = { a: { b: 3 } };
|
||||
const source2 = { a: { c: 3 } };
|
||||
const source3 = { a: { b: 3, c: 3 } };
|
||||
const source4 = { a: { c: 4 } };
|
||||
const expected = { a: { b: 2, c: 3 } };
|
||||
|
||||
expect(defaultsDeep({ a: { b: 2 } }, source1, source2)).toEqual(expected);
|
||||
expect(defaultsDeep({ a: { b: 2 } }, source3, source4)).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should not overwrite `null` values', () => {
|
||||
const object = { a: { b: null } };
|
||||
const source = { a: { b: 2 } };
|
||||
const actual = defaultsDeep(object, source);
|
||||
|
||||
expect(actual.a.b).toBe(null);
|
||||
});
|
||||
|
||||
it('should not overwrite regexp values', () => {
|
||||
const object = { a: { b: /x/ } };
|
||||
const source = { a: { b: /y/ } };
|
||||
const actual = defaultsDeep(object, source);
|
||||
|
||||
expect(actual.a.b).toEqual(/x/);
|
||||
});
|
||||
|
||||
it('should not convert function properties to objects', () => {
|
||||
let actual = defaultsDeep({}, { a: noop });
|
||||
expect(actual.a).toBe(noop);
|
||||
|
||||
actual = defaultsDeep({}, { a: { b: noop } });
|
||||
expect(actual.a.b).toBe(noop);
|
||||
});
|
||||
|
||||
it('should overwrite `undefined` values', () => {
|
||||
const object = { a: { b: undefined } };
|
||||
const source = { a: { b: 2 } };
|
||||
const actual = defaultsDeep(object, source);
|
||||
|
||||
expect(actual.a.b).toBe(2);
|
||||
});
|
||||
|
||||
it('should assign `undefined` values', () => {
|
||||
const source = { a: undefined, b: { c: undefined, d: 1 } };
|
||||
const expected = lodashStable.cloneDeep(source);
|
||||
const actual = defaultsDeep({}, source);
|
||||
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should merge sources containing circular references', () => {
|
||||
const object = {
|
||||
foo: { b: { c: { d: {} } } },
|
||||
bar: { a: 2 },
|
||||
};
|
||||
|
||||
const source = {
|
||||
foo: { b: { c: { d: {} } } },
|
||||
bar: {},
|
||||
};
|
||||
|
||||
object.foo.b.c.d = object;
|
||||
source.foo.b.c.d = source;
|
||||
source.bar.b = source.foo.b;
|
||||
|
||||
const actual = defaultsDeep(object, source);
|
||||
|
||||
expect(actual.bar.b).toBe(actual.foo.b);
|
||||
expect(actual.foo.b.c.d).toBe(actual.foo.b.c.d.foo.b.c.d);
|
||||
});
|
||||
|
||||
it('should not modify sources', () => {
|
||||
const source1 = { a: 1, b: { c: 2 } };
|
||||
const source2 = { b: { c: 3, d: 3 } };
|
||||
const actual = defaultsDeep({}, source1, source2);
|
||||
|
||||
expect(actual, { a: 1, b: { c: 2).toEqual(d: 3 } });
|
||||
expect(source1, { a: 1).toEqual(b: { c: 2 } });
|
||||
expect(source2, { b: { c: 3).toEqual(d: 3 } });
|
||||
});
|
||||
|
||||
it('should not attempt a merge of a string into an array', () => {
|
||||
const actual = defaultsDeep({ a: ['abc'] }, { a: 'abc' });
|
||||
expect(actual.a).toEqual(['abc']);
|
||||
});
|
||||
});
|
||||
@@ -1,101 +0,0 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { noop } from './utils';
|
||||
import defaultsDeep from '../src/defaultsDeep';
|
||||
|
||||
describe('defaultsDeep', () => {
|
||||
it('should deep assign source properties if missing on `object`', () => {
|
||||
const object = { a: { b: 2 }, d: 4 },
|
||||
source = { a: { b: 3, c: 3 }, e: 5 },
|
||||
expected = { a: { b: 2, c: 3 }, d: 4, e: 5 };
|
||||
|
||||
assert.deepStrictEqual(defaultsDeep(object, source), expected);
|
||||
});
|
||||
|
||||
it('should accept multiple sources', () => {
|
||||
const source1 = { a: { b: 3 } },
|
||||
source2 = { a: { c: 3 } },
|
||||
source3 = { a: { b: 3, c: 3 } },
|
||||
source4 = { a: { c: 4 } },
|
||||
expected = { a: { b: 2, c: 3 } };
|
||||
|
||||
assert.deepStrictEqual(defaultsDeep({ a: { b: 2 } }, source1, source2), expected);
|
||||
assert.deepStrictEqual(defaultsDeep({ a: { b: 2 } }, source3, source4), expected);
|
||||
});
|
||||
|
||||
it('should not overwrite `null` values', () => {
|
||||
const object = { a: { b: null } },
|
||||
source = { a: { b: 2 } },
|
||||
actual = defaultsDeep(object, source);
|
||||
|
||||
assert.strictEqual(actual.a.b, null);
|
||||
});
|
||||
|
||||
it('should not overwrite regexp values', () => {
|
||||
const object = { a: { b: /x/ } },
|
||||
source = { a: { b: /y/ } },
|
||||
actual = defaultsDeep(object, source);
|
||||
|
||||
assert.deepStrictEqual(actual.a.b, /x/);
|
||||
});
|
||||
|
||||
it('should not convert function properties to objects', () => {
|
||||
let actual = defaultsDeep({}, { a: noop });
|
||||
assert.strictEqual(actual.a, noop);
|
||||
|
||||
actual = defaultsDeep({}, { a: { b: noop } });
|
||||
assert.strictEqual(actual.a.b, noop);
|
||||
});
|
||||
|
||||
it('should overwrite `undefined` values', () => {
|
||||
const object = { a: { b: undefined } },
|
||||
source = { a: { b: 2 } },
|
||||
actual = defaultsDeep(object, source);
|
||||
|
||||
assert.strictEqual(actual.a.b, 2);
|
||||
});
|
||||
|
||||
it('should assign `undefined` values', () => {
|
||||
const source = { a: undefined, b: { c: undefined, d: 1 } },
|
||||
expected = lodashStable.cloneDeep(source),
|
||||
actual = defaultsDeep({}, source);
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
});
|
||||
|
||||
it('should merge sources containing circular references', () => {
|
||||
const object = {
|
||||
foo: { b: { c: { d: {} } } },
|
||||
bar: { a: 2 },
|
||||
};
|
||||
|
||||
const source = {
|
||||
foo: { b: { c: { d: {} } } },
|
||||
bar: {},
|
||||
};
|
||||
|
||||
object.foo.b.c.d = object;
|
||||
source.foo.b.c.d = source;
|
||||
source.bar.b = source.foo.b;
|
||||
|
||||
const actual = defaultsDeep(object, source);
|
||||
|
||||
assert.strictEqual(actual.bar.b, actual.foo.b);
|
||||
assert.strictEqual(actual.foo.b.c.d, actual.foo.b.c.d.foo.b.c.d);
|
||||
});
|
||||
|
||||
it('should not modify sources', () => {
|
||||
const source1 = { a: 1, b: { c: 2 } },
|
||||
source2 = { b: { c: 3, d: 3 } },
|
||||
actual = defaultsDeep({}, source1, source2);
|
||||
|
||||
assert.deepStrictEqual(actual, { a: 1, b: { c: 2, d: 3 } });
|
||||
assert.deepStrictEqual(source1, { a: 1, b: { c: 2 } });
|
||||
assert.deepStrictEqual(source2, { b: { c: 3, d: 3 } });
|
||||
});
|
||||
|
||||
it('should not attempt a merge of a string into an array', () => {
|
||||
const actual = defaultsDeep({ a: ['abc'] }, { a: 'abc' });
|
||||
assert.deepStrictEqual(actual.a, ['abc']);
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,3 @@
|
||||
import assert from 'node:assert';
|
||||
import { slice } from './utils';
|
||||
import defer from '../src/defer';
|
||||
|
||||
@@ -10,7 +9,7 @@ describe('defer', () => {
|
||||
});
|
||||
|
||||
setTimeout(() => {
|
||||
assert.ok(pass);
|
||||
expect(pass)
|
||||
done();
|
||||
}, 32);
|
||||
});
|
||||
@@ -27,21 +26,21 @@ describe('defer', () => {
|
||||
);
|
||||
|
||||
setTimeout(() => {
|
||||
assert.deepStrictEqual(args, [1, 2]);
|
||||
expect(args, [1).toEqual(2]);
|
||||
done();
|
||||
}, 32);
|
||||
});
|
||||
|
||||
it('should be cancelable', (done) => {
|
||||
let pass = true,
|
||||
timerId = defer(() => {
|
||||
pass = false;
|
||||
});
|
||||
let pass = true;
|
||||
const timerId = defer(() => {
|
||||
pass = false;
|
||||
});
|
||||
|
||||
clearTimeout(timerId);
|
||||
|
||||
setTimeout(() => {
|
||||
assert.ok(pass);
|
||||
expect(pass)
|
||||
done();
|
||||
}, 32);
|
||||
});
|
||||
@@ -1,4 +1,3 @@
|
||||
import assert from 'node:assert';
|
||||
import { slice } from './utils';
|
||||
import delay from '../src/delay';
|
||||
|
||||
@@ -10,11 +9,11 @@ describe('delay', () => {
|
||||
}, 32);
|
||||
|
||||
setTimeout(() => {
|
||||
assert.ok(!pass);
|
||||
expect(pass).toBe(false)
|
||||
}, 1);
|
||||
|
||||
setTimeout(() => {
|
||||
assert.ok(pass);
|
||||
expect(pass)
|
||||
done();
|
||||
}, 64);
|
||||
});
|
||||
@@ -32,7 +31,7 @@ describe('delay', () => {
|
||||
);
|
||||
|
||||
setTimeout(() => {
|
||||
assert.deepStrictEqual(args, [1, 2]);
|
||||
expect(args, [1).toEqual(2]);
|
||||
done();
|
||||
}, 64);
|
||||
});
|
||||
@@ -43,31 +42,31 @@ describe('delay', () => {
|
||||
pass = true;
|
||||
});
|
||||
|
||||
assert.ok(!pass);
|
||||
expect(pass).toBe(false)
|
||||
|
||||
setTimeout(() => {
|
||||
assert.ok(pass);
|
||||
expect(pass)
|
||||
done();
|
||||
}, 0);
|
||||
});
|
||||
|
||||
it('should be cancelable', (done) => {
|
||||
let pass = true,
|
||||
timerId = delay(() => {
|
||||
pass = false;
|
||||
}, 32);
|
||||
let pass = true;
|
||||
const timerId = delay(() => {
|
||||
pass = false;
|
||||
}, 32);
|
||||
|
||||
clearTimeout(timerId);
|
||||
|
||||
setTimeout(() => {
|
||||
assert.ok(pass);
|
||||
expect(pass)
|
||||
done();
|
||||
}, 64);
|
||||
});
|
||||
|
||||
it('should work with mocked `setTimeout`', () => {
|
||||
let pass = false,
|
||||
setTimeout = root.setTimeout;
|
||||
let pass = false;
|
||||
const setTimeout = root.setTimeout;
|
||||
|
||||
setProperty(root, 'setTimeout', (func) => {
|
||||
func();
|
||||
@@ -77,6 +76,6 @@ describe('delay', () => {
|
||||
}, 32);
|
||||
setProperty(root, 'setTimeout', setTimeout);
|
||||
|
||||
assert.ok(pass);
|
||||
expect(pass)
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,3 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { _, LARGE_ARRAY_SIZE, stubOne, stubNaN, args } from './utils';
|
||||
|
||||
@@ -8,12 +7,12 @@ describe('difference methods', () => {
|
||||
|
||||
it(`\`_.${methodName}\` should return the difference of two arrays`, () => {
|
||||
const actual = func([2, 1], [2, 3]);
|
||||
assert.deepStrictEqual(actual, [1]);
|
||||
expect(actual).toEqual([1]);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should return the difference of multiple arrays`, () => {
|
||||
const actual = func([2, 1, 2, 3], [3, 4], [3, 2]);
|
||||
assert.deepStrictEqual(actual, [1]);
|
||||
expect(actual).toEqual([1]);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should treat \`-0\` as \`0\``, () => {
|
||||
@@ -21,27 +20,27 @@ describe('difference methods', () => {
|
||||
|
||||
let actual = lodashStable.map(array, (value) => func(array, [value]));
|
||||
|
||||
assert.deepStrictEqual(actual, [[], []]);
|
||||
expect(actual, [[]).toEqual([]]);
|
||||
|
||||
actual = lodashStable.map(func([-0, 1], [1]), lodashStable.toString);
|
||||
assert.deepStrictEqual(actual, ['0']);
|
||||
expect(actual).toEqual(['0']);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should match \`NaN\``, () => {
|
||||
assert.deepStrictEqual(func([1, NaN, 3], [NaN, 5, NaN]), [1, 3]);
|
||||
expect(func([1, NaN, 3], [NaN, 5, NaN]), [1).toEqual(3]);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should work with large arrays`, () => {
|
||||
const array1 = lodashStable.range(LARGE_ARRAY_SIZE + 1),
|
||||
array2 = lodashStable.range(LARGE_ARRAY_SIZE),
|
||||
a = {},
|
||||
b = {},
|
||||
c = {};
|
||||
const array1 = lodashStable.range(LARGE_ARRAY_SIZE + 1);
|
||||
const array2 = lodashStable.range(LARGE_ARRAY_SIZE);
|
||||
const a = {};
|
||||
const b = {};
|
||||
const c = {};
|
||||
|
||||
array1.push(a, b, c);
|
||||
array2.push(b, c, a);
|
||||
|
||||
assert.deepStrictEqual(func(array1, array2), [LARGE_ARRAY_SIZE]);
|
||||
expect(func(array1, array2)).toEqual([LARGE_ARRAY_SIZE]);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should work with large arrays of \`-0\` as \`0\``, () => {
|
||||
@@ -55,32 +54,32 @@ describe('difference methods', () => {
|
||||
return func(array, largeArray);
|
||||
});
|
||||
|
||||
assert.deepStrictEqual(actual, [[], []]);
|
||||
expect(actual, [[]).toEqual([]]);
|
||||
|
||||
const largeArray = lodashStable.times(LARGE_ARRAY_SIZE, stubOne);
|
||||
actual = lodashStable.map(func([-0, 1], largeArray), lodashStable.toString);
|
||||
assert.deepStrictEqual(actual, ['0']);
|
||||
expect(actual).toEqual(['0']);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should work with large arrays of \`NaN\``, () => {
|
||||
const largeArray = lodashStable.times(LARGE_ARRAY_SIZE, stubNaN);
|
||||
assert.deepStrictEqual(func([1, NaN, 3], largeArray), [1, 3]);
|
||||
expect(func([1, NaN, 3], largeArray), [1).toEqual(3]);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should work with large arrays of objects`, () => {
|
||||
const object1 = {},
|
||||
object2 = {},
|
||||
largeArray = lodashStable.times(LARGE_ARRAY_SIZE, lodashStable.constant(object1));
|
||||
const object1 = {};
|
||||
const object2 = {};
|
||||
const largeArray = lodashStable.times(LARGE_ARRAY_SIZE, lodashStable.constant(object1));
|
||||
|
||||
assert.deepStrictEqual(func([object1, object2], largeArray), [object2]);
|
||||
expect(func([object1, object2], largeArray)).toEqual([object2]);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should ignore values that are not array-like`, () => {
|
||||
const array = [1, null, 3];
|
||||
|
||||
assert.deepStrictEqual(func(args, 3, { '0': 1 }), [1, 2, 3]);
|
||||
assert.deepStrictEqual(func(null, array, 1), []);
|
||||
assert.deepStrictEqual(func(array, args, null), [null]);
|
||||
expect(func(args, 3, { 0: 1 }), [1, 2).toEqual(3]);
|
||||
expect(func(null, array, 1)).toEqual([]);
|
||||
expect(func(array, args, null)).toEqual([null]);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,14 +1,13 @@
|
||||
import assert from 'node:assert';
|
||||
import { slice } from './utils';
|
||||
import differenceBy from '../src/differenceBy';
|
||||
|
||||
describe('differenceBy', () => {
|
||||
it('should accept an `iteratee`', () => {
|
||||
let actual = differenceBy([2.1, 1.2], [2.3, 3.4], Math.floor);
|
||||
assert.deepStrictEqual(actual, [1.2]);
|
||||
expect(actual).toEqual([1.2]);
|
||||
|
||||
actual = differenceBy([{ x: 2 }, { x: 1 }], [{ x: 1 }], 'x');
|
||||
assert.deepStrictEqual(actual, [{ x: 2 }]);
|
||||
expect(actual).toEqual([{ x: 2 }]);
|
||||
});
|
||||
|
||||
it('should provide correct `iteratee` arguments', () => {
|
||||
@@ -18,6 +17,6 @@ describe('differenceBy', () => {
|
||||
args || (args = slice.call(arguments));
|
||||
});
|
||||
|
||||
assert.deepStrictEqual(args, [2.3]);
|
||||
expect(args).toEqual([2.3]);
|
||||
});
|
||||
});
|
||||
28
test/differenceWith.spec.js
Normal file
28
test/differenceWith.spec.js
Normal file
@@ -0,0 +1,28 @@
|
||||
import lodashStable from 'lodash';
|
||||
import { LARGE_ARRAY_SIZE, stubOne } from './utils';
|
||||
import differenceWith from '../src/differenceWith';
|
||||
|
||||
describe('differenceWith', () => {
|
||||
it('should work with a `comparator`', () => {
|
||||
const objects = [
|
||||
{ x: 1, y: 2 },
|
||||
{ x: 2, y: 1 },
|
||||
];
|
||||
const actual = differenceWith(objects, [{ x: 1, y: 2 }], lodashStable.isEqual);
|
||||
|
||||
expect(actual).toEqual([objects[1]]);
|
||||
});
|
||||
|
||||
it('should preserve the sign of `0`', () => {
|
||||
const array = [-0, 1];
|
||||
const largeArray = lodashStable.times(LARGE_ARRAY_SIZE, stubOne);
|
||||
const others = [[1], largeArray];
|
||||
const expected = lodashStable.map(others, lodashStable.constant(['-0']));
|
||||
|
||||
const actual = lodashStable.map(others, (other) =>
|
||||
lodashStable.map(differenceWith(array, other, lodashStable.eq), lodashStable.toString),
|
||||
);
|
||||
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
});
|
||||
@@ -1,29 +0,0 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { LARGE_ARRAY_SIZE, stubOne } from './utils';
|
||||
import differenceWith from '../src/differenceWith';
|
||||
|
||||
describe('differenceWith', () => {
|
||||
it('should work with a `comparator`', () => {
|
||||
const objects = [
|
||||
{ x: 1, y: 2 },
|
||||
{ x: 2, y: 1 },
|
||||
],
|
||||
actual = differenceWith(objects, [{ x: 1, y: 2 }], lodashStable.isEqual);
|
||||
|
||||
assert.deepStrictEqual(actual, [objects[1]]);
|
||||
});
|
||||
|
||||
it('should preserve the sign of `0`', () => {
|
||||
const array = [-0, 1],
|
||||
largeArray = lodashStable.times(LARGE_ARRAY_SIZE, stubOne),
|
||||
others = [[1], largeArray],
|
||||
expected = lodashStable.map(others, lodashStable.constant(['-0']));
|
||||
|
||||
const actual = lodashStable.map(others, (other) =>
|
||||
lodashStable.map(differenceWith(array, other, lodashStable.eq), lodashStable.toString),
|
||||
);
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
});
|
||||
});
|
||||
14
test/divide.spec.js
Normal file
14
test/divide.spec.js
Normal file
@@ -0,0 +1,14 @@
|
||||
import divide from '../src/divide';
|
||||
|
||||
describe('divide', () => {
|
||||
it('should divide two numbers', () => {
|
||||
expect(divide(6, 4)).toBe(1.5);
|
||||
expect(divide(-6, 4)).toBe(-1.5);
|
||||
expect(divide(-6, -4)).toBe(1.5);
|
||||
});
|
||||
|
||||
it('should coerce arguments to numbers', () => {
|
||||
expect(divide('6', '4')).toBe(1.5);
|
||||
expect(divide('x', 'y')).toEqual(NaN);
|
||||
});
|
||||
});
|
||||
@@ -1,15 +0,0 @@
|
||||
import assert from 'node:assert';
|
||||
import divide from '../src/divide';
|
||||
|
||||
describe('divide', () => {
|
||||
it('should divide two numbers', () => {
|
||||
assert.strictEqual(divide(6, 4), 1.5);
|
||||
assert.strictEqual(divide(-6, 4), -1.5);
|
||||
assert.strictEqual(divide(-6, -4), 1.5);
|
||||
});
|
||||
|
||||
it('should coerce arguments to numbers', () => {
|
||||
assert.strictEqual(divide('6', '4'), 1.5);
|
||||
assert.deepStrictEqual(divide('x', 'y'), NaN);
|
||||
});
|
||||
});
|
||||
37
test/drop.spec.js
Normal file
37
test/drop.spec.js
Normal file
@@ -0,0 +1,37 @@
|
||||
import lodashStable from 'lodash';
|
||||
import { falsey } from './utils';
|
||||
import drop from '../src/drop';
|
||||
|
||||
describe('drop', () => {
|
||||
const array = [1, 2, 3];
|
||||
|
||||
it('should drop the first two elements', () => {
|
||||
expect(drop(array, 2)).toEqual([3]);
|
||||
});
|
||||
|
||||
it('should treat falsey `n` values, except `undefined`, as `0`', () => {
|
||||
const expected = lodashStable.map(falsey, (value) =>
|
||||
value === undefined ? [2, 3] : array,
|
||||
);
|
||||
|
||||
const actual = lodashStable.map(falsey, (n) => drop(array, n));
|
||||
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should return all elements when `n` < `1`', () => {
|
||||
lodashStable.each([0, -1, -Infinity], (n) => {
|
||||
expect(drop(array, n)).toEqual(array);
|
||||
});
|
||||
});
|
||||
|
||||
it('should return an empty array when `n` >= `length`', () => {
|
||||
lodashStable.each([3, 4, 2 ** 32, Infinity], (n) => {
|
||||
expect(drop(array, n)).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
it('should coerce `n` to an integer', () => {
|
||||
expect(drop(array, 1.6)).toEqual([2, 3]);
|
||||
});
|
||||
});
|
||||
@@ -1,66 +0,0 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { falsey, LARGE_ARRAY_SIZE, isEven } from './utils';
|
||||
import drop from '../src/drop';
|
||||
|
||||
describe('drop', () => {
|
||||
const array = [1, 2, 3];
|
||||
|
||||
it('should drop the first two elements', () => {
|
||||
assert.deepStrictEqual(drop(array, 2), [3]);
|
||||
});
|
||||
|
||||
it('should treat falsey `n` values, except `undefined`, as `0`', () => {
|
||||
const expected = lodashStable.map(falsey, (value) =>
|
||||
value === undefined ? [2, 3] : array,
|
||||
);
|
||||
|
||||
const actual = lodashStable.map(falsey, (n) => drop(array, n));
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
});
|
||||
|
||||
it('should return all elements when `n` < `1`', () => {
|
||||
lodashStable.each([0, -1, -Infinity], (n) => {
|
||||
assert.deepStrictEqual(drop(array, n), array);
|
||||
});
|
||||
});
|
||||
|
||||
it('should return an empty array when `n` >= `length`', () => {
|
||||
lodashStable.each([3, 4, 2 ** 32, Infinity], (n) => {
|
||||
assert.deepStrictEqual(drop(array, n), []);
|
||||
});
|
||||
});
|
||||
|
||||
it('should coerce `n` to an integer', () => {
|
||||
assert.deepStrictEqual(drop(array, 1.6), [2, 3]);
|
||||
});
|
||||
|
||||
it('should work in a lazy sequence', () => {
|
||||
var array = lodashStable.range(1, LARGE_ARRAY_SIZE + 1),
|
||||
predicate = function (value) {
|
||||
values.push(value);
|
||||
return isEven(value);
|
||||
},
|
||||
values = [],
|
||||
actual = _(array).drop(2).drop().value();
|
||||
|
||||
assert.deepEqual(actual, array.slice(3));
|
||||
|
||||
actual = _(array).filter(predicate).drop(2).drop().value();
|
||||
assert.deepEqual(values, array);
|
||||
assert.deepEqual(actual, drop(drop(_.filter(array, predicate), 2)));
|
||||
|
||||
actual = _(array).drop(2).dropRight().drop().dropRight(2).value();
|
||||
assert.deepEqual(actual, _.dropRight(drop(_.dropRight(drop(array, 2))), 2));
|
||||
|
||||
values = [];
|
||||
|
||||
actual = _(array).drop().filter(predicate).drop(2).dropRight().drop().dropRight(2).value();
|
||||
assert.deepEqual(values, array.slice(1));
|
||||
assert.deepEqual(
|
||||
actual,
|
||||
_.dropRight(drop(_.dropRight(drop(_.filter(drop(array), predicate), 2))), 2),
|
||||
);
|
||||
});
|
||||
});
|
||||
37
test/dropRight.spec.js
Normal file
37
test/dropRight.spec.js
Normal file
@@ -0,0 +1,37 @@
|
||||
import lodashStable from 'lodash';
|
||||
import { falsey } from './utils';
|
||||
import dropRight from '../src/dropRight';
|
||||
|
||||
describe('dropRight', () => {
|
||||
const array = [1, 2, 3];
|
||||
|
||||
it('should drop the last two elements', () => {
|
||||
expect(dropRight(array, 2)).toEqual([1]);
|
||||
});
|
||||
|
||||
it('should treat falsey `n` values, except `undefined`, as `0`', () => {
|
||||
const expected = lodashStable.map(falsey, (value) =>
|
||||
value === undefined ? [1, 2] : array,
|
||||
);
|
||||
|
||||
const actual = lodashStable.map(falsey, (n) => dropRight(array, n));
|
||||
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should return all elements when `n` < `1`', () => {
|
||||
lodashStable.each([0, -1, -Infinity], (n) => {
|
||||
expect(dropRight(array, n)).toEqual(array);
|
||||
});
|
||||
});
|
||||
|
||||
it('should return an empty array when `n` >= `length`', () => {
|
||||
lodashStable.each([3, 4, 2 ** 32, Infinity], (n) => {
|
||||
expect(dropRight(array, n)).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
it('should coerce `n` to an integer', () => {
|
||||
expect(dropRight(array, 1.6)).toEqual([1, 2]);
|
||||
});
|
||||
});
|
||||
@@ -1,73 +0,0 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { falsey, LARGE_ARRAY_SIZE, isEven } from './utils';
|
||||
import dropRight from '../src/dropRight';
|
||||
|
||||
describe('dropRight', () => {
|
||||
const array = [1, 2, 3];
|
||||
|
||||
it('should drop the last two elements', () => {
|
||||
assert.deepStrictEqual(dropRight(array, 2), [1]);
|
||||
});
|
||||
|
||||
it('should treat falsey `n` values, except `undefined`, as `0`', () => {
|
||||
const expected = lodashStable.map(falsey, (value) =>
|
||||
value === undefined ? [1, 2] : array,
|
||||
);
|
||||
|
||||
const actual = lodashStable.map(falsey, (n) => dropRight(array, n));
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
});
|
||||
|
||||
it('should return all elements when `n` < `1`', () => {
|
||||
lodashStable.each([0, -1, -Infinity], (n) => {
|
||||
assert.deepStrictEqual(dropRight(array, n), array);
|
||||
});
|
||||
});
|
||||
|
||||
it('should return an empty array when `n` >= `length`', () => {
|
||||
lodashStable.each([3, 4, 2 ** 32, Infinity], (n) => {
|
||||
assert.deepStrictEqual(dropRight(array, n), []);
|
||||
});
|
||||
});
|
||||
|
||||
it('should coerce `n` to an integer', () => {
|
||||
assert.deepStrictEqual(dropRight(array, 1.6), [1, 2]);
|
||||
});
|
||||
|
||||
it('should work in a lazy sequence', () => {
|
||||
var array = lodashStable.range(1, LARGE_ARRAY_SIZE + 1),
|
||||
predicate = function (value) {
|
||||
values.push(value);
|
||||
return isEven(value);
|
||||
},
|
||||
values = [],
|
||||
actual = _(array).dropRight(2).dropRight().value();
|
||||
|
||||
assert.deepEqual(actual, array.slice(0, -3));
|
||||
|
||||
actual = _(array).filter(predicate).dropRight(2).dropRight().value();
|
||||
assert.deepEqual(values, array);
|
||||
assert.deepEqual(actual, dropRight(dropRight(_.filter(array, predicate), 2)));
|
||||
|
||||
actual = _(array).dropRight(2).drop().dropRight().drop(2).value();
|
||||
assert.deepEqual(actual, _.drop(dropRight(_.drop(dropRight(array, 2))), 2));
|
||||
|
||||
values = [];
|
||||
|
||||
actual = _(array)
|
||||
.dropRight()
|
||||
.filter(predicate)
|
||||
.dropRight(2)
|
||||
.drop()
|
||||
.dropRight()
|
||||
.drop(2)
|
||||
.value();
|
||||
assert.deepEqual(values, array.slice(0, -1));
|
||||
assert.deepEqual(
|
||||
actual,
|
||||
_.drop(dropRight(_.drop(dropRight(_.filter(dropRight(array), predicate), 2))), 2),
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,3 @@
|
||||
import assert from 'node:assert';
|
||||
import { slice } from './utils';
|
||||
import dropRightWhile from '../src/dropRightWhile';
|
||||
|
||||
@@ -14,7 +13,7 @@ describe('dropRightWhile', () => {
|
||||
it('should drop elements while `predicate` returns truthy', () => {
|
||||
const actual = dropRightWhile(array, (n) => n > 2);
|
||||
|
||||
assert.deepStrictEqual(actual, [1, 2]);
|
||||
expect(actual, [1).toEqual(2]);
|
||||
});
|
||||
|
||||
it('should provide correct `predicate` arguments', () => {
|
||||
@@ -24,25 +23,25 @@ describe('dropRightWhile', () => {
|
||||
args = slice.call(arguments);
|
||||
});
|
||||
|
||||
assert.deepStrictEqual(args, [4, 3, array]);
|
||||
expect(args, [4, 3).toEqual(array]);
|
||||
});
|
||||
|
||||
it('should work with `_.matches` shorthands', () => {
|
||||
assert.deepStrictEqual(dropRightWhile(objects, { b: 2 }), objects.slice(0, 2));
|
||||
expect(dropRightWhile(objects, { b: 2 }), objects.slice(0).toEqual(2));
|
||||
});
|
||||
|
||||
it('should work with `_.matchesProperty` shorthands', () => {
|
||||
assert.deepStrictEqual(dropRightWhile(objects, ['b', 2]), objects.slice(0, 2));
|
||||
expect(dropRightWhile(objects, ['b', 2]), objects.slice(0).toEqual(2));
|
||||
});
|
||||
|
||||
it('should work with `_.property` shorthands', () => {
|
||||
assert.deepStrictEqual(dropRightWhile(objects, 'b'), objects.slice(0, 1));
|
||||
expect(dropRightWhile(objects, 'b'), objects.slice(0).toEqual(1));
|
||||
});
|
||||
|
||||
it('should return a wrapped value when chaining', () => {
|
||||
const wrapped = _(array).dropRightWhile((n) => n > 2);
|
||||
|
||||
assert.ok(wrapped instanceof _);
|
||||
assert.deepEqual(wrapped.value(), [1, 2]);
|
||||
expect(wrapped instanceof _)
|
||||
expect(wrapped.value(), [1).toEqual(2]);
|
||||
});
|
||||
});
|
||||
40
test/dropWhile.spec.js
Normal file
40
test/dropWhile.spec.js
Normal file
@@ -0,0 +1,40 @@
|
||||
import { slice } from './utils';
|
||||
import dropWhile from '../src/dropWhile';
|
||||
|
||||
describe('dropWhile', () => {
|
||||
const array = [1, 2, 3, 4];
|
||||
|
||||
const objects = [
|
||||
{ a: 2, b: 2 },
|
||||
{ a: 1, b: 1 },
|
||||
{ a: 0, b: 0 },
|
||||
];
|
||||
|
||||
it('should drop elements while `predicate` returns truthy', () => {
|
||||
const actual = dropWhile(array, (n) => n < 3);
|
||||
|
||||
expect(actual).toEqual([3, 4]);
|
||||
});
|
||||
|
||||
it('should provide correct `predicate` arguments', () => {
|
||||
let args;
|
||||
|
||||
dropWhile(array, function () {
|
||||
args = slice.call(arguments);
|
||||
});
|
||||
|
||||
expect(args).toEqual([1, 0, array]);
|
||||
});
|
||||
|
||||
it('should work with `_.matches` shorthands', () => {
|
||||
expect(dropWhile(objects, { b: 2 })).toEqual(objects.slice(1));
|
||||
});
|
||||
|
||||
it('should work with `_.matchesProperty` shorthands', () => {
|
||||
expect(dropWhile(objects, ['b', 2])).toEqual(objects.slice(1));
|
||||
});
|
||||
|
||||
it('should work with `_.property` shorthands', () => {
|
||||
expect(dropWhile(objects, 'b')).toEqual(objects.slice(2));
|
||||
});
|
||||
});
|
||||
@@ -1,67 +0,0 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { slice, LARGE_ARRAY_SIZE } from './utils';
|
||||
import dropWhile from '../src/dropWhile';
|
||||
|
||||
describe('dropWhile', () => {
|
||||
const array = [1, 2, 3, 4];
|
||||
|
||||
const objects = [
|
||||
{ a: 2, b: 2 },
|
||||
{ a: 1, b: 1 },
|
||||
{ a: 0, b: 0 },
|
||||
];
|
||||
|
||||
it('should drop elements while `predicate` returns truthy', () => {
|
||||
const actual = dropWhile(array, (n) => n < 3);
|
||||
|
||||
assert.deepStrictEqual(actual, [3, 4]);
|
||||
});
|
||||
|
||||
it('should provide correct `predicate` arguments', () => {
|
||||
let args;
|
||||
|
||||
dropWhile(array, function () {
|
||||
args = slice.call(arguments);
|
||||
});
|
||||
|
||||
assert.deepStrictEqual(args, [1, 0, array]);
|
||||
});
|
||||
|
||||
it('should work with `_.matches` shorthands', () => {
|
||||
assert.deepStrictEqual(dropWhile(objects, { b: 2 }), objects.slice(1));
|
||||
});
|
||||
|
||||
it('should work with `_.matchesProperty` shorthands', () => {
|
||||
assert.deepStrictEqual(dropWhile(objects, ['b', 2]), objects.slice(1));
|
||||
});
|
||||
|
||||
it('should work with `_.property` shorthands', () => {
|
||||
assert.deepStrictEqual(dropWhile(objects, 'b'), objects.slice(2));
|
||||
});
|
||||
|
||||
it('should work in a lazy sequence', () => {
|
||||
const array = lodashStable.range(1, LARGE_ARRAY_SIZE + 3),
|
||||
predicate = function (n) {
|
||||
return n < 3;
|
||||
},
|
||||
expected = dropWhile(array, predicate),
|
||||
wrapped = _(array).dropWhile(predicate);
|
||||
|
||||
assert.deepEqual(wrapped.value(), expected);
|
||||
assert.deepEqual(wrapped.reverse().value(), expected.slice().reverse());
|
||||
assert.strictEqual(wrapped.last(), _.last(expected));
|
||||
});
|
||||
|
||||
it('should work in a lazy sequence with `drop`', () => {
|
||||
const array = lodashStable.range(1, LARGE_ARRAY_SIZE + 3);
|
||||
|
||||
const actual = _(array)
|
||||
.dropWhile((n) => n === 1)
|
||||
.drop()
|
||||
.dropWhile((n) => n === 3)
|
||||
.value();
|
||||
|
||||
assert.deepEqual(actual, array.slice(3));
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,3 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { MAX_SAFE_INTEGER, falsey, stubTrue } from './utils';
|
||||
import endsWith from '../src/endsWith';
|
||||
@@ -7,20 +6,20 @@ describe('endsWith', () => {
|
||||
const string = 'abc';
|
||||
|
||||
it('should return `true` if a string ends with `target`', () => {
|
||||
assert.strictEqual(endsWith(string, 'c'), true);
|
||||
expect(endsWith(string, 'c')).toBe(true);
|
||||
});
|
||||
|
||||
it('should return `false` if a string does not end with `target`', () => {
|
||||
assert.strictEqual(endsWith(string, 'b'), false);
|
||||
expect(endsWith(string, 'b')).toBe(false);
|
||||
});
|
||||
|
||||
it('should work with a `position`', () => {
|
||||
assert.strictEqual(endsWith(string, 'b', 2), true);
|
||||
expect(endsWith(string, 'b', 2)).toBe(true);
|
||||
});
|
||||
|
||||
it('should work with `position` >= `length`', () => {
|
||||
lodashStable.each([3, 5, MAX_SAFE_INTEGER, Infinity], (position) => {
|
||||
assert.strictEqual(endsWith(string, 'c', position), true);
|
||||
expect(endsWith(string, 'c', position)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -31,17 +30,17 @@ describe('endsWith', () => {
|
||||
endsWith(string, position === undefined ? 'c' : '', position),
|
||||
);
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should treat a negative `position` as `0`', () => {
|
||||
lodashStable.each([-1, -3, -Infinity], (position) => {
|
||||
assert.ok(lodashStable.every(string, (chr) => !endsWith(string, chr, position)));
|
||||
assert.strictEqual(endsWith(string, '', position), true);
|
||||
expect(lodashStable.every(string, (chr) => !endsWith(string, chr, position)));
|
||||
expect(endsWith(string, '', position)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
it('should coerce `position` to an integer', () => {
|
||||
assert.strictEqual(endsWith(string, 'ab', 2.2), true);
|
||||
expect(endsWith(string, 'ab', 2.2)).toBe(true);
|
||||
});
|
||||
});
|
||||
20
test/eq.spec.js
Normal file
20
test/eq.spec.js
Normal file
@@ -0,0 +1,20 @@
|
||||
import eq from '../src/eq';
|
||||
|
||||
describe('eq', () => {
|
||||
it('should perform a `SameValueZero` comparison of two values', () => {
|
||||
expect(eq()).toBe(true);
|
||||
expect(eq(undefined)).toBe(true);
|
||||
expect(eq(0, -0)).toBe(true);
|
||||
expect(eq(NaN, NaN)).toBe(true);
|
||||
expect(eq(1, 1)).toBe(true);
|
||||
|
||||
expect(eq(null, undefined)).toBe(false);
|
||||
expect(eq(1, Object(1))).toBe(false);
|
||||
expect(eq(1, '1')).toBe(false);
|
||||
expect(eq(1, '1')).toBe(false);
|
||||
|
||||
const object = { a: 1 };
|
||||
expect(eq(object, object)).toBe(true);
|
||||
expect(eq(object, { a: 1 })).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -1,21 +0,0 @@
|
||||
import assert from 'node:assert';
|
||||
import eq from '../src/eq';
|
||||
|
||||
describe('eq', () => {
|
||||
it('should perform a `SameValueZero` comparison of two values', () => {
|
||||
assert.strictEqual(eq(), true);
|
||||
assert.strictEqual(eq(undefined), true);
|
||||
assert.strictEqual(eq(0, -0), true);
|
||||
assert.strictEqual(eq(NaN, NaN), true);
|
||||
assert.strictEqual(eq(1, 1), true);
|
||||
|
||||
assert.strictEqual(eq(null, undefined), false);
|
||||
assert.strictEqual(eq(1, Object(1)), false);
|
||||
assert.strictEqual(eq(1, '1'), false);
|
||||
assert.strictEqual(eq(1, '1'), false);
|
||||
|
||||
const object = { a: 1 };
|
||||
assert.strictEqual(eq(object, object), true);
|
||||
assert.strictEqual(eq(object, { a: 1 }), false);
|
||||
});
|
||||
});
|
||||
@@ -1,30 +1,29 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import escape from '../src/escape';
|
||||
import unescape from '../src/unescape';
|
||||
|
||||
describe('escape', () => {
|
||||
let escaped = '&<>"'/',
|
||||
unescaped = '&<>"\'/';
|
||||
let escaped = '&<>"'/';
|
||||
let unescaped = '&<>"\'/';
|
||||
|
||||
escaped += escaped;
|
||||
unescaped += unescaped;
|
||||
|
||||
it('should escape values', () => {
|
||||
assert.strictEqual(escape(unescaped), escaped);
|
||||
expect(escape(unescaped)).toBe(escaped);
|
||||
});
|
||||
|
||||
it('should handle strings with nothing to escape', () => {
|
||||
assert.strictEqual(escape('abc'), 'abc');
|
||||
expect(escape('abc')).toBe('abc');
|
||||
});
|
||||
|
||||
it('should escape the same characters unescaped by `_.unescape`', () => {
|
||||
assert.strictEqual(escape(unescape(escaped)), escaped);
|
||||
expect(escape(unescape(escaped))).toBe(escaped);
|
||||
});
|
||||
|
||||
lodashStable.each(['`', '/'], (chr) => {
|
||||
it(`should not escape the "${chr}" character`, () => {
|
||||
assert.strictEqual(escape(chr), chr);
|
||||
expect(escape(chr)).toBe(chr);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,28 +1,27 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { stubString } from './utils';
|
||||
import escapeRegExp from '../src/escapeRegExp';
|
||||
|
||||
describe('escapeRegExp', () => {
|
||||
const escaped = '\\^\\$\\.\\*\\+\\?\\(\\)\\[\\]\\{\\}\\|\\\\',
|
||||
unescaped = '^$.*+?()[]{}|\\';
|
||||
const escaped = '\\^\\$\\.\\*\\+\\?\\(\\)\\[\\]\\{\\}\\|\\\\';
|
||||
const unescaped = '^$.*+?()[]{}|\\';
|
||||
|
||||
it('should escape values', () => {
|
||||
assert.strictEqual(escapeRegExp(unescaped + unescaped), escaped + escaped);
|
||||
expect(escapeRegExp(unescaped + unescaped)).toBe(escaped + escaped);
|
||||
});
|
||||
|
||||
it('should handle strings with nothing to escape', () => {
|
||||
assert.strictEqual(escapeRegExp('abc'), 'abc');
|
||||
expect(escapeRegExp('abc')).toBe('abc');
|
||||
});
|
||||
|
||||
it('should return an empty string for empty values', () => {
|
||||
const values = [, null, undefined, ''],
|
||||
expected = lodashStable.map(values, stubString);
|
||||
const values = [, null, undefined, ''];
|
||||
const expected = lodashStable.map(values, stubString);
|
||||
|
||||
const actual = lodashStable.map(values, (value, index) =>
|
||||
index ? escapeRegExp(value) : escapeRegExp(),
|
||||
);
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
});
|
||||
@@ -1,11 +1,10 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { identity, empties, stubTrue, stubFalse } from './utils';
|
||||
import every from '../src/every';
|
||||
|
||||
describe('every', () => {
|
||||
it('should return `true` if `predicate` returns truthy for all elements', () => {
|
||||
assert.strictEqual(lodashStable.every([true, 1, 'a'], identity), true);
|
||||
expect(lodashStable.every([true, 1, 'a'], identity)).toBe(true);
|
||||
});
|
||||
|
||||
it('should return `true` for empty collections', () => {
|
||||
@@ -17,7 +16,7 @@ describe('every', () => {
|
||||
} catch (e) {}
|
||||
});
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should return `false` as soon as `predicate` returns falsey', () => {
|
||||
@@ -31,23 +30,23 @@ describe('every', () => {
|
||||
false,
|
||||
);
|
||||
|
||||
assert.strictEqual(count, 2);
|
||||
expect(count).toBe(2);
|
||||
});
|
||||
|
||||
it('should work with collections of `undefined` values (test in IE < 9)', () => {
|
||||
assert.strictEqual(every([undefined, undefined, undefined], identity), false);
|
||||
expect(every([undefined, undefined, undefined], identity)).toBe(false);
|
||||
});
|
||||
|
||||
it('should use `_.identity` when `predicate` is nullish', () => {
|
||||
let values = [, null, undefined],
|
||||
expected = lodashStable.map(values, stubFalse);
|
||||
const values = [, null, undefined];
|
||||
let expected = lodashStable.map(values, stubFalse);
|
||||
|
||||
let actual = lodashStable.map(values, (value, index) => {
|
||||
const array = [0];
|
||||
return index ? every(array, value) : every(array);
|
||||
});
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
|
||||
expected = lodashStable.map(values, stubTrue);
|
||||
actual = lodashStable.map(values, (value, index) => {
|
||||
@@ -55,7 +54,7 @@ describe('every', () => {
|
||||
return index ? every(array, value) : every(array);
|
||||
});
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should work with `_.property` shorthands', () => {
|
||||
@@ -63,8 +62,8 @@ describe('every', () => {
|
||||
{ a: 0, b: 1 },
|
||||
{ a: 1, b: 2 },
|
||||
];
|
||||
assert.strictEqual(every(objects, 'a'), false);
|
||||
assert.strictEqual(every(objects, 'b'), true);
|
||||
expect(every(objects, 'a')).toBe(false);
|
||||
expect(every(objects, 'b')).toBe(true);
|
||||
});
|
||||
|
||||
it('should work with `_.matches` shorthands', () => {
|
||||
@@ -72,12 +71,12 @@ describe('every', () => {
|
||||
{ a: 0, b: 0 },
|
||||
{ a: 0, b: 1 },
|
||||
];
|
||||
assert.strictEqual(every(objects, { a: 0 }), true);
|
||||
assert.strictEqual(every(objects, { b: 1 }), false);
|
||||
expect(every(objects, { a: 0 })).toBe(true);
|
||||
expect(every(objects, { b: 1 })).toBe(false);
|
||||
});
|
||||
|
||||
it('should work as an iteratee for methods like `_.map`', () => {
|
||||
const actual = lodashStable.map([[1]], every);
|
||||
assert.deepStrictEqual(actual, [true]);
|
||||
expect(actual).toEqual([true]);
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,3 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { _ } from './utils';
|
||||
|
||||
@@ -19,8 +18,8 @@ describe('exit early', () => {
|
||||
|
||||
it(`\`_.${methodName}\` can exit early when iterating arrays`, () => {
|
||||
if (func) {
|
||||
const array = [1, 2, 3],
|
||||
values = [];
|
||||
const array = [1, 2, 3];
|
||||
const values = [];
|
||||
|
||||
func(array, (value, other) => {
|
||||
values.push(lodashStable.isArray(value) ? other : value);
|
||||
@@ -35,15 +34,15 @@ describe('exit early', () => {
|
||||
|
||||
it(`\`_.${methodName}\` can exit early when iterating objects`, () => {
|
||||
if (func) {
|
||||
const object = { a: 1, b: 2, c: 3 },
|
||||
values = [];
|
||||
const object = { a: 1, b: 2, c: 3 };
|
||||
const values = [];
|
||||
|
||||
func(object, (value, other) => {
|
||||
values.push(lodashStable.isArray(value) ? other : value);
|
||||
return false;
|
||||
});
|
||||
|
||||
assert.strictEqual(values.length, 1);
|
||||
expect(values.length).toBe(1);
|
||||
}
|
||||
});
|
||||
},
|
||||
@@ -1,60 +1,59 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { _ } from './utils';
|
||||
|
||||
describe('extremum methods', () => {
|
||||
lodashStable.each(['max', 'maxBy', 'min', 'minBy'], (methodName) => {
|
||||
const func = _[methodName],
|
||||
isMax = /^max/.test(methodName);
|
||||
const func = _[methodName];
|
||||
const isMax = /^max/.test(methodName);
|
||||
|
||||
it(`\`_.${methodName}\` should work with Date objects`, () => {
|
||||
const curr = new Date(),
|
||||
past = new Date(0);
|
||||
const curr = new Date();
|
||||
const past = new Date(0);
|
||||
|
||||
assert.strictEqual(func([curr, past]), isMax ? curr : past);
|
||||
expect(func([curr, past])).toBe(isMax ? curr : past);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should work with extremely large arrays`, () => {
|
||||
const array = lodashStable.range(0, 5e5);
|
||||
assert.strictEqual(func(array), isMax ? 499999 : 0);
|
||||
expect(func(array)).toBe(isMax ? 499999 : 0);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should work when chaining on an array with only one value`, () => {
|
||||
const actual = _([40])[methodName]();
|
||||
assert.strictEqual(actual, 40);
|
||||
expect(actual).toBe(40);
|
||||
});
|
||||
});
|
||||
|
||||
lodashStable.each(['maxBy', 'minBy'], (methodName) => {
|
||||
const array = [1, 2, 3],
|
||||
func = _[methodName],
|
||||
isMax = methodName === 'maxBy';
|
||||
const array = [1, 2, 3];
|
||||
const func = _[methodName];
|
||||
const isMax = methodName === 'maxBy';
|
||||
|
||||
it(`\`_.${methodName}\` should work with an \`iteratee\``, () => {
|
||||
const actual = func(array, (n) => -n);
|
||||
|
||||
assert.strictEqual(actual, isMax ? 1 : 3);
|
||||
expect(actual).toBe(isMax ? 1 : 3);
|
||||
});
|
||||
|
||||
it('should work with `_.property` shorthands', () => {
|
||||
let objects = [{ a: 2 }, { a: 3 }, { a: 1 }],
|
||||
actual = func(objects, 'a');
|
||||
const objects = [{ a: 2 }, { a: 3 }, { a: 1 }];
|
||||
let actual = func(objects, 'a');
|
||||
|
||||
assert.deepStrictEqual(actual, objects[isMax ? 1 : 2]);
|
||||
expect(actual).toEqual(objects[isMax ? 1 : 2]);
|
||||
|
||||
const arrays = [[2], [3], [1]];
|
||||
actual = func(arrays, 0);
|
||||
|
||||
assert.deepStrictEqual(actual, arrays[isMax ? 1 : 2]);
|
||||
expect(actual).toEqual(arrays[isMax ? 1 : 2]);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should work when \`iteratee\` returns +/-Infinity`, () => {
|
||||
const value = isMax ? -Infinity : Infinity,
|
||||
object = { a: value };
|
||||
const value = isMax ? -Infinity : Infinity;
|
||||
const object = { a: value };
|
||||
|
||||
const actual = func([object, { a: value }], (object) => object.a);
|
||||
|
||||
assert.strictEqual(actual, object);
|
||||
expect(actual).toBe(object);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,3 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { falsey } from './utils';
|
||||
import fill from '../src/fill';
|
||||
@@ -6,26 +5,26 @@ import fill from '../src/fill';
|
||||
describe('fill', () => {
|
||||
it('should use a default `start` of `0` and a default `end` of `length`', () => {
|
||||
const array = [1, 2, 3];
|
||||
assert.deepStrictEqual(fill(array, 'a'), ['a', 'a', 'a']);
|
||||
expect(fill(array, 'a'), ['a', 'a').toEqual('a']);
|
||||
});
|
||||
|
||||
it('should use `undefined` for `value` if not given', () => {
|
||||
const array = [1, 2, 3],
|
||||
actual = fill(array);
|
||||
const array = [1, 2, 3];
|
||||
const actual = fill(array);
|
||||
|
||||
assert.deepStrictEqual(actual, Array(3));
|
||||
assert.ok(lodashStable.every(actual, (value, index) => index in actual));
|
||||
expect(actual).toEqual(Array(3));
|
||||
expect(lodashStable.every(actual, (value, index) => index in actual))
|
||||
});
|
||||
|
||||
it('should work with a positive `start`', () => {
|
||||
const array = [1, 2, 3];
|
||||
assert.deepStrictEqual(fill(array, 'a', 1), [1, 'a', 'a']);
|
||||
expect(fill(array, 'a', 1), [1, 'a').toEqual('a']);
|
||||
});
|
||||
|
||||
it('should work with a `start` >= `length`', () => {
|
||||
lodashStable.each([3, 4, 2 ** 32, Infinity], (start) => {
|
||||
const array = [1, 2, 3];
|
||||
assert.deepStrictEqual(fill(array, 'a', start), [1, 2, 3]);
|
||||
expect(fill(array, 'a', start), [1, 2).toEqual(3]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -37,37 +36,37 @@ describe('fill', () => {
|
||||
return fill(array, 'a', start);
|
||||
});
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should work with a negative `start`', () => {
|
||||
const array = [1, 2, 3];
|
||||
assert.deepStrictEqual(fill(array, 'a', -1), [1, 2, 'a']);
|
||||
expect(fill(array, 'a', -1), [1, 2).toEqual('a']);
|
||||
});
|
||||
|
||||
it('should work with a negative `start` <= negative `length`', () => {
|
||||
lodashStable.each([-3, -4, -Infinity], (start) => {
|
||||
const array = [1, 2, 3];
|
||||
assert.deepStrictEqual(fill(array, 'a', start), ['a', 'a', 'a']);
|
||||
expect(fill(array, 'a', start), ['a', 'a').toEqual('a']);
|
||||
});
|
||||
});
|
||||
|
||||
it('should work with `start` >= `end`', () => {
|
||||
lodashStable.each([2, 3], (start) => {
|
||||
const array = [1, 2, 3];
|
||||
assert.deepStrictEqual(fill(array, 'a', start, 2), [1, 2, 3]);
|
||||
expect(fill(array, 'a', start, 2), [1, 2).toEqual(3]);
|
||||
});
|
||||
});
|
||||
|
||||
it('should work with a positive `end`', () => {
|
||||
const array = [1, 2, 3];
|
||||
assert.deepStrictEqual(fill(array, 'a', 0, 1), ['a', 2, 3]);
|
||||
expect(fill(array, 'a', 0, 1), ['a', 2).toEqual(3]);
|
||||
});
|
||||
|
||||
it('should work with a `end` >= `length`', () => {
|
||||
lodashStable.each([3, 4, 2 ** 32, Infinity], (end) => {
|
||||
const array = [1, 2, 3];
|
||||
assert.deepStrictEqual(fill(array, 'a', 0, end), ['a', 'a', 'a']);
|
||||
expect(fill(array, 'a', 0, end), ['a', 'a').toEqual('a']);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -81,18 +80,18 @@ describe('fill', () => {
|
||||
return fill(array, 'a', 0, end);
|
||||
});
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should work with a negative `end`', () => {
|
||||
const array = [1, 2, 3];
|
||||
assert.deepStrictEqual(fill(array, 'a', 0, -1), ['a', 'a', 3]);
|
||||
expect(fill(array, 'a', 0, -1), ['a', 'a').toEqual(3]);
|
||||
});
|
||||
|
||||
it('should work with a negative `end` <= negative `length`', () => {
|
||||
lodashStable.each([-3, -4, -Infinity], (end) => {
|
||||
const array = [1, 2, 3];
|
||||
assert.deepStrictEqual(fill(array, 'a', 0, end), [1, 2, 3]);
|
||||
expect(fill(array, 'a', 0, end), [1, 2).toEqual(3]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -116,10 +115,10 @@ describe('fill', () => {
|
||||
|
||||
it('should work as an iteratee for methods like `_.map`', () => {
|
||||
const array = [
|
||||
[1, 2],
|
||||
[3, 4],
|
||||
],
|
||||
actual = lodashStable.map(array, fill);
|
||||
[1, 2],
|
||||
[3, 4],
|
||||
];
|
||||
const actual = lodashStable.map(array, fill);
|
||||
|
||||
assert.deepStrictEqual(actual, [
|
||||
[0, 0],
|
||||
@@ -128,12 +127,12 @@ describe('fill', () => {
|
||||
});
|
||||
|
||||
it('should return a wrapped value when chaining', () => {
|
||||
const array = [1, 2, 3],
|
||||
wrapped = _(array).fill('a'),
|
||||
actual = wrapped.value();
|
||||
const array = [1, 2, 3];
|
||||
const wrapped = _(array).fill('a');
|
||||
const actual = wrapped.value();
|
||||
|
||||
assert.ok(wrapped instanceof _);
|
||||
assert.strictEqual(actual, array);
|
||||
assert.deepEqual(actual, ['a', 'a', 'a']);
|
||||
expect(wrapped instanceof _)
|
||||
expect(actual).toBe(array);
|
||||
expect(actual, ['a', 'a').toEqual('a']);
|
||||
});
|
||||
});
|
||||
@@ -1,13 +1,12 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { _, LARGE_ARRAY_SIZE, isEven, square } from './utils';
|
||||
|
||||
describe('filter methods', () => {
|
||||
lodashStable.each(['filter', 'reject'], (methodName) => {
|
||||
const array = [1, 2, 3, 4],
|
||||
func = _[methodName],
|
||||
isFilter = methodName === 'filter',
|
||||
objects = [{ a: 0 }, { a: 1 }];
|
||||
const array = [1, 2, 3, 4];
|
||||
const func = _[methodName];
|
||||
const isFilter = methodName === 'filter';
|
||||
const objects = [{ a: 0 }, { a: 1 }];
|
||||
|
||||
it(`\`_.${methodName}\` should not modify the resulting value from within \`predicate\``, () => {
|
||||
const actual = func([0], (value, index, array) => {
|
||||
@@ -15,15 +14,15 @@ describe('filter methods', () => {
|
||||
return isFilter;
|
||||
});
|
||||
|
||||
assert.deepStrictEqual(actual, [0]);
|
||||
expect(actual).toEqual([0]);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should work with \`_.property\` shorthands`, () => {
|
||||
assert.deepStrictEqual(func(objects, 'a'), [objects[isFilter ? 1 : 0]]);
|
||||
expect(func(objects, 'a')).toEqual([objects[isFilter ? 1 : 0]]);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should work with \`_.matches\` shorthands`, () => {
|
||||
assert.deepStrictEqual(func(objects, objects[1]), [objects[isFilter ? 1 : 0]]);
|
||||
expect(func(objects, objects[1])).toEqual([objects[isFilter ? 1 : 0]]);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should not modify wrapped values`, () => {
|
||||
@@ -31,18 +30,18 @@ describe('filter methods', () => {
|
||||
|
||||
let actual = wrapped[methodName]((n) => n < 3);
|
||||
|
||||
assert.deepEqual(actual.value(), isFilter ? [1, 2] : [3, 4]);
|
||||
expect(actual.value(), isFilter ? [1, 2] : [3).toEqual(4]);
|
||||
|
||||
actual = wrapped[methodName]((n) => n > 2);
|
||||
|
||||
assert.deepEqual(actual.value(), isFilter ? [3, 4] : [1, 2]);
|
||||
expect(actual.value(), isFilter ? [3, 4] : [1).toEqual(2]);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should work in a lazy sequence`, () => {
|
||||
const array = lodashStable.range(LARGE_ARRAY_SIZE + 1),
|
||||
predicate = function (value) {
|
||||
return isFilter ? isEven(value) : !isEven(value);
|
||||
};
|
||||
const array = lodashStable.range(LARGE_ARRAY_SIZE + 1);
|
||||
const predicate = function (value) {
|
||||
return isFilter ? isEven(value) : !isEven(value);
|
||||
};
|
||||
|
||||
const object = lodashStable.zipObject(
|
||||
lodashStable.times(LARGE_ARRAY_SIZE, (index) => [`key${index}`, index]),
|
||||
@@ -62,9 +61,9 @@ describe('filter methods', () => {
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should provide correct \`predicate\` arguments in a lazy sequence`, () => {
|
||||
let args,
|
||||
array = lodashStable.range(LARGE_ARRAY_SIZE + 1),
|
||||
expected = [1, 0, lodashStable.map(array.slice(1), square)];
|
||||
let args;
|
||||
const array = lodashStable.range(LARGE_ARRAY_SIZE + 1);
|
||||
const expected = [1, 0, lodashStable.map(array.slice(1), square)];
|
||||
|
||||
_(array)
|
||||
.slice(1)
|
||||
@@ -73,7 +72,7 @@ describe('filter methods', () => {
|
||||
})
|
||||
.value();
|
||||
|
||||
assert.deepEqual(args, [1, 0, array.slice(1)]);
|
||||
expect(args, [1, 0).toEqual(array.slice(1)]);
|
||||
|
||||
args = undefined;
|
||||
_(array)
|
||||
@@ -84,7 +83,7 @@ describe('filter methods', () => {
|
||||
})
|
||||
.value();
|
||||
|
||||
assert.deepEqual(args, expected);
|
||||
expect(args).toEqual(expected);
|
||||
|
||||
args = undefined;
|
||||
_(array)
|
||||
@@ -95,7 +94,7 @@ describe('filter methods', () => {
|
||||
})
|
||||
.value();
|
||||
|
||||
assert.deepEqual(args, expected);
|
||||
expect(args).toEqual(expected);
|
||||
|
||||
args = undefined;
|
||||
_(array)
|
||||
@@ -106,7 +105,7 @@ describe('filter methods', () => {
|
||||
})
|
||||
.value();
|
||||
|
||||
assert.deepEqual(args, [1]);
|
||||
expect(args).toEqual([1]);
|
||||
|
||||
args = undefined;
|
||||
_(array)
|
||||
@@ -117,7 +116,7 @@ describe('filter methods', () => {
|
||||
})
|
||||
.value();
|
||||
|
||||
assert.deepEqual(args, expected);
|
||||
expect(args).toEqual(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,3 @@
|
||||
import assert from 'node:assert';
|
||||
import { isEven } from './utils';
|
||||
import filter from '../src/filter';
|
||||
|
||||
@@ -6,6 +5,6 @@ describe('filter', () => {
|
||||
const array = [1, 2, 3];
|
||||
|
||||
it('should return elements `predicate` returns truthy for', () => {
|
||||
assert.deepStrictEqual(filter(array, isEven), [2]);
|
||||
expect(filter(array, isEven)).toEqual([2]);
|
||||
});
|
||||
});
|
||||
28
test/find-and-findLast.spec.js
Normal file
28
test/find-and-findLast.spec.js
Normal file
@@ -0,0 +1,28 @@
|
||||
import lodashStable from 'lodash';
|
||||
import { _, LARGE_ARRAY_SIZE, square, isEven } from './utils';
|
||||
|
||||
describe('find and findLast', () => {
|
||||
lodashStable.each(['find', 'findLast'], (methodName) => {
|
||||
const isFind = methodName === 'find';
|
||||
|
||||
it(`\`_.${methodName}\` should support shortcut fusion`, () => {
|
||||
let findCount = 0;
|
||||
let mapCount = 0;
|
||||
const array = lodashStable.range(1, LARGE_ARRAY_SIZE + 1);
|
||||
|
||||
const iteratee = function (value) {
|
||||
mapCount++;
|
||||
return square(value);
|
||||
};
|
||||
const predicate = function (value) {
|
||||
findCount++;
|
||||
return isEven(value);
|
||||
};
|
||||
const actual = _(array).map(iteratee)[methodName](predicate);
|
||||
|
||||
expect(findCount).toBe(isFind ? 2 : 1);
|
||||
expect(mapCount).toBe(isFind ? 2 : 1);
|
||||
expect(actual).toBe(isFind ? 4 : square(LARGE_ARRAY_SIZE));
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,28 +1,28 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { LARGE_ARRAY_SIZE, square, isEven } from './utils';
|
||||
import { _, LARGE_ARRAY_SIZE, square, isEven } from './utils';
|
||||
|
||||
describe('find and findLast', () => {
|
||||
lodashStable.each(['find', 'findLast'], (methodName) => {
|
||||
const isFind = methodName === 'find';
|
||||
|
||||
it(`\`_.${methodName}\` should support shortcut fusion`, () => {
|
||||
let findCount = 0,
|
||||
mapCount = 0,
|
||||
array = lodashStable.range(1, LARGE_ARRAY_SIZE + 1),
|
||||
iteratee = function (value) {
|
||||
mapCount++;
|
||||
return square(value);
|
||||
},
|
||||
predicate = function (value) {
|
||||
findCount++;
|
||||
return isEven(value);
|
||||
},
|
||||
actual = _(array).map(iteratee)[methodName](predicate);
|
||||
let findCount = 0;
|
||||
let mapCount = 0;
|
||||
const array = lodashStable.range(1, LARGE_ARRAY_SIZE + 1);
|
||||
|
||||
assert.strictEqual(findCount, isFind ? 2 : 1);
|
||||
assert.strictEqual(mapCount, isFind ? 2 : 1);
|
||||
assert.strictEqual(actual, isFind ? 4 : square(LARGE_ARRAY_SIZE));
|
||||
const iteratee = function (value) {
|
||||
mapCount++;
|
||||
return square(value);
|
||||
};
|
||||
const predicate = function (value) {
|
||||
findCount++;
|
||||
return isEven(value);
|
||||
};
|
||||
const actual = _(array).map(iteratee)[methodName](predicate);
|
||||
|
||||
expect(findCount).toBe(isFind ? 2 : 1);
|
||||
expect(mapCount).toBe(isFind ? 2 : 1);
|
||||
expect(actual).toBe(isFind ? 4 : square(LARGE_ARRAY_SIZE));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { _, identity, args, falsey } from './utils';
|
||||
|
||||
describe('find and includes', () => {
|
||||
lodashStable.each(['includes', 'find'], (methodName) => {
|
||||
const func = _[methodName],
|
||||
isIncludes = methodName === 'includes',
|
||||
resolve = methodName === 'find' ? lodashStable.curry(lodashStable.eq) : identity;
|
||||
const func = _[methodName];
|
||||
const isIncludes = methodName === 'includes';
|
||||
const resolve = methodName === 'find' ? lodashStable.curry(lodashStable.eq) : identity;
|
||||
|
||||
lodashStable.each(
|
||||
{
|
||||
@@ -24,7 +23,7 @@ describe('find and includes', () => {
|
||||
func(collection, resolve(values[1]), 2),
|
||||
];
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should work with ${key} and a \`fromIndex\` >= \`length\``, () => {
|
||||
@@ -41,7 +40,7 @@ describe('find and includes', () => {
|
||||
func(collection, resolve(''), fromIndex),
|
||||
]);
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should work with ${key} and treat falsey \`fromIndex\` values as \`0\``, () => {
|
||||
@@ -54,7 +53,7 @@ describe('find and includes', () => {
|
||||
func(collection, resolve(values[0]), fromIndex),
|
||||
);
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should work with ${key} and coerce \`fromIndex\` to an integer`, () => {
|
||||
@@ -70,7 +69,7 @@ describe('find and includes', () => {
|
||||
func(collection, resolve(values[0]), '1'),
|
||||
];
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should work with ${key} and a negative \`fromIndex\``, () => {
|
||||
@@ -81,21 +80,21 @@ describe('find and includes', () => {
|
||||
func(collection, resolve(values[1]), -1),
|
||||
];
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should work with ${key} and a negative \`fromIndex\` <= \`-length\``, () => {
|
||||
const indexes = [-4, -6, -Infinity],
|
||||
expected = lodashStable.map(
|
||||
indexes,
|
||||
lodashStable.constant(isIncludes || values[0]),
|
||||
);
|
||||
const indexes = [-4, -6, -Infinity];
|
||||
const expected = lodashStable.map(
|
||||
indexes,
|
||||
lodashStable.constant(isIncludes || values[0]),
|
||||
);
|
||||
|
||||
const actual = lodashStable.map(indexes, (fromIndex) =>
|
||||
func(collection, resolve(values[0]), fromIndex),
|
||||
);
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
},
|
||||
);
|
||||
@@ -1,4 +1,3 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { _, empties, LARGE_ARRAY_SIZE, slice } from './utils';
|
||||
import each from '../src/each';
|
||||
@@ -7,8 +6,8 @@ describe('find methods', () => {
|
||||
lodashStable.each(
|
||||
['find', 'findIndex', 'findKey', 'findLast', 'findLastIndex', 'findLastKey'],
|
||||
(methodName) => {
|
||||
const array = [1, 2, 3, 4],
|
||||
func = _[methodName];
|
||||
const array = [1, 2, 3, 4];
|
||||
const func = _[methodName];
|
||||
|
||||
const objects = [
|
||||
{ a: 0, b: 0 },
|
||||
@@ -26,36 +25,38 @@ describe('find methods', () => {
|
||||
}[methodName];
|
||||
|
||||
it(`\`_.${methodName}\` should return the found value`, () => {
|
||||
assert.strictEqual(
|
||||
func(objects, (object) => object.a),
|
||||
expected[0],
|
||||
expect(
|
||||
func(objects, (object) => object.a)
|
||||
).toEqual(
|
||||
expected[0]
|
||||
);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should return \`${expected[1]}\` if value is not found`, () => {
|
||||
assert.strictEqual(
|
||||
func(objects, (object) => object.a === 3),
|
||||
expected[1],
|
||||
expect(
|
||||
func(objects, (object) => object.a === 3)
|
||||
).toEqual(
|
||||
expected[1]
|
||||
);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should work with \`_.matches\` shorthands`, () => {
|
||||
assert.strictEqual(func(objects, { b: 2 }), expected[2]);
|
||||
expect(func(objects, { b: 2 })).toBe(expected[2]);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should work with \`_.matchesProperty\` shorthands`, () => {
|
||||
assert.strictEqual(func(objects, ['b', 2]), expected[2]);
|
||||
expect(func(objects, ['b', 2])).toBe(expected[2]);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should work with \`_.property\` shorthands`, () => {
|
||||
assert.strictEqual(func(objects, 'b'), expected[0]);
|
||||
expect(func(objects, 'b')).toBe(expected[0]);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should return \`${expected[1]}\` for empty collections`, () => {
|
||||
const emptyValues = lodashStable.endsWith(methodName, 'Index')
|
||||
? lodashStable.reject(empties, lodashStable.isPlainObject)
|
||||
: empties,
|
||||
expecting = lodashStable.map(emptyValues, lodashStable.constant(expected[1]));
|
||||
? lodashStable.reject(empties, lodashStable.isPlainObject)
|
||||
: empties;
|
||||
const expecting = lodashStable.map(emptyValues, lodashStable.constant(expected[1]));
|
||||
|
||||
const actual = lodashStable.map(emptyValues, (value) => {
|
||||
try {
|
||||
@@ -63,44 +64,7 @@ describe('find methods', () => {
|
||||
} catch (e) {}
|
||||
});
|
||||
|
||||
assert.deepStrictEqual(actual, expecting);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should return an unwrapped value when implicitly chaining`, () => {
|
||||
const expected = {
|
||||
find: 1,
|
||||
findIndex: 0,
|
||||
findKey: '0',
|
||||
findLast: 4,
|
||||
findLastIndex: 3,
|
||||
findLastKey: '3',
|
||||
}[methodName];
|
||||
|
||||
assert.strictEqual(_(array)[methodName](), expected);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should return a wrapped value when explicitly chaining`, () => {
|
||||
assert.ok(_(array).chain()[methodName]() instanceof _);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should not execute immediately when explicitly chaining`, () => {
|
||||
const wrapped = _(array).chain()[methodName]();
|
||||
assert.strictEqual(wrapped.__wrapped__, array);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should work in a lazy sequence`, () => {
|
||||
const largeArray = lodashStable.range(1, LARGE_ARRAY_SIZE + 1),
|
||||
smallArray = array;
|
||||
|
||||
lodashStable.times(2, (index) => {
|
||||
const array = index ? largeArray : smallArray,
|
||||
wrapped = _(array).filter(isEven);
|
||||
|
||||
assert.strictEqual(
|
||||
wrapped[methodName](),
|
||||
func(lodashStable.filter(array, isEven)),
|
||||
);
|
||||
});
|
||||
expect(actual).toEqual(expecting);
|
||||
});
|
||||
},
|
||||
),
|
||||
@@ -109,14 +73,14 @@ describe('find methods', () => {
|
||||
const func = _[methodName];
|
||||
|
||||
it(`\`_.${methodName}\` should provide correct \`predicate\` arguments for arrays`, () => {
|
||||
let args,
|
||||
array = ['a'];
|
||||
let args;
|
||||
const array = ['a'];
|
||||
|
||||
func(array, function () {
|
||||
args || (args = slice.call(arguments));
|
||||
});
|
||||
|
||||
assert.deepStrictEqual(args, ['a', 0, array]);
|
||||
expect(args, ['a', 0).toEqual(array]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -133,18 +97,18 @@ describe('find methods', () => {
|
||||
findLastKey: 'b',
|
||||
}[methodName];
|
||||
|
||||
assert.strictEqual(actual, expected);
|
||||
expect(actual).toBe(expected);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should provide correct \`predicate\` arguments for objects`, () => {
|
||||
let args,
|
||||
object = { a: 1 };
|
||||
let args;
|
||||
const object = { a: 1 };
|
||||
|
||||
func(object, function () {
|
||||
args || (args = slice.call(arguments));
|
||||
});
|
||||
|
||||
assert.deepStrictEqual(args, [1, 'a', object]);
|
||||
expect(args, [1, 'a').toEqual(object]);
|
||||
});
|
||||
});
|
||||
};
|
||||
@@ -1,4 +1,3 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { args, falsey } from './utils';
|
||||
import findLast from '../src/findLast';
|
||||
@@ -22,7 +21,7 @@ describe('findLast', () => {
|
||||
findLast(collection, resolve(values[2]), 1),
|
||||
];
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it(`should work with ${key} and a \`fromIndex\` >= \`length\``, () => {
|
||||
@@ -39,7 +38,7 @@ describe('findLast', () => {
|
||||
findLast(collection, resolve(''), fromIndex),
|
||||
]);
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it(`should work with ${key} and treat falsey \`fromIndex\` values correctly`, () => {
|
||||
@@ -51,7 +50,7 @@ describe('findLast', () => {
|
||||
findLast(collection, resolve(values[3]), fromIndex),
|
||||
);
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it(`should work with ${key} and coerce \`fromIndex\` to an integer`, () => {
|
||||
@@ -63,7 +62,7 @@ describe('findLast', () => {
|
||||
findLast(collection, resolve(values[2]), '1'),
|
||||
];
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it(`should work with ${key} and a negative \`fromIndex\``, () => {
|
||||
@@ -74,18 +73,18 @@ describe('findLast', () => {
|
||||
findLast(collection, resolve(values[2]), -2),
|
||||
];
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it(`should work with ${key} and a negative \`fromIndex\` <= \`-length\``, () => {
|
||||
const indexes = [-4, -6, -Infinity],
|
||||
expected = lodashStable.map(indexes, lodashStable.constant(values[0]));
|
||||
const indexes = [-4, -6, -Infinity];
|
||||
const expected = lodashStable.map(indexes, lodashStable.constant(values[0]));
|
||||
|
||||
const actual = lodashStable.map(indexes, (fromIndex) =>
|
||||
findLast(collection, resolve(values[0]), fromIndex),
|
||||
);
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
},
|
||||
);
|
||||
@@ -1,4 +1,3 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { identity, stubZero, falsey } from './utils';
|
||||
import findLastIndex from '../src/findLastIndex';
|
||||
@@ -11,22 +10,22 @@ const methods = {
|
||||
|
||||
describe('findLastIndex and lastIndexOf', () => {
|
||||
lodashStable.each(['findLastIndex', 'lastIndexOf'], (methodName) => {
|
||||
const array = [1, 2, 3, 1, 2, 3],
|
||||
func = methods[methodName],
|
||||
resolve =
|
||||
methodName === 'findLastIndex' ? lodashStable.curry(lodashStable.eq) : identity;
|
||||
const array = [1, 2, 3, 1, 2, 3];
|
||||
const func = methods[methodName];
|
||||
const resolve =
|
||||
methodName === 'findLastIndex' ? lodashStable.curry(lodashStable.eq) : identity;
|
||||
|
||||
it(`\`_.${methodName}\` should return the index of the last matched value`, () => {
|
||||
assert.strictEqual(func(array, resolve(3)), 5);
|
||||
expect(func(array, resolve(3))).toBe(5);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should work with a positive \`fromIndex\``, () => {
|
||||
assert.strictEqual(func(array, resolve(1), 2), 0);
|
||||
expect(func(array, resolve(1), 2)).toBe(0);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should work with a \`fromIndex\` >= \`length\``, () => {
|
||||
const values = [6, 8, 2 ** 32, Infinity],
|
||||
expected = lodashStable.map(values, lodashStable.constant([-1, 3, -1]));
|
||||
const values = [6, 8, 2 ** 32, Infinity];
|
||||
const expected = lodashStable.map(values, lodashStable.constant([-1, 3, -1]));
|
||||
|
||||
const actual = lodashStable.map(values, (fromIndex) => [
|
||||
func(array, resolve(undefined), fromIndex),
|
||||
@@ -34,22 +33,22 @@ describe('findLastIndex and lastIndexOf', () => {
|
||||
func(array, resolve(''), fromIndex),
|
||||
]);
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should work with a negative \`fromIndex\``, () => {
|
||||
assert.strictEqual(func(array, resolve(2), -3), 1);
|
||||
expect(func(array, resolve(2), -3)).toBe(1);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should work with a negative \`fromIndex\` <= \`-length\``, () => {
|
||||
const values = [-6, -8, -Infinity],
|
||||
expected = lodashStable.map(values, stubZero);
|
||||
const values = [-6, -8, -Infinity];
|
||||
const expected = lodashStable.map(values, stubZero);
|
||||
|
||||
const actual = lodashStable.map(values, (fromIndex) =>
|
||||
func(array, resolve(1), fromIndex),
|
||||
);
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should treat falsey \`fromIndex\` values correctly`, () => {
|
||||
@@ -59,11 +58,11 @@ describe('findLastIndex and lastIndexOf', () => {
|
||||
func(array, resolve(3), fromIndex),
|
||||
);
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should coerce \`fromIndex\` to an integer`, () => {
|
||||
assert.strictEqual(func(array, resolve(2), 4.2), 4);
|
||||
expect(func(array, resolve(2), 4.2)).toBe(4);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,26 +1,25 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { _, identity, falsey, stubArray } from './utils';
|
||||
|
||||
describe('flatMap methods', () => {
|
||||
lodashStable.each(['flatMap', 'flatMapDeep', 'flatMapDepth'], (methodName) => {
|
||||
const func = _[methodName],
|
||||
array = [1, 2, 3, 4];
|
||||
const func = _[methodName];
|
||||
const array = [1, 2, 3, 4];
|
||||
|
||||
function duplicate(n) {
|
||||
return [n, n];
|
||||
}
|
||||
|
||||
it(`\`_.${methodName}\` should map values in \`array\` to a new flattened array`, () => {
|
||||
const actual = func(array, duplicate),
|
||||
expected = lodashStable.flatten(lodashStable.map(array, duplicate));
|
||||
const actual = func(array, duplicate);
|
||||
const expected = lodashStable.flatten(lodashStable.map(array, duplicate));
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should work with \`_.property\` shorthands`, () => {
|
||||
const objects = [{ a: [1, 2] }, { a: [3, 4] }];
|
||||
assert.deepStrictEqual(func(objects, 'a'), array);
|
||||
expect(func(objects, 'a')).toEqual(array);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should iterate over own string keyed properties of objects`, () => {
|
||||
@@ -30,24 +29,24 @@ describe('flatMap methods', () => {
|
||||
Foo.prototype.b = [3, 4];
|
||||
|
||||
const actual = func(new Foo(), identity);
|
||||
assert.deepStrictEqual(actual, [1, 2]);
|
||||
expect(actual, [1).toEqual(2]);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should use \`_.identity\` when \`iteratee\` is nullish`, () => {
|
||||
const array = [
|
||||
[1, 2],
|
||||
[3, 4],
|
||||
],
|
||||
object = { a: [1, 2], b: [3, 4] },
|
||||
values = [, null, undefined],
|
||||
expected = lodashStable.map(values, lodashStable.constant([1, 2, 3, 4]));
|
||||
[1, 2],
|
||||
[3, 4],
|
||||
];
|
||||
const object = { a: [1, 2], b: [3, 4] };
|
||||
const values = [, null, undefined];
|
||||
const expected = lodashStable.map(values, lodashStable.constant([1, 2, 3, 4]));
|
||||
|
||||
lodashStable.each([array, object], (collection) => {
|
||||
const actual = lodashStable.map(values, (value, index) =>
|
||||
index ? func(collection, value) : func(collection),
|
||||
);
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -60,16 +59,16 @@ describe('flatMap methods', () => {
|
||||
} catch (e) {}
|
||||
});
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should treat number values for \`collection\` as empty`, () => {
|
||||
assert.deepStrictEqual(func(1), []);
|
||||
expect(func(1)).toEqual([]);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should work with objects with non-number length properties`, () => {
|
||||
const object = { length: [1, 2] };
|
||||
assert.deepStrictEqual(func(object, identity), [1, 2]);
|
||||
expect(func(object, identity), [1).toEqual(2]);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,3 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { identity } from './utils';
|
||||
import flatMapDepth from '../src/flatMapDepth';
|
||||
@@ -7,27 +6,27 @@ describe('flatMapDepth', () => {
|
||||
const array = [1, [2, [3, [4]], 5]];
|
||||
|
||||
it('should use a default `depth` of `1`', () => {
|
||||
assert.deepStrictEqual(flatMapDepth(array, identity), [1, 2, [3, [4]], 5]);
|
||||
expect(flatMapDepth(array, identity), [1, 2, [3, [4]]).toEqual(5]);
|
||||
});
|
||||
|
||||
it('should use `_.identity` when `iteratee` is nullish', () => {
|
||||
const values = [, null, undefined],
|
||||
expected = lodashStable.map(values, lodashStable.constant([1, 2, [3, [4]], 5]));
|
||||
const values = [, null, undefined];
|
||||
const expected = lodashStable.map(values, lodashStable.constant([1, 2, [3, [4]], 5]));
|
||||
|
||||
const actual = lodashStable.map(values, (value, index) =>
|
||||
index ? flatMapDepth(array, value) : flatMapDepth(array),
|
||||
);
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should treat a `depth` of < `1` as a shallow clone', () => {
|
||||
lodashStable.each([-1, 0], (depth) => {
|
||||
assert.deepStrictEqual(flatMapDepth(array, identity, depth), [1, [2, [3, [4]], 5]]);
|
||||
expect(flatMapDepth(array, identity, depth), [1, [2, [3, [4]]).toEqual(5]]);
|
||||
});
|
||||
});
|
||||
|
||||
it('should coerce `depth` to an integer', () => {
|
||||
assert.deepStrictEqual(flatMapDepth(array, identity, 2.2), [1, 2, 3, [4], 5]);
|
||||
expect(flatMapDepth(array, identity, 2.2), [1, 2, 3, [4]).toEqual(5]);
|
||||
});
|
||||
});
|
||||
103
test/flatten-methods.spec.js
Normal file
103
test/flatten-methods.spec.js
Normal file
@@ -0,0 +1,103 @@
|
||||
import lodashStable from 'lodash';
|
||||
import { args, _ } from './utils';
|
||||
import flatten from '../src/flatten';
|
||||
import flattenDeep from '../src/flattenDeep';
|
||||
import flattenDepth from '../src/flattenDepth';
|
||||
|
||||
describe('flatten methods', () => {
|
||||
const array = [1, [2, [3, [4]], 5]];
|
||||
const methodNames = ['flatten', 'flattenDeep', 'flattenDepth'];
|
||||
|
||||
it('should flatten `arguments` objects', () => {
|
||||
const array = [args, [args]];
|
||||
|
||||
expect(flatten(array), [1, 2, 3).toEqual(args]);
|
||||
expect(flattenDeep(array), [1, 2, 3, 1, 2).toEqual(3]);
|
||||
expect(flattenDepth(array, 2), [1, 2, 3, 1, 2).toEqual(3]);
|
||||
});
|
||||
|
||||
it('should treat sparse arrays as dense', () => {
|
||||
const array = [[1, 2, 3], Array(3)];
|
||||
const expected = [1, 2, 3];
|
||||
|
||||
expected.push(undefined, undefined, undefined);
|
||||
|
||||
lodashStable.each(methodNames, (methodName) => {
|
||||
const actual = _[methodName](array);
|
||||
expect(actual).toEqual(expected);
|
||||
expect('4' in actual)
|
||||
});
|
||||
});
|
||||
|
||||
it('should flatten objects with a truthy `Symbol.isConcatSpreadable` value', () => {
|
||||
if (Symbol && Symbol.isConcatSpreadable) {
|
||||
const object = { 0: 'a', length: 1 };
|
||||
const array = [object];
|
||||
const expected = lodashStable.map(methodNames, lodashStable.constant(['a']));
|
||||
|
||||
object[Symbol.isConcatSpreadable] = true;
|
||||
|
||||
const actual = lodashStable.map(methodNames, (methodName) => _[methodName](array));
|
||||
|
||||
expect(actual).toEqual(expected);
|
||||
}
|
||||
});
|
||||
|
||||
it('should work with extremely large arrays', () => {
|
||||
lodashStable.times(3, (index) => {
|
||||
const expected = Array(5e5);
|
||||
try {
|
||||
let func = flatten;
|
||||
if (index === 1) {
|
||||
func = flattenDeep;
|
||||
} else if (index === 2) {
|
||||
func = flattenDepth;
|
||||
}
|
||||
expect(func([expected])).toEqual(expected);
|
||||
} catch (e) {
|
||||
expect(false, e.message)
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('should work with empty arrays', () => {
|
||||
const array = [[], [[]], [[], [[[]]]]];
|
||||
|
||||
expect(flatten(array), [[], []).toEqual([[[]]]]);
|
||||
expect(flattenDeep(array)).toEqual([]);
|
||||
expect(flattenDepth(array, 2)).toEqual([[[]]]);
|
||||
});
|
||||
|
||||
it('should support flattening of nested arrays', () => {
|
||||
expect(flatten(array), [1, 2, [3, [4]]).toEqual(5]);
|
||||
expect(flattenDeep(array), [1, 2, 3, 4).toEqual(5]);
|
||||
expect(flattenDepth(array, 2), [1, 2, 3, [4]).toEqual(5]);
|
||||
});
|
||||
|
||||
it('should return an empty array for non array-like objects', () => {
|
||||
const expected = [];
|
||||
const nonArray = { 0: 'a' };
|
||||
|
||||
expect(flatten(nonArray)).toEqual(expected);
|
||||
expect(flattenDeep(nonArray)).toEqual(expected);
|
||||
expect(flattenDepth(nonArray, 2)).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should return a wrapped value when chaining', () => {
|
||||
const wrapped = _(array);
|
||||
let actual = wrapped.flatten();
|
||||
|
||||
expect(actual instanceof _)
|
||||
expect(actual.value(), [1, 2, [3, [4]]).toEqual(5]);
|
||||
|
||||
actual = wrapped.flattenDeep();
|
||||
|
||||
expect(actual instanceof _)
|
||||
expect(actual.value(), [1, 2, 3, 4).toEqual(5]);
|
||||
|
||||
actual = wrapped.flattenDepth(2);
|
||||
|
||||
expect(actual instanceof _)
|
||||
expect(actual.value(), [1, 2, 3, [4]).toEqual(5]);
|
||||
});
|
||||
});
|
||||
@@ -1,104 +0,0 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { args, _ } from './utils';
|
||||
import flatten from '../src/flatten';
|
||||
import flattenDeep from '../src/flattenDeep';
|
||||
import flattenDepth from '../src/flattenDepth';
|
||||
|
||||
describe('flatten methods', () => {
|
||||
const array = [1, [2, [3, [4]], 5]],
|
||||
methodNames = ['flatten', 'flattenDeep', 'flattenDepth'];
|
||||
|
||||
it('should flatten `arguments` objects', () => {
|
||||
const array = [args, [args]];
|
||||
|
||||
assert.deepStrictEqual(flatten(array), [1, 2, 3, args]);
|
||||
assert.deepStrictEqual(flattenDeep(array), [1, 2, 3, 1, 2, 3]);
|
||||
assert.deepStrictEqual(flattenDepth(array, 2), [1, 2, 3, 1, 2, 3]);
|
||||
});
|
||||
|
||||
it('should treat sparse arrays as dense', () => {
|
||||
const array = [[1, 2, 3], Array(3)],
|
||||
expected = [1, 2, 3];
|
||||
|
||||
expected.push(undefined, undefined, undefined);
|
||||
|
||||
lodashStable.each(methodNames, (methodName) => {
|
||||
const actual = _[methodName](array);
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
assert.ok('4' in actual);
|
||||
});
|
||||
});
|
||||
|
||||
it('should flatten objects with a truthy `Symbol.isConcatSpreadable` value', () => {
|
||||
if (Symbol && Symbol.isConcatSpreadable) {
|
||||
const object = { '0': 'a', length: 1 },
|
||||
array = [object],
|
||||
expected = lodashStable.map(methodNames, lodashStable.constant(['a']));
|
||||
|
||||
object[Symbol.isConcatSpreadable] = true;
|
||||
|
||||
const actual = lodashStable.map(methodNames, (methodName) => _[methodName](array));
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
}
|
||||
});
|
||||
|
||||
it('should work with extremely large arrays', () => {
|
||||
lodashStable.times(3, (index) => {
|
||||
const expected = Array(5e5);
|
||||
try {
|
||||
let func = flatten;
|
||||
if (index === 1) {
|
||||
func = flattenDeep;
|
||||
} else if (index === 2) {
|
||||
func = flattenDepth;
|
||||
}
|
||||
assert.deepStrictEqual(func([expected]), expected);
|
||||
} catch (e) {
|
||||
assert.ok(false, e.message);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('should work with empty arrays', () => {
|
||||
const array = [[], [[]], [[], [[[]]]]];
|
||||
|
||||
assert.deepStrictEqual(flatten(array), [[], [], [[[]]]]);
|
||||
assert.deepStrictEqual(flattenDeep(array), []);
|
||||
assert.deepStrictEqual(flattenDepth(array, 2), [[[]]]);
|
||||
});
|
||||
|
||||
it('should support flattening of nested arrays', () => {
|
||||
assert.deepStrictEqual(flatten(array), [1, 2, [3, [4]], 5]);
|
||||
assert.deepStrictEqual(flattenDeep(array), [1, 2, 3, 4, 5]);
|
||||
assert.deepStrictEqual(flattenDepth(array, 2), [1, 2, 3, [4], 5]);
|
||||
});
|
||||
|
||||
it('should return an empty array for non array-like objects', () => {
|
||||
const expected = [],
|
||||
nonArray = { '0': 'a' };
|
||||
|
||||
assert.deepStrictEqual(flatten(nonArray), expected);
|
||||
assert.deepStrictEqual(flattenDeep(nonArray), expected);
|
||||
assert.deepStrictEqual(flattenDepth(nonArray, 2), expected);
|
||||
});
|
||||
|
||||
it('should return a wrapped value when chaining', () => {
|
||||
let wrapped = _(array),
|
||||
actual = wrapped.flatten();
|
||||
|
||||
assert.ok(actual instanceof _);
|
||||
assert.deepEqual(actual.value(), [1, 2, [3, [4]], 5]);
|
||||
|
||||
actual = wrapped.flattenDeep();
|
||||
|
||||
assert.ok(actual instanceof _);
|
||||
assert.deepEqual(actual.value(), [1, 2, 3, 4, 5]);
|
||||
|
||||
actual = wrapped.flattenDepth(2);
|
||||
|
||||
assert.ok(actual instanceof _);
|
||||
assert.deepEqual(actual.value(), [1, 2, 3, [4], 5]);
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,3 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import flattenDepth from '../src/flattenDepth';
|
||||
|
||||
@@ -6,16 +5,16 @@ describe('flattenDepth', () => {
|
||||
const array = [1, [2, [3, [4]], 5]];
|
||||
|
||||
it('should use a default `depth` of `1`', () => {
|
||||
assert.deepStrictEqual(flattenDepth(array), [1, 2, [3, [4]], 5]);
|
||||
expect(flattenDepth(array), [1, 2, [3, [4]]).toEqual(5]);
|
||||
});
|
||||
|
||||
it('should treat a `depth` of < `1` as a shallow clone', () => {
|
||||
lodashStable.each([-1, 0], (depth) => {
|
||||
assert.deepStrictEqual(flattenDepth(array, depth), [1, [2, [3, [4]], 5]]);
|
||||
expect(flattenDepth(array, depth), [1, [2, [3, [4]]).toEqual(5]]);
|
||||
});
|
||||
});
|
||||
|
||||
it('should coerce `depth` to an integer', () => {
|
||||
assert.deepStrictEqual(flattenDepth(array, 2.2), [1, 2, 3, [4], 5]);
|
||||
expect(flattenDepth(array, 2.2), [1, 2, 3, [4]).toEqual(5]);
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,3 @@
|
||||
import assert from 'node:assert';
|
||||
import { slice } from './utils';
|
||||
import flip from '../src/flip';
|
||||
|
||||
@@ -9,6 +8,6 @@ describe('flip', () => {
|
||||
|
||||
it('should flip arguments provided to `func`', () => {
|
||||
const flipped = flip(fn);
|
||||
assert.deepStrictEqual(flipped('a', 'b', 'c', 'd'), ['d', 'c', 'b', 'a']);
|
||||
expect(flipped('a', 'b', 'c', 'd'), ['d', 'c', 'b').toEqual('a']);
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,3 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { add, square, noop, identity } from './utils';
|
||||
import head from '../src/head';
|
||||
@@ -14,16 +13,16 @@ const methods = {
|
||||
|
||||
describe('flow methods', () => {
|
||||
lodashStable.each(['flow', 'flowRight'], (methodName) => {
|
||||
const func = methods[methodName],
|
||||
isFlow = methodName === 'flow';
|
||||
const func = methods[methodName];
|
||||
const isFlow = methodName === 'flow';
|
||||
|
||||
it(`\`_.${methodName}\` should supply each function with the return value of the previous`, () => {
|
||||
const fixed = function (n) {
|
||||
return n.toFixed(1);
|
||||
},
|
||||
combined = isFlow ? func(add, square, fixed) : func(fixed, square, add);
|
||||
return n.toFixed(1);
|
||||
};
|
||||
const combined = isFlow ? func(add, square, fixed) : func(fixed, square, add);
|
||||
|
||||
assert.strictEqual(combined(1, 2), '9.0');
|
||||
expect(combined(1, 2)).toBe('9.0');
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should return a new function`, () => {
|
||||
@@ -35,17 +34,17 @@ describe('flow methods', () => {
|
||||
|
||||
const combined = isFlow ? func(head, curried) : func(curried, head);
|
||||
|
||||
assert.strictEqual(combined([1]), 1);
|
||||
expect(combined([1])).toBe(1);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should work with curried functions with placeholders`, () => {
|
||||
const curried = lodashStable.curry(lodashStable.ary(map, 2), 2),
|
||||
getProp = curried(curried.placeholder, (value) => value.a),
|
||||
objects = [{ a: 1 }, { a: 2 }, { a: 1 }];
|
||||
const curried = lodashStable.curry(lodashStable.ary(map, 2), 2);
|
||||
const getProp = curried(curried.placeholder, (value) => value.a);
|
||||
const objects = [{ a: 1 }, { a: 2 }, { a: 1 }];
|
||||
|
||||
const combined = isFlow ? func(getProp, uniq) : func(uniq, getProp);
|
||||
|
||||
assert.deepStrictEqual(combined(objects), [1, 2]);
|
||||
expect(combined(objects), [1).toEqual(2]);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,9 +1,8 @@
|
||||
import assert from 'node:assert';
|
||||
import each from '../src/each';
|
||||
import forEach from '../src/forEach';
|
||||
|
||||
describe('forEach', () => {
|
||||
it('should be aliased', () => {
|
||||
assert.strictEqual(each, forEach);
|
||||
expect(each).toBe(forEach);
|
||||
});
|
||||
});
|
||||
@@ -1,9 +1,8 @@
|
||||
import assert from 'node:assert';
|
||||
import eachRight from '../src/eachRight';
|
||||
import forEachRight from '../src/forEachRight';
|
||||
|
||||
describe('forEachRight', () => {
|
||||
it('should be aliased', () => {
|
||||
assert.strictEqual(eachRight, forEachRight);
|
||||
expect(eachRight).toBe(forEachRight);
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,3 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { _ } from './utils';
|
||||
|
||||
@@ -16,7 +15,7 @@ describe('forIn methods', () => {
|
||||
func(new Foo(), (value, key) => {
|
||||
keys.push(key);
|
||||
});
|
||||
assert.deepStrictEqual(keys.sort(), ['a', 'b']);
|
||||
expect(keys.sort(), ['a').toEqual('b']);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,3 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { _ } from './utils';
|
||||
|
||||
@@ -7,13 +6,13 @@ describe('forOwn methods', () => {
|
||||
const func = _[methodName];
|
||||
|
||||
it(`\`_.${methodName}\` should iterate over \`length\` properties`, () => {
|
||||
const object = { '0': 'zero', '1': 'one', length: 2 },
|
||||
props = [];
|
||||
const object = { 0: 'zero', 1: 'one', length: 2 };
|
||||
const props = [];
|
||||
|
||||
func(object, (value, prop) => {
|
||||
props.push(prop);
|
||||
});
|
||||
assert.deepStrictEqual(props.sort(), ['0', '1', 'length']);
|
||||
expect(props.sort(), ['0', '1').toEqual('length']);
|
||||
});
|
||||
});
|
||||
});
|
||||
39
test/fromPairs.spec.js
Normal file
39
test/fromPairs.spec.js
Normal file
@@ -0,0 +1,39 @@
|
||||
import lodashStable from 'lodash';
|
||||
import { falsey, stubObject } from './utils';
|
||||
import fromPairs from '../src/fromPairs';
|
||||
import toPairs from '../src/toPairs';
|
||||
|
||||
describe('fromPairs', () => {
|
||||
it('should accept a two dimensional array', () => {
|
||||
const array = [
|
||||
['a', 1],
|
||||
['b', 2],
|
||||
];
|
||||
const object = { a: 1, b: 2 };
|
||||
const actual = fromPairs(array);
|
||||
|
||||
expect(actual).toEqual(object);
|
||||
});
|
||||
|
||||
it('should accept a falsey `array`', () => {
|
||||
const expected = lodashStable.map(falsey, stubObject);
|
||||
|
||||
const actual = lodashStable.map(falsey, (array, index) => {
|
||||
try {
|
||||
return index ? fromPairs(array) : fromPairs();
|
||||
} catch (e) {}
|
||||
});
|
||||
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should not support deep paths', () => {
|
||||
const actual = fromPairs([['a.b', 1]]);
|
||||
expect(actual).toEqual({ 'a.b': 1 });
|
||||
});
|
||||
|
||||
it('should support consuming the return value of `_.toPairs`', () => {
|
||||
const object = { 'a.b': 1 };
|
||||
expect(fromPairs(toPairs(object))).toEqual(object);
|
||||
});
|
||||
});
|
||||
@@ -1,48 +0,0 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { falsey, stubObject, LARGE_ARRAY_SIZE } from './utils';
|
||||
import fromPairs from '../src/fromPairs';
|
||||
import toPairs from '../src/toPairs';
|
||||
|
||||
describe('fromPairs', () => {
|
||||
it('should accept a two dimensional array', () => {
|
||||
const array = [
|
||||
['a', 1],
|
||||
['b', 2],
|
||||
],
|
||||
object = { a: 1, b: 2 },
|
||||
actual = fromPairs(array);
|
||||
|
||||
assert.deepStrictEqual(actual, object);
|
||||
});
|
||||
|
||||
it('should accept a falsey `array`', () => {
|
||||
const expected = lodashStable.map(falsey, stubObject);
|
||||
|
||||
const actual = lodashStable.map(falsey, (array, index) => {
|
||||
try {
|
||||
return index ? fromPairs(array) : fromPairs();
|
||||
} catch (e) {}
|
||||
});
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
});
|
||||
|
||||
it('should not support deep paths', () => {
|
||||
const actual = fromPairs([['a.b', 1]]);
|
||||
assert.deepStrictEqual(actual, { 'a.b': 1 });
|
||||
});
|
||||
|
||||
it('should support consuming the return value of `_.toPairs`', () => {
|
||||
const object = { 'a.b': 1 };
|
||||
assert.deepStrictEqual(fromPairs(toPairs(object)), object);
|
||||
});
|
||||
|
||||
it('should work in a lazy sequence', () => {
|
||||
const array = lodashStable.times(LARGE_ARRAY_SIZE, (index) => [`key${index}`, index]);
|
||||
|
||||
const actual = _(array).fromPairs().map(square).filter(isEven).take().value();
|
||||
|
||||
assert.deepEqual(actual, _.take(_.filter(_.map(fromPairs(array), square), isEven)));
|
||||
});
|
||||
});
|
||||
@@ -1,13 +1,12 @@
|
||||
import assert from 'node:assert';
|
||||
import { identity, noop } from './utils';
|
||||
import functions from '../src/functions';
|
||||
|
||||
describe('functions', () => {
|
||||
it('should return the function names of an object', () => {
|
||||
const object = { a: 'a', b: identity, c: /x/, d: noop },
|
||||
actual = functions(object).sort();
|
||||
const object = { a: 'a', b: identity, c: /x/, d: noop };
|
||||
const actual = functions(object).sort();
|
||||
|
||||
assert.deepStrictEqual(actual, ['b', 'd']);
|
||||
expect(actual, ['b').toEqual('d']);
|
||||
});
|
||||
|
||||
it('should not include inherited functions', () => {
|
||||
@@ -17,6 +16,6 @@ describe('functions', () => {
|
||||
}
|
||||
Foo.prototype.c = noop;
|
||||
|
||||
assert.deepStrictEqual(functions(new Foo()), ['a']);
|
||||
expect(functions(new Foo())).toEqual(['a']);
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,3 @@
|
||||
import assert from 'node:assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { _, symbol, noop, numberProto, empties } from './utils';
|
||||
|
||||
@@ -10,17 +9,17 @@ describe('get and result', () => {
|
||||
const object = { a: 1 };
|
||||
|
||||
lodashStable.each(['a', ['a']], (path) => {
|
||||
assert.strictEqual(func(object, path), 1);
|
||||
expect(func(object, path)).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should preserve the sign of \`0\``, () => {
|
||||
const object = { '-0': 'a', '0': 'b' },
|
||||
props = [-0, Object(-0), 0, Object(0)];
|
||||
const object = { '-0': 'a', 0: 'b' };
|
||||
const props = [-0, Object(-0), 0, Object(0)];
|
||||
|
||||
const actual = lodashStable.map(props, (key) => func(object, key));
|
||||
|
||||
assert.deepStrictEqual(actual, ['a', 'a', 'b', 'b']);
|
||||
expect(actual).toEqual(['a', 'a', 'b', 'b']);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should get symbol keyed property values`, () => {
|
||||
@@ -28,7 +27,7 @@ describe('get and result', () => {
|
||||
const object = {};
|
||||
object[symbol] = 1;
|
||||
|
||||
assert.strictEqual(func(object, symbol), 1);
|
||||
expect(func(object, symbol)).toBe(1);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -36,7 +35,7 @@ describe('get and result', () => {
|
||||
const object = { a: { b: 2 } };
|
||||
|
||||
lodashStable.each(['a.b', ['a', 'b']], (path) => {
|
||||
assert.strictEqual(func(object, path), 2);
|
||||
expect(func(object, path)).toBe(2);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -44,18 +43,18 @@ describe('get and result', () => {
|
||||
const object = { 'a.b': 1, a: { b: 2 } };
|
||||
|
||||
lodashStable.each(['a.b', ['a.b']], (path) => {
|
||||
assert.strictEqual(func(object, path), 1);
|
||||
expect(func(object, path)).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should not coerce array paths to strings`, () => {
|
||||
const object = { 'a,b,c': 3, a: { b: { c: 4 } } };
|
||||
assert.strictEqual(func(object, ['a', 'b', 'c']), 4);
|
||||
expect(func(object, ['a', 'b', 'c'])).toBe(4);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should not ignore empty brackets`, () => {
|
||||
const object = { a: { '': 1 } };
|
||||
assert.strictEqual(func(object, 'a[]'), 1);
|
||||
expect(func(object, 'a[]')).toBe(1);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should handle empty paths`, () => {
|
||||
@@ -65,8 +64,8 @@ describe('get and result', () => {
|
||||
[[], ['']],
|
||||
],
|
||||
(pair) => {
|
||||
assert.strictEqual(func({}, pair[0]), undefined);
|
||||
assert.strictEqual(func({ '': 3 }, pair[1]), 3);
|
||||
expect(func({}, pair[0])).toBe(undefined);
|
||||
expect(func({ '': 3 }, pair[1])).toBe(3);
|
||||
},
|
||||
);
|
||||
});
|
||||
@@ -82,26 +81,29 @@ describe('get and result', () => {
|
||||
];
|
||||
|
||||
lodashStable.each(paths, (path) => {
|
||||
assert.strictEqual(func(object, path), 8);
|
||||
expect(func(object, path)).toBe(8);
|
||||
});
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should return \`undefined\` when \`object\` is nullish`, () => {
|
||||
lodashStable.each(['constructor', ['constructor']], (path) => {
|
||||
assert.strictEqual(func(null, path), undefined);
|
||||
assert.strictEqual(func(undefined, path), undefined);
|
||||
expect(func(null, path)).toBe(undefined);
|
||||
expect(func(undefined, path)).toBe(undefined);
|
||||
});
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should return \`undefined\` for deep paths when \`object\` is nullish`, () => {
|
||||
const values = [null, undefined],
|
||||
expected = lodashStable.map(values, noop),
|
||||
paths = ['constructor.prototype.valueOf', ['constructor', 'prototype', 'valueOf']];
|
||||
const values = [null, undefined];
|
||||
const expected = lodashStable.map(values, noop);
|
||||
const paths = [
|
||||
'constructor.prototype.valueOf',
|
||||
['constructor', 'prototype', 'valueOf'],
|
||||
];
|
||||
|
||||
lodashStable.each(paths, (path) => {
|
||||
const actual = lodashStable.map(values, (value) => func(value, path));
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -109,7 +111,7 @@ describe('get and result', () => {
|
||||
const object = { a: [, null] };
|
||||
|
||||
lodashStable.each(['a[1].b.c', ['a', '1', 'b', 'c']], (path) => {
|
||||
assert.strictEqual(func(object, path), undefined);
|
||||
expect(func(object, path)).toBe(undefined);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -117,7 +119,7 @@ describe('get and result', () => {
|
||||
const object = { a: { b: null } };
|
||||
|
||||
lodashStable.each(['a.b', ['a', 'b']], (path) => {
|
||||
assert.strictEqual(func(object, path), null);
|
||||
expect(func(object, path)).toBe(null);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -126,15 +128,15 @@ describe('get and result', () => {
|
||||
|
||||
lodashStable.each(paths, (path) => {
|
||||
numberProto.a = { b: 2 };
|
||||
assert.strictEqual(func(0, path), 2);
|
||||
expect(func(0, path)).toBe(2);
|
||||
delete numberProto.a;
|
||||
});
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should return the default value for \`undefined\` values`, () => {
|
||||
const object = { a: {} },
|
||||
values = empties.concat(true, new Date(), 1, /x/, 'a'),
|
||||
expected = lodashStable.map(values, (value) => [value, value]);
|
||||
const object = { a: {} };
|
||||
const values = empties.concat(true, new Date(), 1, /x/, 'a');
|
||||
const expected = lodashStable.map(values, (value) => [value, value]);
|
||||
|
||||
lodashStable.each(['a.b', ['a', 'b']], (path) => {
|
||||
const actual = lodashStable.map(values, (value) => [
|
||||
@@ -142,12 +144,12 @@ describe('get and result', () => {
|
||||
func(null, path, value),
|
||||
]);
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should return the default value when \`path\` is empty`, () => {
|
||||
assert.strictEqual(func({}, [], 'a'), 'a');
|
||||
expect(func({}, [], 'a')).toBe('a');
|
||||
});
|
||||
});
|
||||
});
|
||||
65
test/groupBy.spec.js
Normal file
65
test/groupBy.spec.js
Normal file
@@ -0,0 +1,65 @@
|
||||
import lodashStable from 'lodash';
|
||||
import groupBy from '../src/groupBy';
|
||||
|
||||
describe('groupBy', () => {
|
||||
const array = [6.1, 4.2, 6.3];
|
||||
|
||||
it('should transform keys by `iteratee`', () => {
|
||||
const actual = groupBy(array, Math.floor);
|
||||
expect(actual).toEqual({ 4: [4.2], 6: [6.1, 6.3] });
|
||||
});
|
||||
|
||||
it('should use `_.identity` when `iteratee` is nullish', () => {
|
||||
const array = [6, 4, 6];
|
||||
const values = [, null, undefined];
|
||||
const expected = lodashStable.map(values, lodashStable.constant({ 4: [4], 6: [6, 6] }));
|
||||
|
||||
const actual = lodashStable.map(values, (value, index) =>
|
||||
index ? groupBy(array, value) : groupBy(array),
|
||||
);
|
||||
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should work with `_.property` shorthands', () => {
|
||||
const actual = groupBy(['one', 'two', 'three'], 'length');
|
||||
expect(actual).toEqual({ 3: ['one', 'two'], 5: ['three'] });
|
||||
});
|
||||
|
||||
it('should only add values to own, not inherited, properties', () => {
|
||||
const actual = groupBy(array, (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 a number for `iteratee`', () => {
|
||||
const array = [
|
||||
[1, 'a'],
|
||||
[2, 'a'],
|
||||
[2, 'b'],
|
||||
];
|
||||
|
||||
assert.deepStrictEqual(groupBy(array, 0), {
|
||||
1: [[1, 'a']],
|
||||
2: [
|
||||
[2, 'a'],
|
||||
[2, 'b'],
|
||||
],
|
||||
});
|
||||
assert.deepStrictEqual(groupBy(array, 1), {
|
||||
a: [
|
||||
[1, 'a'],
|
||||
[2, 'a'],
|
||||
],
|
||||
b: [[2, 'b']],
|
||||
});
|
||||
});
|
||||
|
||||
it('should work with an object for `collection`', () => {
|
||||
const actual = groupBy({ a: 6.1, b: 4.2, c: 6.3 }, Math.floor);
|
||||
expect(actual).toEqual({ 4: [4.2], 6: [6.1, 6.3] });
|
||||
});
|
||||
});
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user