tiny-uuid-gen 0.1.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/README.md +100 -0
- package/index.cjs +14 -0
- package/index.d.ts +7 -0
- package/index.js +12 -0
- package/package.json +38 -0
package/README.md
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# tiny-uuid
|
|
2
|
+
|
|
3
|
+
UUID v4 generator in 200 bytes. Zero dependencies.
|
|
4
|
+
|
|
5
|
+
## Why?
|
|
6
|
+
|
|
7
|
+
| Package | Size |
|
|
8
|
+
|---------|------|
|
|
9
|
+
| uuid | 8KB |
|
|
10
|
+
| **tiny-uuid** | **200B** |
|
|
11
|
+
|
|
12
|
+
That's **40x smaller**.
|
|
13
|
+
|
|
14
|
+
## Install
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install tiny-uuid-gen
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Usage
|
|
21
|
+
|
|
22
|
+
```javascript
|
|
23
|
+
import { uuid } from 'tiny-uuid-gen';
|
|
24
|
+
|
|
25
|
+
const id = uuid();
|
|
26
|
+
// "550e8400-e29b-41d4-a716-446655440000"
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### CommonJS
|
|
30
|
+
|
|
31
|
+
```javascript
|
|
32
|
+
const uuid = require('tiny-uuid-gen');
|
|
33
|
+
|
|
34
|
+
const id = uuid();
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### TypeScript
|
|
38
|
+
|
|
39
|
+
Full TypeScript support included.
|
|
40
|
+
|
|
41
|
+
```typescript
|
|
42
|
+
import { uuid } from 'tiny-uuid-gen';
|
|
43
|
+
|
|
44
|
+
const id: string = uuid();
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## API
|
|
48
|
+
|
|
49
|
+
### `uuid(): string`
|
|
50
|
+
|
|
51
|
+
Generates a random UUID v4.
|
|
52
|
+
|
|
53
|
+
Returns: `xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx`
|
|
54
|
+
|
|
55
|
+
Where:
|
|
56
|
+
- `x` = random hex digit
|
|
57
|
+
- `4` = version 4 (random)
|
|
58
|
+
- `y` = variant (8, 9, a, or b)
|
|
59
|
+
|
|
60
|
+
## When to use
|
|
61
|
+
|
|
62
|
+
Use tiny-uuid when:
|
|
63
|
+
- You just need random UUIDs
|
|
64
|
+
- Bundle size matters
|
|
65
|
+
- No crypto requirements
|
|
66
|
+
|
|
67
|
+
Use `uuid` package when:
|
|
68
|
+
- You need v1 (timestamp) or v5 (namespace)
|
|
69
|
+
- You need cryptographically secure randomness
|
|
70
|
+
- You're in a security-critical context
|
|
71
|
+
|
|
72
|
+
## How it works
|
|
73
|
+
|
|
74
|
+
```javascript
|
|
75
|
+
export function uuid() {
|
|
76
|
+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
|
|
77
|
+
const r = Math.random() * 16 | 0;
|
|
78
|
+
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
That's it. 5 lines.
|
|
84
|
+
|
|
85
|
+
## Spec Compliance
|
|
86
|
+
|
|
87
|
+
Follows RFC 4122 format:
|
|
88
|
+
- Version nibble: `4` (random)
|
|
89
|
+
- Variant bits: `10xx` (8/9/a/b)
|
|
90
|
+
|
|
91
|
+
Note: Uses `Math.random()`, not `crypto.getRandomValues()`.
|
|
92
|
+
For security-critical applications, use the full `uuid` package.
|
|
93
|
+
|
|
94
|
+
## More Tools
|
|
95
|
+
|
|
96
|
+
See all dev tools: https://takawasi-social.com/en/
|
|
97
|
+
|
|
98
|
+
## License
|
|
99
|
+
|
|
100
|
+
MIT
|
package/index.cjs
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generate a random UUID v4.
|
|
3
|
+
* @returns {string} UUID in format xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
|
|
4
|
+
*/
|
|
5
|
+
function uuid() {
|
|
6
|
+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
|
|
7
|
+
const r = Math.random() * 16 | 0;
|
|
8
|
+
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
module.exports = uuid;
|
|
13
|
+
module.exports.uuid = uuid;
|
|
14
|
+
module.exports.default = uuid;
|
package/index.d.ts
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generate a random UUID v4.
|
|
3
|
+
* @returns {string} UUID in format xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
|
|
4
|
+
*/
|
|
5
|
+
export function uuid() {
|
|
6
|
+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
|
|
7
|
+
const r = Math.random() * 16 | 0;
|
|
8
|
+
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export default uuid;
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "tiny-uuid-gen",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "UUID v4 generator in 200 bytes. Zero dependencies.",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"types": "index.d.ts",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./index.js",
|
|
11
|
+
"require": "./index.cjs",
|
|
12
|
+
"types": "./index.d.ts"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"index.js",
|
|
17
|
+
"index.cjs",
|
|
18
|
+
"index.d.ts"
|
|
19
|
+
],
|
|
20
|
+
"keywords": [
|
|
21
|
+
"uuid",
|
|
22
|
+
"tiny",
|
|
23
|
+
"minimal",
|
|
24
|
+
"lightweight",
|
|
25
|
+
"v4",
|
|
26
|
+
"random"
|
|
27
|
+
],
|
|
28
|
+
"author": "takawasi",
|
|
29
|
+
"license": "MIT",
|
|
30
|
+
"repository": {
|
|
31
|
+
"type": "git",
|
|
32
|
+
"url": "https://github.com/takawasi/tiny-uuid"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"test": "node test.js",
|
|
36
|
+
"size": "gzip -c index.js | wc -c"
|
|
37
|
+
}
|
|
38
|
+
}
|