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 CHANGED
@@ -1,96 +1,207 @@
1
1
  # Human IDs
2
2
 
3
- A library to generate human-readable IDs.
4
-
5
- ## Description
6
-
7
- Human IDs is a JavaScript library that allows you to generate human-readable IDs. The IDs consist of a combination of adjectives, colors, nouns, and numbers, creating unique and memorable identifiers.
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
+ [![npm version](https://img.shields.io/npm/v/human-ids.svg)](https://www.npmjs.com/package/human-ids)
4
+ [![License: ISC](https://img.shields.io/badge/License-ISC-blue.svg)](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
- To use Human IDs in your project, you can install it via npm:
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
- ```she
19
+ ```bash
26
20
  npm install human-ids
27
21
  ```
28
- ## Usage example
22
+
23
+ ## Quick Start
29
24
 
30
25
  ```javascript
31
- import humanId from 'human-ids'
32
-
33
- //If using a custom dictionary
34
- const customDictionary = {
35
- adjectives: {
36
- awesome: 'awesome',
37
- amazing: 'amazing',
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,
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: 999,
64
- sets: 1,
65
- completeWithZeros: false,
66
- },
67
- };
68
-
69
- // Generate an ID using the function
70
- console.log(humanId())
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
- The settings object allows you to customize the ID generation process. The available options are:
94
+ #### Multiple Number Sets
75
95
 
76
- lang: The language of the generated words (e.g., 'en' for English, 'es' for Spanish).
77
- adjective: Set to true to include an adjective in the ID (default: false).
78
- color: Set to true to include a color in the ID (default: false).
79
- noun: Set to true to include a noun in the ID (default: false).
80
- randomOrder: Set to true to include a random order in the ID segments (default: false).
81
- separator: Set to true to change a separator in the ID
82
- asObject: Set to true to return an object with the words
83
- number: An object that configures the number part of the ID:
84
- min: The minimum value of the number (default: 0).
85
- max: The maximum value of the number (default: 999).
86
- completeWithZeros: Set to true to pad the number with leading zeros (default: false).
87
- sets: The number of sets of random numbers to include, separated by a hyphen (-) (default: 1).
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
- ## Test Results
201
+ ## Contributing
90
202
 
91
- While the generated IDs strive for uniqueness, it's important to note that absolute uniqueness cannot be guaranteed, especially with a finite set of words and numbers. During the uniqueness test, the generator produced 999,722 unique IDs out of the expected 1,000,000. This means that a small number of duplicates may occur in practice.
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
- This project is licensed under the ISC License.
205
+ ## License
95
206
 
96
- Feel free to modify the `README.md` file to fit your project's specific details and requirements.
207
+ This project is licensed under the [ISC License](https://opensource.org/licenses/ISC).
@@ -0,0 +1,3 @@
1
+ import type { Dictionary } from '../index';
2
+ declare const words: Dictionary;
3
+ export { words as en };