Skip to content

Instantly share code, notes, and snippets.

// Delete
import {
S3Client,
ListObjectsV2Command,
DeleteObjectsCommand,
} from "@aws-sdk/client-s3";
import dotenv from "dotenv";
dotenv.config();
@1mehdifaraji
1mehdifaraji / e2p.ts
Created August 29, 2025 11:01
Converting English (Latin) numbers to Persian and vice versa.
export const e2p = (input: string | number) => {
const persianDigits = ["۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹"];
return input.toString().replace(/\d/g, (d) => persianDigits[parseInt(d)]);
};
export const p2e = (input: string | number) => {
const persianDigits = ["۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹"];
return input
.toString()
.replace(/[۰-۹]/g, (d) => persianDigits.indexOf(d).toString());
@1mehdifaraji
1mehdifaraji / image.tsx
Created August 13, 2025 11:58
Dominant image color extractor
/* eslint-disable @next/next/no-img-element */
"use client";
import ColorThief from "colorthief";
import type { HTMLProps } from "react";
import { useState } from "react";
export const ProductImage = ({ ...props }: HTMLProps<HTMLImageElement>) => {
const [color, setColor] = useState("");
@1mehdifaraji
1mehdifaraji / App.tsx
Created December 2, 2024 17:37
React native coffee carousel animation
import { useCallback, useEffect, useRef, useState } from "react";
import {
StatusBar,
Image,
FlatList,
Dimensions,
Animated,
Text,
View,
SafeAreaView,
export const enNums = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
export const faNums = ["۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹"];
export const arNums = ["٠", "١", "٢", "٣", "٤", "٥", "٦", "٧", "٨", "٩"];
export const enDigitsRegex = /[0-9]/g;
export const faDigitsRegex = /[۰۱۲۳۴۵۶۷۸۹]/g;
export const arDigitsRegex = /[٠١٢٣٤٥٦٧٨٩]/g;
const ar2p = (value) => String(value).replace(arDigitsRegex, (char) => faNums[arNums.indexOf(char)])
@1mehdifaraji
1mehdifaraji / otp.ts
Created November 11, 2023 03:43
OTP generation and verification with speakeasy library in express js framework
import speakeasy from "speakeasy";
export const generateOtp = (secret: string): string =>
speakeasy.time({
encoding: "base32",
secret,
digits: 4,
step: 300, // expire after 5 mins
});
@1mehdifaraji
1mehdifaraji / auth.ts
Last active November 11, 2023 03:36
Jsonwebtoken jwt token generation and verification with express js framework
import { RequestHandler } from "express";
import jwt, { Secret } from "jsonwebtoken";
import { errors } from "@util/locale/errors";
export const generateToken = (userId: string) =>
jwt.sign(userId, process.env.SECRET as Secret);
export const verifyToken = (async (req, res, next) => {
const bearerHeader = req.headers["authorization"] as string;
@1mehdifaraji
1mehdifaraji / regex.ts
Last active November 11, 2023 03:31
Regexes mostly for iran country
export const regexes = {
iran_melli_code: /^(?!(\d)\1{9})\d{10}$/,
number_only: /^[0-9\b]+$/,
latin_number_only: /^[0-9]+$/,
iran_phone: /^(0)9(0[1-5]|[1 3]\d|2[0-2]|9[0-4]|98)\d{7}$/,
iran_shaba: /^(?:IR)(?=.{24}$)[0-9]*$/,
};
// How to use
// const isValidPhone = regexes.iran_phone.test("09371101600")
@1mehdifaraji
1mehdifaraji / helper.ts
Created November 11, 2023 03:26
Persian price toLocaleString
export const toLocaleString = (
num: number,
options?: Intl.NumberFormatOptions
) => num.toLocaleString("fa-IR", options).replace(/\٬/g, ",");