Skip to content

Instantly share code, notes, and snippets.

View Twitter unfollow
(function clean() {
const allowed = [];
const container = document.querySelector("section");
const $allTopicsRaw = [...container.querySelectorAll("[role=button]")];
const $allTopics = $allTopicsRaw.reduce((acc, el, i) => {
const isTopic = i % 2 === 0;
isTopic ? acc.push({ topic: el }): acc[acc.length - 1].cancel = el
return acc;
}, []);
View state-machine.ts
interface IStateMachine<TState,TEvent, TPayload = {}> {
getState():TState;
dispatch(event: TEvent, payload?: TPayload): IStateMachine<TState,TEvent, TPayload>;
}
type MyEvent = 'start' | 'move' | 'stop';
type MyState = {speed: number, isHealthy: boolean};
class MyStateMachine implements IStateMachine<MyState, MyEvent> {
constructor(private _state: MyState, private readonly _reaction: Record<MyEvent,(state:MyState)=> MyState>){}
View pipe.ts
export function pipe<I, R1, R>(f1: (a: I) => R1, f2: (a: R1) => R): (a: I) => R;
export function pipe<I, I1, R1, R>(f1: (a: I, b: I1) => R1, f2: (a: R1) => R): (a: I, b: I1) => R;
export function pipe<I, R1, R2, R>(
f1: (a: I) => R1,
f2: (a: R1) => R2,
f3: (a: R2) => R
): (a: I) => R;
export function pipe<I, I1, R1, R2, R>(
f1: (a: I, b?: I1) => R1,
f2: (a: R1) => R2,
View foxy-map.ts
type User = { name: string };
type MyModel = {
name: string;
users: User[];
}
type OnlyArrays<TModel> = Exclude<{ [key in keyof TModel]?: TModel[key] extends any[] ? key : never }[keyof TModel], undefined>;
type Numbers = '0' | '1' | '2' | '3' | '4' | '5';
type StringKeyof<TModel> = keyof TModel extends string ? keyof TModel : never;
@Drag13
Drag13 / get-render-blocking-resources.js
Created Jul 25, 2021
Returns list of render blocking resources from the Chrome trace
View get-render-blocking-resources.js
const { readFileSync } = require("fs");
const rawData = readFileSync('./data.json');
const data = JSON.parse(rawData);
const res = data
.filter(x => x.args?.data?.renderBlocking)
.map(x => ({
timestamp: new Date(x.ts),
url: x.args.data.url,
priority: x.args.data.priority,
@Drag13
Drag13 / locator-example.tsx
Created Mar 25, 2021
Service locator injector example
View locator-example.tsx
import { FC, createContext } from "react";
// Service locator:
interface IService {
test: () => string;
}
type ServiceLocator = {
myTestService: IService;
@Drag13
Drag13 / DeepPartial.ts
Last active Aug 5, 2020
Deep Partial Type in TypeScript
View DeepPartial.ts
type DeepPartial<T> = {
[P in keyof T]?: T[P] extends Array<infer U>
? Array<DeepPartial<U>>
: T[P] extends ReadonlyArray<infer U>
? ReadonlyArray<DeepPartial<U>>
: DeepPartial<T[P]>;
};
// Credits goes to the https://stackoverflow.com/!
@Drag13
Drag13 / advanced-types.ts
Last active Nov 18, 2021
TypeScript Advanced Types and Trics
View advanced-types.ts
// At least one property
type AtLeastOne<T, Keys extends keyof T = keyof T> = Pick<T, Exclude<keyof T, Keys>> & {
[K in Keys]-?: Required<Pick<T, K>>
}[Keys]
type PerformanceBudget = AtLeastOne<{ fcp: number, lcp: number }>
// Typed Key
View Add all, commit with message and push
[alias]
acp = "!f() { git add . && git commit -m \"$1\" && git push; }; f"
@Drag13
Drag13 / v8-debug-flags
Last active Aug 11, 2021
v8 engine flags for DEBUG version v9.4.143
View v8-debug-flags
Options:
--abort-on-contradictory-flags (Disallow flags or implications overriding each other.)
type: bool default: true
--allow-overwriting-for-next-flag (temporary disable flag contradiction to allow overwriting just the next flag)
type: bool default: false
--use-strict (enforce strict mode)
type: bool default: false
--harmony (enable all completed harmony features)
type: bool default: false
--harmony-shipping (enable all shipped harmony features)