This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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("obama@gmail.com"); // true |
This file contains hidden or 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")); |
NewerOlder