mktemp
Version:
create temporary files and directories
1 lines • 2.38 kB
Source Map (JSON)
{"version":3,"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 Math.pow(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,MAAM;AAWnB,SAAS,gBAAgB,UAA0B;AACxD,MAAI,OAAO,aAAa,UAAU;AAChC,UAAM,IAAI,UAAU,8BAA8B,QAAQ,EAAE;AAAA,EAC9D;AAEA,QAAM,UAAU,SAAS,MAAM,gBAAgB;AAE/C,MAAI,YAAY,QAAQ,CAAC,QAAQ,CAAC,GAAG;AACnC,WAAO;AAAA,EACT;AAEA,SAAO,KAAK,IAAI,aAAa,QAAQ,CAAC,EAAE,MAAM;AAChD;AAWO,SAAS,mBAAmB,UAA0B;AAC3D,MAAI,OAAO,aAAa,UAAU;AAChC,UAAM,IAAI,UAAU,8BAA8B,QAAQ,EAAE;AAAA,EAC9D;AAEA,QAAM,UAAU,SAAS,MAAM,gBAAgB;AAE/C,MAAI,YAAY,QAAQ,CAAC,QAAQ,CAAC,GAAG;AACnC,WAAO;AAAA,EACT;AAEA,QAAM,SAAS,CAAC;AAEhB,WAAS,IAAI,GAAG,MAAM,QAAQ,CAAC,EAAE,QAAQ,IAAI,KAAK,KAAK,GAAG;AACxD,WAAO,KAAK,MAAM,KAAK,MAAM,KAAK,OAAO,IAAI,WAAW,CAAC,CAAC;AAAA,EAC5D;AAEA,QAAM,EAAE,QAAQ,EAAE,IAAI;AAEtB,SACE,SAAS,MAAM,GAAG,KAAK,IACvB,OAAO,KAAK,EAAE,IACd,SAAS,MAAM,QAAQ,OAAO,MAAM;AAExC;","names":[]}