Last active
April 21, 2022 13:08
-
-
Save Omkaragrawal/5577d1548ceaf3c594fb7660a9563229 to your computer and use it in GitHub Desktop.
Use nanoid to generate random unique string. This function has lots of available formatting.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { customAlphabet, nanoid } from 'nanoid'; | |
import nanoidDictionary from 'nanoid-dictionary'; | |
export enum NanoidStringEnum { | |
NUMERIC = 'NUMERIC', | |
LOWERCASE = 'LOWERCASE', | |
UPPERCASE = 'UPPERCASE', | |
ALPHABETS = 'ALPHABETS', | |
ALPHANUMERIC = 'ALPHANUMERIC', | |
LOWERCASE_NUMERIC = 'LOWERCASE_NUMERIC', | |
UPPERCASE_NUMERIC = 'UPPERCASE_NUMERIC', | |
// BuilderObject = 'BuilderObject', | |
CUSTOM = 'CUSTOM', | |
UUID = 'UUID', | |
} | |
type NanoidStringType = keyof typeof NanoidStringEnum; | |
/** | |
* Generate random string of supplied length using supplied set of characters. | |
* @param {NanoidStringType} [stringType="AlphaNumeric"] type of values that shall be included in the generated string, defaults to 'AlphaNumeric'. | |
* @param {number} [stringLength=10] Length of the string, default to 10. | |
* @param {string} custom to be entered only when stringType is "Custom" or "BuilderObject". When "Custom", characters in this string; and when "BuilderObject", this object; will be used to generate the string. | |
* @returns {string} | |
*/ | |
public static generateRandomUniqueStringOfLength( | |
stringType: NanoidStringType = NanoidStringEnum.ALPHANUMERIC, | |
stringLength?: number, | |
custom?: string, | |
// custom?: string | Record<'includeNumeric' | 'includeLowercase' | 'includeUppercase' | 'includeHyphenAndUnderscore' | 'includeSpace' | 'includeDot', true | false> | |
): string { | |
let contentString = nanoidDictionary.alphanumeric; | |
if (stringType === NanoidStringEnum.NUMERIC) { | |
contentString = nanoidDictionary.numbers; | |
} else if (stringType === NanoidStringEnum.ALPHABETS) { | |
contentString = nanoidDictionary.lowercase + nanoidDictionary.uppercase; | |
} else if (stringType === NanoidStringEnum.LOWERCASE) { | |
contentString = nanoidDictionary.lowercase; | |
} else if (stringType === NanoidStringEnum.LOWERCASE_NUMERIC) { | |
contentString = nanoidDictionary.lowercase + nanoidDictionary.numbers; | |
} else if (stringType === NanoidStringEnum.UPPERCASE) { | |
contentString = nanoidDictionary.uppercase; | |
} else if (stringType === NanoidStringEnum.UPPERCASE_NUMERIC) { | |
contentString = nanoidDictionary.uppercase + nanoidDictionary.numbers; | |
} | |
// TODO: implement BuilderObject | |
/** | |
* else if (stringType === NanoidStringEnum.BuilderObject) { | |
* if (!this.isEmptyObject(custom as BuilderObjectType)) { | |
* contentString = ''; | |
* if (custom.includeNumeric === true) { | |
* contentString += nanoidNumbers; | |
* } | |
* if (custom.includeLowercase === true) { | |
* contentString += nanoidLowercase; | |
* } | |
* if (custom.includeUppercase === true) { | |
* contentString += nanoidUppercase; | |
* } | |
* if (custom.includeHyphenAndUnderscore === true) { | |
* contentString += "-_"; | |
* } | |
* if (custom.includeSpace === true) { | |
* contentString += " "; | |
* } | |
* if (custom.includeDot === true) { | |
* contentString += "."; | |
* } | |
* if (!!!contentString) { | |
* contentString = nanoidAlphanumeric; | |
* } | |
* } | |
* } | |
*/ | |
else if (stringType === NanoidStringEnum.CUSTOM && _.isString(custom) && String(custom).length > 1) { | |
contentString = nanoidDictionary.lowercase + nanoidDictionary.uppercase; | |
} else { | |
contentString = nanoidDictionary.alphanumeric; | |
} | |
if (stringType === NanoidStringEnum.UUID) { | |
if (this.isNumber(String(stringLength))) { | |
nanoid(Number(stringLength)); | |
} | |
return nanoid(); | |
} | |
return customAlphabet(contentString, 10)(this.isNumber(String(stringLength)) ? Number(stringLength) : 10); | |
} |
Author
Omkaragrawal
commented
Apr 21, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment