Skip to content

Instantly share code, notes, and snippets.

View js2me's full-sized avatar
🚙
I may be slow to respond.

Sergey S. Volkov js2me

🚙
I may be slow to respond.
View GitHub Profile
@js2me
js2me / utils.d.ts
Last active March 1, 2023 10:05
My used TypeScript advanced types
type Maybe<T> = T | null | undefined;
/**
* Позволяет переопределить тип свойства {PropertyName} у типа {T} на новое значение {NewValue}
*/
type Override<T, PropertyName extends keyof T, NewValue> = Omit<T, PropertyName> &
{ [K in keyof T]: K extends PropertyName ? NewValue : T[K] };
interface FixedLengthArray<T extends unknown, L extends number> extends Array<T> {
@js2me
js2me / use-async-effect.ts
Created November 3, 2022 13:51
use async effect
import { MutableRefObject, useEffect, useRef, useState } from "react";
type AsyncResult<T> = {
status: AsyncStatus;
data?: T;
error?: Error;
};
enum AsyncStatus {
Loading,
@js2me
js2me / vscode_dark++.css
Last active October 26, 2022 11:19
VS code common dark theme with neon highlights
.mtk13 {
color: #9cdcfe;
text-shadow: 0 0 2px #100c0f, 0 0 5px #ffdda926, 0 0 10px #fff3;
}
.mtk12 {
color: #ffdda9;
text-shadow: 0 0 2px #100c0f, 0 0 5px #ffdda926, 0 0 10px #fff3;
}
@js2me
js2me / global.css
Created October 16, 2022 15:38
fluid typography adaptive font size
body {
font-size: calc([minimum size] + ([maximum size] - [minimum size]) * ((100vw - [minimum viewport width]) / ([maximum viewport width] - [minimum viewport width])));
}
@js2me
js2me / fixed-header-sticky-table.css
Created July 26, 2019 09:09
FIxed header in table using pure css (position: sticky)
.fixed-header-table { overflow-y: auto; height: 100px; }
.fixed-header-table thead th {
position: sticky;
top: 0;
/* don't use border, just use box-shadow */
box-shadow: 0 1px 0 #c6c7c9;
}
table { border-collapse: collapse; width: 100%; }
@js2me
js2me / clamp.ts
Created December 22, 2021 11:47
clamp algorithm on typescript
export const clamp = (num: number, min: number, max: number): number =>
Math.max(min, Math.min(max, num))
@js2me
js2me / useConstants.ts
Created December 22, 2021 09:48
useConstant hook
import { useRef } from 'react'
/**
* Создает константное значение в функциональном React компоненте
* useMemo не дает гарантий, что функция определения значения не вызовется повторно
* https://reactjs.org/docs/hooks-faq.html#how-to-create-expensive-objects-lazily
* https://reactjs.org/docs/hooks-reference.html#usememo
*
* @param defineValue функция определения значения
*/
@js2me
js2me / copy.ts
Last active December 17, 2021 09:40
deep copy js
export const clone = <O extends unknown | unknown[]>(value: O, history?: Set<unknown>): O => {
if (value == null || typeof value !== "object") return value;
const stack = history || new WeakSet();
if (stack.has(value)) {
return value;
}
stack.add(value);
@js2me
js2me / useSyncRef.ts
Created November 29, 2021 08:55
use sync ref
import { MutableRefObject, useEffect, useRef } from 'react'
export const useSyncRef = <T>(value: T): MutableRefObject<T> => {
const ref = useRef<T>(value)
useEffect(() => {
ref.current = value
}, [value])
return ref
@js2me
js2me / useSimpleReducer.ts
Created November 19, 2021 11:05
simple reducer for usage in React
/* eslint-disable @typescript-eslint/ban-types */
import { Reducer, useCallback, useReducer } from 'react'
import { useConstant } from 'hooks/useConstant'
const reducerCreator =
<S extends object>(filter?: (prev: S, curr: S) => boolean) =>
(state: S, update: (prevState: S) => S | void) => {
const nextState = update(state)
return nextState === void 0 || (filter && filter(state, nextState))