Skip to content

Instantly share code, notes, and snippets.

@OliverJAsh
OliverJAsh / foo.ts
Created Jan 3, 2019
TypeScript object filter
View foo.ts
const tuple = <T extends Array<unknown>>(...args: T): T => args;
// `Object.keys` does not return the keys as string literals, only strings. Use this helper as a
// workaround. https://github.com/Microsoft/TypeScript/pull/12253#issuecomment-263132208
const keys = <O extends object>(obj: O) => Object.keys(obj) as Array<keyof O>;
// `Object.entries` is ES2017, so we must define our own version.
const entries = <K extends string, V>(obj: Record<K, V>) =>
keys(obj).map(key => tuple(key, obj[key]));
const fromEntries = <K extends string, V>(arr: Array<[K, V]>) =>
View _git-merge-pr
#compdef git-merge-pr
_git-merge-pr () {
# Multiple lines = -l
# compadd -l -d "('foo:a b c' 'bar:d e f')" foo bar
# lines=('foo:a b c' 'bar:d e f')
# compadd -l -d lines foo bar
# lines=('foo:a b c' 'bar:d e f')
View foo.ts
import * as Reader from 'fp-ts/lib/Reader';
import { RouteData } from 'helpers/routes/types';
import { pipe, pipeWith } from 'pipe-ts';
// This will soon be part of fp-ts core
// https://github.com/gcanti/fp-ts/issues/904#issuecomment-619346296
const chainW: <Q, A, B>(
f: (a: A) => Reader.Reader<Q, B>,
) => <R>(ma: Reader.Reader<R, A>) => Reader.Reader<R & Q, B> = Reader.chain as Unrestricted;
View foo.ts
import { from } from 'ix/iterable';
import { map } from 'ix/iterable/operators';
const compare = (a: string, b: string) =>
from(a).pipe(map((value, index) => value === b[index]));
for (const v of compare('yes', 'yas')) {
console.log(v);
}
// true
@OliverJAsh
OliverJAsh / foo.md
Last active Apr 6, 2020
JavaScript function declarations vs. expressions
View foo.md
View foo.ts
describe.only('createDataCron', () => {
it(
'init',
marbles(m => {
const source$ = m.cold('--a| ');
const sourceS = ' ^------';
const ms = m.time(' ------|');
const expected = ' --a| ';
const cron = createDataCron(source$, ms);
View foo.ts
describe.only('createDataCron', () => {
it(
'init',
marbles(m => {
const source$ = m.cold('--a| ');
const sourceS = ' ^------';
const ms = m.time(' ------|');
const expected = ' --a| ';
const cron = createDataCron(source$, ms);
View foo.ts
describe.only('createDataCron', () => {
it(
'init',
marbles(m => {
const source$ = m.cold('--a| ');
const sourceS = ' ^------';
const ms = m.time(' ------|');
const expected = ' --a| ';
const cron = createDataCron(source$, ms);
@OliverJAsh
OliverJAsh / example.ts
Last active Mar 31, 2020
async iterable operators (map, filter) using async generators
View example.ts
const getName = async function*() {
yield 'bob';
yield 'baz';
yield 'bar';
};
const getAge = (name: string) => {
const responseJson = fetch('https://httpbin.org/get')
.then(response => response.json())
// Let's pretend the API is returning an age for us
View foo.ts
import { marbles } from 'rxjs-marbles/jest';
import { delayUntil } from '../operators';
describe('delayUntil', () => {
it(
'if notifier emits nothing and then completes after source emits, emits when notifier completes',
marbles(m => {
const source$ = m.cold(' --a--(b|)');
const notifier$ = m.cold('----------|');
You can’t perform that action at this time.