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
// 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
import * as _ from 'lodash'; | |
let n = 2; | |
let someString; | |
// NOTE `_.pickBy(params, _.negate(_.isNil))` removes undefined/null entries | |
// so you don't have to worry about `undefined` getting coerced to a string | |
const formatUrl = (urlStr, params) => urlStr + '?' + | |
new URLSearchParams(_.pickBy(params, _.negate(_.isNil))).toString(); |
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
// see working demo here: https://jsfiddle.net/ccnokes/jhu635r8/73/ | |
async function runTasks(taskFns /* Array<() => Promise<any>> */, concurrency = 3) { | |
let results = []; | |
taskFns = new Set(taskFns); | |
let pending = new Set(); | |
for (let task of taskFns) { | |
if (pending.size >= concurrency) { | |
await Promise.race(pending); | |
} |
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
// see demo: https://jsfiddle.net/ccnokes/6hb92mde/ | |
class Deferred { | |
constructor() { | |
this.promise = new Promise((resolve, reject) => { | |
this.resolve = resolve; | |
this.reject = reject; | |
}); | |
this.then = this.promise.then.bind(this.promise); | |
this.catch = this.promise.catch.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
//wrap a fn that returns a function into a decorator | |
function makeFnWrapDecorator(fnWrapper: Function) { | |
return function decoratorWrapper(...args) { | |
return function decorator(target, propertyKey: string, descriptor: PropertyDescriptor) { | |
const fn = descriptor.value; | |
let wrappedFn = fnWrapper.apply(null, [fn, ...args]); | |
return { | |
configurable: true, | |
get() { | |
return wrappedFn; |
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
// See demo here: https://jsfiddle.net/ccnokes/xfu3yzce/ | |
/* fake request for remote data */ | |
function request(offset = 0) { | |
return new Promise((res) => { | |
let data = new Array(5).fill(offset).map((n, i) => n + i); | |
setTimeout(() => res({ total: 20, data }), 1000); | |
}); | |
} |