Skip to content

Instantly share code, notes, and snippets.

View OliverJAsh's full-sized avatar

Oliver Joseph Ash OliverJAsh

View GitHub Profile
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$,
),
),
@OliverJAsh
OliverJAsh / foo.md
Last active September 4, 2023 15:31
`Option` vs non-`Option`

Option vs non-Option

Option<T> non-Option (T | undefined)
accessing property userOption.map(user => user.age) userNullish?.age
calling a method userOption.map(user => user.fn()) userNullish?.fn()
providing fallback ageOption.getOrElse(0) ageNullish ?? 0
filter ageOption.filter(checkIsOddNumber) `ageNull
@OliverJAsh
OliverJAsh / foo.js
Created January 22, 2020 12:38
ESLint `no-logical-not`
/** @type {import('eslint').Rule.RuleModule} */
const noLogicalNot = {
meta: { fixable: 'code' },
create: context => {
/** @type {(node: import('estree').Node) => void} */
const listener = node => {
context.report({
node,
message: 'Prefer `x === false`.',
fix: fixer => {
@OliverJAsh
OliverJAsh / foo.js
Last active January 5, 2020 14:28
Single pass array transformations with IxJS (iterables + generators)
const add1 = n => n + 1;
const square = n => n * n;
//
// Array methods
//
// Each time a method is called, a new array is immediately constructed.
[1, 2, 3, 4]