mktemp
Version:
create temporary files and directories
43 lines (42 loc) • 1.54 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
//#region src/unique_name.ts
const placeholderRegex = /(X+)[^X]*$/;
const table = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
const tableLength = 62;
/**
* count the number of possible outcomes for the template
*
* @param template - template string
* @returns count of possible outcomes
*
* @throws {@link TypeError}
* if template is not a string
*/
function getOutcomeCount(template) {
if (typeof template !== "string") throw new TypeError(`template must be a string: ${template}`);
const matches = template.match(placeholderRegex);
if (matches === null || !matches[1]) return 1;
return tableLength ** matches[1].length;
}
/**
* generate unique name
*
* @param template - template string
* @returns unique name string
*
* @throws {@link TypeError}
* if template is not a string
*/
function generateUniqueName(template) {
if (typeof template !== "string") throw new TypeError(`template must be a string: ${template}`);
const matches = template.match(placeholderRegex);
if (matches === null || !matches[1]) return template;
const result = [];
for (let i = 0, len = matches[1].length; i < len; i += 1) result.push(table[Math.floor(Math.random() * tableLength)]);
const { index = 0 } = matches;
return template.slice(0, index) + result.join("") + template.slice(index + result.length);
}
//#endregion
exports.generateUniqueName = generateUniqueName;
exports.getOutcomeCount = getOutcomeCount;
//# sourceMappingURL=unique_name.cjs.map