From ba7ced118d05ddecc88372a89695577386dc3a46 Mon Sep 17 00:00:00 2001 From: anushka bhandari Date: Mon, 19 Feb 2024 13:16:17 +0530 Subject: [PATCH] generateRandomString --- Readme.md | 4 ++++ src/index.ts | 2 ++ src/utils/utilityFunctions.ts | 10 ++++++++++ 3 files changed, 16 insertions(+) diff --git a/Readme.md b/Readme.md index 429305a..63096be 100644 --- a/Readme.md +++ b/Readme.md @@ -233,6 +233,10 @@ This function remove the number from string. validator.addFullstop(str) This function replaces space in string with fullstop. ``` +``` +validator.generateRandomString(length) +This function generates the random string of given length. +``` # Contributing Guidelines diff --git a/src/index.ts b/src/index.ts index 851906c..f5e46b4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -35,6 +35,7 @@ import { removeUnderscore, addFullstop, removeNumber, + generateRandomString } from "./utils/utilityFunctions"; @@ -92,6 +93,7 @@ const validator = { decodeJWT, addFullstop, removeNumber, + generateRandomString }; export default validator; diff --git a/src/utils/utilityFunctions.ts b/src/utils/utilityFunctions.ts index 8e9887c..125847e 100644 --- a/src/utils/utilityFunctions.ts +++ b/src/utils/utilityFunctions.ts @@ -322,6 +322,16 @@ export const removeNumber = (str: string): string => { export const addFullstop = (str: string): string => { return str.replace(/\s/g, '.'); }; +export const generateRandomString = (length: number): string => { + const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+'; + let result = ''; + const charactersLength = characters.length; + for (let i = 0; i < length; i++) { + result += characters.charAt(Math.floor(Math.random() * charactersLength)); + } + return result; +}; +