@nevware21/ts-utils
    Preparing search index...

    Function polyArrFrom

    • The polyArrFrom creates an new shallow-copied array from an array-like object or an iterable.

      Like the native Array.from():

      • A value with an iterator function ([Symbol.iterator], which is only read once) is iterated using the iterator it returns, so an iterator which is not iterable (eg. one created by createIterator or createRangeIterator) returns an empty array (use iterToArray to read its values). Otherwise (when the iterator function is null or undefined) the value is used as an array-like value (eg. arguments or a NodeList), so every index up to its length is read (an empty slot or missing index is undefined) and any other value (eg. a number) returns an empty array. An array is always read using its indexes, like the native array iterator its length is read for each index, so a value added to it while it is read (eg. by the mapFn) is also read and the reading stops at its new length when values are removed, while (like the native function) any other array-like is read up to the length it had when it was called.
      • A string (or a String object) is split into its code points (so a surrogate pair is a single value), this is also done for runtimes which don't support Symbol.iterator.
      • A Map returns its [key, value] entries and a Set its values, this is also done (since 0.20.0) for a Map or Set which has no iterator function (eg. on IE 11) using its forEach().
      • The mapFn is called with the value and its index (without using its own call property), and each value is added as an own property of the new array (even when Array.prototype has a setter or read-only property for the index).
      • A null or undefined value, a mapFn which is not a function, an iterator function which is not a function or one which doesn't return an iterator, or an iterator result which is not an object throws a TypeError, and a length which is longer than an array can be (2^32 - 1) throws a RangeError.

      Type Parameters

      • T

        Identifies the element type of the array-like or iterable.

      • U = T

        Identifies returned type of the array

      Parameters

      • theValue: ArrayLike<T> | Iterable<T>

        An array-like object or iterable to convert to an array.

      • OptionalmapFn: ArrFromMapFn<T, U>

        A mapping function to call on every element of the array. If provided, every value to be added to the array is first passed through this map function, and the return value is added to the array instead. The function is called with the value and its index (mapFn(value, index)) and the thisArg as this.

      • OptionalthisArg: any

        Value of 'this' used to invoke the mapfn.

      Returns U[]

      0.9.7

      polyArrFrom("Hello");
      // [ "H", "e", "l", "l", "o" ]

      polyArrFrom(new Set(["Hello", "Darkness", "my", "old", "friend"]));
      // ["Hello", "Darkness", "my", "old", "friend"]

      let map = new Map([
      [ 1, "Hello" ],
      [ 2, "Darkness" ],
      [ 3, "my" ],
      [ 4, "old" ],
      [ 5, "friend"]
      ]);

      polyArrFrom(map.values());
      // ["Hello", "Darkness", "my", "old", "friend"]

      polyArrFrom(map.keys());
      // [ 1, 2, 3, 4, 5 ]

      polyArrFrom(map.entries());
      // [ [ 1, "Hello" ], [ 2, "Darkness" ], [ 3, "my" ], [ 4, "old" ], [ 5, "friend"] ]

      polyArrFrom(map, ([ key, value ]) => ({ [key]: value }));
      // [ {"1": "Hello"}, {"2": "Darkness"}, {"3": "my"}, {"4": "old"}, {"5": "friend"} ]