Skip to content

Instantly share code, notes, and snippets.

@MichaelGitArt
Last active August 1, 2021 11:09
Show Gist options
  • Save MichaelGitArt/4c2944de01dc573c845d80d24f25502b to your computer and use it in GitHub Desktop.
Save MichaelGitArt/4c2944de01dc573c845d80d24f25502b to your computer and use it in GitHub Desktop.
js-helper-functions
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)
}
}
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
}
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
}
/**
* 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