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 scrollToTop = (element) => element.scrollIntoView({ behavior: "smooth" }); | |
// Usage | |
scrollToTop(document.querySelector("#element")); |
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 validateEmail = (email) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email); | |
// Usage | |
validateEmail("[email protected]"); // true |
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 validateObject = (object, rules) => | |
Object.keys(rules).every((key) => rules[key](object[key])); | |
// Usage | |
const rules = { | |
name: (name) => name.length > 0, | |
email: (email) => validateEmail(email), | |
password: (password) => password.length > 0, | |
}; |
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 rgbToHex = (r, g, b) => | |
"#" + ((r << 16) + (g << 8) + b).toString(16).padStart(6, "0"); | |
// Usage | |
rgbToHex(255, 0, 0); // #ff0000 |
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
formatFn("string") + formatFn("string"); | |
const f = (...args) => String.raw(...args); // This returns a normal string | |
formatFn(f`string` + f`string`); |
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 compareObjects = (obj1, obj2) => { | |
const c = Object.keys(obj1), | |
n = Object.keys(obj2); | |
return c.length === n.length && c.every((c) => obj1[c] === obj2[c]); | |
}; | |
// Usage | |
compareObjects({ a: 1, b: 2 }, { a: 1, b: 2 }); // true |
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 getSelectedText = () => window.getSelection().toString(); | |
// Usage | |
getSelectedText(); // Selected text |
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 getQueryParams = (url) => new URLSearchParams(url.split("?")[1]); | |
// Usage | |
getQueryParams("https://example.com?a=1&b=2"); // { a: "1", b: "2" } |
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 copyToClipboard = (text) => | |
navigator?.clipboard?.writeText(text) ?? false; | |
// Usage | |
copyToClipboard("Hello World!"); |
OlderNewer