mirror of
https://github.com/hanzoai/core-js.git
synced 2026-09-24 16:58:15 +00:00
36 lines
1.4 KiB
JavaScript
36 lines
1.4 KiB
JavaScript
import { createIterable } from '../helpers/helpers.js';
|
|
|
|
QUnit.test('Object.groupBy', assert => {
|
|
const { groupBy, getPrototypeOf, entries } = Object;
|
|
|
|
assert.isFunction(groupBy);
|
|
assert.arity(groupBy, 2);
|
|
assert.name(groupBy, 'groupBy');
|
|
assert.looksNative(groupBy);
|
|
assert.nonEnumerable(Object, 'groupBy');
|
|
|
|
assert.same(getPrototypeOf(groupBy([], it => it)), null, 'null proto');
|
|
|
|
assert.deepEqual(entries(groupBy([], it => it)), []);
|
|
assert.deepEqual(entries(groupBy([1, 2], it => it ** 2)), [['1', [1]], ['4', [2]]]);
|
|
assert.deepEqual(entries(groupBy([1, 2, 1], it => it ** 2)), [['1', [1, 1]], ['4', [2]]]);
|
|
assert.deepEqual(entries(groupBy(createIterable([1, 2]), it => it ** 2)), [['1', [1]], ['4', [2]]]);
|
|
assert.deepEqual(entries(groupBy('qwe', it => it)), [['q', ['q']], ['w', ['w']], ['e', ['e']]], 'iterable string');
|
|
|
|
const element = {};
|
|
groupBy([element], function (it, i) {
|
|
assert.same(arguments.length, 2, 'correct number of callback arguments');
|
|
assert.same(it, element, 'correct value in callback');
|
|
assert.same(i, 0, 'correct index in callback');
|
|
});
|
|
|
|
const even = Symbol('even');
|
|
const odd = Symbol('odd');
|
|
const grouped = Object.groupBy([1, 2, 3, 4, 5, 6], num => {
|
|
if (num % 2 === 0) return even;
|
|
return odd;
|
|
});
|
|
assert.deepEqual(grouped[even], [2, 4, 6]);
|
|
assert.deepEqual(grouped[odd], [1, 3, 5]);
|
|
});
|