ts-id-lite 1.0.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.
package/dist/id.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ export declare class IdUtils {
2
+ private readonly base36Chars;
3
+ uuid(): string;
4
+ nanoid(size?: number): string;
5
+ shortId(length?: number): string;
6
+ snowflake(): string;
7
+ hash(input: string): string;
8
+ generateCode(length?: number, charset?: 'numeric' | 'alpha' | 'alphanumeric'): string;
9
+ generateToken(length?: number): string;
10
+ }
package/dist/id.js ADDED
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.IdUtils = void 0;
4
+ class IdUtils {
5
+ constructor() {
6
+ this.base36Chars = '0123456789abcdefghijklmnopqrstuvwxyz';
7
+ }
8
+ uuid() {
9
+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
10
+ const r = Math.random() * 16 | 0;
11
+ const v = c === 'x' ? r : (r & 0x3 | 0x8);
12
+ return v.toString(16);
13
+ });
14
+ }
15
+ nanoid(size = 21) {
16
+ const bytes = new Uint8Array(size);
17
+ crypto.getRandomValues(bytes);
18
+ return Array.from(bytes, b => this.base36Chars[b & 63]).join('');
19
+ }
20
+ shortId(length = 10) {
21
+ return this.nanoid(length).replace(/[_=-]/g, '');
22
+ }
23
+ snowflake() {
24
+ const timestamp = BigInt(Date.now() - 1700000000000);
25
+ const machineId = BigInt(Math.floor(Math.random() * 1024));
26
+ const sequence = BigInt(Math.floor(Math.random() * 4096));
27
+ const id = (timestamp << 22n) | (machineId << 12n) | sequence;
28
+ return id.toString();
29
+ }
30
+ hash(input) {
31
+ let hash = 0;
32
+ for (let i = 0; i < input.length; i++) {
33
+ const char = input.charCodeAt(i);
34
+ hash = ((hash << 5) - hash) + char;
35
+ hash = hash & hash;
36
+ }
37
+ return Math.abs(hash).toString(36);
38
+ }
39
+ generateCode(length = 6, charset = 'numeric') {
40
+ const charsets = {
41
+ numeric: '0123456789',
42
+ alpha: 'abcdefghijklmnopqrstuvwxyz',
43
+ alphanumeric: '0123456789abcdefghijklmnopqrstuvwxyz'
44
+ };
45
+ const chars = charsets[charset];
46
+ return Array(length).fill(0).map(() => chars[Math.floor(Math.random() * chars.length)]).join('');
47
+ }
48
+ generateToken(length = 32) {
49
+ const bytes = new Uint8Array(length);
50
+ crypto.getRandomValues(bytes);
51
+ return Array.from(bytes, b => b.toString(16).padStart(2, '0')).join('');
52
+ }
53
+ }
54
+ exports.IdUtils = IdUtils;
@@ -0,0 +1 @@
1
+ export * from './id';
package/dist/index.js ADDED
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./id"), exports);
package/package.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "ts-id-lite",
3
+ "version": "1.0.0",
4
+ "description": "Lightweight, type-safe ID generation utility library for web developers",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js",
12
+ "require": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": ["dist"],
16
+ "keywords": ["utils", "typescript", "id", "uuid", "helpers", "lite"],
17
+ "author": "Gaurang Mody - G8X",
18
+ "license": "MIT"
19
+ }