source: imaps-frontend/node_modules/es-abstract/2024/AddValueToKeyedGroup.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: 1.3 KB
Line 
1'use strict';
2
3var $TypeError = require('es-errors/type');
4
5var callBound = require('call-bind/callBound');
6
7var $push = callBound('Array.prototype.push');
8
9var SameValue = require('./SameValue');
10
11var IsArray = require('../helpers/IsArray');
12var every = require('../helpers/every');
13var forEach = require('../helpers/forEach');
14
15var hasOwn = require('hasown');
16
17var isKeyedGroup = function (group) {
18 return hasOwn(group, '[[Key]]')
19 && hasOwn(group, '[[Elements]]')
20 && IsArray(group['[[Elements]]']);
21};
22
23// https://tc39.es/ecma262/#sec-add-value-to-keyed-group
24
25module.exports = function AddValueToKeyedGroup(groups, key, value) {
26 if (!IsArray(groups) || (groups.length > 0 && !every(groups, isKeyedGroup))) {
27 throw new $TypeError('Assertion failed: `groups` must be a List of Records with [[Key]] and [[Elements]]');
28 }
29
30 var matched = 0;
31 forEach(groups, function (g) { // step 1
32 if (SameValue(g['[[Key]]'], key)) { // step 2
33 matched += 1;
34 if (matched > 1) {
35 throw new $TypeError('Assertion failed: Exactly one element of groups meets this criterion'); // step 2.a
36 }
37
38 $push(g['[[Elements]]'], value); // step 2.b
39 }
40 });
41
42 if (matched === 0) {
43 var group = { '[[Key]]': key, '[[Elements]]': [value] }; // step 2
44
45 $push(groups, group); // step 3
46 }
47};
Note: See TracBrowser for help on using the repository browser.