UNPKG

ts-randomstring

Version:

A library used for generating random strings, written in TypeScript and based on Node.

127 lines (126 loc) 6.04 kB
import { Capitalisation, Capitalization, CharacterSetType } from "./characterSetHandler"; /** * The base interface for random string options. */ interface RandomStringOptionsBase { length?: number; charSetType?: CharacterSetType; } /** * The interface for controlling the nature of random string generation (UK English). */ export interface RandomStringOptions extends RandomStringOptionsBase { capitalisation?: Capitalisation; } /** * The interface for controlling the nature of random string generation (US English). */ export interface RandomStringOptions extends RandomStringOptionsBase { capitalization?: Capitalization; } /** * Generates and returns a random string. * The optional random string options define the nature of the string to generated. * * This method instantiates a new `RandomString` class before executing `generate`. * For multiple string generations, it is recommended to instantiate a `RandomString` instance yourself. * * @param options The random string options to parse and apply to string generation. * Supported options: * ``` * length: number (default=32) * charSetType: CharacterSetType (default=CharacterSetType.Alphanumeric) * capitalisation/capitalization: Capitalisation/Capitalization (default=Capitalisation.Mixed) * ``` * @returns The randomly generated string. */ export declare const generateRandomString: (options?: RandomStringOptions | undefined) => string; /** * Asynchronously generates a random string, executing the passed callback when generation is complete which contains the generated string. * The optional random string options define the nature of the string to generated. * * This method instantiates a new `RandomString` class before executing `generateAsync`. * For multiple string generations, it is recommended to instantiate a `RandomString` instance yourself. * * @param callback The callback (`error`, `generated`) to execute when generation is complete. * @param options The random string options to parse and apply to string generation. * Supported options: * ``` * length: number (default=32) * charSetType: CharacterSetType (default=CharacterSetType.Alphanumeric) * capitalisation/capitalization: Capitalisation/Capitalization (default=Capitalisation.Mixed) * ``` * @returns void. */ export declare const generateRandomStringAsync: (callback: (error: Error | undefined, generated: string | undefined) => void, options?: RandomStringOptions | undefined) => void; /** * The primary class for generating random strings. */ export declare class RandomString { /** * Generates and returns a random string. * The optional random string options define the nature of the string to generated. * * @param options The random string options to parse and apply to string generation. * Supported options: * ``` * length: number (default=32) * charSetType: CharacterSetType (default=CharacterSetType.Alphanumeric) * capitalisation/capitalization: Capitalisation/Capitalization (default=Capitalisation.Mixed) * ``` * @returns The randomly generated string. */ generate(options?: RandomStringOptions): string; /** * Asynchronously generates a random string, executing the passed callback when generation is complete which contains the generated string. * The optional random string options define the nature of the string to generated. * * @param callback The callback (`error`, `generated`) to execute when generation is complete. * @param options The random string options to parse and apply to string generation. * Supported options: * ``` * length: number (default=32) * charSetType: CharacterSetType (default=CharacterSetType.Alphanumeric) * capitalisation/capitalization: Capitalisation/Capitalization (default=Capitalisation.Mixed) * ``` * @returns void. */ generateAsync(callback: (error: Error | undefined, generated: string | undefined) => void, options?: RandomStringOptions): void; /** * Parses the passed random string options, returning their values if specified or their default values if not. * * @param options The random string options to parse. * @returns The parsed options values. */ private parseOptions; /** * Returns the maximum byte for a particular character set. * * @param length The length of the current character set (i.e., the unique characters in the set). * @returns The maximum byte value. */ private getMaxByte; /** * Safely attempts to generate a buffer of randomly generated bytes. * * @param length The required buffer length of the randomly generated buffer. * @returns The randomly generated buffer or an error if an error is caught. */ private getRandomBytesSafe; /** * Generates a random string. * @param buffer The randomly generated buffer which is used as the base for random characters. * @param state See {@link GeneratedStringState} for generated string state documentation. * @returns The generated string. */ private generateString; /** * Generates a random string asynchronously. That is, the passed callback is executed when generation completes * * @param state See {@link GeneratedStringState} for generated string state documentation. * @param callback The callback (`error`, `generated`) executed when string generation is complete. * @returns void. */ private generateStringAsync; } export {};