human-ids 1.0.14 → 2.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/README.md +178 -112
- package/dist/cjs/dictionaries/en.d.ts +6 -0
- package/dist/cjs/dictionaries/en.js +973 -0
- package/dist/cjs/dictionaries/es.d.ts +6 -0
- package/dist/cjs/dictionaries/es.js +973 -0
- package/dist/cjs/dictionaries/index.d.ts +18 -0
- package/dist/cjs/dictionaries/index.js +25 -0
- package/dist/cjs/index.d.ts +246 -0
- package/dist/cjs/index.js +127 -0
- package/dist/esm/dictionaries/en.d.ts +6 -0
- package/dist/esm/dictionaries/en.js +970 -0
- package/dist/esm/dictionaries/es.d.ts +6 -0
- package/dist/esm/dictionaries/es.js +970 -0
- package/dist/esm/dictionaries/index.d.ts +18 -0
- package/dist/esm/dictionaries/index.js +22 -0
- package/dist/esm/index.d.ts +246 -0
- package/dist/esm/index.js +123 -0
- package/dist/esm/package.json +1 -0
- package/package.json +51 -11
- package/CHANGELOG.md +0 -72
- package/dictionaries/en.js +0 -224
- package/dictionaries/es.js +0 -221
- package/dictionaries/index.js +0 -9
- package/index.d.ts +0 -34
- package/index.js +0 -123
- package/tests/index.test.js +0 -82
package/README.md
CHANGED
|
@@ -1,139 +1,205 @@
|
|
|
1
1
|
# Human IDs
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
|
9
|
+
|
|
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
|
+
- **ESM + CJS**: Ships both ES Module and CommonJS builds with a full `exports` map
|
|
15
|
+
- **TypeScript**: Full type definitions with JSDoc documentation
|
|
16
|
+
- **Customizable**: Configure separators, components, number ranges, and even provide custom dictionaries
|
|
22
17
|
|
|
23
|
-
|
|
18
|
+
## Installation
|
|
24
19
|
|
|
25
|
-
```
|
|
20
|
+
```bash
|
|
26
21
|
npm install human-ids
|
|
27
22
|
```
|
|
28
|
-
|
|
23
|
+
|
|
24
|
+
## Quick Start
|
|
25
|
+
|
|
26
|
+
```javascript
|
|
27
|
+
import { generateId } from 'human-ids';
|
|
28
|
+
|
|
29
|
+
// Generate a random ID
|
|
30
|
+
const id = generateId();
|
|
31
|
+
// => "stellar-jade-horizon-7392"
|
|
32
|
+
|
|
33
|
+
// Spanish
|
|
34
|
+
const spanishId = generateId({ lang: 'es' });
|
|
35
|
+
// => "aurora-esmeralda-brillante-4518"
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Output Examples
|
|
39
|
+
|
|
40
|
+
```
|
|
41
|
+
English: Spanish:
|
|
42
|
+
bubbly-amber-phoenix-3847 aurora-cobalto-magnifico-6729
|
|
43
|
+
stellar-jade-horizon-7392 cascada-esmeralda-valiente-1854
|
|
44
|
+
cosmic-sapphire-nebula-9034 bosque-ambar-sereno-5043
|
|
45
|
+
radiant-coral-serenity-2671 estrella-jade-luminoso-8156
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Usage
|
|
49
|
+
|
|
50
|
+
### Basic Usage
|
|
51
|
+
|
|
52
|
+
```javascript
|
|
53
|
+
import { generateId } from 'human-ids';
|
|
54
|
+
|
|
55
|
+
// Default: English, all components enabled
|
|
56
|
+
const id = generateId();
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Configuration Options
|
|
29
60
|
|
|
30
61
|
```javascript
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
//
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
},
|
|
40
|
-
colors: {
|
|
41
|
-
red: 'red',
|
|
42
|
-
blue: 'blue',
|
|
43
|
-
// ...
|
|
44
|
-
},
|
|
45
|
-
nouns: {
|
|
46
|
-
cat: 'cat',
|
|
47
|
-
dog: 'dog',
|
|
48
|
-
// ...
|
|
49
|
-
},
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
// Initialize the settings object (these are the defaults)
|
|
53
|
-
const settings = {
|
|
54
|
-
lang: 'en',
|
|
55
|
-
adjective: true,
|
|
56
|
-
color: true,
|
|
57
|
-
noun: true,
|
|
58
|
-
randomOrder: false,
|
|
59
|
-
separator: '-',
|
|
60
|
-
asObject: false,
|
|
61
|
-
semantics: {
|
|
62
|
-
es: ['noun', 'color', 'adjective', 'number'],
|
|
63
|
-
en: ['adjective', 'color', 'noun', 'number'],
|
|
64
|
-
},
|
|
62
|
+
const id = generateId({
|
|
63
|
+
lang: 'en', // Language: 'en' or 'es'
|
|
64
|
+
adjective: true, // Include adjective
|
|
65
|
+
color: true, // Include color
|
|
66
|
+
noun: true, // Include noun
|
|
67
|
+
separator: '-', // Component separator
|
|
68
|
+
randomOrder: false, // Shuffle component order
|
|
69
|
+
asObject: false, // Return object instead of string
|
|
65
70
|
number: {
|
|
66
|
-
min: 0,
|
|
67
|
-
max:
|
|
68
|
-
sets: 1,
|
|
69
|
-
completeWithZeros: false
|
|
70
|
-
}
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
// Generate an ID using the function
|
|
74
|
-
console.log(humanId())
|
|
71
|
+
min: 0, // Minimum number value
|
|
72
|
+
max: 9999, // Maximum number value
|
|
73
|
+
sets: 1, // Number of number components
|
|
74
|
+
completeWithZeros: false // Pad with leading zeros
|
|
75
|
+
}
|
|
76
|
+
});
|
|
75
77
|
```
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
userSettings (optional): An object containing various options to customize the generated ID. It includes the following properties:
|
|
81
|
-
lang (string): The language for the ID generation. You can specify the language code (e.g., 'en' for English, 'es' for Spanish).
|
|
82
|
-
adjective (boolean): Include an adjective component in the generated ID. (Default: true)
|
|
83
|
-
color (boolean): Include a color component in the generated ID. (Default: true)
|
|
84
|
-
noun (boolean): Include a noun component in the generated ID. (Default: true)
|
|
85
|
-
randomOrder (boolean): Randomize the order of ID components. (Default: false)
|
|
86
|
-
separator (string): The separator used to join the ID components. (Default: '-')
|
|
87
|
-
asObject (boolean): Return the ID as an object instead of a string. (Default: false)
|
|
88
|
-
semantics (object|null): An object specifying the order of components based on language. For example, you can set the order for Spanish using "es": ['noun', 'color', 'adjective', 'number'].
|
|
89
|
-
number (object): An object containing settings for the number component:
|
|
90
|
-
min (number): The minimum value for the number component. (Default: 0)
|
|
91
|
-
max (number): The maximum value for the number component. (Default: 999)
|
|
92
|
-
completeWithZeros (boolean): Pad the number with leading zeros to match the maximum length. (Default: false)
|
|
93
|
-
dictionary (object): An object containing custom dictionaries for adjectives, colors, and nouns:
|
|
94
|
-
adjectives (object): A dictionary of adjectives for different languages.
|
|
95
|
-
colors (object): A dictionary of colors for different languages.
|
|
96
|
-
nouns (object): A dictionary of nouns for different languages.
|
|
97
|
-
|
|
98
|
-
## Return Value
|
|
99
|
-
The generateId function returns a unique identifier as a string by default. If the asObject option is set to true, the function returns an object containing the individual components of the ID.
|
|
100
|
-
|
|
101
|
-
## Examples
|
|
102
|
-
|
|
103
|
-
### Generate an ID with default settings:
|
|
78
|
+
|
|
79
|
+
### Examples
|
|
80
|
+
|
|
81
|
+
#### Without Colors
|
|
104
82
|
|
|
105
83
|
```javascript
|
|
84
|
+
generateId({ color: false });
|
|
85
|
+
// => "happy-cat-3456"
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
#### Custom Separator
|
|
106
89
|
|
|
107
|
-
|
|
108
|
-
|
|
90
|
+
```javascript
|
|
91
|
+
generateId({ separator: '_' });
|
|
92
|
+
// => "happy_blue_cat_9012"
|
|
109
93
|
```
|
|
110
94
|
|
|
111
|
-
|
|
95
|
+
#### Multiple Number Sets
|
|
112
96
|
|
|
113
97
|
```javascript
|
|
98
|
+
generateId({ number: { sets: 2 } });
|
|
99
|
+
// => "happy-blue-cat-1234-5678"
|
|
100
|
+
```
|
|
114
101
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
//
|
|
120
|
-
// color: 'verde',
|
|
121
|
-
// noun: 'montaña',
|
|
122
|
-
// number: '0123'
|
|
123
|
-
// }
|
|
102
|
+
#### Zero-Padded Numbers
|
|
103
|
+
|
|
104
|
+
```javascript
|
|
105
|
+
generateId({ number: { min: 0, max: 999, completeWithZeros: true } });
|
|
106
|
+
// => "happy-blue-cat-042"
|
|
124
107
|
```
|
|
125
|
-
## Test Results
|
|
126
108
|
|
|
127
|
-
|
|
128
|
-
License
|
|
109
|
+
#### Get ID as Object
|
|
129
110
|
|
|
130
|
-
|
|
111
|
+
```javascript
|
|
112
|
+
generateId({ asObject: true });
|
|
113
|
+
// => { adjective: 'happy', color: 'blue', noun: 'cat', number1: '1234' }
|
|
114
|
+
```
|
|
131
115
|
|
|
132
|
-
|
|
116
|
+
#### Random Component Order
|
|
117
|
+
|
|
118
|
+
```javascript
|
|
119
|
+
generateId({ randomOrder: true });
|
|
120
|
+
// => "1234-cat-happy-blue" (random each time)
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
#### Custom Dictionary
|
|
124
|
+
|
|
125
|
+
```javascript
|
|
126
|
+
generateId({
|
|
127
|
+
dictionary: {
|
|
128
|
+
adjectives: ['cool', 'awesome'],
|
|
129
|
+
colors: ['red', 'blue'],
|
|
130
|
+
nouns: ['rocket', 'star']
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
// => "cool-red-rocket-5678"
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## CommonJS Support
|
|
137
|
+
|
|
138
|
+
```javascript
|
|
139
|
+
const { generateId, dictionaries } = require('human-ids');
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## Uniqueness & Collision Probability
|
|
143
|
+
|
|
144
|
+
The library includes extensive dictionaries to maximize uniqueness:
|
|
145
|
+
|
|
146
|
+
| Language | Adjectives | Colors | Nouns | Numbers (default) | Total Combinations |
|
|
147
|
+
|----------|------------|--------|-------|-------------------|-------------------|
|
|
148
|
+
| English | 372 | 120 | 469 | 10,000 (0-9999) | ~209 billion |
|
|
149
|
+
| Spanish | 372 | 120 | 469 | 10,000 (0-9999) | ~209 billion |
|
|
150
|
+
|
|
151
|
+
With ~209 billion combinations, collision probability is negligible for most use cases. For even higher uniqueness:
|
|
152
|
+
|
|
153
|
+
- **Increase number range**: `{ number: { max: 99999 } }` → 10x more combinations
|
|
154
|
+
- **Use multiple number sets**: `{ number: { sets: 2 } }` → 10,000x more combinations
|
|
155
|
+
- **Combine both**: ~20 quadrillion combinations
|
|
156
|
+
|
|
157
|
+
> **Note**: While collision probability is extremely low, absolute uniqueness cannot be guaranteed with random generation. For guaranteed uniqueness, implement collision detection with your datastore.
|
|
158
|
+
|
|
159
|
+
## API Reference
|
|
160
|
+
|
|
161
|
+
### `generateId(settings?): string | IdParts`
|
|
162
|
+
|
|
163
|
+
Generates a human-readable ID.
|
|
164
|
+
|
|
165
|
+
#### Parameters
|
|
166
|
+
|
|
167
|
+
| Parameter | Type | Default | Description |
|
|
168
|
+
|-----------|------|---------|-------------|
|
|
169
|
+
| `lang` | `string` | `'en'` | Language code (`'en'` or `'es'`) |
|
|
170
|
+
| `adjective` | `boolean` | `true` | Include adjective component |
|
|
171
|
+
| `color` | `boolean` | `true` | Include color component |
|
|
172
|
+
| `noun` | `boolean` | `true` | Include noun component |
|
|
173
|
+
| `separator` | `string` | `'-'` | Separator between components |
|
|
174
|
+
| `randomOrder` | `boolean` | `false` | Randomize component order |
|
|
175
|
+
| `asObject` | `boolean` | `false` | Return object instead of string |
|
|
176
|
+
| `number.min` | `number` | `0` | Minimum number value |
|
|
177
|
+
| `number.max` | `number` | `9999` | Maximum number value |
|
|
178
|
+
| `number.sets` | `number` | `1` | Number of number components |
|
|
179
|
+
| `number.completeWithZeros` | `boolean` | `false` | Pad numbers with leading zeros |
|
|
180
|
+
| `dictionary` | `Partial<Dictionary>` | - | Custom word dictionary |
|
|
181
|
+
|
|
182
|
+
#### Returns
|
|
183
|
+
|
|
184
|
+
- **String** (default): `"adjective-color-noun-number"`
|
|
185
|
+
- **Object** (when `asObject: true`): `{ adjective, color, noun, number1, ... }`
|
|
186
|
+
|
|
187
|
+
### `dictionaries`
|
|
188
|
+
|
|
189
|
+
Access the built-in word dictionaries:
|
|
190
|
+
|
|
191
|
+
```javascript
|
|
192
|
+
import { dictionaries } from 'human-ids';
|
|
193
|
+
|
|
194
|
+
console.log(Object.keys(dictionaries)); // ['en', 'es']
|
|
195
|
+
console.log(Object.keys(dictionaries.en)); // ['adjectives', 'colors', 'nouns']
|
|
196
|
+
console.log(dictionaries.en.adjectives[0]); // 'silly'
|
|
197
|
+
```
|
|
133
198
|
|
|
134
199
|
## Contributing
|
|
135
200
|
|
|
136
|
-
Contributions and bug reports are welcome! Feel free to open an issue or submit a pull request on the GitHub repository.
|
|
201
|
+
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).
|
|
202
|
+
|
|
137
203
|
## License
|
|
138
204
|
|
|
139
|
-
This project is licensed under the
|
|
205
|
+
This project is licensed under the [ISC License](https://opensource.org/licenses/ISC).
|