froebel
Version:
TypeScript utility library
47 lines (40 loc) • 1.51 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.unzipWith = exports.default = void 0;
/**
* Reverse of {@link zip}. Takes a list of tuples and deconstructs them into
* an array (of length of the tuples length) of lists each containing all the
* elements in all tuples at the lists index.
*
* @example
* const [nums, chars] = unzip([1,'a'], [2,'b'], [3,'c'])
* console.log(nums) // prints: [1, 2, 3]
* console.log(chars) // prints: ['a','b','c']
*/
const unzip = (...zipped) => zipped.reduce((a, c) => c.map((v, i) => [...(a[i] ?? []), v]), []);
var _default = unzip;
/**
* Same as {@link unzip} but accepts an `unzipper` function for each tuple
* index. The `unzipper`'s return value is used as the value in the list at
* that index returned from `unzipWith`.
*
* The `unzipper` takes the current element as its first argument, an
* accumulator as second argument (initially `undefined`) and its return value
* is the accumulator passed into the next invocation.
*
* @example
* const [nums, str] = unzipWith(
* [ [1,'a'], [2,'b'], [3,'c'] ],
* (n, acc: number[] = []) => [...acc, n],
* (c, str = '') => str + c
* )
*
* console.log(nums) // prints: [1, 2, 3]
* console.log(str) // prints: 'abc'
*/
exports.default = _default;
const unzipWith = (zipped, ...unzippers) => zipped.reduce((a, c) => c.map((v, i) => unzippers[i](v, a[i])), []);
exports.unzipWith = unzipWith;
module.exports = Object.assign(exports.default || {}, exports);