human-ids 1.0.12 → 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 CHANGED
@@ -4,6 +4,16 @@ 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
+
13
+ ## [1.0.13] - 2023-07-10
14
+
15
+ ### Added
16
+ - Added asObject setting to return an object with the words
7
17
  ## [1.0.12] - 2023-07-07
8
18
 
9
19
  ### Fixed
package/README.md CHANGED
@@ -55,6 +55,13 @@ const settings = {
55
55
  adjective: true,
56
56
  color: true,
57
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
+ },
58
65
  number: {
59
66
  min: 0,
60
67
  max: 999,
@@ -70,18 +77,51 @@ console.log(humanId())
70
77
 
71
78
  The settings object allows you to customize the ID generation process. The available options are:
72
79
 
73
- lang: The language of the generated words (e.g., 'en' for English, 'es' for Spanish).
74
- adjective: Set to true to include an adjective in the ID (default: false).
75
- color: Set to true to include a color in the ID (default: false).
76
- noun: Set to true to include a noun in the ID (default: false).
77
- randomOrder: Set to true to include a random order in the ID segments (default: false).
78
- separator: Set to true to change a separator in the ID
79
- number: An object that configures the number part of the ID:
80
- min: The minimum value of the number (default: 0).
81
- max: The maximum value of the number (default: 999).
82
- completeWithZeros: Set to true to pad the number with leading zeros (default: false).
83
- sets: The number of sets of random numbers to include, separated by a hyphen (-) (default: 1).
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:
84
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
+ ```
85
125
  ## Test Results
86
126
 
87
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.
@@ -89,4 +129,11 @@ License
89
129
 
90
130
  This project is licensed under the ISC License.
91
131
 
92
- 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
@@ -6,6 +6,10 @@ declare module 'human-ids' {
6
6
  noun?: boolean;
7
7
  randomOrder?: boolean;
8
8
  separator?: string;
9
+ asObject?: boolean;
10
+ semantics?: {
11
+ [key: string]: string['noun' | 'adjective' | 'color' | 'number'];
12
+ } | null;
9
13
  number?: {
10
14
  min?: number;
11
15
  max?: number;
package/index.js CHANGED
@@ -1,12 +1,17 @@
1
1
  const dictionaries = require('./dictionaries');
2
2
 
3
3
  const defaultSettings = {
4
- lang: 'es',
4
+ lang: 'en',
5
5
  adjective: true,
6
6
  color: true,
7
7
  noun: true,
8
8
  randomOrder: false,
9
9
  separator: '-',
10
+ asObject: false,
11
+ semantics: {
12
+ es: ['noun', 'color', 'adjective', 'number'],
13
+ en: ['adjective', 'color', 'noun', 'number'],
14
+ },
10
15
  number: {
11
16
  min: 0,
12
17
  max: 999,
@@ -16,15 +21,22 @@ const defaultSettings = {
16
21
  };
17
22
 
18
23
  function generateId(userSettings = {}) {
19
- const settings = {...defaultSettings, ...userSettings};
20
- const lang = dictionaries[settings.lang] ? settings.lang : dictionaries[settings.lang?.split('-')[0]] ? settings.lang?.split('-')[0] : 'en';
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';
21
30
  const useAdjective = settings.adjective || false;
22
31
  const useColor = settings.color || false;
23
32
  const useNoun = settings.noun || false;
24
- const numberMin = settings.number && settings.number.min !== undefined ? settings.number.min : 0;
25
- const numberMax = settings.number && settings.number.max !== undefined ? settings.number.max : 999;
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;
26
37
  const numberSets = settings.number && settings.number.sets || 1;
27
- const completeWithZeros = settings.number && settings.number.completeWithZeros || false;
38
+ const completeWithZeros =
39
+ settings.number && settings.number.completeWithZeros || false;
28
40
 
29
41
  function getRandomNumber(min, max) {
30
42
  return Math.floor(Math.random() * (max - min + 1)) + min;
@@ -40,48 +52,72 @@ function generateId(userSettings = {}) {
40
52
  }
41
53
 
42
54
  function getAdjectives() {
43
- return settings.dictionary && settings.dictionary.adjectives ? settings.dictionary.adjectives : dictionaries[lang].adjectives;
55
+ return settings.dictionary && settings.dictionary.adjectives
56
+ ? settings.dictionary.adjectives
57
+ : dictionaries[lang].adjectives;
44
58
  }
45
59
 
46
60
  function getColors() {
47
- return settings.dictionary && settings.dictionary.colors ? settings.dictionary.colors : dictionaries[lang].colors;
61
+ return settings.dictionary && settings.dictionary.colors
62
+ ? settings.dictionary.colors
63
+ : dictionaries[lang].colors;
48
64
  }
49
65
 
50
66
  function getNouns() {
51
- return settings.dictionary && settings.dictionary.nouns ? settings.dictionary.nouns : dictionaries[lang].nouns;
67
+ return settings.dictionary && settings.dictionary.nouns
68
+ ? settings.dictionary.nouns
69
+ : dictionaries[lang].nouns;
52
70
  }
53
71
 
54
- const parts = [];
72
+ const parts = {};
55
73
 
56
74
  if (useAdjective) {
57
75
  const adjectives = Object.values(getAdjectives());
58
76
  const adjective = adjectives[Math.floor(Math.random() * adjectives.length)];
59
- parts.push(adjective);
77
+ parts.adjective = adjective;
60
78
  }
61
79
 
62
80
  if (useColor) {
63
81
  const colors = Object.values(getColors());
64
82
  const color = colors[Math.floor(Math.random() * colors.length)];
65
- parts.push(color);
83
+ parts.color = color;
66
84
  }
67
85
 
68
86
  if (useNoun) {
69
87
  const nouns = Object.values(getNouns());
70
88
  const noun = nouns[Math.floor(Math.random() * nouns.length)];
71
- parts.push(noun);
89
+ parts.noun = noun;
72
90
  }
73
91
 
74
92
  for (let i = 0; i < numberSets; i++) {
75
93
  const number = getRandomNumber(numberMin, numberMax);
76
94
  const formattedNumber = formatNumber(number);
77
- parts.push(formattedNumber);
95
+ parts[`number${i + 1}`] = formattedNumber;
78
96
  }
79
97
 
80
98
  if (settings.randomOrder) {
81
- parts.sort(() => Math.random() - 0.5);
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}`);
106
+ }
107
+
108
+ if (settings.asObject) {
109
+ return parts;
82
110
  }
83
111
 
84
- return parts.join(`${settings.separator}`);
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}`);
85
121
  }
86
122
 
87
- module.exports = generateId;
123
+ module.exports = generateId;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "human-ids",
3
- "version": "1.0.12",
3
+ "version": "1.0.14",
4
4
  "description": "A library to generate human readable IDs",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -1,15 +1,64 @@
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: 'en',
55
+ lang: 'es',
8
56
  adjective: true,
9
57
  color: true,
10
58
  noun: true,
11
59
  randomOrder: true,
12
60
  separator: '-',
61
+ asObject: false,
13
62
  number: {
14
63
  min: 0,
15
64
  max: 9999,
@@ -30,4 +79,4 @@ describe('generateId', () => {
30
79
  // Assert uniqueness
31
80
  expect(ids.size).toBe(testLength);
32
81
  });
33
- });
82
+ });