human-ids 1.0.13 → 1.0.14
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/CHANGELOG.md +6 -0
- package/README.md +56 -13
- package/index.d.ts +3 -0
- package/index.js +49 -23
- package/package.json +1 -1
- package/tests/index.test.js +51 -3
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
|
|
|
4
4
|
|
|
5
5
|
## [Unreleased]
|
|
6
6
|
|
|
7
|
+
## [1.0.14] - 2023-07-19
|
|
8
|
+
|
|
9
|
+
### Added
|
|
10
|
+
- Added semantics support to be passed as user settings to oder words according to the language
|
|
11
|
+
- Added default semantics for english and spanish languages
|
|
12
|
+
|
|
7
13
|
## [1.0.13] - 2023-07-10
|
|
8
14
|
|
|
9
15
|
### Added
|
package/README.md
CHANGED
|
@@ -58,6 +58,10 @@ const settings = {
|
|
|
58
58
|
randomOrder: false,
|
|
59
59
|
separator: '-',
|
|
60
60
|
asObject: false,
|
|
61
|
+
semantics: {
|
|
62
|
+
es: ['noun', 'color', 'adjective', 'number'],
|
|
63
|
+
en: ['adjective', 'color', 'noun', 'number'],
|
|
64
|
+
},
|
|
61
65
|
number: {
|
|
62
66
|
min: 0,
|
|
63
67
|
max: 999,
|
|
@@ -73,19 +77,51 @@ console.log(humanId())
|
|
|
73
77
|
|
|
74
78
|
The settings object allows you to customize the ID generation process. The available options are:
|
|
75
79
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
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:
|
|
104
|
+
|
|
105
|
+
```javascript
|
|
106
|
+
|
|
107
|
+
const id = generateId({ lang: 'en' });
|
|
108
|
+
console.log(id); // Example output: "green-house-123"
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### Generate an ID as an object:
|
|
88
112
|
|
|
113
|
+
```javascript
|
|
114
|
+
|
|
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
|
+
// }
|
|
124
|
+
```
|
|
89
125
|
## Test Results
|
|
90
126
|
|
|
91
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.
|
|
@@ -93,4 +129,11 @@ License
|
|
|
93
129
|
|
|
94
130
|
This project is licensed under the ISC License.
|
|
95
131
|
|
|
96
|
-
Feel free to modify the `README.md` file to fit your project's specific details and requirements.
|
|
132
|
+
Feel free to modify the `README.md` file to fit your project's specific details and requirements.
|
|
133
|
+
|
|
134
|
+
## Contributing
|
|
135
|
+
|
|
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.
|
|
137
|
+
## License
|
|
138
|
+
|
|
139
|
+
This project is licensed under the MIT License.
|
package/index.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
const dictionaries = require('./dictionaries');
|
|
2
2
|
|
|
3
3
|
const defaultSettings = {
|
|
4
|
-
lang: '
|
|
4
|
+
lang: 'en',
|
|
5
5
|
adjective: true,
|
|
6
6
|
color: true,
|
|
7
7
|
noun: true,
|
|
8
8
|
randomOrder: false,
|
|
9
9
|
separator: '-',
|
|
10
10
|
asObject: false,
|
|
11
|
+
semantics: {
|
|
12
|
+
es: ['noun', 'color', 'adjective', 'number'],
|
|
13
|
+
en: ['adjective', 'color', 'noun', 'number'],
|
|
14
|
+
},
|
|
11
15
|
number: {
|
|
12
16
|
min: 0,
|
|
13
17
|
max: 999,
|
|
@@ -17,15 +21,22 @@ const defaultSettings = {
|
|
|
17
21
|
};
|
|
18
22
|
|
|
19
23
|
function generateId(userSettings = {}) {
|
|
20
|
-
const settings = {...defaultSettings, ...userSettings};
|
|
21
|
-
const lang = dictionaries[settings.lang]
|
|
24
|
+
const settings = { ...defaultSettings, ...userSettings };
|
|
25
|
+
const lang = dictionaries[settings.lang]
|
|
26
|
+
? settings.lang
|
|
27
|
+
: dictionaries[settings.lang?.split('-')[0]]
|
|
28
|
+
? settings.lang?.split('-')[0]
|
|
29
|
+
: 'en';
|
|
22
30
|
const useAdjective = settings.adjective || false;
|
|
23
31
|
const useColor = settings.color || false;
|
|
24
32
|
const useNoun = settings.noun || false;
|
|
25
|
-
const numberMin =
|
|
26
|
-
|
|
33
|
+
const numberMin =
|
|
34
|
+
settings.number && settings.number.min !== undefined ? settings.number.min : 0;
|
|
35
|
+
const numberMax =
|
|
36
|
+
settings.number && settings.number.max !== undefined ? settings.number.max : 999;
|
|
27
37
|
const numberSets = settings.number && settings.number.sets || 1;
|
|
28
|
-
const completeWithZeros =
|
|
38
|
+
const completeWithZeros =
|
|
39
|
+
settings.number && settings.number.completeWithZeros || false;
|
|
29
40
|
|
|
30
41
|
function getRandomNumber(min, max) {
|
|
31
42
|
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
@@ -41,57 +52,72 @@ function generateId(userSettings = {}) {
|
|
|
41
52
|
}
|
|
42
53
|
|
|
43
54
|
function getAdjectives() {
|
|
44
|
-
return settings.dictionary && settings.dictionary.adjectives
|
|
55
|
+
return settings.dictionary && settings.dictionary.adjectives
|
|
56
|
+
? settings.dictionary.adjectives
|
|
57
|
+
: dictionaries[lang].adjectives;
|
|
45
58
|
}
|
|
46
59
|
|
|
47
60
|
function getColors() {
|
|
48
|
-
return settings.dictionary && settings.dictionary.colors
|
|
61
|
+
return settings.dictionary && settings.dictionary.colors
|
|
62
|
+
? settings.dictionary.colors
|
|
63
|
+
: dictionaries[lang].colors;
|
|
49
64
|
}
|
|
50
65
|
|
|
51
66
|
function getNouns() {
|
|
52
|
-
return settings.dictionary && settings.dictionary.nouns
|
|
67
|
+
return settings.dictionary && settings.dictionary.nouns
|
|
68
|
+
? settings.dictionary.nouns
|
|
69
|
+
: dictionaries[lang].nouns;
|
|
53
70
|
}
|
|
54
71
|
|
|
55
|
-
const parts =
|
|
72
|
+
const parts = {};
|
|
56
73
|
|
|
57
74
|
if (useAdjective) {
|
|
58
75
|
const adjectives = Object.values(getAdjectives());
|
|
59
76
|
const adjective = adjectives[Math.floor(Math.random() * adjectives.length)];
|
|
60
|
-
parts.
|
|
77
|
+
parts.adjective = adjective;
|
|
61
78
|
}
|
|
62
79
|
|
|
63
80
|
if (useColor) {
|
|
64
81
|
const colors = Object.values(getColors());
|
|
65
82
|
const color = colors[Math.floor(Math.random() * colors.length)];
|
|
66
|
-
parts.
|
|
83
|
+
parts.color = color;
|
|
67
84
|
}
|
|
68
85
|
|
|
69
86
|
if (useNoun) {
|
|
70
87
|
const nouns = Object.values(getNouns());
|
|
71
88
|
const noun = nouns[Math.floor(Math.random() * nouns.length)];
|
|
72
|
-
parts.
|
|
89
|
+
parts.noun = noun;
|
|
73
90
|
}
|
|
74
91
|
|
|
75
92
|
for (let i = 0; i < numberSets; i++) {
|
|
76
93
|
const number = getRandomNumber(numberMin, numberMax);
|
|
77
94
|
const formattedNumber = formatNumber(number);
|
|
78
|
-
parts
|
|
95
|
+
parts[`number${i + 1}`] = formattedNumber;
|
|
79
96
|
}
|
|
80
97
|
|
|
81
98
|
if (settings.randomOrder) {
|
|
82
|
-
|
|
99
|
+
const keys = Object.keys(parts);
|
|
100
|
+
const shuffledKeys = keys.sort(() => Math.random() - 0.5);
|
|
101
|
+
const shuffledParts = [];
|
|
102
|
+
for (const key of shuffledKeys) {
|
|
103
|
+
shuffledParts.push(parts[key]);
|
|
104
|
+
}
|
|
105
|
+
return shuffledParts.join(`${settings.separator}`);
|
|
83
106
|
}
|
|
84
107
|
|
|
85
108
|
if (settings.asObject) {
|
|
86
|
-
|
|
87
|
-
const keys = ['adjective', 'color', 'noun', 'number'];
|
|
88
|
-
parts.forEach((part, index) => {
|
|
89
|
-
object[keys[index]] = part;
|
|
90
|
-
});
|
|
91
|
-
return object;
|
|
109
|
+
return parts;
|
|
92
110
|
}
|
|
93
111
|
|
|
94
|
-
|
|
112
|
+
|
|
113
|
+
let semantics = settings.semantics && settings.semantics[lang] || settings.semantics.en;
|
|
114
|
+
|
|
115
|
+
const result = semantics.map((key) => {
|
|
116
|
+
if (key !== 'number') return parts[key];
|
|
117
|
+
|
|
118
|
+
return Object.values(parts).filter((part) => !isNaN(part)).join('-');
|
|
119
|
+
});
|
|
120
|
+
return result.join(`${settings.separator}`);
|
|
95
121
|
}
|
|
96
122
|
|
|
97
|
-
module.exports = generateId;
|
|
123
|
+
module.exports = generateId;
|
package/package.json
CHANGED
package/tests/index.test.js
CHANGED
|
@@ -1,10 +1,58 @@
|
|
|
1
1
|
const generateId = require('../index');
|
|
2
2
|
|
|
3
3
|
describe('generateId', () => {
|
|
4
|
-
test('should return a unique ID', () => {
|
|
4
|
+
test('should return a unique ID with all possible scenarios', () => {
|
|
5
|
+
const allScenarios = [
|
|
6
|
+
{
|
|
7
|
+
lang: 'es',
|
|
8
|
+
adjective: true,
|
|
9
|
+
color: true,
|
|
10
|
+
noun: true,
|
|
11
|
+
randomOrder: false,
|
|
12
|
+
separator: '-',
|
|
13
|
+
asObject: false,
|
|
14
|
+
number: {
|
|
15
|
+
min: 0,
|
|
16
|
+
max: 9999,
|
|
17
|
+
sets: 1,
|
|
18
|
+
completeWithZeros: true
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
lang: 'en',
|
|
23
|
+
adjective: false,
|
|
24
|
+
color: true,
|
|
25
|
+
noun: true,
|
|
26
|
+
randomOrder: true,
|
|
27
|
+
separator: '_',
|
|
28
|
+
asObject: true,
|
|
29
|
+
number: {
|
|
30
|
+
min: 1000,
|
|
31
|
+
max: 9999,
|
|
32
|
+
sets: 2,
|
|
33
|
+
completeWithZeros: false
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
// Add more scenarios here...
|
|
37
|
+
];
|
|
38
|
+
|
|
39
|
+
const ids = new Set();
|
|
40
|
+
|
|
41
|
+
for (const scenario of allScenarios) {
|
|
42
|
+
const id = generateId(scenario);
|
|
43
|
+
|
|
44
|
+
console.log(`Scenario: ${JSON.stringify(scenario, null, 2)}\nID: ${id}`);
|
|
45
|
+
ids.add(id);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Assert uniqueness for all scenarios
|
|
49
|
+
expect(ids.size).toBe(allScenarios.length);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test('should return a unique ID with iterations', () => {
|
|
5
53
|
const testLength = 1000;
|
|
6
54
|
const settings = {
|
|
7
|
-
lang: '
|
|
55
|
+
lang: 'es',
|
|
8
56
|
adjective: true,
|
|
9
57
|
color: true,
|
|
10
58
|
noun: true,
|
|
@@ -31,4 +79,4 @@ describe('generateId', () => {
|
|
|
31
79
|
// Assert uniqueness
|
|
32
80
|
expect(ids.size).toBe(testLength);
|
|
33
81
|
});
|
|
34
|
-
});
|
|
82
|
+
});
|