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
function iterate(iterator, cb) { | |
let next = iterator.next(); | |
while(!next.done) { | |
cb(next.value); | |
next = iterator.next(); | |
} | |
} | |
// iterate(new Set([1,2,3]).values(), console.log) |
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
function makeAbortablePromise(promise, abort = () => {}) { | |
const promiseWithAbort = { | |
abort, | |
promise, | |
// proxy methods | |
then: (...args) => { | |
promiseWithAbort.promise = promiseWithAbort.promise.then.apply( | |
promiseWithAbort.promise, | |
args | |
); |
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
import * as React from 'react'; | |
// see https://github.com/Microsoft/TypeScript/pull/13743 for what the typing in here's all about | |
type Constructor<T> = new (...args: any[]) => T; | |
const isMounted_Symbol = Symbol('isMounted'); | |
/** | |
* This is for when you're calling setState in uncancellable async callbacks. | |
* NOTE: Doing this can mask memory leaks so be careful |
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
// find if number is even or odd | |
const isEven = val => val % 2 === 0; | |
isEven(11); //false | |
isEven(22); //true | |
// do time related things... | |
const formatMovieTime = val => { | |
const hours = Math.floor(val / 60); //get the hours, discard any remainder via `floor` | |
const mins = val % 60; //get the remainder of minutes left over as an integer | |
return `${hours}:${mins < 10 ? '0' + mins : mins}`; |
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
{ | |
"name": "my-ts-lib", | |
"version": "1.0.0", | |
"description": "My npm package written in TS", | |
"main": "dist/index.js", | |
"types": "dist/index.d.ts", | |
"scripts": { | |
"build": "tsc" | |
}, | |
"author": "Cameron Nokes", |
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
{ | |
"compilerOptions": { | |
"target": "es5", | |
"module": "commonjs", | |
"lib": ["es2017", "es7", "es6", "dom"], | |
"declaration": true, | |
"outDir": "dist", | |
"strict": true, | |
"esModuleInterop": true | |
}, |
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
// class version | |
class Queue { | |
private p: Promise<any> = Promise.resolve(); | |
push<T = any>(fn: () => Promise<any>): Promise<T> { | |
this.p = this.p | |
.then(() => fn()) | |
.catch(err => { | |
console.error(err); | |
return fn(); //keep going to next queued |
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
class Deferred<T = any> { | |
resolve: (value?: T | PromiseLike<T>) => void; | |
reject: (reason?: any) => void; | |
promise = new Promise<T>((resolve, reject) => { | |
this.resolve = resolve; | |
this.reject = reject; | |
}); | |
then = this.promise.then.bind(this.promise); |
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
mkfifo fifo | |
# produce data and send into fifo | |
# have to background it because it blocks until the fifo drains/completes | |
ls -l > fifo & | |
cat fifo # this drains it and completes the job |
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
// dummy async task | |
function asyncTask(n) { | |
return new Promise(resolve => setTimeout(() => resolve(n), 500)); | |
} | |
// takes a flat array and returns a nested one, eg. chunkArray([1,2,3], 1) ==> [[1],[2],[3]] | |
function chunkArray(arr, chunkSize) { | |
return arr.reduce((aggr, item) => { | |
let lastArr = aggr[aggr.length - 1]; | |
if (lastArr.length < chunkSize) { |