source: imaps-frontend/node_modules/es-abstract/2024/GroupBy.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 2.3 KB
RevLine 
[d565449]1'use strict';
2
3var $TypeError = require('es-errors/type');
4
5var AddValueToKeyedGroup = require('./AddValueToKeyedGroup');
6var Call = require('./Call');
7var GetIterator = require('./GetIterator');
8var IsCallable = require('./IsCallable');
9var IteratorClose = require('./IteratorClose');
10var IteratorStep = require('./IteratorStep');
11var IteratorValue = require('./IteratorValue');
12var RequireObjectCoercible = require('./RequireObjectCoercible');
13var ThrowCompletion = require('./ThrowCompletion');
14var ToPropertyKey = require('./ToPropertyKey');
15
16var isNegativeZero = require('../helpers/isNegativeZero');
17var maxSafeInteger = require('../helpers/maxSafeInteger');
18
19// https://tc39.es/ecma262/#sec-groupby
20
21module.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};
Note: See TracBrowser for help on using the repository browser.