Skip to content

Instantly share code, notes, and snippets.

View smitroshin's full-sized avatar

Serghei Mitroșin smitroshin

  • Amdaris
  • Chisinau, Moldova
View GitHub Profile
@smitroshin
smitroshin / useStatefulRef.ts
Last active November 27, 2023 18:22
React hook - useStatefulRef
import { useState } from "react";
/**
* As an API it's the same useRef,
* but it uses useState under the hood.
*
* Used to watch the ref changing between re-renders.
*
* Source: https://non-traditional.dev/creating-a-stateful-ref-object-in-react-fcd56d9dea58
*/
@smitroshin
smitroshin / useDebouncedWindowResize.ts
Last active November 21, 2023 13:20
React hook - useDebouncedWindowResize
import { useState } from "react";
import { useMount, usePrevious, useToggle } from "react-use";
export function debounce(func: Function, timeout = 300) {
let timer: any;
return (...args: any[]) => {
clearTimeout(timer);
timer = setTimeout(() => {
func(...args);
}, timeout);
@smitroshin
smitroshin / getPdfPageCount.ts
Created November 15, 2023 12:58
Get PDF page count from a PDF Blob
/**
* Get PDF page count from a PDF Blob
*
* Source: https://stackoverflow.com/a/58089003
*/
export const getPdfPageCount = (pdfFile: Blob) =>
new Promise<number>((resolve, reject) => {
const reader = new FileReader();
reader.readAsBinaryString(pdfFile);
@smitroshin
smitroshin / axiosAsyncHelper.ts
Last active August 30, 2023 08:48
Async + Await Error Handling Strategy using Axios
import { AxiosError, AxiosResponse } from "axios";
/**
* Axios types overload
*/
export function asyncHelper<ResponseType = any, ErrorType = any>(
promise: Promise<AxiosResponse<ResponseType | ErrorType, any>> | null | undefined | false
): Promise<{ response: AxiosResponse<ResponseType> | null; error: AxiosError<ErrorType> | null }>;
/**
@smitroshin
smitroshin / pxToRem.ts
Created July 12, 2023 15:10
Convert "px" to "rem"
/**
* Assuming 1rem = 16px
*
* 14 => 0.875
*/
export const pxToRem = (val: number, htmlFontSize?: number) => val / (htmlFontSize ?? 16);
/**
* Assuming 1rem = 16px
*
@smitroshin
smitroshin / toKebabCase.ts
Created June 26, 2023 14:30
Convert a text to kebab-case format
/**
* Convert a text to kebab-case format
*/
export const toKebabCase = (text: string) =>
text
.trim()
.replace(/([a-z])([A-Z])/g, "$1-$2") // insert dash between lowercase and uppercase letters
.replace(/\s+/g, "-") // replace spaces with dashes
.toLowerCase(); // convert all characters to lowercase
@smitroshin
smitroshin / getNumericEnum.ts
Created February 2, 2023 14:24
Get from a numeric `enum` just `keys` or just 'values'
/**
* Get from a numeric `enum` just `keys` or just 'values'
*
* Source: https://www.sharooq.com/how-to-access-keys-and-values-of-an-enum-in-typescript
*
* @param target
* @param NumericEnum
* @returns
*/
export const getNumericEnum = (target: "keys" | "values", NumericEnum: Record<any, any>) =>
@smitroshin
smitroshin / asyncHelper.js
Last active January 21, 2023 16:06
Async + Await Error Handling Strategy
/**
* Source: https://youtu.be/wsoQ-fgaoyQ
*/
export const asyncHelper = (promise) =>
Promise.allSettled([promise]).then(([{ value, reason }]) => ({
data: value,
error: reason,
}));
@smitroshin
smitroshin / makeData.js
Created January 17, 2023 08:52
Generate a list of persons
/**
* Source: https://github.com/TanStack/table/blob/v7/examples/filtering/src/makeData.js
*/
import namor from 'namor'
const range = len => {
const arr = []
for (let i = 0; i < len; i++) {
arr.push(i)
@smitroshin
smitroshin / rainbow_text.css
Created May 20, 2022 11:44
CSS Rainbow Text
.rainbow-text {
background-image: var(--rainbow-gradient,#fff);
background-size: 100%;
background-repeat: repeat;
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
-moz-background-clip: text;
-moz-text-fill-color: transparent;
filter: drop-shadow(0 0 2rem #000);
text-shadow: none;