@arrows/array
Version:
Functional tools for JS arrays
46 lines (45 loc) • 1.41 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.zip_ = void 0;
const curry_1 = require("@arrows/composition/curry");
const _zipAll_ = (otherArr, arr) => {
const length = Math.max(arr.length, otherArr.length);
const newArr = new Array(length);
for (let i = 0; i < length; i++) {
newArr[i] = [arr[i], otherArr[i]];
}
return newArr;
};
const zipAll_ = curry_1.default(_zipAll_);
const _zip_ = (otherArr, arr) => {
const length = Math.min(arr.length, otherArr.length);
const newArr = new Array(length);
for (let i = 0; i < length; i++) {
newArr[i] = [arr[i], otherArr[i]];
}
return newArr;
};
const curriedZip_ = curry_1.default(_zip_);
/**
* Zips two arrays creating an array of pairs
* containing values on corresponding indexes.
* Zips until the length of the shorter array is reached.
*
* @param otherArr Array that you want to zip with initial array
* @param arr Initial array
* @returns New, zipped array
*
* @method all Zips until the length of the longer array is reached
*/
const zip_ = Object.assign(curriedZip_, {
/**
* Zips until the length of the longer array is reached.
*
* @param otherArr Array that you want to zip with initial array
* @param arr Initial array
* @returns New, zipped array
*/
all: zipAll_,
});
exports.zip_ = zip_;
exports.default = zip_;
;