Skip to content

Instantly share code, notes, and snippets.

View ccnokes's full-sized avatar

Cameron Nokes ccnokes

View GitHub Profile
@ccnokes
ccnokes / modulo-use-cases.js
Last active March 2, 2018 17:55
Modulo use cases because math is hard
// 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}`;
@ccnokes
ccnokes / ts-npm-package-sample.json
Created March 8, 2018 03:48
Sample package.json for a TS lib that's published to NPM
{
"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",
@ccnokes
ccnokes / async-serial-queue.ts
Last active March 23, 2018 19:12
Queues async actions so they occur one after another
// 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
@ccnokes
ccnokes / Deferred.ts
Last active March 23, 2018 19:28
AngularJS $q style deferred from a native Promise, in TypeScript
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);
@ccnokes
ccnokes / fifo_test.sh
Created June 2, 2018 04:52
Bash fifos
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
@ccnokes
ccnokes / format-url.js
Last active June 17, 2018 03:42
`formatUrl` function that formats query params and such
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();
@ccnokes
ccnokes / batch-async-tasks-v2.js
Last active June 25, 2018 19:29
Chunk async tasks into batches and process in parallel v2. Demo: https://jsfiddle.net/ccnokes/jhu635r8
// 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);
}
@ccnokes
ccnokes / task-queue.js
Last active June 28, 2018 04:14
Task queue with configurable concurrency and priority. Demo: https://jsfiddle.net/ccnokes/6hb92mde/
// 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);
}
@ccnokes
ccnokes / make-lodash-decorator.js
Last active July 5, 2018 09:17
Wrap a lodash method in a TS decorator
//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;
@ccnokes
ccnokes / infinite-load-generator.js
Last active July 12, 2018 19:12
Generator function for infinite loading things
// 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);
});
}