@necto/id 1.6.10 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,83 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+ let _necto_assert = require("@necto/assert");
3
+ let _necto_crypto = require("@necto/crypto");
4
+ let _necto_math = require("@necto/math");
5
+ let _necto_constants = require("@necto/constants");
6
+
7
+ //#region src/constants.ts
8
+ const NUMERIC = _necto_constants.NUMBERS.DIGITS.join("");
9
+ const LOWERCASE = _necto_constants.ALPHABET.LOWERCASE.join("");
10
+ const UPPERCASE = _necto_constants.ALPHABET.CAPITALIZED.join("");
11
+ /**
12
+ * Ready-made alphabets for `id()`.
13
+ *
14
+ * @example
15
+ * id(7, Alphabet.LowercaseAlphanumeric); // 'k3x9a0q'
16
+ */
17
+ const Alphabet = {
18
+ /** a-z */
19
+ Lowercase: LOWERCASE,
20
+ /** A-Z */
21
+ Uppercase: UPPERCASE,
22
+ /** a-z A-Z */
23
+ Letters: `${LOWERCASE}${UPPERCASE}`,
24
+ /** 0-9 */
25
+ Numeric: NUMERIC,
26
+ /** a-z A-Z 0-9 */
27
+ Alphanumeric: `${LOWERCASE}${UPPERCASE}${NUMERIC}`,
28
+ /** a-z 0-9 */
29
+ LowercaseAlphanumeric: `${LOWERCASE}${NUMERIC}`,
30
+ /** a-z A-Z 0-9 _ - (safe in URLs, file names and HTML ids) */
31
+ UrlSafe: `${LOWERCASE}${UPPERCASE}${NUMERIC}_-`,
32
+ /** 0-9 a-f */
33
+ Hex: `${NUMERIC}abcdef`
34
+ };
35
+
36
+ //#endregion
37
+ //#region src/id.ts
38
+ /**
39
+ * Copyright (c) Corinvo, LLC. and affiliates.
40
+ *
41
+ * This source code is licensed under the MIT license found in the
42
+ * LICENSE file in the root directory of this source tree.
43
+ *
44
+ */
45
+ /**
46
+ * Generates a random ID of the specified length from the given alphabet.
47
+ *
48
+ * Every character of the alphabet is equally likely: random bytes that fall
49
+ * outside the alphabet are discarded instead of being wrapped around, which
50
+ * would favour the first characters.
51
+ *
52
+ * @param length - The length of the ID to generate.
53
+ * @param alphabet - The characters to draw from: an `Alphabet` preset, any
54
+ * string, or an array of characters. Defaults to `Alphabet.Letters`.
55
+ * @returns A randomly generated ID string.
56
+ *
57
+ * @example
58
+ * id(); // 21 letters
59
+ * id(7, Alphabet.LowercaseAlphanumeric); // 'k3x9a0q'
60
+ * id(12, 'abc123'); // custom alphabet
61
+ */
62
+ function id(length = 21, alphabet = Alphabet.Letters) {
63
+ (0, _necto_assert.assert)(Number.isInteger(length) && length > 0, `[necto.id] NT0601: id requires a positive integer length, received ${String(length)}.`);
64
+ const characters = typeof alphabet === "string" ? Array.from(alphabet) : alphabet;
65
+ (0, _necto_assert.assert)(characters.length > 0 && characters.length <= 256, `[necto.id] NT0602: id requires an alphabet of 1 to 256 characters, received ${characters.length}.`);
66
+ if (characters.length === 1) return characters[0].repeat(length);
67
+ const mask = (2 << (0, _necto_math.floor)((0, _necto_math.log2)(characters.length - 1))) - 1;
68
+ const batchSize = (0, _necto_math.ceil)(1.6 * mask * length / characters.length);
69
+ const result = [];
70
+ while (result.length < length) {
71
+ const bytes = (0, _necto_crypto.randomBytes)(batchSize);
72
+ for (let index = 0; index < bytes.length && result.length < length; index++) {
73
+ const position = bytes[index] & mask;
74
+ if (position < characters.length) result.push(characters[position]);
75
+ }
76
+ }
77
+ return result.join("");
78
+ }
79
+
80
+ //#endregion
81
+ exports.Alphabet = Alphabet;
82
+ exports.id = id;
83
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","names":["NUMBERS","ALPHABET"],"sources":["../../src/constants.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\nimport { ALPHABET, NUMBERS } from '@necto/constants';\n\nconst NUMERIC: string = NUMBERS.DIGITS.join('');\nconst LOWERCASE: string = ALPHABET.LOWERCASE.join('');\nconst UPPERCASE: string = ALPHABET.CAPITALIZED.join('');\n\n/**\n * Ready-made alphabets for `id()`.\n *\n * @example\n * id(7, Alphabet.LowercaseAlphanumeric); // 'k3x9a0q'\n */\nexport const Alphabet = {\n /** a-z */\n Lowercase: LOWERCASE,\n\n /** A-Z */\n Uppercase: UPPERCASE,\n\n /** a-z A-Z */\n Letters: `${LOWERCASE}${UPPERCASE}`,\n\n /** 0-9 */\n Numeric: NUMERIC,\n /** a-z A-Z 0-9 */\n\n Alphanumeric: `${LOWERCASE}${UPPERCASE}${NUMERIC}`,\n /** a-z 0-9 */\n LowercaseAlphanumeric: `${LOWERCASE}${NUMERIC}`,\n\n /** a-z A-Z 0-9 _ - (safe in URLs, file names and HTML ids) */\n UrlSafe: `${LOWERCASE}${UPPERCASE}${NUMERIC}_-`,\n\n /** 0-9 a-f */\n Hex: `${NUMERIC}abcdef`\n} as const;\n\nexport type Alphabet = (typeof Alphabet)[keyof typeof Alphabet];\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 { assert } from '@necto/assert';\nimport { randomBytes } from '@necto/crypto';\nimport { floor, log2, ceil } from '@necto/math';\n\nimport { Alphabet } from './constants';\n\n/**\n * Generates a random ID of the specified length from the given alphabet.\n *\n * Every character of the alphabet is equally likely: random bytes that fall\n * outside the alphabet are discarded instead of being wrapped around, which\n * would favour the first characters.\n *\n * @param length - The length of the ID to generate.\n * @param alphabet - The characters to draw from: an `Alphabet` preset, any\n * string, or an array of characters. Defaults to `Alphabet.Letters`.\n * @returns A randomly generated ID string.\n *\n * @example\n * id(); // 21 letters\n * id(7, Alphabet.LowercaseAlphanumeric); // 'k3x9a0q'\n * id(12, 'abc123'); // custom alphabet\n */\nexport function id(\n length: number = 21,\n alphabet: string | readonly string[] = Alphabet.Letters\n): string {\n assert(\n Number.isInteger(length) && length > 0,\n __DEV__\n ? `[necto.id] NT0601: id requires a positive integer length, received ${String(length)}.`\n : '[necto.id] NT0601: https://necto.dev/errors/NT0601'\n );\n\n const characters: readonly string[] =\n typeof alphabet === 'string' ? Array.from(alphabet) : alphabet;\n\n assert(\n characters.length > 0 && characters.length <= 256,\n __DEV__\n ? `[necto.id] NT0602: id requires an alphabet of 1 to 256 characters, received ${characters.length}.`\n : '[necto.id] NT0602: https://necto.dev/errors/NT0602'\n );\n\n if (characters.length === 1) {\n return characters[0].repeat(length);\n }\n\n const mask: number = (2 << floor(log2(characters.length - 1))) - 1;\n const batchSize: number = ceil((1.6 * mask * length) / characters.length);\n\n const result: string[] = [];\n\n while (result.length < length) {\n const bytes: Uint8Array = randomBytes(batchSize);\n\n for (let index: number = 0; index < bytes.length && result.length < length; index++) {\n const position: number = bytes[index] & mask;\n\n if (position < characters.length) {\n result.push(characters[position]);\n }\n }\n }\n\n return result.join('');\n}\n"],"mappings":";;;;;;;AASA,MAAM,UAAkBA,yBAAQ,OAAO,KAAK,EAAE;AAC9C,MAAM,YAAoBC,0BAAS,UAAU,KAAK,EAAE;AACpD,MAAM,YAAoBA,0BAAS,YAAY,KAAK,EAAE;;;;;;;AAQtD,MAAa,WAAW;;CAEtB,WAAW;;CAGX,WAAW;;CAGX,SAAS,GAAG,YAAY;;CAGxB,SAAS;;CAGT,cAAc,GAAG,YAAY,YAAY;;CAEzC,uBAAuB,GAAG,YAAY;;CAGtC,SAAS,GAAG,YAAY,YAAY,QAAQ;;CAG5C,KAAK,GAAG,QAAQ;AAClB;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACXA,SAAgB,GACd,SAAiB,IACjB,WAAuC,SAAS,SACxC;CACR,0BACE,OAAO,UAAU,MAAM,KAAK,SAAS,GAEjC,sEAAsE,OAAO,MAAM,EAAE,EAE3F;CAEA,MAAM,aACJ,OAAO,aAAa,WAAW,MAAM,KAAK,QAAQ,IAAI;CAExD,0BACE,WAAW,SAAS,KAAK,WAAW,UAAU,KAE1C,+EAA+E,WAAW,OAAO,EAEvG;CAEA,IAAI,WAAW,WAAW,GACxB,OAAO,WAAW,EAAE,CAAC,OAAO,MAAM;CAGpC,MAAM,QAAgB,kDAAgB,WAAW,SAAS,CAAC,CAAC,KAAK;CACjE,MAAM,kCAA0B,MAAM,OAAO,SAAU,WAAW,MAAM;CAExE,MAAM,SAAmB,CAAC;CAE1B,OAAO,OAAO,SAAS,QAAQ;EAC7B,MAAM,uCAAgC,SAAS;EAE/C,KAAK,IAAI,QAAgB,GAAG,QAAQ,MAAM,UAAU,OAAO,SAAS,QAAQ,SAAS;GACnF,MAAM,WAAmB,MAAM,SAAS;GAExC,IAAI,WAAW,WAAW,QACxB,OAAO,KAAK,WAAW,SAAS;EAEpC;CACF;CAEA,OAAO,OAAO,KAAK,EAAE;AACvB"}
@@ -0,0 +1,81 @@
1
+ import { assert } from "@necto/assert";
2
+ import { randomBytes } from "@necto/crypto";
3
+ import { ceil, floor, log2 } from "@necto/math";
4
+ import { ALPHABET, NUMBERS } from "@necto/constants";
5
+
6
+ //#region src/constants.ts
7
+ const NUMERIC = NUMBERS.DIGITS.join("");
8
+ const LOWERCASE = ALPHABET.LOWERCASE.join("");
9
+ const UPPERCASE = ALPHABET.CAPITALIZED.join("");
10
+ /**
11
+ * Ready-made alphabets for `id()`.
12
+ *
13
+ * @example
14
+ * id(7, Alphabet.LowercaseAlphanumeric); // 'k3x9a0q'
15
+ */
16
+ const Alphabet = {
17
+ /** a-z */
18
+ Lowercase: LOWERCASE,
19
+ /** A-Z */
20
+ Uppercase: UPPERCASE,
21
+ /** a-z A-Z */
22
+ Letters: `${LOWERCASE}${UPPERCASE}`,
23
+ /** 0-9 */
24
+ Numeric: NUMERIC,
25
+ /** a-z A-Z 0-9 */
26
+ Alphanumeric: `${LOWERCASE}${UPPERCASE}${NUMERIC}`,
27
+ /** a-z 0-9 */
28
+ LowercaseAlphanumeric: `${LOWERCASE}${NUMERIC}`,
29
+ /** a-z A-Z 0-9 _ - (safe in URLs, file names and HTML ids) */
30
+ UrlSafe: `${LOWERCASE}${UPPERCASE}${NUMERIC}_-`,
31
+ /** 0-9 a-f */
32
+ Hex: `${NUMERIC}abcdef`
33
+ };
34
+
35
+ //#endregion
36
+ //#region src/id.ts
37
+ /**
38
+ * Copyright (c) Corinvo, LLC. and affiliates.
39
+ *
40
+ * This source code is licensed under the MIT license found in the
41
+ * LICENSE file in the root directory of this source tree.
42
+ *
43
+ */
44
+ /**
45
+ * Generates a random ID of the specified length from the given alphabet.
46
+ *
47
+ * Every character of the alphabet is equally likely: random bytes that fall
48
+ * outside the alphabet are discarded instead of being wrapped around, which
49
+ * would favour the first characters.
50
+ *
51
+ * @param length - The length of the ID to generate.
52
+ * @param alphabet - The characters to draw from: an `Alphabet` preset, any
53
+ * string, or an array of characters. Defaults to `Alphabet.Letters`.
54
+ * @returns A randomly generated ID string.
55
+ *
56
+ * @example
57
+ * id(); // 21 letters
58
+ * id(7, Alphabet.LowercaseAlphanumeric); // 'k3x9a0q'
59
+ * id(12, 'abc123'); // custom alphabet
60
+ */
61
+ function id(length = 21, alphabet = Alphabet.Letters) {
62
+ assert(Number.isInteger(length) && length > 0, `[necto.id] NT0601: id requires a positive integer length, received ${String(length)}.`);
63
+ const characters = typeof alphabet === "string" ? Array.from(alphabet) : alphabet;
64
+ assert(characters.length > 0 && characters.length <= 256, `[necto.id] NT0602: id requires an alphabet of 1 to 256 characters, received ${characters.length}.`);
65
+ if (characters.length === 1) return characters[0].repeat(length);
66
+ const mask = (2 << floor(log2(characters.length - 1))) - 1;
67
+ const batchSize = ceil(1.6 * mask * length / characters.length);
68
+ const result = [];
69
+ while (result.length < length) {
70
+ const bytes = randomBytes(batchSize);
71
+ for (let index = 0; index < bytes.length && result.length < length; index++) {
72
+ const position = bytes[index] & mask;
73
+ if (position < characters.length) result.push(characters[position]);
74
+ }
75
+ }
76
+ return result.join("");
77
+ }
78
+
79
+ //#endregion
80
+ export { Alphabet, id };
81
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../../src/constants.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\nimport { ALPHABET, NUMBERS } from '@necto/constants';\n\nconst NUMERIC: string = NUMBERS.DIGITS.join('');\nconst LOWERCASE: string = ALPHABET.LOWERCASE.join('');\nconst UPPERCASE: string = ALPHABET.CAPITALIZED.join('');\n\n/**\n * Ready-made alphabets for `id()`.\n *\n * @example\n * id(7, Alphabet.LowercaseAlphanumeric); // 'k3x9a0q'\n */\nexport const Alphabet = {\n /** a-z */\n Lowercase: LOWERCASE,\n\n /** A-Z */\n Uppercase: UPPERCASE,\n\n /** a-z A-Z */\n Letters: `${LOWERCASE}${UPPERCASE}`,\n\n /** 0-9 */\n Numeric: NUMERIC,\n /** a-z A-Z 0-9 */\n\n Alphanumeric: `${LOWERCASE}${UPPERCASE}${NUMERIC}`,\n /** a-z 0-9 */\n LowercaseAlphanumeric: `${LOWERCASE}${NUMERIC}`,\n\n /** a-z A-Z 0-9 _ - (safe in URLs, file names and HTML ids) */\n UrlSafe: `${LOWERCASE}${UPPERCASE}${NUMERIC}_-`,\n\n /** 0-9 a-f */\n Hex: `${NUMERIC}abcdef`\n} as const;\n\nexport type Alphabet = (typeof Alphabet)[keyof typeof Alphabet];\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 { assert } from '@necto/assert';\nimport { randomBytes } from '@necto/crypto';\nimport { floor, log2, ceil } from '@necto/math';\n\nimport { Alphabet } from './constants';\n\n/**\n * Generates a random ID of the specified length from the given alphabet.\n *\n * Every character of the alphabet is equally likely: random bytes that fall\n * outside the alphabet are discarded instead of being wrapped around, which\n * would favour the first characters.\n *\n * @param length - The length of the ID to generate.\n * @param alphabet - The characters to draw from: an `Alphabet` preset, any\n * string, or an array of characters. Defaults to `Alphabet.Letters`.\n * @returns A randomly generated ID string.\n *\n * @example\n * id(); // 21 letters\n * id(7, Alphabet.LowercaseAlphanumeric); // 'k3x9a0q'\n * id(12, 'abc123'); // custom alphabet\n */\nexport function id(\n length: number = 21,\n alphabet: string | readonly string[] = Alphabet.Letters\n): string {\n assert(\n Number.isInteger(length) && length > 0,\n __DEV__\n ? `[necto.id] NT0601: id requires a positive integer length, received ${String(length)}.`\n : '[necto.id] NT0601: https://necto.dev/errors/NT0601'\n );\n\n const characters: readonly string[] =\n typeof alphabet === 'string' ? Array.from(alphabet) : alphabet;\n\n assert(\n characters.length > 0 && characters.length <= 256,\n __DEV__\n ? `[necto.id] NT0602: id requires an alphabet of 1 to 256 characters, received ${characters.length}.`\n : '[necto.id] NT0602: https://necto.dev/errors/NT0602'\n );\n\n if (characters.length === 1) {\n return characters[0].repeat(length);\n }\n\n const mask: number = (2 << floor(log2(characters.length - 1))) - 1;\n const batchSize: number = ceil((1.6 * mask * length) / characters.length);\n\n const result: string[] = [];\n\n while (result.length < length) {\n const bytes: Uint8Array = randomBytes(batchSize);\n\n for (let index: number = 0; index < bytes.length && result.length < length; index++) {\n const position: number = bytes[index] & mask;\n\n if (position < characters.length) {\n result.push(characters[position]);\n }\n }\n }\n\n return result.join('');\n}\n"],"mappings":";;;;;;AASA,MAAM,UAAkB,QAAQ,OAAO,KAAK,EAAE;AAC9C,MAAM,YAAoB,SAAS,UAAU,KAAK,EAAE;AACpD,MAAM,YAAoB,SAAS,YAAY,KAAK,EAAE;;;;;;;AAQtD,MAAa,WAAW;;CAEtB,WAAW;;CAGX,WAAW;;CAGX,SAAS,GAAG,YAAY;;CAGxB,SAAS;;CAGT,cAAc,GAAG,YAAY,YAAY;;CAEzC,uBAAuB,GAAG,YAAY;;CAGtC,SAAS,GAAG,YAAY,YAAY,QAAQ;;CAG5C,KAAK,GAAG,QAAQ;AAClB;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACXA,SAAgB,GACd,SAAiB,IACjB,WAAuC,SAAS,SACxC;CACR,OACE,OAAO,UAAU,MAAM,KAAK,SAAS,GAEjC,sEAAsE,OAAO,MAAM,EAAE,EAE3F;CAEA,MAAM,aACJ,OAAO,aAAa,WAAW,MAAM,KAAK,QAAQ,IAAI;CAExD,OACE,WAAW,SAAS,KAAK,WAAW,UAAU,KAE1C,+EAA+E,WAAW,OAAO,EAEvG;CAEA,IAAI,WAAW,WAAW,GACxB,OAAO,WAAW,EAAE,CAAC,OAAO,MAAM;CAGpC,MAAM,QAAgB,KAAK,MAAM,KAAK,WAAW,SAAS,CAAC,CAAC,KAAK;CACjE,MAAM,YAAoB,KAAM,MAAM,OAAO,SAAU,WAAW,MAAM;CAExE,MAAM,SAAmB,CAAC;CAE1B,OAAO,OAAO,SAAS,QAAQ;EAC7B,MAAM,QAAoB,YAAY,SAAS;EAE/C,KAAK,IAAI,QAAgB,GAAG,QAAQ,MAAM,UAAU,OAAO,SAAS,QAAQ,SAAS;GACnF,MAAM,WAAmB,MAAM,SAAS;GAExC,IAAI,WAAW,WAAW,QACxB,OAAO,KAAK,WAAW,SAAS;EAEpC;CACF;CAEA,OAAO,OAAO,KAAK,EAAE;AACvB"}
package/dist/index.cjs CHANGED
@@ -1,2 +1,2 @@
1
- Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require("@necto/assert"),t=require("@necto/constants"),n=require("@necto/crypto");function r(r=21,i=Array.from([...t.ALPHABET.LOWERCASE,...t.ALPHABET.CAPITALIZED])){(0,e.assert)(r>0,`Length must be a positive integer.`);let a=(0,n.randomBytes)(r);return Array.from(a).map(e=>i[e%i.length]).join(``)}exports.id=r;
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require("@necto/assert"),t=require("@necto/crypto"),n=require("@necto/math"),r=require("@necto/constants");const i=r.NUMBERS.DIGITS.join(``),a=r.ALPHABET.LOWERCASE.join(``),o=r.ALPHABET.CAPITALIZED.join(``),s={Lowercase:a,Uppercase:o,Letters:`${a}${o}`,Numeric:i,Alphanumeric:`${a}${o}${i}`,LowercaseAlphanumeric:`${a}${i}`,UrlSafe:`${a}${o}${i}_-`,Hex:`${i}abcdef`};function c(r=21,i=s.Letters){(0,e.assert)(Number.isInteger(r)&&r>0,`[necto.id] NT0601: https://necto.dev/errors/NT0601`);let a=typeof i==`string`?Array.from(i):i;if((0,e.assert)(a.length>0&&a.length<=256,`[necto.id] NT0602: https://necto.dev/errors/NT0602`),a.length===1)return a[0].repeat(r);let o=(2<<(0,n.floor)((0,n.log2)(a.length-1)))-1,c=(0,n.ceil)(1.6*o*r/a.length),l=[];for(;l.length<r;){let e=(0,t.randomBytes)(c);for(let t=0;t<e.length&&l.length<r;t++){let n=e[t]&o;n<a.length&&l.push(a[n])}}return l.join(``)}exports.Alphabet=s,exports.id=c;
2
2
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","names":["ALPHABET"],"sources":["../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\nimport { assert } from '@necto/assert';\nimport { ALPHABET } from '@necto/constants';\nimport { randomBytes } from '@necto/crypto';\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([...ALPHABET.LOWERCASE, ...ALPHABET.CAPITALIZED])\n): string {\n assert(length > 0, 'Length must be a positive integer.');\n\n const randomValues: Uint8Array = randomBytes(length);\n return Array.from(randomValues as Iterable<number>)\n .map((value: number): string => charset[value % charset.length])\n .join('');\n}\n"],"mappings":"2JAkBA,SAAgB,EACd,EAAS,GACT,EAAoB,MAAM,KAAK,CAAC,GAAGA,EAAAA,SAAS,UAAW,GAAGA,EAAAA,SAAS,WAAW,CAAC,EACvE,EACR,EAAA,EAAA,OAAA,CAAO,EAAS,EAAG,oCAAoC,EAEvD,IAAM,GAAA,EAAA,EAAA,YAAA,CAAuC,CAAM,EACnD,OAAO,MAAM,KAAK,CAAgC,CAAC,CAChD,IAAK,GAA0B,EAAQ,EAAQ,EAAQ,OAAO,CAAC,CAC/D,KAAK,EAAE,CACZ"}
1
+ {"version":3,"file":"index.cjs","names":["NUMBERS","ALPHABET"],"sources":["../src/constants.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\nimport { ALPHABET, NUMBERS } from '@necto/constants';\n\nconst NUMERIC: string = NUMBERS.DIGITS.join('');\nconst LOWERCASE: string = ALPHABET.LOWERCASE.join('');\nconst UPPERCASE: string = ALPHABET.CAPITALIZED.join('');\n\n/**\n * Ready-made alphabets for `id()`.\n *\n * @example\n * id(7, Alphabet.LowercaseAlphanumeric); // 'k3x9a0q'\n */\nexport const Alphabet = {\n /** a-z */\n Lowercase: LOWERCASE,\n\n /** A-Z */\n Uppercase: UPPERCASE,\n\n /** a-z A-Z */\n Letters: `${LOWERCASE}${UPPERCASE}`,\n\n /** 0-9 */\n Numeric: NUMERIC,\n /** a-z A-Z 0-9 */\n\n Alphanumeric: `${LOWERCASE}${UPPERCASE}${NUMERIC}`,\n /** a-z 0-9 */\n LowercaseAlphanumeric: `${LOWERCASE}${NUMERIC}`,\n\n /** a-z A-Z 0-9 _ - (safe in URLs, file names and HTML ids) */\n UrlSafe: `${LOWERCASE}${UPPERCASE}${NUMERIC}_-`,\n\n /** 0-9 a-f */\n Hex: `${NUMERIC}abcdef`\n} as const;\n\nexport type Alphabet = (typeof Alphabet)[keyof typeof Alphabet];\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 { assert } from '@necto/assert';\nimport { randomBytes } from '@necto/crypto';\nimport { floor, log2, ceil } from '@necto/math';\n\nimport { Alphabet } from './constants';\n\n/**\n * Generates a random ID of the specified length from the given alphabet.\n *\n * Every character of the alphabet is equally likely: random bytes that fall\n * outside the alphabet are discarded instead of being wrapped around, which\n * would favour the first characters.\n *\n * @param length - The length of the ID to generate.\n * @param alphabet - The characters to draw from: an `Alphabet` preset, any\n * string, or an array of characters. Defaults to `Alphabet.Letters`.\n * @returns A randomly generated ID string.\n *\n * @example\n * id(); // 21 letters\n * id(7, Alphabet.LowercaseAlphanumeric); // 'k3x9a0q'\n * id(12, 'abc123'); // custom alphabet\n */\nexport function id(\n length: number = 21,\n alphabet: string | readonly string[] = Alphabet.Letters\n): string {\n assert(\n Number.isInteger(length) && length > 0,\n __DEV__\n ? `[necto.id] NT0601: id requires a positive integer length, received ${String(length)}.`\n : '[necto.id] NT0601: https://necto.dev/errors/NT0601'\n );\n\n const characters: readonly string[] =\n typeof alphabet === 'string' ? Array.from(alphabet) : alphabet;\n\n assert(\n characters.length > 0 && characters.length <= 256,\n __DEV__\n ? `[necto.id] NT0602: id requires an alphabet of 1 to 256 characters, received ${characters.length}.`\n : '[necto.id] NT0602: https://necto.dev/errors/NT0602'\n );\n\n if (characters.length === 1) {\n return characters[0].repeat(length);\n }\n\n const mask: number = (2 << floor(log2(characters.length - 1))) - 1;\n const batchSize: number = ceil((1.6 * mask * length) / characters.length);\n\n const result: string[] = [];\n\n while (result.length < length) {\n const bytes: Uint8Array = randomBytes(batchSize);\n\n for (let index: number = 0; index < bytes.length && result.length < length; index++) {\n const position: number = bytes[index] & mask;\n\n if (position < characters.length) {\n result.push(characters[position]);\n }\n }\n }\n\n return result.join('');\n}\n"],"mappings":"oLASA,MAAM,EAAkBA,EAAAA,QAAQ,OAAO,KAAK,EAAE,EACxC,EAAoBC,EAAAA,SAAS,UAAU,KAAK,EAAE,EAC9C,EAAoBA,EAAAA,SAAS,YAAY,KAAK,EAAE,EAQzC,EAAW,CAEtB,UAAW,EAGX,UAAW,EAGX,QAAS,GAAG,IAAY,IAGxB,QAAS,EAGT,aAAc,GAAG,IAAY,IAAY,IAEzC,sBAAuB,GAAG,IAAY,IAGtC,QAAS,GAAG,IAAY,IAAY,EAAQ,IAG5C,IAAK,GAAG,EAAQ,OAClB,ECXA,SAAgB,EACd,EAAiB,GACjB,EAAuC,EAAS,QACxC,EACR,EAAA,EAAA,OAAA,CACE,OAAO,UAAU,CAAM,GAAK,EAAS,EAGjC,oDACN,EAEA,IAAM,EACJ,OAAO,GAAa,SAAW,MAAM,KAAK,CAAQ,EAAI,EASxD,IAPA,EAAA,EAAA,OAAA,CACE,EAAW,OAAS,GAAK,EAAW,QAAU,IAG1C,oDACN,EAEI,EAAW,SAAW,EACxB,OAAO,EAAW,EAAE,CAAC,OAAO,CAAM,EAGpC,IAAM,GAAgB,IAAA,EAAA,EAAA,MAAA,EAAA,EAAA,EAAA,KAAA,CAAgB,EAAW,OAAS,CAAC,CAAC,GAAK,EAC3D,GAAA,EAAA,EAAA,KAAA,CAA0B,IAAM,EAAO,EAAU,EAAW,MAAM,EAElE,EAAmB,CAAC,EAE1B,KAAO,EAAO,OAAS,GAAQ,CAC7B,IAAM,GAAA,EAAA,EAAA,YAAA,CAAgC,CAAS,EAE/C,IAAK,IAAI,EAAgB,EAAG,EAAQ,EAAM,QAAU,EAAO,OAAS,EAAQ,IAAS,CACnF,IAAM,EAAmB,EAAM,GAAS,EAEpC,EAAW,EAAW,QACxB,EAAO,KAAK,EAAW,EAAS,CAEpC,CACF,CAEA,OAAO,EAAO,KAAK,EAAE,CACvB"}
package/dist/index.d.cts CHANGED
@@ -7,12 +7,42 @@
7
7
  *
