Skip to content

Instantly share code, notes, and snippets.

View hamzakaya's full-sized avatar
:atom:
writing code

Hamza KAYA hamzakaya

:atom:
writing code
View GitHub Profile
@hamzakaya
hamzakaya / querySelector.ts
Created July 13, 2022 21:14
typed querySelector
type Split<S extends string, D extends string> = S extends `${infer T}${D}${infer U}` ? [T, ...Split<U, D>] : [S];
type TakeLast<V> = V extends [] ? never : V extends [string] ? V[0] : V extends [string, ...infer R] ? TakeLast<R> : never;
type TrimLeft<V extends string> = V extends ` ${infer R}` ? TrimLeft<R> : V;
type TrimRight<V extends string> = V extends `${infer R} ` ? TrimRight<R> : V;
type Trim<V extends string> = TrimLeft<TrimRight<V>>;
type StripModifier<V extends string, M extends string> = V extends `${infer L}${M}${infer A}` ? L : V;
type StripModifiers<V extends string> = StripModifier<StripModifier<StripModifier<StripModifier<V, '.'>, '#'>, '['>, ':'>;
type TakeLastAfterToken<V extends string, T extends string> = StripModifiers<TakeLast<Split<Trim<V>, T>>>;
type GetLastElementName<V extends string> = TakeLastAfterToken<TakeLastAfterToken<V, ' '>, '>'>;
type GetEachElementName<V, L extends string[] = []> =
import {
useMemo,
useRef,
type MutableRefObject,
type Ref,
type RefCallback,
} from 'react';
const useLifecycleRef = <T extends Element>({
onAttach,
import { useLayoutEffect, useRef } from "react";
type AnyFunction = (...args: any[]) => any;
const useLayoutEffectRef = typeof window !== "undefined" ? useLayoutEffect : () => {};
export function useCallbackRef<TCallback extends AnyFunction>(callback: TCallback): TCallback {
const latestRef = useRef<TCallback>(useCallbackRef_shouldNotBeInvokedBeforeMount as any);
useLayoutEffectRef(() => {
latestRef.current = callback;
}, [callback]);
const _defaultOptions = {
fileName: `${new Date().toLocaleString('tr-TR')}- screen record`,
recording: {
video: true,
audio: true,
},
callbackRecording: (s: boolean) => {},
};
const secure = location.protocol === 'https:' || location.hostname === 'localhost',
@hamzakaya
hamzakaya / useLocalSlice.ts
Created April 24, 2022 21:25
hook like createSlice from redux-toolkit and immer
import { useReducer, useMemo, useDebugValue } from 'react'
import produce, { Draft } from 'immer'
export type PayloadAction<P> = {
type: string
payload: P
}
export type PayloadActionDispatch<P = void> = void extends P
type BubbleSort<
A extends any[],
Curr extends number = A["length"]
> = Curr extends 1
? A
: A extends [infer F, infer S, ...infer Rest]
? BubbleSort<
[
...(Math.Comparator<Math.Num<F>, Math.Num<S>> extends true
? [S, ...BubbleSort<[F, ...Rest], Math.Sub<Curr, 1>>]
@hamzakaya
hamzakaya / circular-stringify.js
Last active April 2, 2022 15:58
JSON.stringify for circular
export default function circularStringify(object, { indentation } = {}) {
if (Array.isArray(object)) {
object = object.map((element) =>
JSON.parse(JSON.stringify(element, replacer())),
)
}
return JSON.stringify(object, replacer(), indentation)
}
const replacer = () => {
@hamzakaya
hamzakaya / fireEvent.ts
Last active April 2, 2022 07:19
Fire Event (pub/sub/unsub)
type EventType = string | symbol
type Handler<T = unknown> = (event: T) => void
type OtherHandler<T = Record<string, unknown>> = (
type: keyof T,
event: T[keyof T],
) => void
type EventHandlerList<T = unknown> = Array<Handler<T>>
type OtherHandlerList<T = Record<string, unknown>> = Array<OtherHandler<T>>
type EventHandlerMap<Events extends Record<EventType, unknown>> = Map<
keyof Events | '*',
import { useReducer, Reducer, Dispatch, useMemo } from "react";
type Action<T, P> = {
type: T;
payload: P;
};
type ActionMap<A> = {
[K in keyof A]: (payload?: A[K]) => void;
};
function httpRequest(url, method, data) {
const init = { method }
switch (method) {
case 'GET':
if (data) url = `${url}?${new URLSearchParams(data)}`
break
case 'POST':
case 'PUT':
case 'PATCH':
init.body = JSON.stringify(data)