Skip to content

Instantly share code, notes, and snippets.

View OliverJAsh's full-sized avatar

Oliver Joseph Ash OliverJAsh

View GitHub Profile
@OliverJAsh
OliverJAsh / a.rb
Last active January 22, 2017 15:36
Simple iterator helper (functified)
class Functified {
constructor(iterable) {
this.iterable = iterable;
}
*[Symbol.iterator]() {
for (const value of this.iterable) {
yield value;
}
}
@OliverJAsh
OliverJAsh / foo.js
Created December 24, 2016 11:44
Mapping a Map: spread vs manual performance
(() => {
const IterableH = {
map: fn => iterable => [...iterable].map(fn),
};
const MapH = {
map: fn => map => new Map(IterableH.map(([ key, value ]) => [ key, fn(value) ])(map)),
map2: fn => map => {
const newMap = new Map()
for ([ key, value ] of map) {
@OliverJAsh
OliverJAsh / foo.ts
Created December 15, 2016 17:38
TypeScript key lookup helpers
export type Maybe<T> = undefined | T;
export type StringDictionary<T> = { [k: string]: T };
export const getInDictionary = <T>(dict: StringDictionary<T>, prop: string): Maybe<T> => dict[prop]
export const getInArray = <T>(array: T[], index: number): Maybe<T> => array[index]
@OliverJAsh
OliverJAsh / foo.ts
Created November 25, 2016 15:40
Immutable.js TypeScript issues
{
// Parameter types should not be nullable
// https://github.com/facebook/immutable-js/pull/919
const xs: List<number> = List([1])
xs.forEach(x => {
x // number | undefined, should be number
})
// Workaround:
xs.forEach((x: number) => {
x // number
@OliverJAsh
OliverJAsh / example.ts
Last active September 5, 2016 16:08
Smart component using RxJS Observables
const reportFetchAction = (result?: Result<Report>): ReportFetch => (
{ type: ActionType.ReportFetch, result }
);
// fetchReport = (id: number) => Observable<Result<Report>>
const runFetchReportData = (id: number): Observable<ReportFetch> => {
const report$: Observable<Result<Report>> = fetchReport(id)
return Observable.of(reportFetchAction()).concat(report$.map(reportFetchAction))
}
type State = { reportId: number };
@OliverJAsh
OliverJAsh / foo.ts
Last active August 26, 2016 08:01
Option vs vanilla strict null checking
// findPersonWithName = string => Person | undefined;
// findParentForPerson = Person => Person | undefined;
// isOver50 = Person => boolean;
// isSubscribed = Person => boolean;
// Without Options
// In TS1 this is not type safe, in TS2 it is
const person = findPersonWithName('bob')
const parent = person !== undefined ? findParentForPerson(person) : undefined;
const parentOver50 = parent !== undefined ? (isOver50(parent) ? parent : undefined) : undefined;
@OliverJAsh
OliverJAsh / a.rb
Created August 20, 2016 17:22
Option vs vanilla strict null checking
(() => {
const f = (c: number): Option<number> => c === 2 ? Some(c) : None
const add1 = (c: number): number => c + 1;
const maybeNumber = Option(1)
maybeNumber.flatMap(f).map(add1).getOrElse(5)
})()
(() => {
const f = (c: number): number | undefined => c === 2 ? c : undefined;
const add1 = (c: number): number => c + 1;
@OliverJAsh
OliverJAsh / _git-apply-pr
Created June 13, 2016 09:27
git apply-pr
#compdef git-apply-pr
_git-apply-pr() {
_git-branch
}
@OliverJAsh
OliverJAsh / config.js
Created February 7, 2016 16:40
Using virtual DOM for progressive enhancement
System.config({
baseURL: "/",
defaultJSExtensions: true,
transpiler: "babel",
babelOptions: {
"optional": [
"runtime",
"optimisation.modules.system"
]
},
@OliverJAsh
OliverJAsh / foo.js
Created November 28, 2015 14:59
waitForDomReady promise
const domContentLoaded = 'DOMContentLoaded';
const promise = new Promise((resolve, reject) => {
let loaded = /^loaded|^i|^c/.test(document.readyState);
if (loaded) {
resolve();
} else {
const listener = () => {
// If an async error occurs we want to push it to the promise
try {