8
8
  */
9
9
  /**
10
- * Generates a random ID of the specified length using the provided character set.
10
+ * Generates a random ID of the specified length from the given alphabet.
11
+ *
12
+ * Every character of the alphabet is equally likely: random bytes that fall
13
+ * outside the alphabet are discarded instead of being wrapped around, which
14
+ * would favour the first characters.
15
+ *
11
16
  * @param length - The length of the ID to generate.
12
- * @param charset - The character set to use for generating the ID (default: ALPHABET_COMBINED).
17
+ * @param alphabet - The characters to draw from: an `Alphabet` preset, any
18
+ * string, or an array of characters. Defaults to `Alphabet.Letters`.
13
19
  * @returns A randomly generated ID string.
20
+ *
21
+ * @example
22
+ * id(); // 21 letters
23
+ * id(7, Alphabet.LowercaseAlphanumeric); // 'k3x9a0q'
24
+ * id(12, 'abc123'); // custom alphabet
25
+ */
26
+ declare function id(length?: number, alphabet?: string | readonly string[]): string;
27
+ //#endregion
28
+ //#region src/constants.d.ts
29
+ /**
30
+ * Ready-made alphabets for `id()`.
31
+ *
32
+ * @example
33
+ * id(7, Alphabet.LowercaseAlphanumeric); // 'k3x9a0q'
14
34
  */
