@necto/id
Version:
Necto's library for generating algorithmic ID's based on specified parameters.
1 lines • 1.86 kB
Source Map (JSON)
{"version":3,"sources":["../src/index.ts","../src/id.ts"],"sourcesContent":["/**\n * Copyright (c) Corinvo, LLC. and affiliates.\n *\n * This source code is licensed under the MIT license found in the\n * LICENSE file in the root directory of this source tree.\n *\n */\n\nexport * from './id';\n","/**\n * Copyright (c) Corinvo, LLC. and affiliates.\n *\n * This source code is licensed under the MIT license found in the\n * LICENSE file in the root directory of this source tree.\n *\n */\n\nimport randomBytes from 'randombytes';\nimport { ALPHABET } from '@necto/constants';\n\n/**\n * Generates a random ID of the specified length using the provided character set.\n * @param length - The length of the ID to generate.\n * @param charset - The character set to use for generating the ID (default: ALPHABET_COMBINED).\n * @returns A randomly generated ID string.\n */\nexport function id(\n length = 21,\n charset: string[] = Array.from([\n ...ALPHABET.LOWERCASE,\n ...ALPHABET.CAPITALIZED\n ])\n): string {\n if (length <= 0) {\n throw new Error('Length must be a positive integer.');\n }\n\n const charsetLength = charset.length;\n const randomValues = randomBytes(length);\n\n return Array.from(randomValues)\n .map((value) => charset[value % charsetLength])\n .join('');\n}\n"],"mappings":"0jBAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,QAAAE,IAAA,eAAAC,EAAAH,GCQA,IAAAI,EAAwB,4BACxBC,EAAyB,4BAQlB,SAASC,EACdC,EAAS,GACTC,EAAoB,MAAM,KAAK,CAC7B,GAAG,WAAS,UACZ,GAAG,WAAS,WACd,CAAC,EACO,CACR,GAAID,GAAU,EACZ,MAAM,IAAI,MAAM,oCAAoC,EAGtD,IAAME,EAAgBD,EAAQ,OACxBE,KAAe,EAAAC,SAAYJ,CAAM,EAEvC,OAAO,MAAM,KAAKG,CAAY,EAC3B,IAAKE,GAAUJ,EAAQI,EAAQH,CAAa,CAAC,EAC7C,KAAK,EAAE,CACZ","names":["index_exports","__export","id","__toCommonJS","import_randombytes","import_constants","id","length","charset","charsetLength","randomValues","randomBytes","value"]}