Last active
August 1, 2021 11:09
-
-
Save MichaelGitArt/4c2944de01dc573c845d80d24f25502b to your computer and use it in GitHub Desktop.
js-helper-functions
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 debounce = (fn, ms) => { | |
let isCooldown = false | |
return function () { | |
if (isCooldown) { | |
return | |
} | |
// eslint-disable-next-line prefer-rest-params | |
fn.apply(this, arguments) | |
isCooldown = true | |
setTimeout(() => { | |
isCooldown = false | |
}, ms) | |
} | |
} | |
// Don't call untill I stom calling fn | |
const debounceLazy = (fn, ms) => { | |
let timerId | |
return function () { | |
clearTimeout(timerId) | |
// eslint-disable-next-line prefer-rest-params | |
const args = arguments | |
timerId = setTimeout(() => { | |
fn.apply(this, args) | |
}, ms) | |
} | |
} |
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 deepValue = (obj, path) => { | |
const len = path.length; | |
let returnObj; | |
for (let i = 0; i < len; i += 1) { | |
returnObj = returnObj ? returnObj[path[i]] : obj[path[i]]; | |
} | |
return returnObj; | |
}; | |
export default { | |
deepValue | |
} |
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
export const getScrollbarWidth = () => { | |
const container = document.createElement('div') | |
container.style.visibility = 'hidden' | |
container.style.overflow = 'scroll' | |
const inner = document.createElement('div') | |
container.appendChild(inner) | |
document.body.appendChild(container) | |
const scrollbarWidth = container.offsetWidth - inner.offsetWidth | |
document.body.removeChild(container) | |
return scrollbarWidth | |
} |
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
/** | |
* put some pad before string of number | |
* @param {number|string} n any value where pad a zero | |
* @param {number} width | |
* @param {number} z our pad | |
* @returns {string} string with a zero pad | |
* @example | |
* zeroPad(5); | |
* // returns "05" | |
*/ | |
const zeroPad = (n, width = 2, z = 0) => (String(z).repeat(width) + String(n)).slice(String(n).length); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment