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 April 26, 2022 07:39
`no-observable-pipe` ESLint rule
const { getTypeServices } = require('eslint-etc');
const {
ESLintUtils,
TSESTree,
TSESLint,
} = require('@typescript-eslint/experimental-utils');
const ruleCreator = ESLintUtils.RuleCreator(
(_name) =>
// This should be the URL of the docs for this rule, but since this is a custom/local rule, we
@OliverJAsh
OliverJAsh / prettier-diff.sh
Created January 14, 2022 08:59
prettier-diff
#!/bin/bash
# Exit immediately if a command exits with a non-zero status, amongst other improvements
# https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/
set -eux
#
# Example usage:
# ./prettier-diff.sh --no-index my-minified-file-before.js my-minified-file-after.js
#
@OliverJAsh
OliverJAsh / new-jsx-transform.js
Created January 14, 2022 08:51
Old vs new JSX transform
return (0, a.jsxs)(
ae.im,
{
id: s,
style: { marginLeft: -ve },
popoverProps: c,
children: [
(0, a.jsx)(
ae.xz,
{ showDropdownSymbol: !0, className: (0, ne.Bt)({}), children: t(d) },
import { pipe } from 'fp-ts/function';
import * as NonEmptyArray from 'fp-ts/NonEmptyArray';
import * as O from 'fp-ts/Option';
const runMain = () => {
require('./main');
};
const createTimeoutPromise = (timeout: number): Promise<void> =>
new Promise<void>((resolve) =>
@OliverJAsh
OliverJAsh / doc.md
Created June 22, 2021 11:32
Unsplash: Architectural Decision Record (ADR): Mocking API requests in E2E (Cypress) tests

Mocking API requests in E2E (Cypress) tests

Problem

Testing at the network level

In our E2E tests, when the test runner navigates through the site, we don't want to make real API requests but rather we want to mock these requests so that we can easily test many different scenarios, and so that our tests are more resilient and less likely to flake if API is suffering any downtime.

Up until now, this has been achieved by "mocking the fetch function". For example:

function reverseFormatNumber(val,locale){
var parts = new Intl.NumberFormat(locale).formatToParts(1111.1);
var group = parts.find(part => part.type === 'group').value;
var decimal = parts.find(part => part.type === 'decimal').value;
var reversedVal = val.replace(new RegExp('\\' + group, 'g'), '');
reversedVal = reversedVal.replace(new RegExp('\\' + decimal, 'g'), '.');
return Number.isNaN(reversedVal)?0:+reversedVal;
}
console.log(reverseFormatNumber('1,234.56','en'));
@OliverJAsh
OliverJAsh / script.js
Last active January 14, 2022 11:39
webpack: list reasons why a given module was included (dependents)
// @ts-check
const treeify = require('treeify');
/**
* @param {import('webpack').StatsCompilation} stats
* @param {string | number} id
*/
const getModuleById = (stats, id) => stats.modules.find((module) => module.id === id);
@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;