Skip to content

Instantly share code, notes, and snippets.

View MauricioRobayo's full-sized avatar

Mauricio Robayo MauricioRobayo

View GitHub Profile
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(...)).
const sureThing = (promise) =>
promise
.then((data) => ({ ok: true, data }))
.catch((error) => ({ ok: false, error }));
const randBetween = (min, max) => Math.floor((Math.random() * (max - min + 1) + min))
@MauricioRobayo
MauricioRobayo / slice.txt
Last active June 7, 2021 19:49
slice.js #gogofast
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)
@MauricioRobayo
MauricioRobayo / sort.txt
Last active August 23, 2021 02:29
#gogofast
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.
@MauricioRobayo
MauricioRobayo / optionalChainingOperator.txt
Last active August 15, 2022 19:53
Optional chaining operator
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)
@MauricioRobayo
MauricioRobayo / promises-writeFile.js
Last active June 1, 2021 01:19
nodejs write to a file #gogofast
const fs = require('fs').promises;
fs.writeFile('helloworld.txt', 'Hello World!', 'utf8')
.then(() => console.log('Hello World > helloworld.txt'))
.catch((err) => console.log(err));
@MauricioRobayo
MauricioRobayo / usePrefersColorScheme.ts
Last active July 22, 2021 10:41
Custom react hook to get system color theme preference
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) => {
@MauricioRobayo
MauricioRobayo / useDeviceDetect.ts
Last active November 13, 2021 13:27
React hook to detect the device type
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"
);
@MauricioRobayo
MauricioRobayo / quote.txt
Last active May 21, 2021 01:25
by Kent Beck #gogofast
I'm not a great programmer; I'm just a good programmer with great habits.