Skip to content

Instantly share code, notes, and snippets.

View levchenkod's full-sized avatar
🏄‍♂️

Dmytro Levchenko levchenkod

🏄‍♂️
View GitHub Profile
@levchenkod
levchenkod / .zshrc
Created April 20, 2025 18:47
`p` - terminal command to add all, commit and push to the current branch
function gitacp() {
if git diff --quiet && git diff --cached --quiet; then
echo "✅ No changes to commit. Exiting."
return 0
fi
if [ -z "$1" ]; then
echo "Usage: gitac \"commit message\""
return 1
fi
//https://www.youtube.com/watch?v=AdmGHwvgaVs&t=414s
const catchErrorTyped = <T, E extends new (message?: string) =>
Error>(
p: Promise<T>,
customErrors?: E[]
):Promise<(undefined| T)[] | [E]> => {
return p.then((data:T) => {
return [undefined, data];
@levchenkod
levchenkod / next-router-mock.test.jsx
Created July 2, 2024 23:07
Mock next/router with next-router-mock lib
import mockRouter from "next-router-mock";
jest.mock("next/router", () => jest.requireActual("next-router-mock"));
describe("Page useRouter hook", () => {
it("renders", () => {
mockRouter.push("/home?with=query");
//...
});
@levchenkod
levchenkod / swr-infinite-mock.test.jsx
Created July 2, 2024 23:05
Mock useSWRInfinite with Jest
import useSWRInfinite from "swr/infinite";
jest.mock("swr/infinite");
describe("Page with SWRInfinite", () => {
it("renders", () => {
(useSWRInfinite as jest.Mock).mockResolvedValue({
data: [{ data: [] }],
error: null,
});
@levchenkod
levchenkod / zoho.getAccessToken.ts
Last active June 10, 2024 00:12
Zoho API create Lead example
/**
* Based on this article
* https://medium.com/geekculture/using-zoho-api-for-leads-in-node-js-ea204e98d41b
*/
/**
* IMPORTANT: pay attention to the super-domain ("com", "eu", etc).
* Must be the same you used for the client registration.
* Otherwise will cause: `{ "error": "invalid_client" }`
*/
@levchenkod
levchenkod / enumToArray.test.ts
Created April 22, 2024 01:47
Safely convert enum to array
import enumToArray from "./enumToArray";
enum Numbers {
One = 'One',
Two = 'Two',
Three = 'Three'
};
enum Empty {}
@levchenkod
levchenkod / nullish_coalescing_replacement.md
Last active March 21, 2024 20:29
RegExp to replace Nullish coalescing assignment with a custom logic. For old Node.js versions

RegExp Query:

([\(\)\._a-zA-Z]*)\s\?\?=\s([\._a-zA-Z\(\)]*)

RegExp Substitue With:

$1 = ($1 === null || $1 === undefined) ? $2 : $1

Example:

@levchenkod
levchenkod / next.config.js
Last active February 12, 2023 18:53
NextJS config to ignore test files inside /page dir
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
pageExtensions: [
'mdx',
'md',
'jsx',
'tsx',
'ts'
@levchenkod
levchenkod / currencies_ratio.js
Last active January 21, 2021 11:41
Fetch currencies rate and get ratio between two currencies
/**
* @function fetchRates
* @description fetch currencies rate
* @param {string} [url='https://bank.gov.ua/NBUStatService/v1/statdirectory/exchange?json'] - currencies rate api endpoint
* @returns {promise}
* @example
* let rates = [];
* fetchRates().then(newRates => {
* rates = newRates;
* });
@levchenkod
levchenkod / randomAva.js
Last active June 3, 2020 21:13
Random ava url generator
const random = (max) => {
return Math.floor(Math.random() * max);
};
const getRandomAvaURL = () => {
const source = 'https://randomuser.me/api/portraits';
const gender = ['women', 'men'][random(2)];
const count = 100;
const id = random(count);
const extention = 'jpg';