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
var test = () => { | |
console.log('fired on DOMContentLoaded or after'); | |
} | |
window.onwheneva = test; | |
document.readyState !== 'loading' ? onwheneva() : | |
document.addEventListener('DOMContentLoaded', onwheneva); |
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
// Thanks to https://vincent.billey.me/pure-javascript-immutable-array | |
import React, { Component } from 'react'; | |
const setStateArraySplice = ( | |
stateKey, | |
arrayStart, | |
arrayDeleteCount, | |
...items | |
) => ({ |
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
new URLSearchParams([...new FormData(ev.target)]) |
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
open -a Preview ~/Library/Messages/Attachments |
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 getCircularArrayIndex = (i, { length }) => | |
(i % length + length) % length; | |
const testArray = new Array(5); | |
for (let i = -30; i < 31; i++) { | |
console.log(i, getCircularArrayIndex(i, testArray)) | |
} |
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
Exclude file types | |
!*.snap, !*.test.jsx, !*.test.js |
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
// Thanks to eddmann.com/posts/cartesian-product-in-javascript | |
const cartesianProduct = (...sets) => | |
sets.reduce((sets, set) => sets.flatMap(x => set.map(y => [...x, y])), [[]]); | |
cartesianProduct( | |
['a', 'b', 'c'], | |
['x', 'y', 'z'], | |
); |
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 areUnsortedArraysEqual = (...arrs) => { | |
const everyEqual = ([first, ...arr], isEqual) => | |
arr.every(item => isEqual(first, item)); | |
return ( | |
everyEqual(arrs, (first, arr) => arr.length === first.length) && | |
everyEqual( | |
arrs.map(arr => | |
arr.reduce( | |
(map, item) => map.set(item, (map.get(item) || 0) + 1), |
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
type DeepRequiredNonNullable<T> = T extends null | undefined | |
? never | |
: (T extends (infer ElementType)[] | |
? DeepRequiredNonNullable<ElementType>[] | |
: (T extends Record<string | number, any> | |
? { [key in keyof T]-?: DeepRequiredNonNullable<T[key]> } | |
: T)); | |
// Number test | |
type BadPrimitive = number | null | undefined | 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
// React hook that returns a new key | |
// when its passed in value goes from false to true. | |
// Use this for resetting a child component, firing events, etc! | |
const useNewKey = bool => { | |
const [key, setKey] = useState(bool ? 0 : 1); | |
const newKey = Boolean(bool) === Boolean(key % 2) ? key + 1 : key; | |
useEffect(() => { | |
setKey(newKey); |
OlderNewer