AddValueToKeyedGroup.js 850 B

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