unqommented
Version:
A Node.js utility that quickly identifies files with uncommented code in your codebase. Designed for developers who want to efficiently tell LLMs exactly which files need comments added.
29 lines (26 loc) • 1.04 kB
JavaScript
/**
* @file ID generation utilities
* @description Provides functions for generating unique identifiers
* @module id-utils
*/
const crypto = require('crypto');
const localVars = require('../config/localVars');
/**
* @function generateId
* @description Generates a unique identifier using Node.js's crypto module.
* @rationale Using `crypto.randomBytes` is a secure method for generating random IDs and
* avoids ESM-only dependencies that could complicate the build or execution of the CLI.
* @param {number} [length=8] - The desired length of the ID.
* @returns {string} A random alphanumeric ID.
* @throws {Error} If the length is not a positive number.
*/
function generateId(length = 8) {
if (!Number.isInteger(length) || length <= 0) {
throw new Error(localVars.ERROR_MESSAGES.LENGTH_NOT_POSITIVE);
}
// Generate random bytes and convert to a hex string, then slice to the desired length.
return crypto.randomBytes(Math.ceil(length / 2)).toString('hex').slice(0, length);
}
module.exports = {
generateId,
};