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

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 2.4 KB
RevLine 
[d565449]1'use strict';
2
3var $TypeError = require('es-errors/type');
[79a0317]4var isNegativeZero = require('math-intrinsics/isNegativeZero');
5var MAX_SAFE_INTEGER = require('math-intrinsics/constants/maxSafeInteger');
[d565449]6
7var AddValueToKeyedGroup = require('./AddValueToKeyedGroup');
8var Call = require('./Call');
9var GetIterator = require('./GetIterator');
10var IsCallable = require('./IsCallable');
11var IteratorClose = require('./IteratorClose');
12var IteratorStep = require('./IteratorStep');
13var IteratorValue = require('./IteratorValue');
14var RequireObjectCoercible = require('./RequireObjectCoercible');
15var ThrowCompletion = require('./ThrowCompletion');
16var ToPropertyKey = require('./ToPropertyKey');
17
[0c6b92a]18// https://262.ecma-international.org/15.0/#sec-groupby
[d565449]19
20module.exports = function GroupBy(items, callbackfn, keyCoercion) {
21 if (keyCoercion !== 'PROPERTY' && keyCoercion !== 'ZERO') {
22 throw new $TypeError('Assertion failed: `keyCoercion` must be `"PROPERTY"` or `"ZERO"`');
23 }
24
25 RequireObjectCoercible(items); // step 1
26
27 if (!IsCallable(callbackfn)) {
28 throw new $TypeError('callbackfn must be callable'); // step 2
29 }
30
31 var groups = []; // step 3
32
33 var iteratorRecord = GetIterator(items, 'SYNC'); // step 4
34
35 var k = 0; // step 5
36
37 // eslint-disable-next-line no-constant-condition
38 while (true) { // step 6
[79a0317]39 if (k >= MAX_SAFE_INTEGER) { // step 6.a
[d565449]40 var error = ThrowCompletion(new $TypeError('k must be less than 2 ** 53 - 1')); // step 6.a.i
[79a0317]41 IteratorClose(iteratorRecord, error); // step 6.a.ii
42 return void undefined;
[d565449]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) {
[79a0317]55 IteratorClose(iteratorRecord, ThrowCompletion(e)); // step 6.f
56 return void undefined;
[d565449]57 }
58
59 if (keyCoercion === 'PROPERTY') { // step 6.g
60 try {
61 key = ToPropertyKey(key); // step 6.g.i
62 } catch (e) {
[79a0317]63 IteratorClose(iteratorRecord, ThrowCompletion(e)); // step 6.g.ii
64 return void undefined;
[d565449]65 }
66 } else { // step 6.h
67 if (keyCoercion !== 'ZERO') {
68 throw new $TypeError('keyCoercion must be ~PROPERTY~ or ~ZERO~'); // step 6.h.i
69 }
70 if (isNegativeZero(key)) {
71 key = +0; // step 6.h.ii
72 }
73 }
74
75 AddValueToKeyedGroup(groups, key, value); // step 6.i
76
77 k += 1; // step 6.j
78 }
79};
Note: See TracBrowser for help on using the repository browser.