human-ids 1.0.14 → 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 +180 -112
- 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 -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,207 @@
|
|
|
1
1
|
# Human IDs
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
|
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
|
+
- **TypeScript**: Full type definitions with JSDoc documentation
|
|
15
|
+
- **Customizable**: Configure separators, components, number ranges, and even provide custom dictionaries
|
|
22
16
|
|
|
23
|
-
|
|
17
|
+
## Installation
|
|
24
18
|
|
|
25
|
-
```
|
|
19
|
+
```bash
|
|
26
20
|
npm install human-ids
|
|
27
21
|
```
|
|
28
|
-
|
|
22
|
+
|
|
23
|
+
## Quick Start
|
|
24
|
+
|
|
25
|
+
```javascript
|
|
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
|
|
29
59
|
|
|
30
60
|
```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
|
-
},
|
|
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
|
|
65
69
|
number: {
|
|
66
|
-
min: 0,
|
|
67
|
-
max:
|
|
68
|
-
sets: 1,
|
|
69
|
-
completeWithZeros: false
|
|
70
|
-
}
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
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"
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
#### Multiple Number Sets
|
|
95
|
+
|
|
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' }
|
|
75
113
|
```
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
The settings object allows you to customize the ID generation process. The available options are:
|
|
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:
|
|
114
|
+
|
|
115
|
+
#### Random Component Order
|
|
104
116
|
|
|
105
117
|
```javascript
|
|
118
|
+
generateId({ randomOrder: true });
|
|
119
|
+
// => "1234-cat-happy-blue" (random each time)
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
#### Custom Dictionary
|
|
106
123
|
|
|
107
|
-
|
|
108
|
-
|
|
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"
|
|
109
133
|
```
|
|
110
134
|
|
|
111
|
-
|
|
135
|
+
## CommonJS Support
|
|
112
136
|
|
|
113
137
|
```javascript
|
|
138
|
+
// Default import
|
|
139
|
+
const generateId = require('human-ids');
|
|
114
140
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
// Example output:
|
|
118
|
-
// {
|
|
119
|
-
// adjective: 'casa',
|
|
120
|
-
// color: 'verde',
|
|
121
|
-
// noun: 'montaña',
|
|
122
|
-
// number: '0123'
|
|
123
|
-
// }
|
|
141
|
+
// Named imports
|
|
142
|
+
const { generateId, dictionaries } = require('human-ids');
|
|
124
143
|
```
|
|
125
|
-
## Test Results
|
|
126
144
|
|
|
127
|
-
|
|
128
|
-
|
|
145
|
+
## Uniqueness & Collision Probability
|
|
146
|
+
|
|
147
|
+
The library includes extensive dictionaries to maximize uniqueness:
|
|
129
148
|
|
|
130
|
-
|
|
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 |
|
|
131
153
|
|
|
132
|
-
|
|
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
|
+
```
|
|
133
200
|
|
|
134
201
|
## Contributing
|
|
135
202
|
|
|
136
|
-
Contributions and bug reports are welcome! Feel free to open an issue or submit a pull request on the GitHub repository.
|
|
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).
|
|
204
|
+
|
|
137
205
|
## License
|
|
138
206
|
|
|
139
|
-
This project is licensed under the
|
|
207
|
+
This project is licensed under the [ISC License](https://opensource.org/licenses/ISC).
|