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 CHANGED
@@ -1,139 +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
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
- To use Human IDs in your project, you can install it via npm:
17
+ ## Installation
24
18
 
25
- ```she
19
+ ```bash
26
20
  npm install human-ids
27
21
  ```
28
- ## Usage example
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
- 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,
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: 999,
68
- sets: 1,
69
- completeWithZeros: false,
70
- },
71
- };
72
-
73
- // Generate an ID using the function
74
- 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"
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
- ## Configuration
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
- const id = generateId({ lang: 'en' });
108
- console.log(id); // Example output: "green-house-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"
109
133
  ```
110
134
 
111
- ### Generate an ID as an object:
135
+ ## CommonJS Support
112
136
 
113
137
  ```javascript
138
+ // Default import
139
+ const generateId = require('human-ids');
114
140
 
115
- const idObject = generateId({ lang: 'es', asObject: true });
116
- console.log(idObject);
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
- 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.
128
- License
145
+ ## Uniqueness & Collision Probability
146
+
147
+ The library includes extensive dictionaries to maximize uniqueness:
129
148
 
130
- This project is licensed under the ISC License.
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
- Feel free to modify the `README.md` file to fit your project's specific details and requirements.
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. Please ensure that your code follows the project's coding standards and includes appropriate test coverage.
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 MIT License.
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 };