source: frontend/node_modules/es-abstract/2024/AddValueToKeyedGroup.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 1.3 KB
Line 
1'use strict';
2
3var $TypeError = require('es-errors/type');
4
5var SameValue = require('./SameValue');
6
7var IsArray = require('../helpers/IsArray');
8var every = require('../helpers/every');
9var forEach = require('../helpers/forEach');
10
11var hasOwn = require('hasown');
12
13var isKeyedGroup = function (group) {
14 return hasOwn(group, '[[Key]]')
15 && hasOwn(group, '[[Elements]]')
16 && IsArray(group['[[Elements]]']);
17};
18
19// https://262.ecma-international.org/15.0/#sec-add-value-to-keyed-group
20
21module.exports = function AddValueToKeyedGroup(groups, key, value) {
22 if (!IsArray(groups) || (groups.length > 0 && !every(groups, isKeyedGroup))) {
23 throw new $TypeError('Assertion failed: `groups` must be a List of Records with [[Key]] and [[Elements]]');
24 }
25
26 var matched = 0;
27 forEach(groups, function (g) { // step 1
28 if (SameValue(g['[[Key]]'], key)) { // step 2
29 matched += 1;
30 if (matched > 1) {
31 throw new $TypeError('Assertion failed: Exactly one element of groups meets this criterion'); // step 2.a
32 }
33
34 var arr = g['[[Elements]]'];
35 arr[arr.length] = value; // step 2.b
36 }
37 });
38
39 if (matched === 0) {
40 var group = { '[[Key]]': key, '[[Elements]]': [value] }; // step 2
41
42 // eslint-disable-next-line no-param-reassign
43 groups[groups.length] = group; // step 3
44 }
45};
Note: See TracBrowser for help on using the repository browser.