15
- declare function id(length?: number, charset?: string[]): string;
35
+ declare const Alphabet: {
36
+ /** a-z */readonly Lowercase: string; /** A-Z */
37
+ readonly Uppercase: string; /** a-z A-Z */
38
+ readonly Letters: string; /** 0-9 */
39
+ readonly Numeric: string; /** a-z A-Z 0-9 */
40
+ readonly Alphanumeric: string; /** a-z 0-9 */
41
+ readonly LowercaseAlphanumeric: string; /** a-z A-Z 0-9 _ - (safe in URLs, file names and HTML ids) */
42
+ readonly UrlSafe: `${string}${string}${string}_-`; /** 0-9 a-f */
43
+ readonly Hex: `${string}abcdef`;
44
+ };
45
+ type Alphabet = (typeof Alphabet)[keyof typeof Alphabet];
16
46
  //#endregion
17
- export { id };
47
+ export { Alphabet, id };
18
48
  //# sourceMappingURL=index.d.cts.map
package/dist/index.d.mts CHANGED
@@ -7,12 +7,42 @@
7
7
  *
8
8
  */
9
9
  /**
10
- * Generates a random ID of the specified length using the provided character set.
10
+ * Generates a random ID of the specified length from the given alphabet.
11
+ *
12
+ * Every character of the alphabet is equally likely: random bytes that fall
13
+ * outside the alphabet are discarded instead of being wrapped around, which
14
+ * would favour the first characters.
15
+ *
11
16
  * @param length - The length of the ID to generate.
12
- * @param charset - The character set to use for generating the ID (default: ALPHABET_COMBINED).
17
+ * @param alphabet - The characters to draw from: an `Alphabet` preset, any
18
+ * string, or an array of characters. Defaults to `Alphabet.Letters`.
13
19
  * @returns A randomly generated ID string.
20
+ *
21
+ * @example
22
+ * id(); // 21 letters
23
+ * id(7, Alphabet.LowercaseAlphanumeric); // 'k3x9a0q'
24
+ * id(12, 'abc123'); // custom alphabet
25
+ */
26
+ declare function id(length?: number, alphabet?: string | readonly string[]): string;
27
+ //#endregion
28
+ //#region src/constants.d.ts
29
+ /**
30
+ * Ready-made alphabets for `id()`.
31
+ *
32
+ * @example
33
+ * id(7, Alphabet.LowercaseAlphanumeric); // 'k3x9a0q'
14
34
  */
