View memoize.ts
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
function memoize<T extends (...args: any[]) => any>( | |
func: T | |
): (...funcArgs: Parameters<T>) => ReturnType<T> { | |
const results = {}; | |
return (...args: Parameters<T>): ReturnType<T> => { | |
const cacheKey = JSON.stringify(args); | |
if (!results[cacheKey]) { | |
results[cacheKey] = func(...args); |
View is-target-element.js
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
function isTargetElement(element, selector) { | |
let target = element; | |
while (target) { | |
if (target?.matches && target?.matches(selector)) { | |
break; | |
} | |
target = target?.parentNode; | |
} |
View get-hash-from-value.js
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
function getHash(value, length = 16) { | |
let hash = 0; | |
for (let index = 0; index < value.length; index++) { | |
hash = (hash << 5) - hash + value.charCodeAt(index); | |
hash = hash & hash; | |
} | |
hash = Math.abs(hash); |
View check-prime.js
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
function isPrimeNumber(value) { | |
for(let index = 2; index < value; index++) { | |
if(value % index === 0) { | |
return false; | |
} | |
return value > 1; | |
} | |
return false; |
View tiny-debounce.js
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
function debounce(callback, frequency = 250, timer = null) { | |
return (...args) => ( | |
clearTimeout(timer), (timer = setTimeout(callback, frequency, ...args)) | |
); | |
} |