@xxkatt/utils 0.0.0-stage → 0.0.1

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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 xxkatt
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,68 @@
1
- # Temporary Holding Version
1
+ # @xxkatt/utils
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ 常用的 JavaScript / TypeScript 工具函数,提供 ESM 导出和 TypeScript 类型声明,无运行时依赖。
4
+
5
+ ## 安装
6
+
7
+ ```sh
8
+ pnpm add @xxkatt/utils
9
+ ```
10
+
11
+ ## 使用
12
+
13
+ ```ts
14
+ import { randomId, uuid } from '@xxkatt/utils'
15
+
16
+ uuid() // 32 位 UUID v4,不带横杠
17
+ uuid('', true) // 标准 UUID v4:xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
18
+ uuid('user_') // 添加前缀:user_…
19
+ uuid('user_', true) // 添加前缀并保留横杠
20
+
21
+ randomId() // 32 个随机的小写十六进制字符
22
+ randomId(8, 10) // 8 位随机数字,可能以 0 开头
23
+ randomId(21, 62) // 21 个随机字符,字符来自 0–9、a–z、A–Z
24
+ ```
25
+
26
+ ## API
27
+
28
+ ### uuid(prefix?, hyphens?)
29
+
30
+ 生成 UUID v4,返回 `string`。
31
+
32
+ | 参数 | 类型 | 默认值 | 说明 |
33
+ | --------- | --------- | ------- | ------------------------------------ |
34
+ | `prefix` | `string` | `''` | 原样拼接在 UUID 前,分隔符需自行提供 |
35
+ | `hyphens` | `boolean` | `false` | 是否保留标准 UUID 中的横杠 |
36
+
37
+ 不计前缀时,默认返回 32 个小写十六进制字符;保留横杠时长度为 36。
38
+
39
+ ### randomId(length?, radix?)
40
+
41
+ 使用加密安全随机数生成自定义 ID,返回 `string`。
42
+
43
+ | 参数 | 类型 | 默认值 | 说明 |
44
+ | -------- | -------- | ------ | ---------------------------------------------- |
45
+ | `length` | `number` | `32` | 字符数量,必须为非负安全整数;`0` 返回空字符串 |
46
+ | `radix` | `number` | `16` | 基数,必须为 `2` 至 `62` 的整数(含边界) |
47
+
48
+ 字符表为 `0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`,使用其中前 `radix` 个字符。参数不符合要求时抛出 `RangeError`;`length` 为 `0` 时仍会校验 `radix`。
49
+
50
+ 自定义 ID 不遵循 UUID 格式,随机结果可能重复。需要标准 UUID v4 时请使用 `uuid`。
51
+
52
+ ## 运行环境
53
+
54
+ 支持 Node.js 24+,以及支持 Web Crypto API 和 ES2022 的现代浏览器。
55
+
56
+ - `uuid` 依赖 `globalThis.crypto.randomUUID()`;浏览器中需要 HTTPS 或 localhost 等安全上下文。
57
+ - `randomId` 依赖 `globalThis.crypto.getRandomValues()`。
58
+ - 通过 ESM `import` 使用。
59
+
60
+ ## 文档与反馈
61
+
62
+ - [完整 API 文档](https://github.com/xxkatt/xxkatt-utils/tree/master/apps/docs/js/utils)
63
+ - [源码仓库](https://github.com/xxkatt/xxkatt-utils)
64
+ - [问题反馈](https://github.com/xxkatt/xxkatt-utils/issues)
65
+
66
+ ## 许可证
67
+
68
+ [MIT](./LICENSE) © 2026 xxkatt
@@ -0,0 +1,24 @@
1
+ /**
2
+ * 生成 UUID v4,默认返回 32 个小写十六进制字符。
3
+ *
4
+ * @param prefix 原样拼接在 UUID 前的前缀,分隔符需自行提供。
5
+ * @param hyphens 是否保留标准 UUID 中的横杠,默认 false。
6
+ * @example
7
+ * uuid()
8
+ * uuid('', true)
9
+ * uuid('user_')
10
+ * uuid('user_', true)
11
+ */
12
+ export declare function uuid(prefix?: string, hyphens?: boolean): string;
13
+ /**
14
+ * 使用加密安全随机数生成 ID;自定义长度的 ID 不遵循 UUID 格式。
15
+ * 字符表依次为 0–9、a–z、A–Z,使用前 radix 个字符。
16
+ *
17
+ * @param length 字符数量,必须为非负安全整数,默认 32;0 返回空字符串。
18
+ * @param radix 基数,必须为 2–62 的整数,默认 16。
19
+ * @example
20
+ * randomId(8, 10)
21
+ * randomId(21, 62)
22
+ */
23
+ export declare function randomId(length?: number, radix?: number): string;
24
+ //# sourceMappingURL=index.d.ts.map
package/dist/index.js ADDED
@@ -0,0 +1,47 @@
1
+ // src/uuid/index.ts
2
+ const ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
3
+ /**
4
+ * 生成 UUID v4,默认返回 32 个小写十六进制字符。
5
+ *
6
+ * @param prefix 原样拼接在 UUID 前的前缀,分隔符需自行提供。
7
+ * @param hyphens 是否保留标准 UUID 中的横杠,默认 false。
8
+ * @example
9
+ * uuid()
10
+ * uuid('', true)
11
+ * uuid('user_')
12
+ * uuid('user_', true)
13
+ */
14
+ function uuid(prefix = "", hyphens = false) {
15
+ const value = globalThis.crypto.randomUUID();
16
+ return prefix + (hyphens ? value : value.replaceAll("-", ""));
17
+ }
18
+ /**
19
+ * 使用加密安全随机数生成 ID;自定义长度的 ID 不遵循 UUID 格式。
20
+ * 字符表依次为 0–9、a–z、A–Z,使用前 radix 个字符。
21
+ *
22
+ * @param length 字符数量,必须为非负安全整数,默认 32;0 返回空字符串。
23
+ * @param radix 基数,必须为 2–62 的整数,默认 16。
24
+ * @example
25
+ * randomId(8, 10)
26
+ * randomId(21, 62)
27
+ */
28
+ function randomId(length = 32, radix = 16) {
29
+ if (!Number.isSafeInteger(length) || length < 0) throw new RangeError("length must be a non-negative safe integer");
30
+ if (!Number.isInteger(radix) || radix < 2 || radix > 62) throw new RangeError("radix must be an integer between 2 and 62");
31
+ if (length === 0) return "";
32
+ const bytes = new Uint8Array(Math.min(length, 65536));
33
+ const limit = 256 - 256 % radix;
34
+ let value = "";
35
+ while (value.length < length) {
36
+ globalThis.crypto.getRandomValues(bytes);
37
+ for (const byte of bytes) {
38
+ if (byte >= limit) continue;
39
+ value += ALPHABET.charAt(byte % radix);
40
+ if (value.length === length) break;
41
+ }
42
+ }
43
+ return value;
44
+ }
45
+ export { randomId, uuid };
46
+
47
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/uuid/index.ts"],"sourcesContent":["const ALPHABET = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'\n\n/**\n * 生成 UUID v4,默认返回 32 个小写十六进制字符。\n *\n * @param prefix 原样拼接在 UUID 前的前缀,分隔符需自行提供。\n * @param hyphens 是否保留标准 UUID 中的横杠,默认 false。\n * @example\n * uuid()\n * uuid('', true)\n * uuid('user_')\n * uuid('user_', true)\n */\nexport function uuid(prefix: string = '', hyphens: boolean = false): string {\n const value = globalThis.crypto.randomUUID()\n return prefix + (hyphens ? value : value.replaceAll('-', ''))\n}\n\n/**\n * 使用加密安全随机数生成 ID;自定义长度的 ID 不遵循 UUID 格式。\n * 字符表依次为 0–9、a–z、A–Z,使用前 radix 个字符。\n *\n * @param length 字符数量,必须为非负安全整数,默认 32;0 返回空字符串。\n * @param radix 基数,必须为 2–62 的整数,默认 16。\n * @example\n * randomId(8, 10)\n * randomId(21, 62)\n */\nexport function randomId(length: number = 32, radix: number = 16): string {\n if (!Number.isSafeInteger(length) || length < 0) {\n throw new RangeError('length must be a non-negative safe integer')\n }\n if (!Number.isInteger(radix) || radix < 2 || radix > ALPHABET.length) {\n throw new RangeError('radix must be an integer between 2 and 62')\n }\n if (length === 0) return ''\n\n const bytes = new Uint8Array(Math.min(length, 65_536))\n const limit = 256 - (256 % radix)\n let value = ''\n\n while (value.length < length) {\n globalThis.crypto.getRandomValues(bytes)\n for (const byte of bytes) {\n // 丢弃无法均分到字符表的尾部区间,避免取模导致分布偏差。\n if (byte >= limit) continue\n value += ALPHABET.charAt(byte % radix)\n if (value.length === length) break\n }\n }\n\n return value\n}\n"],"mappings":";AAAA,MAAM,WAAW;;;;;;;;;;;;AAajB,SAAgB,KAAK,SAAiB,IAAI,UAAmB,OAAe;CAC1E,MAAM,QAAQ,WAAW,OAAO,WAAW;CAC3C,OAAO,UAAU,UAAU,QAAQ,MAAM,WAAW,KAAK,EAAE;AAC7D;;;;;;;;;;;AAYA,SAAgB,SAAS,SAAiB,IAAI,QAAgB,IAAY;CACxE,IAAI,CAAC,OAAO,cAAc,MAAM,KAAK,SAAS,GAC5C,MAAM,IAAI,WAAW,4CAA4C;CAEnE,IAAI,CAAC,OAAO,UAAU,KAAK,KAAK,QAAQ,KAAK,QAAQ,IACnD,MAAM,IAAI,WAAW,2CAA2C;CAElE,IAAI,WAAW,GAAG,OAAO;CAEzB,MAAM,QAAQ,IAAI,WAAW,KAAK,IAAI,QAAQ,KAAM,CAAC;CACrD,MAAM,QAAQ,MAAO,MAAM;CAC3B,IAAI,QAAQ;CAEZ,OAAO,MAAM,SAAS,QAAQ;EAC5B,WAAW,OAAO,gBAAgB,KAAK;EACvC,KAAK,MAAM,QAAQ,OAAO;GAExB,IAAI,QAAQ,OAAO;GACnB,SAAS,SAAS,OAAO,OAAO,KAAK;GACrC,IAAI,MAAM,WAAW,QAAQ;EAC/B;CACF;CAEA,OAAO;AACT"}
package/package.json CHANGED
@@ -1,6 +1,35 @@
1
1
  {
2
2
  "name": "@xxkatt/utils",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.0.1",
4
+ "description": "常用的 JavaScript / TypeScript 工具函数",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/xxkatt/xxkatt-utils.git",
9
+ "directory": "packages/js/utils"
10
+ },
11
+ "files": [
12
+ "dist"
13
+ ],
14
+ "type": "module",
15
+ "sideEffects": false,
16
+ "main": "./dist/index.js",
17
+ "types": "./dist/index.d.ts",
18
+ "exports": {
19
+ ".": {
20
+ "types": "./dist/index.d.ts",
21
+ "import": "./dist/index.js"
22
+ }
23
+ },
24
+ "publishConfig": {
25
+ "access": "public",
26
+ "registry": "https://registry.npmjs.org/"
27
+ },
28
+ "scripts": {
29
+ "build": "tsdown",
30
+ "typecheck": "tsc -p tsconfig.json",
31
+ "test": "vitest run",
32
+ "test:watch": "vitest",
33
+ "clean": "node -e \"require('node:fs').rmSync('dist', { recursive: true, force: true })\""
34
+ }
6
35
  }