Skip to content

Instantly share code, notes, and snippets.

[alias]
acp = "!f() { git add . && git commit -m \"$1\" && git push; }; f"
@Drag13
Drag13 / DeepPartial.ts
Last active August 5, 2020 19:46
Deep Partial Type in TypeScript
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 / locator-example.tsx
Created March 25, 2021 10:01
Service locator injector example
import { FC, createContext } from "react";
// Service locator:
interface IService {
test: () => string;
}
type ServiceLocator = {
myTestService: IService;
@Drag13
Drag13 / get-render-blocking-resources.js
Created July 25, 2021 17:10
Returns list of render blocking resources from the Chrome trace
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,
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;
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>){}
@Drag13
Drag13 / pipe.ts
Last active November 12, 2021 14:02
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,
@Drag13
Drag13 / advanced-types.ts
Last active November 18, 2021 06:44
TypeScript Advanced Types and Trics
// 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
(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;
}, []);
@Drag13
Drag13 / chrome.md
Last active November 2, 2022 07:42

Chrome useful flags for web development

Localhost

chrome://flags/#allow-insecure-localhost

NonLocalHost

chrome://flags/#unsafely-treat-insecure-origin-as-secure