Skip to content

Instantly share code, notes, and snippets.

Tyler Deitz tvler

Block or report user

Report or block tvler

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@tvler
tvler / DeepRequiredNonNullable.ts
Last active Jan 22, 2020
Recursively remove all undefined and null types from data structures of arbitrary-depth
View DeepRequiredNonNullable.ts
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;
@tvler
tvler / RequiredNonNullableProperties.ts
Created Apr 18, 2019
Make all of a type's properties required and non-nullable
View RequiredNonNullableProperties.ts
type RequiredNonNullableProperties<T> = {
[key in keyof T]-?: NonNullable<T[key]>;
};
// ex
type OptionalNullableObj = {
a?: number | null;
b?: number | null;
};
@tvler
tvler / cartesianProduct.js
Created Feb 19, 2019
Get the cartesian product of multiple sets
View cartesianProduct.js
// 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'],
);
@tvler
tvler / useNewKey.jsx
Last active Nov 16, 2018
React hook that returns a new key when its passed in value goes from false to true
View useNewKey.jsx
// 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);
if (Boolean(bool) === Boolean(key % 2)) {
setKey(key + 1);
}
return Math.floor(key / 2);
};
@tvler
tvler / areUnsortedArraysEqual.js
Last active Apr 18, 2019
A variadic function to see if any number of unsorted arrays all have the same values
View areUnsortedArraysEqual.js
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),
@tvler
tvler / Atom⇧⌘F
Last active May 25, 2018
Atom ⇧⌘F helpers
View Atom⇧⌘F
Exclude file types
!*.snap, !*.test.jsx, !*.test.js
@tvler
tvler / getCircularArrayIndex.js
Created Apr 16, 2018
Get circular array index
View getCircularArrayIndex.js
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))
}
@tvler
tvler / imessage-pics.sh
Created Mar 22, 2018
(MacOS) Open all iMessage attachments in Preview
View imessage-pics.sh
open -a Preview ~/Library/Messages/Attachments
@tvler
tvler / FormDataToURLSearchParams.js
Created Jan 14, 2018
FormData into URLSearchParams one-liner
View FormDataToURLSearchParams.js
new URLSearchParams([...new FormData(ev.target)])
@tvler
tvler / pick.js
Last active Jan 12, 2018
Experimental notation for picking a subset of a js object
View pick.js
const getProxy = (target = {}, key) =>
target[key] || (target[key] = new Proxy({}, { get: getProxy }));
const fillPick = (proxy, fn, fromObject = fn(proxy)) =>
Object.assign(
{},
...Object.entries(proxy).map(([key, value]) => ({
[key]: Object.keys(value).length
? fillPick(value, fn, fromObject[key])
: fromObject[key],
You can’t perform that action at this time.