Skip to content

Instantly share code, notes, and snippets.

View aaronbeall's full-sized avatar

Aaron Beall aaronbeall

View GitHub Profile
@aaronbeall
aaronbeall / node-colorize-console.ts
Last active November 13, 2017 17:48
TypeScript NodeJS colorized console output
// Colors: https://coderwall.com/p/yphywg/printing-colorful-text-in-terminal-when-run-node-js-script
const {log, error, warn, info, debug} = console;
console.error = (...args: any[]) => _console.error("\x1b[31m", ...args, "\x1b[0m") // red
console.warn = (...args: any[]) => _console.warn("\x1b[33m", ...args, "\x1b[0m") // yellow
console.info = (...args: any[]) => _console.info("\x1b[34m", ...args, "\x1b[0m") // blue
console.debug = (...args: any[]) => _console.debug("\x1b[2m", ...args, "\x1b[0m") // dim
// extras
declare global {
interface Console {
@aaronbeall
aaronbeall / sortOn.ts
Created November 8, 2017 20:05
TypeScript array sortOn key
function sortOn<T>(array: T[], ...fields: (keyof T)[]) {
return array.sort((a, b) => {
for (const f of fields) {
if (a[f] > b[f]) return 1;
if (a[f] < b[f]) return -1;
}
return 0;
});
}
// ---------- webpack.config.ts ---------- //
import * as path from "path";
import * as webpack from "webpack";
import * as loaders from "./webpack/loaders";
import { plugins } from "./webpack/plugins";
const config: webpack.Configuration = {
entry: {
app: './src/index.tsx',
styleguide: './styleguide/index.tsx'
@aaronbeall
aaronbeall / SnapSVGAnimator.d.ts
Created December 8, 2017 17:13
[WIP] SnapSVG-Animator TypeScript definitions
// From: https://github.com/cjgammon/SnapSVG-Animator/wiki/API
declare class SVGAnim {
public linkage: object;
public mc: MovieClip;
public s: Snap.Element;
public resourceManager: any;
constructor(json: any, width: string | number, height: string | number, fps: string | number);
}
// Based on: https://api.highcharts.com/highcharts/tree.json
type Schema = { _meta: SchemaMeta; } & Defs;
interface SchemaMeta {
commit: string;
branch: string;
version: string;
date: string;
}
Inferred type of "plotOptions.cmf.params.volumeSeriesID" as {string} from default value {"volume"}
Inferred type of "plotOptions.macd.groupPadding" as {number} from default value {0.1}
Inferred type of "plotOptions.macd.minPointLength" as {number} from default value {0}
Inferred type of "plotOptions.macd.pointPadding" as {number} from default value {0.1}
Inferred type of "plotOptions.vbp.dataLabels.style.fontSize" as {string} from default value {"7px"}
Inferred type of "plotOptions.vbp.crisp" as {boolean} from default value {true}
Inferred type of "plotOptions.vbp.pointPadding" as {number} from default value {0}
Inferred type of "plotOptions.bellcurve.intervals" as {number} from default value {3}
Inferred type of "plotOptions.bellcurve.pointsInInterval" as {number} from default value {3}
Inferred type of "plotOptions.series.label.style.fontWeight" as {string} from default value {"bold"}
type Color = string;
// Type definitions for Highcharts 6.0.3 (HEAD commit d351c6e on Fri Nov 24 2017 13:38:58 GMT+0100 (W. Europe Standard Time))
// Project: http://www.highcharts.com/
// Definitions generated from https://api.highcharts.com/
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.6
declare namespace Highcharts {
// plotOptions.ad.params
Patching "plotOptions.series.dataLabels.filter.operator" with { doclet: { values: [Function: values] } }
Patching "plotOptions.tilemap.tileShape" with { doclet: { values: '["hexagon", "circle", "diamond", "square"]' } }
Patching "yAxis.tooltipValueFormat" with { doclet: { type: { names: [Array] } } }
Patching "chart.parallelAxes.title" with { doclet: { extends: 'xAxis.labels' } }
Type already exists for [plotOptions,ad,name] at [plotOptions.sma.name] of {String,}, removing own def with type {undefined}
Type already exists for [plotOptions,atr,name] at [plotOptions.sma.name] of {String,}, removing own def with type {undefined}
Type already exists for [plotOptions,atr,params,period] at [plotOptions.sma.params.period] of {Number,}, removing own def with type {undefined}
Removed all children of [plotOptions,atr,params] and no type information is left, removing def
Type already exists for [plotOptions,bb,name] at [plotOptions.sma.name] of {String,}, removing own def with type {undefined}
Type already exists for [p
function arrayToMap<T, K extends keyof T>(array: T[], key: K): { [key: string]: T } {
return array.reduce((map, item) => ({
...map,
[`${item[key]}`]: item
}), {});
}
// Example
const array = [{ id: "a"}, { id: "b" }, { id: "c" }];
const byId = arrayToMap(array, "id"); // { "a": { id: "a"}, "b": { id: "b"}, "c": { id: "c"} }
const flatten = array => (
array.reduce((accumulated, current) => accumulated.concat(
Array.isArray(current) ? flatten(current) : current
), [])
);
// Test
const before = [[1,2,[3]],4];
const after = flatten(before);
const expected = [1,2,3,4];