Created
February 18, 2020 15:21
-
-
Save jsg2021/ec3033ca72d1d659b59c31265d944603 to your computer and use it in GitHub Desktop.
Render Range Utility
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
/** | |
* | |
* @generator | |
* @param {number} start | |
* @param {number} end | |
* @yeilds {number} The next number in the range | |
*/ | |
export function* range(start, end) { | |
let x = start; | |
while (x++ < end) { | |
yield x; | |
} | |
} | |
/** | |
* This callback returns anything for the given item. Use it to render or map one to another. | |
* @callback generateItems~render | |
* @param {*} item - the current iterated item. | |
* @returns {*} | |
*/ | |
/** | |
* Generate things | |
* @generator | |
* @param {number|Iterator} iter Some iterable | |
* @param {generateItems~render} render | |
* @yeilds {*} The next value in the iteration. | |
*/ | |
export function* generateItems(iter, render) { | |
if (typeof iter === 'number') { | |
iter = range(0, iter); | |
} | |
for (const x of iter) { | |
yield render(x); | |
} | |
} | |
/** | |
* Given a currentIndex compute the range around it given some bounds. | |
* | |
* @generator | |
* @param {number} windowSize | |
* @param {number} currentIndex | |
* @param {number} total | |
* @yeilds {number} The next number in the windowed range | |
*/ | |
export function computeRangeWindow(windowSize, currentIndex, total) { | |
const start = Math.max(0, currentIndex - Math.ceil(windowSize / 2)); | |
const end = Math.min(start + windowSize, total); | |
const size = start - end; | |
return range( | |
(size < windowSize) | |
? end - windowSize // backfill | |
: start, | |
end | |
); | |
} |
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
import {generateItems} from './ranges.js'; | |
console.log( | |
[ | |
...generateItems(5, x => `Item ${x}`) | |
] | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment