Skip to content

Instantly share code, notes, and snippets.

View mscolnick's full-sized avatar

Myles Scolnick mscolnick

View GitHub Profile
var Limit;
(function (Limit) {
Limit[Limit["All"] = 0] = "All";
Limit[Limit["Two"] = 1] = "Two";
Limit[Limit["One"] = 2] = "One";
})(Limit || (Limit = {}));
let config;
let rootDocument;
export function finder(input, options) {
if (input.nodeType !== Node.ELEMENT_NODE) {
function exampleOne() {
console.log('One');
return Promise.resolve()
.then(() => console.log('Two'))
.catch(() => console.log('Three'))
.finally(() => console.log('Four'));
}
function exampleTwo() {
@mscolnick
mscolnick / vite-cypress.js
Created March 11, 2022 07:29
Plugin js for vite + cypress
const path = require('path');
const { createServer } = require('vite');
module.exports = async (on, config) => {
const host = process.env.HOST || 'localhost';
const port = process.env.PORT || 3000;
// Vite config
const viteConfig = {
configFile: path.resolve(__dirname, "../vite.config.ts"),
type _Fib<NUM extends any[]> = NUM['length'] extends 1 | 0
? NUM
: Add<_Fib<Minus1<NUM>>, _Fib<Minus2<NUM>>>;
type Fib<N extends number> = _Fib<ArrayOfSize<N>>['length'];
type F1 = Fib<1>; // 1
type F2 = Fib<2>; // 1
type F3 = Fib<3>; // 2
type Plus1<A extends any[]> = [true, ...A];
type Add<A extends any[], B extends any[]> = [...A, ...B];
type Minus1<A extends any[]> = A extends readonly [any?, ...infer U] ? U : [...A];
type Minus2<A extends any[]> = Minus1<Minus1<A>>;
// verify
type D = Plus1<A>; // [true, true, true, true]
type E = Minus1<A>; // [true, true]
type F = Minus2<A>; // [true]
// number -> array
type ArrayOfSize<N extends number> = N extends N ? (number extends N ? boolean[] : _ArrayOfSize<N, []>) : never;
type _ArrayOfSize<N extends number, R extends unknown[]> = R['length'] extends N ? R : _ArrayOfSize<N, [true, ...R]>;
// verify
type A = ArrayOfSize<3>; // [true, true, true]
type B = ArrayOfSize<1>; // [true]
type C = ArrayOfSize<0>; // []
// Awaiting promises
type Awaited<T> =
T extends null | undefined ? T :
T extends PromiseLike<infer U> ? Awaited<U> :
T;
type P1 = Awaited<Promise<string>>; // string
type P2 = Awaited<Promise<Promise<string>>>; // string
type P3 = Awaited<Promise<string | Promise<Promise<number> | undefined>>; // string | number | undefined
// Variadic tuple elements
type Foo<T extends unknown[]> = [string, ...T, number];
type T1 = Foo<[boolean]>; // [string, boolean, number]
type T2 = Foo<[number, number]>; // [string, number, number, number]
type T3 = Foo<[]>; // [string, number]
// Strongly typed tuple concatenation
type Unpacked<T> =
T extends (infer U)[] ? U :
T extends (...args: any[]) => infer U ? U :
T extends Promise<infer U> ? U :
T;
type T0 = Unpacked<string>; // string
type T1 = Unpacked<string[]>; // string
type T2 = Unpacked<() => string>; // string
type T3 = Unpacked<Promise<string>>; // string
type TypeName<T> =
T extends string ? "string" :
T extends number ? "number" :
T extends boolean ? "boolean" :
T extends undefined ? "undefined" :
T extends Function ? "function" :
"object";
type T0 = TypeName<string>; // "string"
type T1 = TypeName<"a">; // "string"