Stage 1 Draft / October 10, 2024

Array.zip

23 Array Objects

23.1 The Array Constructor

23.1.2 Properties of the Array Constructor

23.1.2.5 Array.zip ( iterables [ , options ] )

This method performs the following steps when called:

  1. If iterables is not an Object, throw a TypeError exception.
  2. Set options to ? GetOptionsObject(options).
  3. Let mode be ? Get(options, "mode").
  4. If mode is undefined, set mode to "shortest".
  5. If mode is not one of "shortest", "longest", or "strict", throw a TypeError exception.
  6. Let paddingOption be undefined.
  7. If mode is "longest", then
    1. Set paddingOption to ? Get(options, "padding").
    2. If paddingOption is not undefined and paddingOption is not an Object, throw a TypeError exception.
  8. Let iters be a new empty List.
  9. Let padding be a new empty List.
  10. Let inputIter be ? GetIterator(iterables, sync).
  11. Let next be not-started.
  12. Repeat, while next is not done,
    1. Set next to Completion(IteratorStepValue(inputIter)).
    2. IfAbruptCloseIterators(next, iters).
    3. If next is not done, then
      1. Let iter be Completion(GetIteratorFlattenable(next, reject-strings)).
      2. IfAbruptCloseIterators(iter, the list-concatenation of « inputIter » and iters).
      3. Append iter to iters.
  13. Let iterCount be the number of elements in iters.
  14. If mode is "longest", then
    1. If paddingOption is undefined, then
      1. Perform the following steps iterCount times:
        1. Append undefined to padding.
    2. Else,
      1. Let paddingIter be Completion(GetIterator(paddingOption, sync)).
      2. IfAbruptCloseIterators(paddingIter, iters).
      3. If paddingIter is a String, throw a TypeError exception.
      4. Let usingIterator be true.
      5. Perform the following steps iterCount times:
        1. If usingIterator is true, then
          1. Set next to Completion(IteratorStepValue(paddingIter)).
          2. IfAbruptCloseIterators(next, iters).
          3. If next is done, then
            1. Set usingIterator to false.
          4. Else,
            1. Append next to padding.
        2. If usingIterator is false, append undefined to padding.
      6. If usingIterator is true, then
        1. Let completion be Completion(IteratorClose(paddingIter, ReturnCompletion(undefined))).
        2. IfAbruptCloseIterators(completion, iters).
  15. Let finishResults be a new Abstract Closure with parameters (results) that captures nothing and performs the following steps when called:
    1. Return CreateArrayFromList(results).
  16. Let gen be IteratorZip(iters, mode, padding, finishResults).
  17. Let items be a new empty List.
  18. Repeat,
    1. Let value be ? IteratorStepValue(gen).
    2. If value is done, return CreateArrayFromList(items).
    3. Append value to items.

23.1.2.6 Array.zipKeyed ( iterables [ , options ] )

This method performs the following steps when called:

  1. If iterables is not an Object, throw a TypeError exception.
  2. Set options to ? GetOptionsObject(options).
  3. Let mode be ? Get(options, "mode").
  4. If mode is undefined, set mode to "shortest".
  5. If mode is not one of "shortest", "longest", or "strict", throw a TypeError exception.
  6. Let paddingOption be undefined.
  7. If mode is "longest", then
    1. Set paddingOption to ? Get(options, "padding").
    2. If paddingOption is not undefined and paddingOption is not an Object, throw a TypeError exception.
  8. Let iters be a new empty List.
  9. Let padding be a new empty List.
  10. Let allKeys be ? iterables.[[OwnPropertyKeys]]().
  11. Let keys be a new empty List.
  12. For each element key of allKeys, do
    1. Let desc be ? iterables.[[GetOwnProperty]](key).
    2. If desc is not undefined and desc.[[Enumerable]] is true, then
      1. Let value be undefined.
      2. If IsDataDescriptor(desc) is true, then
        1. Set value to desc.[[Value]].
      3. Else,
        1. Assert: IsAccessorDescriptor(desc) is true.
        2. Let getter be desc.[[Get]].
        3. If getter is not undefined, then
          1. Let getterResult be Completion(Call(getter, iterables)).
          2. IfAbruptCloseIterators(getterResult, iters).
          3. Set value to getterResult.
      4. If value is not undefined, then
        1. Append key to keys.
        2. Let iter be Completion(GetIteratorFlattenable(value, reject-strings)).
        3. IfAbruptCloseIterators(iter, iters).
        4. Append iter to iters.
  13. Let iterCount be the number of elements in iters.
  14. If mode is "longest", then
    1. If paddingOption is undefined, then
      1. Perform the following steps iterCount times:
        1. Append undefined to padding.
    2. Else,
      1. For each element key of keys, do
        1. Let value be Completion(Get(paddingOption, key)).
        2. IfAbruptCloseIterators(value, iters).
        3. Append value to padding.
  15. Let finishResults be a new Abstract Closure with parameters (results) that captures keys and iterCount and performs the following steps when called:
    1. Let obj be OrdinaryObjectCreate(null).
    2. For each integer i such that 0 ≤ i < iterCount, in ascending order, do
      1. Perform ! CreateDataPropertyOrThrow(obj, keys[i], results[i]).
    3. Return obj.
  16. Let gen be IteratorZip(iters, mode, padding, finishResults).
  17. Let items be a new empty List.
  18. Repeat,
    1. Let value be ? IteratorStepValue(gen).
    2. If value is done, return CreateArrayFromList(items).
    3. Append value to items.

A Copyright & Software License

Copyright Notice

© 2024 Jordan Harband

Software License

All Software contained in this document ("Software") is protected by copyright and is being made available under the "BSD License", included below. This Software may be subject to third party rights (rights from parties other than Ecma International), including patent rights, and no licenses under such third party rights are granted under this license even if the third party concerned is a member of Ecma International. SEE THE ECMA CODE OF CONDUCT IN PATENT MATTERS AVAILABLE AT https://ecma-international.org/memento/codeofconduct.htm FOR INFORMATION REGARDING THE LICENSING OF PATENT CLAIMS THAT ARE REQUIRED TO IMPLEMENT ECMA INTERNATIONAL STANDARDS.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  3. Neither the name of the authors nor Ecma International may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE ECMA INTERNATIONAL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ECMA INTERNATIONAL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.