GroupBy.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. 'use strict';
  2. var $TypeError = require('es-errors/type');
  3. var AddValueToKeyedGroup = require('./AddValueToKeyedGroup');
  4. var Call = require('./Call');
  5. var GetIterator = require('./GetIterator');
  6. var IsCallable = require('./IsCallable');
  7. var IteratorClose = require('./IteratorClose');
  8. var IteratorStep = require('./IteratorStep');
  9. var IteratorValue = require('./IteratorValue');
  10. var RequireObjectCoercible = require('./RequireObjectCoercible');
  11. var ThrowCompletion = require('./ThrowCompletion');
  12. var ToPropertyKey = require('./ToPropertyKey');
  13. var isNegativeZero = require('../helpers/isNegativeZero');
  14. var maxSafeInteger = require('../helpers/maxSafeInteger');
  15. // https://tc39.es/ecma262/#sec-groupby
  16. module.exports = function GroupBy(items, callbackfn, keyCoercion) {
  17. if (keyCoercion !== 'PROPERTY' && keyCoercion !== 'ZERO') {
  18. throw new $TypeError('Assertion failed: `keyCoercion` must be `"PROPERTY"` or `"ZERO"`');
  19. }
  20. RequireObjectCoercible(items); // step 1
  21. if (!IsCallable(callbackfn)) {
  22. throw new $TypeError('callbackfn must be callable'); // step 2
  23. }
  24. var groups = []; // step 3
  25. var iteratorRecord = GetIterator(items, 'SYNC'); // step 4
  26. var k = 0; // step 5
  27. // eslint-disable-next-line no-constant-condition
  28. while (true) { // step 6
  29. if (k >= maxSafeInteger) { // step 6.a
  30. var error = ThrowCompletion(new $TypeError('k must be less than 2 ** 53 - 1')); // step 6.a.i
  31. return IteratorClose(iteratorRecord, error); // step 6.a.ii
  32. }
  33. var next = IteratorStep(iteratorRecord); // step 6.b
  34. if (!next) { // step 6.c
  35. return groups; // step 6.c.i
  36. }
  37. var value = IteratorValue(next); // step 6.dv
  38. var key;
  39. try {
  40. key = Call(callbackfn, undefined, [value, k]); // step 6.e
  41. } catch (e) {
  42. return IteratorClose(iteratorRecord, ThrowCompletion(e)); // step 6.f
  43. }
  44. if (keyCoercion === 'PROPERTY') { // step 6.g
  45. try {
  46. key = ToPropertyKey(key); // step 6.g.i
  47. } catch (e) {
  48. return IteratorClose(iteratorRecord, ThrowCompletion(e)); // step 6.g.ii
  49. }
  50. } else { // step 6.h
  51. if (keyCoercion !== 'ZERO') {
  52. throw new $TypeError('keyCoercion must be ~PROPERTY~ or ~ZERO~'); // step 6.h.i
  53. }
  54. if (isNegativeZero(key)) {
  55. key = +0; // step 6.h.ii
  56. }
  57. }
  58. AddValueToKeyedGroup(groups, key, value); // step 6.i
  59. k += 1; // step 6.j
  60. }
  61. };