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
| function binarySearch(list, item) { | |
| let min = 0; | |
| let max = list.length - 1; | |
| let guess; | |
| while (min <= max) { | |
| guess = Math.floor((min + max) / 2); | |
| if (list[guess] === item) { | |
| return guess; | |
| } else { |
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 obj = { | |
| [Symbol("my_key")]: 1, | |
| [Symbol("second_key")]: 2, | |
| enum: 3, | |
| nonEnum: 4, | |
| }; | |
| Object.defineProperty(obj, "nonEnum", { | |
| enumerable: false, | |
| }); |
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 serializableSymbol = Symbol.for("serializable"); | |
| class User { | |
| constructor(name) { | |
| this.name = name; | |
| this[serializableSymbol] = ["name", "age"]; // Mark 'name' and 'age' for serialization | |
| } | |
| getSerializableProperties() { | |
| const properties = []; |
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 obj = { | |
| [Symbol.toPrimitive]: function (hint) { | |
| if (hint === "number") { | |
| return 42; | |
| } else if (hint === "string") { | |
| return "Hello"; | |
| } else { | |
| return 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
| /* | |
| Josh Comeau Reset https://www.joshwcomeau.com/css/custom-css-reset/ with my tweaks | |
| */ | |
| *, | |
| *::before, | |
| *::after { | |
| box-sizing: border-box; | |
| } | |
| * { | |
| margin: 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
| import { | |
| createContext, | |
| memo, | |
| useCallback, | |
| useContext, | |
| useEffect, | |
| useId, | |
| useLayoutEffect, | |
| useMemo, | |
| useReducer, |
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
| export default function App() { | |
| const [isOpen, setIsOpen] = React.useState(false); | |
| const ref = React.useRef(null); | |
| React.useEffect(() => { | |
| if (isOpen === true) { | |
| const handleEvent = (e) => { | |
| const element = ref.current; | |
| if (element && !element.contains(e.target)) { |
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
| function slow(id, cb) { | |
| setTimeout(function () { | |
| cb(null, id) | |
| }, Math.random() * 3000) | |
| } | |
| let promises = [] | |
| for (let i = 0; i < 3; i++) { | |
| const prom = new Promise((resolve, reject) => { | |
| slow(i, function (err, res) { |
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
| // **** Types **** // | |
| export type TFunc = (...args: any[]) => any; | |
| export type TEmail = `${string}@${string}`; | |
| export type TColor = `#${string}`; | |
| // **** Variables **** // | |
| const EMAIL_RGX = /^(?!\.)(?!.*\.\.)([A-Z0-9_'+\-.]*)[A-Z0-9_+-]@([A-Z0-9][A-Z0-9-]*\.)+[A-Z]{2,}$/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
| // Types for the result object with discriminated union | |
| type Success<T> = { | |
| data: T; | |
| error: null; | |
| }; | |
| type Failure<E> = { | |
| data: null; | |
| error: E; | |
| }; |