mktemp
Version:
create temporary files and directories
1 lines • 2.28 kB
Source Map (JSON)
{"version":3,"file":"unique_name.cjs","names":[],"sources":["../src/unique_name.ts"],"sourcesContent":["const placeholderRegex = /(X+)[^X]*$/;\nconst table = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';\nconst tableLength = table.length;\n\n/**\n * count the number of possible outcomes for the template\n *\n * @param template - template string\n * @returns count of possible outcomes\n *\n * @throws {@link TypeError}\n * if template is not a string\n */\nexport function getOutcomeCount(template: string): number {\n if (typeof template !== 'string') {\n throw new TypeError(`template must be a string: ${template}`);\n }\n\n const matches = template.match(placeholderRegex);\n\n if (matches === null || !matches[1]) {\n return 1;\n }\n\n return tableLength ** matches[1].length;\n}\n\n/**\n * generate unique name\n *\n * @param template - template string\n * @returns unique name string\n *\n * @throws {@link TypeError}\n * if template is not a string\n */\nexport function generateUniqueName(template: string): string {\n if (typeof template !== 'string') {\n throw new TypeError(`template must be a string: ${template}`);\n }\n\n const matches = template.match(placeholderRegex);\n\n if (matches === null || !matches[1]) {\n return template;\n }\n\n const result = [];\n\n for (let i = 0, len = matches[1].length; i < len; i += 1) {\n result.push(table[Math.floor(Math.random() * tableLength)]);\n }\n\n const { index = 0 } = matches;\n\n return (\n template.slice(0, index) +\n result.join('') +\n template.slice(index + result.length)\n );\n}\n"],"mappings":";;AAAA,MAAM,mBAAmB;AACzB,MAAM,QAAQ;AACd,MAAM,cAAc;;;;;;;;;;AAWpB,SAAgB,gBAAgB,UAA0B;AACxD,KAAI,OAAO,aAAa,SACtB,OAAM,IAAI,UAAU,8BAA8B,WAAW;CAG/D,MAAM,UAAU,SAAS,MAAM,iBAAiB;AAEhD,KAAI,YAAY,QAAQ,CAAC,QAAQ,GAC/B,QAAO;AAGT,QAAO,eAAe,QAAQ,GAAG;;;;;;;;;;;AAYnC,SAAgB,mBAAmB,UAA0B;AAC3D,KAAI,OAAO,aAAa,SACtB,OAAM,IAAI,UAAU,8BAA8B,WAAW;CAG/D,MAAM,UAAU,SAAS,MAAM,iBAAiB;AAEhD,KAAI,YAAY,QAAQ,CAAC,QAAQ,GAC/B,QAAO;CAGT,MAAM,SAAS,EAAE;AAEjB,MAAK,IAAI,IAAI,GAAG,MAAM,QAAQ,GAAG,QAAQ,IAAI,KAAK,KAAK,EACrD,QAAO,KAAK,MAAM,KAAK,MAAM,KAAK,QAAQ,GAAG,YAAY,EAAE;CAG7D,MAAM,EAAE,QAAQ,MAAM;AAEtB,QACE,SAAS,MAAM,GAAG,MAAM,GACxB,OAAO,KAAK,GAAG,GACf,SAAS,MAAM,QAAQ,OAAO,OAAO"}