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
// Uppercase first letter of a string | |
function capitalize(str = '') { | |
return str ? str.charAt(0).toUpperCase() + str.substring(1) : ''; | |
} |
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
// Focused tab | |
const isBrowserTabFocused = () => !document.hidden; | |
isBrowserTabFocused(); | |
// Redirect to... | |
const redirect = (url, replace = false) => { | |
if (asLink) { | |
window.location.href = url; | |
} |
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
// Redirect to... | |
const redirect = (url, replace = false) => { | |
if (asLink) { | |
window.location.href = url; | |
} else { | |
window.location.replace(url); | |
} | |
}; | |
redirect('https://overment.com'); |
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
// Redirect to... | |
const redirect = (url, replace = false) => { | |
if (replace) { | |
return window.location.replace(url); | |
} | |
window.location.href = url; | |
}; | |
redirect('https://overment.com'); |
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
// Named parameters | |
// Instead this: | |
articles(ids, published, orderBy, order) | |
articles([1, 4, 6], true, 'created_at', 'DESC') | |
// Do this: | |
articles({ ids, published, orderBy, order }) | |
articles({ ids: [1, 4,6 ], published: true, orderBy: 'created_at', order: 'ASC'}) |
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
// Rename object keys | |
const renameKeys = (keysMap, obj) => | |
Object.keys(obj).reduce( | |
(acc, key) => ({ | |
...acc, | |
...{ [keysMap[key] || key]: obj[key] }, | |
}), | |
{} | |
); |
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
// Remove property from object with rest | |
const data = { id: 1, name: 'overment', www: 'overment.com' } | |
const { ['www']: removed, ...rest} = data; | |
console.log(rest); | |
// Inspiration from: Todd Motto |
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
// Toggle visibility (CSS: .hidden { display: none }) | |
const hide = (els, visibilityClass = 'hidden') => [...els].forEach(el => el.classList.toggle(visibilityClass)); | |
// Example: hide all img elements | |
hide(document.querySelectorAll('img')); |
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
// Smooth scroll to top | |
const scrollTop = () => { | |
const currentPosition = document.documentElement.scrollTop || document.body.scrollTop; | |
if (currentPosition > 0) { | |
window.requestAnimationFrame(scrollTop); | |
window.scrollTo(0, currentPosition - currentPosition / 8); | |
} | |
}; | |
scrollTop(); |
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
// Check if the element contains another element | |
const contains = (element, child) => element !== child && element.contains(child); | |
const element = document.querySelector('.parent'); | |
const child = document.querySelector('.child'); | |
contains(element, child); // true | false |