diff --git a/index.html b/index.html index d96dd1d..d886de3 100644 --- a/index.html +++ b/index.html @@ -2264,7 +2264,7 @@

2.1 Array.prototype.groupBy ( callbackfnThe return value of groupBy is an object that does not inherit from Object.prototype.

When the groupBy method is called with one or two arguments, the following steps are taken:

-
  1. Let O be ? ToObject(this value).
  2. Let len be ? LengthOfArrayLike(O).
  3. If IsCallable(callbackfn) is false, throw a TypeError exception.
  4. Let k be 0.
  5. Let obj be ! OrdinaryObjectCreate(null).
  6. Assert: obj is an extensible ordinary object with no own properties.
  7. Repeat, while k < len
    1. Let Pk be ! ToString(k).
    2. Let kValue be ? Get(O, Pk).
    3. Let propertyKey be ? ToPropertyKey(? Call(callbackfn, thisArg, « kValue, k, O »)).
    4. Let seen be ? HasOwnProperty(obj, propertyKey).
    5. If seen is true, then
      1. Let arr be ! Get(obj, propertyKey).
    6. Else
      1. Let arr be ? ArraySpeciesCreate(O, 0).
      2. Perform ! CreateDataPropertyOrThrow(obj, propertyKey, arr).
    7. Perform ? Call(%Array.prototype.push%, arr, kValue).
    8. Set k to k + 1.
  8. Return obj.
+
  1. Let O be ? ToObject(this value).
  2. Let len be ? LengthOfArrayLike(O).
  3. If IsCallable(callbackfn) is false, throw a TypeError exception.
  4. Let k be 0.
  5. Let groups be a new empty List.
  6. Repeat, while k < len
    1. Let Pk be ! ToString(k).
    2. Let kValue be ? Get(O, Pk).
    3. Let propertyKey be ? ToPropertyKey(? Call(callbackfn, thisArg, « kValue, k, O »)).
    4. Let group be empty.
    5. For each Record { [[Key]], [[Elements]] } g of groups, do
      1. If ! SameValueNonNumeric(g.[[Key]], propertyKey) is true, then
        1. Set group to g.
    6. If group is empty, then
      1. Let elements be a new empty List.
      2. Set group to the Record { [[Key]]: propertyKey, [[Elements]]: elements }.
      3. Append group as the last element of groups.
    7. Append kValue as the last element of group.[[Elements]].
    8. Set k to k + 1.
  7. Let obj be ! OrdinaryObjectCreate(null).
  8. For each Record { [[Key]], [[Elements]] } g of groups, do
    1. Let len be the number of elements in g.[[Elements]].
    2. Let elements be ? ArraySpeciesCreate(O, len).
    3. Let i be 0.
    4. For each element e of g.[[Elements]], do
      1. Perform ? CreateDataPropertyOrThrow(elements, ! ToString(𝔽(i)), e).
      2. Set i to i + 1.
    5. Perform ! CreateDataPropertyOrThrow(obj, g.[[Key]], elements).
  9. Return obj.
Note 2

The groupBy function is intentionally generic; it does not require that its this value be an Array object. Therefore it can be transferred to other kinds of objects for use as a method.

diff --git a/spec.emu b/spec.emu index 1cf3a80..810c816 100644 --- a/spec.emu +++ b/spec.emu @@ -38,20 +38,30 @@ location: https://tc39.es/proposal-array-grouping/ 1. Let _len_ be ? LengthOfArrayLike(_O_). 1. If IsCallable(_callbackfn_) is *false*, throw a *TypeError* exception. 1. Let _k_ be 0. - 1. Let _obj_ be ! OrdinaryObjectCreate(null). - 1. Assert: _obj_ is an extensible ordinary object with no own properties. + 1. Let _groups_ be a new empty List. 1. Repeat, while _k_ < _len_ 1. Let _Pk_ be ! ToString(_k_). 1. Let _kValue_ be ? Get(_O_, _Pk_). 1. Let _propertyKey_ be ? ToPropertyKey(? Call(_callbackfn_, _thisArg_, « _kValue_, _k_, _O_ »)). - 1. Let _seen_ be ? HasOwnProperty(_obj_, _propertyKey_). - 1. If _seen_ is *true*, then - 1. Let _arr_ be ! Get(_obj_, _propertyKey_). - 1. Else - 1. Let _arr_ be ? ArraySpeciesCreate(O, 0). - 1. Perform ! CreateDataPropertyOrThrow(_obj_, _propertyKey_, _arr_). - 1. Perform ? Call(%Array.prototype.push%, _arr_, _kValue_). + 1. Let _group_ be ~empty~. + 1. For each Record { [[Key]], [[Elements]] } _g_ of _groups_, do + 1. If ! SameValueNonNumeric(_g_.[[Key]], _propertyKey_) is *true*, then + 1. Set _group_ to _g_. + 1. If _group_ is ~empty~, then + 1. Let _elements_ be a new empty List. + 1. Set _group_ to the Record { [[Key]]: _propertyKey_, [[Elements]]: _elements_ }. + 1. Append _group_ as the last element of _groups_. + 1. Append _kValue_ as the last element of _group_.[[Elements]]. 1. Set _k_ to _k_ + 1. + 1. Let _obj_ be ! OrdinaryObjectCreate(*null*). + 1. For each Record { [[Key]], [[Elements]] } _g_ of _groups_, do + 1. Let _len_ be the number of elements in _g_.[[Elements]]. + 1. Let _elements_ be ? ArraySpeciesCreate(_O_, _len_). + 1. Let _i_ be 0. + 1. For each element _e_ of _g_.[[Elements]], do + 1. Perform ? CreateDataPropertyOrThrow(_elements_, ! ToString(𝔽(_i_)), _e_). + 1. Set _i_ to _i_ + 1. + 1. Perform ! CreateDataPropertyOrThrow(_obj_, _g_.[[Key]], _elements_). 1. Return _obj_.