Skip to content

Instantly share code, notes, and snippets.

View OliverJAsh's full-sized avatar

Oliver Joseph Ash OliverJAsh

View GitHub Profile
@OliverJAsh
OliverJAsh / foo.ts
Created December 3, 2020 17:48
ts-morph: Convert named imports to namespace import
// https://gist.github.com/OliverJAsh/5de515ad1f81b88409c13cd548c20893
// https://twitter.com/OliverJAsh/status/1334537098469265413
const { Project } = require('ts-morph');
const project = new Project({
tsConfigFilePath: 'tsconfig.app.no-references.json',
});
const PATH_TO_MATCH = '/Users/oliverash/Development/unsplash-web/shared/helpers/booleans.ts';
@OliverJAsh
OliverJAsh / foo.ts
Last active October 26, 2022 15:07
TypeScript: infer function parameter as a tuple, not an array
{
declare const fn: <T>(fn: (t: T) => void, t: T) => void;
fn(
(t) => {
// $ExpectType [number, string]
// ❌
// Actual: (string | number)[)
t;
},
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;
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
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);
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);
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 March 31, 2020 07:41
async iterable operators (map, filter) using async generators
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
@OliverJAsh
OliverJAsh / foo.ts
Last active February 24, 2020 10:30
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('----------|');
export const delayUntil = <T>(
signal$: RxJS.Observable<unknown>,
): RxJS.OperatorFunction<T, T> => ob$ =>
ob$.pipe(
RxJSOperators.publishReplay(undefined, undefined, publishedOb$ =>
RxJS.concat(
signal$.pipe(RxJSOperators.first(), RxJSOperators.mergeMapTo(RxJS.EMPTY)),
publishedOb$,
),
),