human-ids 1.0.13 → 1.2.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 +191 -80
- package/dist/dictionaries/en.d.ts +3 -0
- package/dist/dictionaries/en.js +983 -0
- package/dist/dictionaries/es.d.ts +3 -0
- package/dist/dictionaries/es.js +986 -0
- package/dist/dictionaries/index.d.ts +45 -0
- package/dist/dictionaries/index.js +57 -0
- package/dist/index.d.ts +246 -0
- package/dist/index.js +135 -0
- package/package.json +38 -11
- package/CHANGELOG.md +0 -66
- package/dictionaries/en.js +0 -224
- package/dictionaries/es.js +0 -221
- package/dictionaries/index.js +0 -9
- package/index.d.ts +0 -31
- package/index.js +0 -97
- package/tests/index.test.js +0 -34
package/README.md
CHANGED
|
@@ -1,96 +1,207 @@
|
|
|
1
1
|
# Human IDs
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
Supports English and Spanish
|
|
10
|
-
## Output example
|
|
11
|
-
bubbly-beige-fire-38
|
|
12
|
-
breezy-teal-faith-870
|
|
13
|
-
silly-indigo-imagination-440
|
|
14
|
-
genuine-turquoise-light-718
|
|
15
|
-
bubbly-purple-pen-450
|
|
16
|
-
genuine-silver-magic-935
|
|
17
|
-
delightful-brown-planet-642
|
|
18
|
-
cozy-orange-boat-449
|
|
19
|
-
excelente-morado-creatividad-829
|
|
20
|
-
energetico-oliva-libertad-765
|
|
21
|
-
## Installation
|
|
3
|
+
[](https://www.npmjs.com/package/human-ids)
|
|
4
|
+
[](https://opensource.org/licenses/ISC)
|
|
5
|
+
|
|
6
|
+
A zero-dependency TypeScript library that generates human-readable IDs by combining adjectives, colors, nouns, and numbers. Perfect for creating memorable identifiers for users, sessions, or any entity that needs a friendly name.
|
|
7
|
+
|
|
8
|
+
## Features
|
|
22
9
|
|
|
23
|
-
|
|
10
|
+
- **Human-readable**: Generated IDs like `stellar-jade-horizon-7392` are easy to remember and communicate
|
|
11
|
+
- **Multi-language**: Built-in support for English and Spanish with proper word ordering
|
|
12
|
+
- **Highly unique**: ~209 billion possible combinations per language
|
|
13
|
+
- **Zero dependencies**: Lightweight and secure
|
|
14
|
+
- **TypeScript**: Full type definitions with JSDoc documentation
|
|
15
|
+
- **Customizable**: Configure separators, components, number ranges, and even provide custom dictionaries
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
24
18
|
|
|
25
|
-
```
|
|
19
|
+
```bash
|
|
26
20
|
npm install human-ids
|
|
27
21
|
```
|
|
28
|
-
|
|
22
|
+
|
|
23
|
+
## Quick Start
|
|
29
24
|
|
|
30
25
|
```javascript
|
|
31
|
-
import
|
|
32
|
-
|
|
33
|
-
//
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
26
|
+
import generateId from 'human-ids';
|
|
27
|
+
|
|
28
|
+
// Generate a random ID
|
|
29
|
+
const id = generateId();
|
|
30
|
+
// => "stellar-jade-horizon-7392"
|
|
31
|
+
|
|
32
|
+
// Spanish
|
|
33
|
+
const spanishId = generateId({ lang: 'es' });
|
|
34
|
+
// => "aurora-esmeralda-brillante-4518"
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Output Examples
|
|
38
|
+
|
|
39
|
+
```
|
|
40
|
+
English: Spanish:
|
|
41
|
+
bubbly-amber-phoenix-3847 aurora-cobalto-magnifico-6729
|
|
42
|
+
stellar-jade-horizon-7392 cascada-esmeralda-valiente-1854
|
|
43
|
+
cosmic-sapphire-nebula-9034 bosque-ambar-sereno-5043
|
|
44
|
+
radiant-coral-serenity-2671 estrella-jade-luminoso-8156
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Usage
|
|
48
|
+
|
|
49
|
+
### Basic Usage
|
|
50
|
+
|
|
51
|
+
```javascript
|
|
52
|
+
import generateId from 'human-ids';
|
|
53
|
+
|
|
54
|
+
// Default: English, all components enabled
|
|
55
|
+
const id = generateId();
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Configuration Options
|
|
59
|
+
|
|
60
|
+
```javascript
|
|
61
|
+
const id = generateId({
|
|
62
|
+
lang: 'en', // Language: 'en' or 'es'
|
|
63
|
+
adjective: true, // Include adjective
|
|
64
|
+
color: true, // Include color
|
|
65
|
+
noun: true, // Include noun
|
|
66
|
+
separator: '-', // Component separator
|
|
67
|
+
randomOrder: false, // Shuffle component order
|
|
68
|
+
asObject: false, // Return object instead of string
|
|
61
69
|
number: {
|
|
62
|
-
min: 0,
|
|
63
|
-
max:
|
|
64
|
-
sets: 1,
|
|
65
|
-
completeWithZeros: false
|
|
66
|
-
}
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
70
|
+
min: 0, // Minimum number value
|
|
71
|
+
max: 9999, // Maximum number value
|
|
72
|
+
sets: 1, // Number of number components
|
|
73
|
+
completeWithZeros: false // Pad with leading zeros
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### Examples
|
|
79
|
+
|
|
80
|
+
#### Without Colors
|
|
81
|
+
|
|
82
|
+
```javascript
|
|
83
|
+
generateId({ color: false });
|
|
84
|
+
// => "happy-cat-3456"
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
#### Custom Separator
|
|
88
|
+
|
|
89
|
+
```javascript
|
|
90
|
+
generateId({ separator: '_' });
|
|
91
|
+
// => "happy_blue_cat_9012"
|
|
71
92
|
```
|
|
72
|
-
## Configuration
|
|
73
93
|
|
|
74
|
-
|
|
94
|
+
#### Multiple Number Sets
|
|
75
95
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
96
|
+
```javascript
|
|
97
|
+
generateId({ number: { sets: 2 } });
|
|
98
|
+
// => "happy-blue-cat-1234-5678"
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
#### Zero-Padded Numbers
|
|
102
|
+
|
|
103
|
+
```javascript
|
|
104
|
+
generateId({ number: { min: 0, max: 999, completeWithZeros: true } });
|
|
105
|
+
// => "happy-blue-cat-042"
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
#### Get ID as Object
|
|
109
|
+
|
|
110
|
+
```javascript
|
|
111
|
+
generateId({ asObject: true });
|
|
112
|
+
// => { adjective: 'happy', color: 'blue', noun: 'cat', number1: '1234' }
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
#### Random Component Order
|
|
116
|
+
|
|
117
|
+
```javascript
|
|
118
|
+
generateId({ randomOrder: true });
|
|
119
|
+
// => "1234-cat-happy-blue" (random each time)
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
#### Custom Dictionary
|
|
123
|
+
|
|
124
|
+
```javascript
|
|
125
|
+
generateId({
|
|
126
|
+
dictionary: {
|
|
127
|
+
adjectives: { cool: 'cool', awesome: 'awesome' },
|
|
128
|
+
colors: { red: 'red', blue: 'blue' },
|
|
129
|
+
nouns: { rocket: 'rocket', star: 'star' }
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
// => "cool-red-rocket-5678"
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## CommonJS Support
|
|
136
|
+
|
|
137
|
+
```javascript
|
|
138
|
+
// Default import
|
|
139
|
+
const generateId = require('human-ids');
|
|
140
|
+
|
|
141
|
+
// Named imports
|
|
142
|
+
const { generateId, dictionaries } = require('human-ids');
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
## Uniqueness & Collision Probability
|
|
146
|
+
|
|
147
|
+
The library includes extensive dictionaries to maximize uniqueness:
|
|
148
|
+
|
|
149
|
+
| Language | Adjectives | Colors | Nouns | Numbers (default) | Total Combinations |
|
|
150
|
+
|----------|------------|--------|-------|-------------------|-------------------|
|
|
151
|
+
| English | 372 | 120 | 469 | 10,000 (0-9999) | ~209 billion |
|
|
152
|
+
| Spanish | 372 | 120 | 469 | 10,000 (0-9999) | ~209 billion |
|
|
153
|
+
|
|
154
|
+
With ~209 billion combinations, collision probability is negligible for most use cases. For even higher uniqueness:
|
|
155
|
+
|
|
156
|
+
- **Increase number range**: `{ number: { max: 99999 } }` → 10x more combinations
|
|
157
|
+
- **Use multiple number sets**: `{ number: { sets: 2 } }` → 10,000x more combinations
|
|
158
|
+
- **Combine both**: ~20 quadrillion combinations
|
|
159
|
+
|
|
160
|
+
> **Note**: While collision probability is extremely low, absolute uniqueness cannot be guaranteed with random generation. For guaranteed uniqueness, implement collision detection with your datastore.
|
|
161
|
+
|
|
162
|
+
## API Reference
|
|
163
|
+
|
|
164
|
+
### `generateId(settings?): string | IdParts`
|
|
165
|
+
|
|
166
|
+
Generates a human-readable ID.
|
|
167
|
+
|
|
168
|
+
#### Parameters
|
|
169
|
+
|
|
170
|
+
| Parameter | Type | Default | Description |
|
|
171
|
+
|-----------|------|---------|-------------|
|
|
172
|
+
| `lang` | `string` | `'en'` | Language code (`'en'` or `'es'`) |
|
|
173
|
+
| `adjective` | `boolean` | `true` | Include adjective component |
|
|
174
|
+
| `color` | `boolean` | `true` | Include color component |
|
|
175
|
+
| `noun` | `boolean` | `true` | Include noun component |
|
|
176
|
+
| `separator` | `string` | `'-'` | Separator between components |
|
|
177
|
+
| `randomOrder` | `boolean` | `false` | Randomize component order |
|
|
178
|
+
| `asObject` | `boolean` | `false` | Return object instead of string |
|
|
179
|
+
| `number.min` | `number` | `0` | Minimum number value |
|
|
180
|
+
| `number.max` | `number` | `9999` | Maximum number value |
|
|
181
|
+
| `number.sets` | `number` | `1` | Number of number components |
|
|
182
|
+
| `number.completeWithZeros` | `boolean` | `false` | Pad numbers with leading zeros |
|
|
183
|
+
| `dictionary` | `Partial<Dictionary>` | - | Custom word dictionary |
|
|
184
|
+
|
|
185
|
+
#### Returns
|
|
186
|
+
|
|
187
|
+
- **String** (default): `"adjective-color-noun-number"`
|
|
188
|
+
- **Object** (when `asObject: true`): `{ adjective, color, noun, number1, ... }`
|
|
189
|
+
|
|
190
|
+
### `dictionaries`
|
|
191
|
+
|
|
192
|
+
Access the built-in word dictionaries:
|
|
193
|
+
|
|
194
|
+
```javascript
|
|
195
|
+
import { dictionaries } from 'human-ids';
|
|
196
|
+
|
|
197
|
+
console.log(Object.keys(dictionaries)); // ['en', 'es']
|
|
198
|
+
console.log(Object.keys(dictionaries.en)); // ['adjectives', 'colors', 'nouns']
|
|
199
|
+
```
|
|
88
200
|
|
|
89
|
-
##
|
|
201
|
+
## Contributing
|
|
90
202
|
|
|
91
|
-
|
|
92
|
-
License
|
|
203
|
+
Contributions and bug reports are welcome! Feel free to open an issue or submit a pull request on the [GitHub repository](https://github.com/jcmujica/human-ids).
|
|
93
204
|
|
|
94
|
-
|
|
205
|
+
## License
|
|
95
206
|
|
|
96
|
-
|
|
207
|
+
This project is licensed under the [ISC License](https://opensource.org/licenses/ISC).
|