Skip to content

Instantly share code, notes, and snippets.

@petsel
Last active February 23, 2021 19:22
Show Gist options
  • Save petsel/beac5a314ad7fc84e5ed to your computer and use it in GitHub Desktop.
Save petsel/beac5a314ad7fc84e5ed to your computer and use it in GitHub Desktop.
Implementation of a prototypal `Array` method which, from both processed iterable structures, interlocks each item of same index, pairwise and repeatedly into a new array. Sparse locations on either side will be recognized and accordingly (re)assigned.
/**
*
* [Array.interlock.js]
*
* Implementation of a prototypal `Array` method which,
* from both processed iterable structures, interlocks
* each item of same index, pairwise and repeatedly
* into a new array.
* Sparse locations on either side will be recognized
* and accordingly (re)assigned.
*
* - `Array.prototype.interlock`
*
*/
(function (Object, Array) {
const arrPrototype = Array.prototype;
const arrayFrom = Array.from;
const { isArray } = Array;
function interlockIterableItems(leftList, rightList) {
const arr = [];
let idx = -1;
let leftIdx = -1;
let rightIdx = -1;
const leftCount = leftList.length;
const rightCount = rightList.length;
const overallCount = (leftCount + rightCount);
arr.length = overallCount;
/* eslint-disable no-plusplus */
while ((idx + 1) < overallCount) {
// console.log("within while :: 1st part :: idx, overallCount : ", idx, overallCount);
if (++leftIdx < leftCount) {
++idx;
// eslint-disable-next-line no-prototype-builtins
if (leftList.hasOwnProperty(leftIdx)) { // take *sparse array* into account.
arr[idx] = leftList[leftIdx];
}
// console.log("within A condition :: leftIdx, leftCount, arr : ", leftIdx, leftCount, arr);
}
// console.log("within while :: 2nd part :: idx, overallCount : ", idx, overallCount);
if (++rightIdx < rightCount) {
++idx;
// eslint-disable-next-line no-prototype-builtins
if (rightList.hasOwnProperty(rightIdx)) { // take *sparse array* into account.
arr[idx] = rightList[rightIdx];
}
// console.log("within B condition :: rightIdx, rightCount, arr : ", rightIdx, rightCount, arr);
}
}
/* eslint-enable no-plusplus */
return arr;
}
function interlock(iterable) {
/*
* do not immediately "array from" at expected iterable structures
* since `Array.from` does not preserve empty positions of sparse
* arrays, but (re)assigns such positions the undefined value.
*/
const leftList = (isArray(this) && this) || arrayFrom(this ?? []);
const rightList = (isArray(iterable) && iterable) || arrayFrom(iterable ?? []);
return interlockIterableItems(leftList, rightList);
}
Object.defineProperty(arrPrototype, 'interlock', {
configurable: true,
writable: true,
value: interlock
});
Object.defineProperty(arrPrototype.interlock, 'toString', {
value: () => 'function interlock() { [custom code] }'
});
// // provide static implementation as well.
//
// function staticInterlock(arr, condition, target) {
// return interlock.call(arr, condition, target);
// }
//
// Object.defineProperty(Array, 'interlock', {
// configurable: true,
// writable: true,
// value: staticInterlock
// });
// Object.defineProperty(Array.interlock, 'toString', {
// value: () => 'function interlock() { [custom code] }'
// });
// return Array;
}(Object, Array));
console.log('[1,2,3].interlock("a");', [1,2,3].interlock("a"));
console.log('[1,2,3].interlock(["a"]);', [1,2,3].interlock(["a"]));
console.log('[1,2,3].interlock("a").join("");', [1,2,3].interlock("a").join(""));
console.log('[1,2,3].interlock(["a"]).join("");', [1,2,3].interlock(["a"]).join(""));
console.log('[1,2,3].interlock("ab");', [1,2,3].interlock("ab"));
console.log('[1,2,3].interlock(["a", "b"]);', [1,2,3].interlock(["a", "b"]));
console.log('[1,2,3].interlock("ab").join("");', [1,2,3].interlock("ab").join(""));
console.log('[1,2,3].interlock(["a", "b"]).join("");', [1,2,3].interlock(["a", "b"]).join(""));
console.log('[1,2,3].interlock("abc");', [1,2,3].interlock("abc"));
console.log('[1,2,3].interlock(["a", "b", "c"]);', [1,2,3].interlock(["a", "b", "c"]));
console.log('[1,2,3].interlock("abc").join("");', [1,2,3].interlock("abc").join(""));
console.log('[1,2,3].interlock(["a", "b", "c"]).join("");', [1,2,3].interlock(["a", "b", "c"]).join(""));
console.log("[1,3,5,7,9,11,13].interlock([2,4,6]);", `${[1,3,5,7,9,11,13].interlock([2,4,6])}`);
console.log("[2,4,6].interlock([1,3,5,7,9,11,13]);", `${[2,4,6].interlock([1,3,5,7,9,11,13])}`);
/* eslint-disable no-sparse-arrays */
console.log("[2,,,4,,,6].interlock([1,3,5,7,9,11,13,]);", `${[2,,,4,,,6].interlock([1,3,5,7,9,11,13,])}`);
console.log("[2,,,4,,,6].interlock([1,3,5,7,9,11,13,,]);", `${[2,,,4,,,6].interlock([1,3,5,7,9,11,13,,])}`);
console.log("[2,,,4,,,6].interlock([1,3,5,7,9,11,13,,,]);", `${[2,,,4,,,6].interlock([1,3,5,7,9,11,13,,,])}`);
console.log("[2,,,4,,,6].interlock([1,3,,5,7,,9,,11,13,,,]);", `${[2,,,4,,,6].interlock([1,3,,5,7,,9,,11,13,,,])}`);
console.log("[2,,,4,,,6].interlock([1,,3,,5,,7,,,9,,,11,13,,,]);", `${[2,,,4,,,6].interlock([1,,3,,5,,7,,,9,,,11,13,,,])}`);
/* eslint-enable no-sparse-arrays */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment