Skip to content

Instantly share code, notes, and snippets.

View lukeggchapman's full-sized avatar

Chappo lukeggchapman

  • Sydney, Australia
View GitHub Profile
@lukeggchapman
lukeggchapman / memoizeMethodDecorator.ts
Last active February 6, 2020 06:10
Simple memoize method decorator in TS
function memoize(_target: object, _key: string, descriptor: PropertyDescriptor) {
const cache = {};
const original = descriptor.value;
descriptor.value = async function(...args: any[]) {
const argsStr = JSON.stringify(args);
cache[argsStr] = cache[argsStr] || original.apply(this, args);
return cache[argsStr];
};
@lukeggchapman
lukeggchapman / gist:604932a2076af75c2fcdd5930bab81a5
Created January 28, 2020 10:23
Rename all js files in a folder to ts
find src -name "*.js" -exec sh -c 'mv "$0" "${0%.js}.ts"' {} \;
@lukeggchapman
lukeggchapman / mapObj.ts
Created September 5, 2019 05:39
Typescript map object
// Like array.map, but for objects
const mapObj = <U, T extends object>(
obj: T,
mapFn: (val: T[keyof T], key?: keyof T) => U
): { [K in keyof T]: U } => {
return Object.assign(
{},
...Object.entries(obj).map(([k, v]) => ({ [k]: mapFn(v, k as keyof T) }))
);
};
@lukeggchapman
lukeggchapman / promiseAllObj.ts
Created September 3, 2019 04:58
Typescript promiseAllObj. Like Promise.all but for objects. Similar to bluebird.props.
// Like Promise.all, but for objects of {key: Promise}
const promsieAllObj = async <T>(obj: Record<string, Promise<T>>): Promise<Record<string, T>> => {
const arr = Object.entries(obj).map(([key, value]) => value.then(res => [key, res]));
return Object.assign(
{},
...(await Promise.all(arr).then(res => res.map(([k, v]) => ({ [k as string]: v as T }))))
);
};
@lukeggchapman
lukeggchapman / typescriptreact.json
Created April 28, 2019 03:25
Typescript + react snippets
{
"React Component": {
"prefix": "rcc",
"body": [
"import * as React from 'react'",
"",
"export interface ${1:IApp}Props {",
" ${2:}",
"}",
"",