AddValueToKeyedGroup.js 899 B

1234567891011121314151617181920212223242526
  1. 'use strict';
  2. var callBound = require('call-bind/callBound');
  3. var GetIntrinsic = require('get-intrinsic');
  4. var SameValue = require('es-abstract/2022/SameValue');
  5. var $TypeError = GetIntrinsic('%TypeError%');
  6. var $filter = callBound('Array.prototype.filter');
  7. var $push = callBound('Array.prototype.push');
  8. module.exports = function AddValueToKeyedGroup(groups, key, value) {
  9. var found = $filter(groups, function (group) {
  10. return SameValue(group['[[Key]]'], key); // eslint-disable-line new-cap
  11. });
  12. if (found.length > 0) {
  13. var g = found[0];
  14. if (found.length !== 1) {
  15. throw new $TypeError('Assertion failed: more than 1 Record inside `groups` has a `[[Key]]` that is SameValue to `key`');
  16. }
  17. $push(g['[[Elements]]'], value); // step 1.a.ii
  18. } else {
  19. var group = { '[[Key]]': key, '[[Elements]]': [value] }; // eslint-disable-line sort-keys
  20. $push(groups, group); // step 3
  21. }
  22. };