[d565449] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | var $TypeError = require('es-errors/type');
|
---|
| 4 |
|
---|
| 5 | var AddValueToKeyedGroup = require('./AddValueToKeyedGroup');
|
---|
| 6 | var Call = require('./Call');
|
---|
| 7 | var GetIterator = require('./GetIterator');
|
---|
| 8 | var IsCallable = require('./IsCallable');
|
---|
| 9 | var IteratorClose = require('./IteratorClose');
|
---|
| 10 | var IteratorStep = require('./IteratorStep');
|
---|
| 11 | var IteratorValue = require('./IteratorValue');
|
---|
| 12 | var RequireObjectCoercible = require('./RequireObjectCoercible');
|
---|
| 13 | var ThrowCompletion = require('./ThrowCompletion');
|
---|
| 14 | var ToPropertyKey = require('./ToPropertyKey');
|
---|
| 15 |
|
---|
| 16 | var isNegativeZero = require('../helpers/isNegativeZero');
|
---|
| 17 | var maxSafeInteger = require('../helpers/maxSafeInteger');
|
---|
| 18 |
|
---|
| 19 | // https://tc39.es/ecma262/#sec-groupby
|
---|
| 20 |
|
---|
| 21 | module.exports = function GroupBy(items, callbackfn, keyCoercion) {
|
---|
| 22 | if (keyCoercion !== 'PROPERTY' && keyCoercion !== 'ZERO') {
|
---|
| 23 | throw new $TypeError('Assertion failed: `keyCoercion` must be `"PROPERTY"` or `"ZERO"`');
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 | RequireObjectCoercible(items); // step 1
|
---|
| 27 |
|
---|
| 28 | if (!IsCallable(callbackfn)) {
|
---|
| 29 | throw new $TypeError('callbackfn must be callable'); // step 2
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | var groups = []; // step 3
|
---|
| 33 |
|
---|
| 34 | var iteratorRecord = GetIterator(items, 'SYNC'); // step 4
|
---|
| 35 |
|
---|
| 36 | var k = 0; // step 5
|
---|
| 37 |
|
---|
| 38 | // eslint-disable-next-line no-constant-condition
|
---|
| 39 | while (true) { // step 6
|
---|
| 40 | if (k >= maxSafeInteger) { // step 6.a
|
---|
| 41 | var error = ThrowCompletion(new $TypeError('k must be less than 2 ** 53 - 1')); // step 6.a.i
|
---|
| 42 | return IteratorClose(iteratorRecord, error); // step 6.a.ii
|
---|
| 43 | }
|
---|
| 44 | var next = IteratorStep(iteratorRecord); // step 6.b
|
---|
| 45 | if (!next) { // step 6.c
|
---|
| 46 | return groups; // step 6.c.i
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | var value = IteratorValue(next); // step 6.dv
|
---|
| 50 |
|
---|
| 51 | var key;
|
---|
| 52 | try {
|
---|
| 53 | key = Call(callbackfn, undefined, [value, k]); // step 6.e
|
---|
| 54 | } catch (e) {
|
---|
| 55 | return IteratorClose(iteratorRecord, ThrowCompletion(e)); // step 6.f
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | if (keyCoercion === 'PROPERTY') { // step 6.g
|
---|
| 59 | try {
|
---|
| 60 | key = ToPropertyKey(key); // step 6.g.i
|
---|
| 61 | } catch (e) {
|
---|
| 62 | return IteratorClose(iteratorRecord, ThrowCompletion(e)); // step 6.g.ii
|
---|
| 63 | }
|
---|
| 64 | } else { // step 6.h
|
---|
| 65 | if (keyCoercion !== 'ZERO') {
|
---|
| 66 | throw new $TypeError('keyCoercion must be ~PROPERTY~ or ~ZERO~'); // step 6.h.i
|
---|
| 67 | }
|
---|
| 68 | if (isNegativeZero(key)) {
|
---|
| 69 | key = +0; // step 6.h.ii
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | AddValueToKeyedGroup(groups, key, value); // step 6.i
|
---|
| 74 |
|
---|
| 75 | k += 1; // step 6.j
|
---|
| 76 | }
|
---|
| 77 | };
|
---|