Skip to content

Instantly share code, notes, and snippets.

View OliverJAsh's full-sized avatar

Oliver Joseph Ash OliverJAsh

View GitHub Profile
@joshburgess
joshburgess / newtypes-redux-api-actions.ts
Last active January 26, 2019 06:51
Using newtype-ts newtypes to dynamically generate multiple API Action types representing API request states
import { Predicate } from 'fp-ts/lib/function'
import { Prism } from 'monocle-ts'
import { Newtype, iso, prism, unsafeCoerce } from 'newtype-ts'
interface Optimistic<A extends string>
extends Newtype<
{
readonly Optimistic: unique symbol
readonly phantom: A
},
@Magellol
Magellol / ts-custom-intrinsic-elements.tsx
Created December 9, 2018 18:28
Override react types to exclusively allow a set of custom intrinsic elements without inheriting from HTML.
// By overriding a subset of the interfaces from the `@types/react` package,
// we're able to strictly type check what our JSX must look like and avoid inheriting from HTML elements (since they're not valid in our environment for instance).
import * as React from 'react';
declare module 'react' {
namespace JSX {
interface ElementChildrenAttribute {
children: {};
}
import { defer, Observable } from "rxjs";
import { finalize, tap } from "rxjs/operators";
export const log = <T>(source: Observable<T>, name: string) => defer(() => {
console.log(`${name}: subscribed`);
return source.pipe(
tap({
next: value => console.log(`${name}: ${value}`),
complete: () => console.log(`${name}: complete`)
}),
@sledorze
sledorze / Fold.ts
Last active July 17, 2023 10:10
Type safe Typescript definition of an ad'hoc Fold for a Tagged Sum Type (multiple tags supported)
// Definition
type Compact<A> = { [k in keyof A]: A[k] }
type FStruct<R extends Record<any, any>, K extends keyof R = keyof R> = {
[k in K]: { [kv in R[k]]: R extends { [r in k]: kv } ? Compact<R> : never }
}
type Match<StructK, R> = { [KV in keyof StructK]: (v: StructK[KV]) => R }
@ruizb
ruizb / advanced-example.md
Last active September 26, 2023 20:21
Reader monad example using fp-ts

The following section is not part of monet.js documentation, but I think it's worth showing how we can compose readers using fp-ts.

Reader composition

Let's say we have the following piece of code:

interface Dependencies {
  logger: { log: (message: string) => void }
 env: 'development' | 'production'
import { concat, NEVER, Observable, OperatorFunction } from "rxjs";
import { buffer, mergeAll, publish, take } from "rxjs/operators";
function delayUntil<T>(notifier: Observable<any>): OperatorFunction<T, T> {
return source =>
source.pipe(
publish(published =>
concat(
concat(published, NEVER).pipe(buffer(notifier), take(1), mergeAll()),
published
@waynevanson
waynevanson / match-ts.ts
Created April 12, 2020 10:29
A port of switch-case statements for fp-ts.
/**
* A port of switch-case statements for fp-ts.
*
* This could be broken down further and perhaps extended into a new type-class.
* It does the specific job I needed it to.
*
* If you have an abstraction that might help, i'd love to hear about it!
*/
import { array, either, option } from "fp-ts";

Iterables, AsyncIterables, Observables, Oh My!

I know there is a lot of confusion around Observables, Iterables, AsyncIterables and AsyncObservables. Let's try to break this down and the reasons for each.

Pull versus Push Collections

When it comes to collections, you have two ways of thinking about collections, push versus pull. With pull, the consumer is in control of when you get the items, but with push, you get the values when the producer is ready.

Pull Based Collections

@sindresorhus
sindresorhus / esm-package.md
Last active October 31, 2025 00:11
Pure ESM package

Pure ESM package

The package that linked you here is now pure ESM. It cannot be require()'d from CommonJS.

This means you have the following choices:

  1. Use ESM yourself. (preferred)
    Use import foo from 'foo' instead of const foo = require('foo') to import the package. You also need to put "type": "module" in your package.json and more. Follow the below guide.
  2. If the package is used in an async context, you could use await import(…) from CommonJS instead of require(…).
  3. Stay on the existing version of the package until you can move to ESM.
@tabatkins
tabatkins / pipeline.md
Last active October 17, 2022 22:40
Comparing the three pipeline proposals

We've been deadlocked for a while on the pipeline operator proposal, for a few reasons. Partially it's just low implementor interest, as this is fundamentally just syntactic sugar, but also because there are three competing proposals and the proponents of each haven't been convinced by the others yet.

In this essay I hope to briefly outline the problem space, summarize the three proposals, and talk about what's gained/lost by each of them. (Spoiler: they're all nearly identical; we're arguing over very small potatoes.)