Last active
July 30, 2021 23:05
-
-
Save petsel/42d89ad83cc5e90dc0f61a4d030fc17d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const | |
arrPrototype = Object.getPrototypeOf([]); | |
const { | |
from: arrayFrom, | |
isArray, | |
} = Array; | |
function createListOfChunkLists(arr, chunkCount) { | |
return arr.reduce((list, item, idx) => { | |
if (idx % chunkCount === 0) { | |
list.push([ item ]); | |
} else { | |
// list.at(-1).push(item); | |
list[list.length - 1].push(item); | |
} | |
return list; | |
}, []); | |
} | |
function getNthItemOfChunkCount(arr, nthIndex, chunkCount) { | |
if (!isArray(arr)) { | |
throw (new TypeError( | |
'The "nth().of()" chain needs to operate a list like structure.' | |
)); | |
} | |
if (Number.isNaN(chunkCount)) { | |
throw (new TypeError( | |
'The chunk count needs to be an integer value.' | |
)); | |
} | |
if (chunkCount > arr.length) { | |
throw (new RangeError( | |
"The chunk count value has to be lower than or equal to the operated list structure's length." | |
)); | |
} | |
if ( | |
Number.isNaN(nthIndex) | |
|| (nthIndex < 0) | |
//|| ((nthIndex < 0) && ((nthIndex + chunkCount) <= 0)) | |
|| ((nthIndex >= 0) && ((nthIndex + 1) >= chunkCount)) | |
) { | |
throw (new RangeError( | |
"The nth index needs to be an integer value which has to be reasonable within the chunk count's context." | |
)); | |
} | |
return createListOfChunkLists(arr, chunkCount) | |
.map((chunkList, idx) => ({ | |
item: chunkList[nthIndex], | |
idx: ((idx * chunkCount) + nthIndex), | |
})); | |
} | |
class NthItemOfChunkCount { | |
#arr; | |
#idx; | |
constructor(arr, idx) { | |
this.#arr = arr; | |
this.#idx = idx; | |
} | |
of(int) { | |
return getNthItemOfChunkCount(this.#arr, this.#idx, parseInt(int, 10)); | |
} | |
} | |
function nthItemOfChunkCount(int) { | |
const arr = (isArray(this) && this) || arrayFrom(this ?? []); | |
const idx = parseInt(int, 10) - 1; | |
return (new NthItemOfChunkCount(arr, idx)); | |
} | |
Object.defineProperty(arrPrototype, 'nth', { | |
configurable: true, | |
writable: true, | |
value: nthItemOfChunkCount | |
}); | |
Object.defineProperty(arrPrototype.nth, 'toString', { | |
value: () => 'function nth() { [custom code] }' | |
}); | |
Object.defineProperty(NthItemOfChunkCount.prototype.of, 'toString', { | |
value: () => 'function of() { [custom code] }' | |
}); | |
// export default Array; | |
/** | |
* Get every nth element of an array | |
* [https://stackoverflow.com/questions/68588326/get-every-nth-element-of-an-array/68593956#68593956] | |
*/ | |
/* const cells = [ | |
'a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7', 'a8', 'a9', | |
'b1', 'b2', 'b3', 'b4', 'b5', 'b6', 'b7', 'b8', 'b9', | |
'c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9', | |
]; | |
console.log( | |
"cells.nth(7).of(9).map(({ item }) => item) ...", | |
cells.nth(7).of(9).map(({ item }) => item) | |
); | |
console.log( | |
"cells.nth(7).of(9) ...", | |
cells.nth(7).of(9) | |
); | |
console.log( | |
"cells.nth(3).of(6) ...", | |
cells.nth(3).of(6) | |
); | |
console.log( | |
"cells.nth(2).of(3) ...", | |
cells.nth(2).of(3) | |
); | |
console.log( | |
"cells.nth ...", | |
cells.nth | |
); | |
console.log( | |
"cells.nth(2) ...", | |
cells.nth(2) | |
); | |
console.log( | |
"cells.nth(2).of ...", | |
cells.nth(2).of | |
); */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment