Skip to content

Instantly share code, notes, and snippets.

Avatar

Jamie Mason JamieMason

View GitHub Profile
View iterateOverSyncOrAsyncGenerator.ts
type GetFn = () => {
next(iteration: IteratorResult<any, any>): IteratorResult<any, any>;
};
function iterateOverSyncOrAsyncGenerator<T extends Iterable<any>>(getIterator: GetFn, gen: T): Iterable<any>;
function iterateOverSyncOrAsyncGenerator<T extends AsyncIterable<any>>(getIterator: GetFn, gen: T): AsyncIterable<any>;
function iterateOverSyncOrAsyncGenerator<T extends Iterable<any> | AsyncIterable<any>>(
getIterator: GetFn,
gen: T
): Iterable<any> | AsyncIterable<any> {
View ts-belt-result-helpers.ts
import { R } from '@mobily/ts-belt';
/** Additional helpers for https://mobily.github.io/ts-belt/api/result */
export const $R = {
/**
* 1. Return an R.Ok<output[]> if every R.Result succeeds
* 2. Return an R.Error<Error> for the first failure encountered
*/
all<Input, Output = Input>(
getResult: (value: Input) => R.Result<Output, Error>,
@JamieMason
JamieMason / find-nested-dependencies.md
Created March 2, 2022 12:49
Find all modules which a given JavaScript module depends on, and all they modules they depend on, and all they modules they depend on etc.
View find-nested-dependencies.md

Find nested dependencies or imports of a JavaScript Module

Find all deep/nested/recursive/descendant dependencies/imports/requires of a JavaScript Module.

Related to sverweij/dependency-cruiser#564, find all modules which a given JavaScript module depends on, and all the modules they depend on, and all the modules they depend on etc.

Installation

npm install -g ts-node
View list-github-pull-request-reviewers.md

List All GitHub Pull Request Reviewers for a Repo

Needs jq installed.

#!/bin/bash

for pull_number in {1..500}
do
  USER="YourGitHubUserName"
@JamieMason
JamieMason / README.md
Last active December 8, 2021 20:34
Lossless Debounce Function in JavaScript
View README.md

Lossless Debounce Function

Returns a function which will capture and collect all invocation arguments, to process in batches once ms consecutive resting time has passed.

Know a more common name for this function? Let me know in this discussion.

Demo

@JamieMason
JamieMason / github-bulk-mark-as-viewed.md
Created November 29, 2021 16:55
Tick "Viewed" on every file you've scrolled past on a GitHub Pull Request
View github-bulk-mark-as-viewed.md

GitHub PR bulk mark file as viewed

Tick "Viewed" on every file you've scrolled past on a GitHub Pull Request

// Tick "Viewed" on every file you've scrolled past on a
// GitHub Pull Request
$$('.js-reviewed-checkbox').forEach((el) => {
  if (!el.checked && window.scrollY > el.getBoundingClientRect().top) {
 el.click();
@JamieMason
JamieMason / next.config.js.md
Last active January 11, 2023 11:22
Next.js chrome devtools coverage settings
View next.config.js.md

Next.js chrome devtools coverage settings

I found these settings really useful when using the Chrome Devtools Coverage Inspector to look for unused JavaScript in a Next.js App.

Build the app for production with these settings and the output will be easier to debug.

// next.config.js
module.exports = {
 webpack(config) {
@JamieMason
JamieMason / machine.js
Created October 19, 2021 11:19
Generated by XState Viz: https://xstate.js.org/viz
View machine.js
const fetchMachine = Machine(
{
initial: "evaluating",
states: {
evaluating: {
on: {
"done.invoke.loadAddressBook": [
{
cond: "hasAddresses",
target: "addressBookAndForm",
@JamieMason
JamieMason / fp-ts-get-props.md
Last active February 8, 2023 10:54
fp-ts – Lodash's `_.get` and Ramda's `R.props` that returns an `Option`
View fp-ts-get-props.md

fp-ts – Lodash _.get, Ramda R.props that returns an Option

A function like Lodash's _.get, Ramda's R.props, and Immutable.js's getIn, written in fp-ts.

import { Json } from 'fp-ts/lib/Json';
import { none, Option, some } from 'fp-ts/lib/Option';

export function getIn<T = Json>(props: string[], origin: unknown): Option<T> {
  let value: unknown = origin;
@JamieMason
JamieMason / xstate-machine-types.md
Created September 11, 2021 18:31
Attempt to reduce boilerplate related to a Machine's types in XState
View xstate-machine-types.md

Attempt to reduce boilerplate related to a Machine's types in XState

import type {
  ActionObject,
  EventObject,
  Interpreter,
  Sender,
  State,
 StateMachine,