backend-randomid
v1.1.6
Published
This section documents the UUID generation utility provided by the `@willeder-inc/backend` package. It includes the `generatedId` function, which generates a unique identifier using the UUID v4 standard.
Downloads
82
Readme
UUID Generation Utility
This section documents the UUID generation utility provided by the @willeder-inc/backend
package. It includes the generatedId
function, which generates a unique identifier using the UUID v4 standard.
generatedId
The generatedId
function creates a universally unique identifier (UUID) using the uuid
package. This is useful for generating unique keys for database entries, session IDs, or any other use case where a unique identifier is required.
Example Usage
Here’s an example of how to use the generatedId
function:
import { generatedId } from '@willeder-inc/backend';
const uniqueId = generatedId();
console.log('Generated ID:', uniqueId); // Example output: 'e6f2a60c-d4c3-4c6e-9d56-7bcd1e5f0f83'
Unique ID Generation Utility
This section documents the unique ID generation utility provided by the @willeder-inc/backend
package. It includes the generateUniqueId
function, which creates a shortened unique identifier from a UUID v4.
generateUniqueId
The generateUniqueId
function generates a unique identifier by creating a UUID v4 and then truncating it to the first 12 characters. This is useful for scenarios where a shorter unique key is needed while still maintaining a low risk of collisions.
Example Usage
Here’s an example of how to use the generateUniqueId
function:
import { generateUniqueId } from '@willeder-inc/backend';
const uniqueId = generateUniqueId();
console.log('Generated Unique ID:', uniqueId); // Example output: 'f6e0b38f7c12'
Random Number Generation Utility
This section documents the random number generation utility provided by the @willeder-inc/backend
package. It includes the randomNumber
function, which generates a random number or string based on specified length and character set.
randomNumber
The randomNumber
function generates a random string of a specified length using a defined character set. This function can be used to create unique identifiers, tokens, or any random strings needed for your application.
Parameters
- length (
number
): The desired length of the generated string. Defaults to6
if not specified. - charset (
'alphanumeric' | 'alphabetic' | 'numeric' | 'hex' | 'binary' | 'octal'
): The character set to use for generating the string:alphanumeric
: Includes letters and numbers (e.g.,A-Z
,a-z
,0-9
).alphabetic
: Includes only letters (e.g.,A-Z
,a-z
).numeric
: Includes only numbers (e.g.,0-9
).hex
: Includes hexadecimal characters (e.g.,0-9
,a-f
).binary
: Includes binary characters (e.g.,0
,1
).octal
: Includes octal characters (e.g.,0-7
).
Returns
- string: A randomly generated string of the specified length and character set.
Example Usage
Here’s an example of how to use the randomNumber
function:
import { randomNumber } from '@willeder-inc/backend';
const randomNum = randomNumber(8, 'alphanumeric');
console.log('Generated Random Number:', randomNum); // Example output: 'A3cF7g9Z'
MongoDB ObjectId Generation Utility
This section documents the MongoDB ObjectId generation utility provided by the @willeder-inc/backend
package. It includes the objectId
function, which creates a new unique ObjectId that can be used for database documents.
objectId
The objectId
function generates a new MongoDB ObjectId. ObjectIds are 12-byte identifiers typically used as unique identifiers for documents in MongoDB.
Returns
- mongoose.Types.ObjectId: A newly created ObjectId that can be used as an identifier in MongoDB.
Example Usage
Here’s an example of how to use the objectId
function:
import { objectId } from '@willeder-inc/backend';
const newId = objectId();
console.log('Generated ObjectId:', newId); // Example output: ObjectId("507f1f77bcf86cd799439011")
Password Generation Utility
This section documents the password generation utility provided by the @willeder-inc/backend
package. It includes the generatePassword
function, which creates a secure password that meets specified criteria.
generatePassword
The generatePassword
function generates a random password of a specified length and ensures that it meets the requirements defined by a regular expression.
Parameters
- length:
number
- The desired length of the generated password.
- RegExp:
RegExp
- A regular expression that the generated password must match. This allows for enforcing rules such as the inclusion of special characters, uppercase letters, etc.
Returns
- string: A randomly generated password that meets the specified length and regex criteria.
Example Usage
Here’s an example of how to use the generatePassword
function:
import { generatePassword } from '@willeder-inc/backend';
// Example regex: at least one uppercase letter and one digit
const passwordRegex = /^(?=.*[A-Z])(?=.*\d).{8,}$/; // At least 8 characters, one uppercase letter, one digit
const passwordLength = 12;
const newPassword = generatePassword(passwordLength, passwordRegex);
console.log('Generated Password:', newPassword); // Example output: 'A1b2c3D4e5F6'