15
- declare function id(length?: number, charset?: string[]): string;
35
+ declare const Alphabet: {
36
+ /** a-z */readonly Lowercase: string; /** A-Z */
37
+ readonly Uppercase: string; /** a-z A-Z */
38
+ readonly Letters: string; /** 0-9 */
39
+ readonly Numeric: string; /** a-z A-Z 0-9 */
40
+ readonly Alphanumeric: string; /** a-z 0-9 */
41
+ readonly LowercaseAlphanumeric: string; /** a-z A-Z 0-9 _ - (safe in URLs, file names and HTML ids) */
42
+ readonly UrlSafe: `${string}${string}${string}_-`; /** 0-9 a-f */
43
+ readonly Hex: `${string}abcdef`;
44
+ };
45
+ type Alphabet = (typeof Alphabet)[keyof typeof Alphabet];
16
46
  //#endregion
17
- export { id };
47
+ export { Alphabet, id };
18
48
  //# sourceMappingURL=index.d.mts.map
package/dist/index.mjs CHANGED
@@ -1,2 +1,2 @@
1
- import{assert as e}from"@necto/assert";import{ALPHABET as t}from"@necto/constants";import{randomBytes as n}from"@necto/crypto";function r(r=21,i=Array.from([...t.LOWERCASE,...t.CAPITALIZED])){e(r>0,`Length must be a positive integer.`);let a=n(r);return Array.from(a).map(e=>i[e%i.length]).join(``)}export{r as id};
1
+ import{assert as e}from"@necto/assert";import{randomBytes as t}from"@necto/crypto";import{ceil as n,floor as r,log2 as i}from"@necto/math";import{ALPHABET as a,NUMBERS as o}from"@necto/constants";const s=o.DIGITS.join(``),c=a.LOWERCASE.join(``),l=a.CAPITALIZED.join(``),u={Lowercase:c,Uppercase:l,Letters:`${c}${l}`,Numeric:s,Alphanumeric:`${c}${l}${s}`,LowercaseAlphanumeric:`${c}${s}`,UrlSafe:`${c}${l}${s}_-`,Hex:`${s}abcdef`};function d(a=21,o=u.Letters){e(Number.isInteger(a)&&a>0,`[necto.id] NT0601: https://necto.dev/errors/NT0601`);let s=typeof o==`string`?Array.from(o):o;if(e(s.length>0&&s.length<=256,`[necto.id] NT0602: https://necto.dev/errors/NT0602`),s.length===1)return s[0].repeat(a);let c=(2<<r(i(s.length-1)))-1,l=n(1.6*c*a/s.length),d=[];for(;d.length<a;){let e=t(l);for(let t=0;t<e.length&&d.length<a;t++){let n=e[t]&c;n<s.length&&d.push(s[n])}}return d.join(``)}export{u as Alphabet,d as id};
2
2
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","names":[],"sources":["../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\nimport { assert } from '@necto/assert';\nimport { ALPHABET } from '@necto/constants';\nimport { randomBytes } from '@necto/crypto';\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([...ALPHABET.LOWERCASE, ...ALPHABET.CAPITALIZED])\n): string {\n assert(length > 0, 'Length must be a positive integer.');\n\n const randomValues: Uint8Array = randomBytes(length);\n return Array.from(randomValues as Iterable<number>)\n .map((value: number): string => charset[value % charset.length])\n .join('');\n}\n"],"mappings":"+HAkBA,SAAgB,EACd,EAAS,GACT,EAAoB,MAAM,KAAK,CAAC,GAAG,EAAS,UAAW,GAAG,EAAS,WAAW,CAAC,EACvE,CACR,EAAO,EAAS,EAAG,oCAAoC,EAEvD,IAAM,EAA2B,EAAY,CAAM,EACnD,OAAO,MAAM,KAAK,CAAgC,CAAC,CAChD,IAAK,GAA0B,EAAQ,EAAQ,EAAQ,OAAO,CAAC,CAC/D,KAAK,EAAE,CACZ"}
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../src/constants.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\nimport { ALPHABET, NUMBERS } from '@necto/constants';\n\nconst NUMERIC: string = NUMBERS.DIGITS.join('');\nconst LOWERCASE: string = ALPHABET.LOWERCASE.join('');\nconst UPPERCASE: string = ALPHABET.CAPITALIZED.join('');\n\n/**\n * Ready-made alphabets for `id()`.\n *\n * @example\n * id(7, Alphabet.LowercaseAlphanumeric); // 'k3x9a0q'\n */\nexport const Alphabet = {\n /** a-z */\n Lowercase: LOWERCASE,\n\n /** A-Z */\n Uppercase: UPPERCASE,\n\n /** a-z A-Z */\n Letters: `${LOWERCASE}${UPPERCASE}`,\n\n /** 0-9 */\n Numeric: NUMERIC,\n /** a-z A-Z 0-9 */\n\n Alphanumeric: `${LOWERCASE}${UPPERCASE}${NUMERIC}`,\n /** a-z 0-9 */\n LowercaseAlphanumeric: `${LOWERCASE}${NUMERIC}`,\n\n /** a-z A-Z 0-9 _ - (safe in URLs, file names and HTML ids) */\n UrlSafe: `${LOWERCASE}${UPPERCASE}${NUMERIC}_-`,\n\n /** 0-9 a-f */\n Hex: `${NUMERIC}abcdef`\n} as const;\n\nexport type Alphabet = (typeof Alphabet)[keyof typeof Alphabet];\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 { assert } from '@necto/assert';\nimport { randomBytes } from '@necto/crypto';\nimport { floor, log2, ceil } from '@necto/math';\n\nimport { Alphabet } from './constants';\n\n/**\n * Generates a random ID of the specified length from the given alphabet.\n *\n * Every character of the alphabet is equally likely: random bytes that fall\n * outside the alphabet are discarded instead of being wrapped around, which\n * would favour the first characters.\n *\n * @param length - The length of the ID to generate.\n * @param alphabet - The characters to draw from: an `Alphabet` preset, any\n * string, or an array of characters. Defaults to `Alphabet.Letters`.\n * @returns A randomly generated ID string.\n *\n * @example\n * id(); // 21 letters\n * id(7, Alphabet.LowercaseAlphanumeric); // 'k3x9a0q'\n * id(12, 'abc123'); // custom alphabet\n */\nexport function id(\n length: number = 21,\n alphabet: string | readonly string[] = Alphabet.Letters\n): string {\n assert(\n Number.isInteger(length) && length > 0,\n __DEV__\n ? `[necto.id] NT0601: id requires a positive integer length, received ${String(length)}.`\n : '[necto.id] NT0601: https://necto.dev/errors/NT0601'\n );\n\n const characters: readonly string[] =\n typeof alphabet === 'string' ? Array.from(alphabet) : alphabet;\n\n assert(\n characters.length > 0 && characters.length <= 256,\n __DEV__\n ? `[necto.id] NT0602: id requires an alphabet of 1 to 256 characters, received ${characters.length}.`\n : '[necto.id] NT0602: https://necto.dev/errors/NT0602'\n );\n\n if (characters.length === 1) {\n return characters[0].repeat(length);\n }\n\n const mask: number = (2 << floor(log2(characters.length - 1))) - 1;\n const batchSize: number = ceil((1.6 * mask * length) / characters.length);\n\n const result: string[] = [];\n\n while (result.length < length) {\n const bytes: Uint8Array = randomBytes(batchSize);\n\n for (let index: number = 0; index < bytes.length && result.length < length; index++) {\n const position: number = bytes[index] & mask;\n\n if (position < characters.length) {\n result.push(characters[position]);\n }\n }\n }\n\n return result.join('');\n}\n"],"mappings":"oMASA,MAAM,EAAkB,EAAQ,OAAO,KAAK,EAAE,EACxC,EAAoB,EAAS,UAAU,KAAK,EAAE,EAC9C,EAAoB,EAAS,YAAY,KAAK,EAAE,EAQzC,EAAW,CAEtB,UAAW,EAGX,UAAW,EAGX,QAAS,GAAG,IAAY,IAGxB,QAAS,EAGT,aAAc,GAAG,IAAY,IAAY,IAEzC,sBAAuB,GAAG,IAAY,IAGtC,QAAS,GAAG,IAAY,IAAY,EAAQ,IAG5C,IAAK,GAAG,EAAQ,OAClB,ECXA,SAAgB,EACd,EAAiB,GACjB,EAAuC,EAAS,QACxC,CACR,EACE,OAAO,UAAU,CAAM,GAAK,EAAS,EAGjC,oDACN,EAEA,IAAM,EACJ,OAAO,GAAa,SAAW,MAAM,KAAK,CAAQ,EAAI,EASxD,GAPA,EACE,EAAW,OAAS,GAAK,EAAW,QAAU,IAG1C,oDACN,EAEI,EAAW,SAAW,EACxB,OAAO,EAAW,EAAE,CAAC,OAAO,CAAM,EAGpC,IAAM,GAAgB,GAAK,EAAM,EAAK,EAAW,OAAS,CAAC,CAAC,GAAK,EAC3D,EAAoB,EAAM,IAAM,EAAO,EAAU,EAAW,MAAM,EAElE,EAAmB,CAAC,EAE1B,KAAO,EAAO,OAAS,GAAQ,CAC7B,IAAM,EAAoB,EAAY,CAAS,EAE/C,IAAK,IAAI,EAAgB,EAAG,EAAQ,EAAM,QAAU,EAAO,OAAS,EAAQ,IAAS,CACnF,IAAM,EAAmB,EAAM,GAAS,EAEpC,EAAW,EAAW,QACxB,EAAO,KAAK,EAAW,EAAS,CAEpC,CACF,CAEA,OAAO,EAAO,KAAK,EAAE,CACvB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@necto/id",
3
- "version": "1.6.10",
3
+ "version": "1.7.0",
4
4
  "type": "module",
