ts-id-lite 2.0.0 → 2.0.16
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/README.md +45 -0
- package/dist/id.d.ts +10 -0
- package/dist/id.js +50 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/package.json +7 -2
package/README.md
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# ts-id-lite
|
|
2
|
+
|
|
3
|
+
Lightweight, type-safe ID generation utility library for web developers.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install ts-id-lite
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { Injectable } from '@angular/core';
|
|
15
|
+
import { IdUtils } from 'ts-id-lite';
|
|
16
|
+
|
|
17
|
+
@Injectable({ providedIn: 'root' })
|
|
18
|
+
export class MyService {
|
|
19
|
+
constructor(private id: IdUtils) { }
|
|
20
|
+
|
|
21
|
+
myMethod(): void {
|
|
22
|
+
this.id.uuid(); // '550e8400-e29b-41d4-a716-446655440000'
|
|
23
|
+
this.id.nanoid(); // 'V1StGXR8_Z'
|
|
24
|
+
this.id.shortId(); // random short ID
|
|
25
|
+
this.id.hash('text'); // hash string
|
|
26
|
+
this.id.snowflake(); // snowflake ID
|
|
27
|
+
this.id.generateCode(6, 'numeric'); // '482931'
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Methods
|
|
33
|
+
|
|
34
|
+
| Method | Description | Example |
|
|
35
|
+
|--------|-------------|---------|
|
|
36
|
+
| `uuid` | Generate UUID v4 | → `'550e8400-...' ` |
|
|
37
|
+
| `nanoid` | Generate nanoid | → `'V1StGXR8_Z'` |
|
|
38
|
+
| `shortId` | Generate short ID | → random short string |
|
|
39
|
+
| `hash` | Hash string | `'text'` → hash |
|
|
40
|
+
| `snowflake` | Generate snowflake ID | → timestamp-based ID |
|
|
41
|
+
| `generateCode` | Generate code | `6, 'numeric'` → `'482931'` |
|
|
42
|
+
|
|
43
|
+
## License
|
|
44
|
+
|
|
45
|
+
MIT
|
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,50 @@
|
|
|
1
|
+
export class IdUtils {
|
|
2
|
+
constructor() {
|
|
3
|
+
this.base36Chars = '0123456789abcdefghijklmnopqrstuvwxyz';
|
|
4
|
+
}
|
|
5
|
+
uuid() {
|
|
6
|
+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
|
|
7
|
+
const r = Math.random() * 16 | 0;
|
|
8
|
+
const v = c === 'x' ? r : (r & 0x3 | 0x8);
|
|
9
|
+
return v.toString(16);
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
nanoid(size = 21) {
|
|
13
|
+
const bytes = new Uint8Array(size);
|
|
14
|
+
crypto.getRandomValues(bytes);
|
|
15
|
+
return Array.from(bytes, b => this.base36Chars[b & 63]).join('');
|
|
16
|
+
}
|
|
17
|
+
shortId(length = 10) {
|
|
18
|
+
return this.nanoid(length).replace(/[_=-]/g, '');
|
|
19
|
+
}
|
|
20
|
+
snowflake() {
|
|
21
|
+
const timestamp = BigInt(Date.now() - 1700000000000);
|
|
22
|
+
const machineId = BigInt(Math.floor(Math.random() * 1024));
|
|
23
|
+
const sequence = BigInt(Math.floor(Math.random() * 4096));
|
|
24
|
+
const id = (timestamp << 22n) | (machineId << 12n) | sequence;
|
|
25
|
+
return id.toString();
|
|
26
|
+
}
|
|
27
|
+
hash(input) {
|
|
28
|
+
let hash = 0;
|
|
29
|
+
for (let i = 0; i < input.length; i++) {
|
|
30
|
+
const char = input.charCodeAt(i);
|
|
31
|
+
hash = ((hash << 5) - hash) + char;
|
|
32
|
+
hash = hash & hash;
|
|
33
|
+
}
|
|
34
|
+
return Math.abs(hash).toString(36);
|
|
35
|
+
}
|
|
36
|
+
generateCode(length = 6, charset = 'numeric') {
|
|
37
|
+
const charsets = {
|
|
38
|
+
numeric: '0123456789',
|
|
39
|
+
alpha: 'abcdefghijklmnopqrstuvwxyz',
|
|
40
|
+
alphanumeric: '0123456789abcdefghijklmnopqrstuvwxyz'
|
|
41
|
+
};
|
|
42
|
+
const chars = charsets[charset];
|
|
43
|
+
return Array(length).fill(0).map(() => chars[Math.floor(Math.random() * chars.length)]).join('');
|
|
44
|
+
}
|
|
45
|
+
generateToken(length = 32) {
|
|
46
|
+
const bytes = new Uint8Array(length);
|
|
47
|
+
crypto.getRandomValues(bytes);
|
|
48
|
+
return Array.from(bytes, b => b.toString(16).padStart(2, '0')).join('');
|
|
49
|
+
}
|
|
50
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './id';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './id';
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ts-id-lite",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.16",
|
|
4
|
+
"deprecated": "Previous versions are deprecated. Please upgrade to 2.0.15 or later.",
|
|
4
5
|
"description": "Lightweight, type-safe ID generation utility library for web developers",
|
|
5
6
|
"main": "./dist/index.js",
|
|
6
7
|
"module": "./dist/index.js",
|
|
@@ -27,5 +28,9 @@
|
|
|
27
28
|
"lite"
|
|
28
29
|
],
|
|
29
30
|
"author": "Gaurang Mody - G8X",
|
|
30
|
-
"license": "MIT"
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "https://github.com/gbmrocks/ts-utils-lite.git"
|
|
35
|
+
}
|
|
31
36
|
}
|