Skip to content

Instantly share code, notes, and snippets.

View jthoms1's full-sized avatar
💻
Writing Code

Josh Thomas jthoms1

💻
Writing Code
View GitHub Profile
@jthoms1
jthoms1 / gist:f78bdf176ef4a9144011
Last active February 18, 2016 20:06
Babelrc for Node4
{
"plugins": [
"transform-es2015-destructuring",
"transform-es2015-function-name",
"transform-es2015-modules-commonjs",
"transform-es2015-parameters",
"transform-es2015-spread",
"transform-object-rest-spread",
"transform-async-to-generator",
"transform-function-bind"
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
@jthoms1
jthoms1 / promisify.ts
Last active October 20, 2016 20:16
Create a promise from a node async function.
export interface Promisify {
<T>(func: (callback: (err: any, result: T) => void) => void): () => Promise<T>;
<T, A1>(func: (arg1: A1, callback: (err: any, result: T) => void) => void): (arg1: A1) => Promise<T>;
<T, A1, A2>(func: (arg1: A1, arg2: A2, callback: (err: any, result: T) => void) => void): (arg1: A1, arg2: A2) => Promise<T>;
<T, A1, A2, A3>(func: (arg1: A1, arg2: A2, arg3: A3, callback: (err: any, result: T) => void) => void): (arg1: A1, arg2: A2, arg3: A3) => Promise<T>;
<T, A1, A2, A3, A4>(func: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, callback: (err: any, result: T) => void) => void): (arg1: A1, arg2: A2, arg3: A3, arg4: A4) => Promise<T>;
<T, A1, A2, A3, A4, A5>(func: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, callback: (err: any, result: T) => void) => void): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5) => Promise<T>;
}
/**
* @example: const rReadFile = promisify<Buffer, string>(fs.readFile);
@jthoms1
jthoms1 / things.js
Created November 10, 2016 00:23
array chaining
let things = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let other = things
.map(function(num) { return num + 1; }) // creates a new array that contains [2, 3, 4, 5, 6, 7, 8, 9, 10, 11] and returns it
.filter(function(num) { return num % 3 === 0; }) // reduces the array contents so that it contains [3, 6, 9] and returns it
.join(' -> '); // joins the values and returns a string '3 -> 6 -> 9'
// This could just as easily be written as follows
let a = things.map(function(num) { return num + 1; });
@jthoms1
jthoms1 / transform-test.ts
Created September 20, 2017 02:31
Typescript transform test tool
function transformSourceFile(sourceText: string, transformers: ts.TransformerFactory<ts.SourceFile>[]) {
const transformed = ts.transform(ts.createSourceFile('source.ts', sourceText, ts.ScriptTarget.ES2015), transformers);
const printer = ts.createPrinter({ newLine: ts.NewLineKind.CarriageReturnLineFeed }, {
onEmitNode: transformed.emitNodeWithNotification,
substituteNode: transformed.substituteNode
});
const result = printer.printBundle(ts.createBundle(transformed.transformed));
transformed.dispose();
return result;
}
@jthoms1
jthoms1 / pit.md
Last active August 7, 2018 01:32
Pit of Success

The Pit of Success: in stark contrast to a summit, a peak, or a journey across a desert to find victory through many trials and surprises, we want our customers to simply fall into winning practices by using our platform and frameworks. To the extent that we make it easy to get into trouble we fail.

Rico Mariani, MS Research MindSwap Oct 2003.


I had a chance hear Rico Mariani do

/**
* This is an autogenerated file created by the Stencil compiler.
* It contains typing information for all components that exist in this project.
*/
/* tslint:disable */
import { JSXElements } from '@stencil/core';
import '@stencil/router';
import '@stencil/state-tunnel';
@jthoms1
jthoms1 / waterfall.ts
Created September 4, 2018 19:08
Simple promise waterfall.
function waterFallExec<T, S>(listOfItems: T[], func: (item: T) => Promise<S>): Promise<S[]> {
const results: S[] = []
return listOfItems.reduce(function (lastPromise, item) {
return lastPromise.then(function (res) {
return func(item).then(function (result) {
res.push(result);
return res;
});
});

Keybase proof

I hereby claim:

  • I am jthoms1 on github.
  • I am jthoms1 (https://keybase.io/jthoms1) on keybase.
  • I have a public key whose fingerprint is 43F1 2702 C01E 2E33 6B63 151A E50F 4DE2 8157 8CE6

To claim this, I am signing this object:

@jthoms1
jthoms1 / styles.d.ts
Created July 21, 2020 03:01
Much better way of using tokens in Styled Components
import { ThemeType } from 'some/path/to/my/tokens';
import 'styled-components';
declare module 'styled-components' {
export interface DefaultTheme extends ThemeType {}
}