5
5
  "description": "Necto's library for generating algorithmic ID's based on specified parameters.",
6
6
  "main": "./dist/index.cjs",
@@ -8,9 +8,19 @@
8
8
  "types": "./dist/index.d.mts",
9
9
  "exports": {
10
10
  ".": {
11
- "types": "./dist/index.d.mts",
12
- "import": "./dist/index.mjs",
13
- "require": "./dist/index.cjs"
11
+ "development": {
12
+ "types": "./dist/index.d.mts",
13
+ "import": "./dist/dev/index.mjs",
14
+ "require": "./dist/dev/index.cjs"
15
+ },
16
+ "import": {
17
+ "types": "./dist/index.d.mts",
18
+ "default": "./dist/index.mjs"
19
+ },
20
+ "require": {
21
+ "types": "./dist/index.d.cts",
22
+ "default": "./dist/index.cjs"
23
+ }
14
24
  }
15
25
  },
16
26
  "files": [
@@ -26,9 +36,10 @@
26
36
  "author": "Corinvo OSS Team",
27
37
  "license": "MIT",
28
38
  "dependencies": {
29
- "@necto/assert": "1.0.2",
30
- "@necto/crypto": "1.0.2",
31
- "@necto/constants": "2.1.0"
39
+ "@necto/assert": "1.1.0",
40
+ "@necto/constants": "2.2.0",
41
+ "@necto/crypto": "1.0.3",
42
+ "@necto/math": "1.1.0"
32
43
  },
33
44
  "repository": {
34
45
  "type": "git",