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
| Iterating objects with for (... in ...) is error prone. It will include enumerable properties from the prototype chain. Do not use unfiltered for (... in ...) statements. Either filter values explicitly with an if statement, or use for (... of Object.keys(...)). |
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 sureThing = (promise) => | |
| promise | |
| .then((data) => ({ ok: true, data })) | |
| .catch((error) => ({ ok: false, error })); |
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 randBetween = (min, max) => Math.floor((Math.random() * (max - min + 1) + min)) |
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
| The slice() method returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array. The original array will not be modified. | |
| slice() | |
| slice(start) | |
| slice(start, end) |
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
| The sort() method sorts the elements of an array in place and returns the sorted array. The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values. | |
| items.sort((a, b) => a.localeCompare(b)); | |
| sort() and reverse() perform in place operations, modifying the original array. |
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
| The ?. operator is like the . chaining operator, except that instead of causing an error if a reference is nullish (null or undefined), the expression short-circuits with a return value of undefined. | |
| obj.val?.prop | |
| obj.val?.[expr] | |
| obj.arr?.[index] | |
| obj.func?.(args) |
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 fs = require('fs').promises; | |
| fs.writeFile('helloworld.txt', 'Hello World!', 'utf8') | |
| .then(() => console.log('Hello World > helloworld.txt')) | |
| .catch((err) => console.log(err)); |
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
| import { useEffect, useState } from "react"; | |
| const darkModeQuery = window.matchMedia("(prefers-color-scheme: dark)"); | |
| const usePrefersColorScheme = () => { | |
| const [colorScheme, setColorScheme] = useState<"dark" | "light">( | |
| darkModeQuery.matches ? "dark" : "light" | |
| ); | |
| useEffect(() => { | |
| const updateColorScheme = (event: MediaQueryListEvent) => { |
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
| import { useEffect, useState } from "react"; | |
| const useDeviceDetect = () => { | |
| const [isMobile, setIsMobile] = useState<boolean | null>(null); | |
| const mobileRegExp = new RegExp( | |
| "Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile|WPDesktop", | |
| "i" | |
| ); |
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
| I'm not a great programmer; I'm just a good programmer with great habits. |