human-ids 1.0.8 → 1.0.10

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,7 +4,17 @@ All notable changes to this project will be documented in this file.
4
4
 
5
5
  ## [Unreleased]
6
6
 
7
- ## [1.0.7] - 2023-07-07
7
+ ## [1.0.10] - 2023-07-07
8
+
9
+ ### Added
10
+ - Added separator customization
11
+ - Added random ordering for segments
12
+ - Added support for custom dictionary
13
+ ## [1.0.9] - 2023-07-07
14
+
15
+ ### Changed
16
+ - Changed removed init console.log
17
+ ## [1.0.8] - 2023-07-07
8
18
 
9
19
  ### Changed
10
20
  - Changed README.md to correctly display usage information
package/README.md CHANGED
@@ -30,6 +30,25 @@ npm install human-ids
30
30
  ```javascript
31
31
  import humanId from 'human-ids'
32
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
+
33
52
  // Initialize the settings object (these are the defaults)
34
53
  const settings = {
35
54
  lang: 'en',
@@ -55,6 +74,8 @@ The settings object allows you to customize the ID generation process. The avail
55
74
  adjective: Set to true to include an adjective in the ID (default: false).
56
75
  color: Set to true to include a color in the ID (default: false).
57
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
58
79
  number: An object that configures the number part of the ID:
59
80
  min: The minimum value of the number (default: 0).
60
81
  max: The maximum value of the number (default: 999).
package/index.d.ts CHANGED
@@ -4,11 +4,24 @@ declare module 'human-ids' {
4
4
  adjective?: boolean;
5
5
  color?: boolean;
6
6
  noun?: boolean;
7
+ randomOrder?: boolean;
8
+ separator?: string;
7
9
  number?: {
8
10
  min?: number;
9
11
  max?: number;
10
12
  completeWithZeros?: boolean;
11
13
  };
14
+ dictionary?: {
15
+ adjectives?: {
16
+ [key: string]: string[];
17
+ };
18
+ colors?: {
19
+ [key: string]: string[];
20
+ };
21
+ nouns?: {
22
+ [key: string]: string[];
23
+ }
24
+ };
12
25
  }
13
26
 
14
27
  function generateId(settings?: IdGeneratorSettings): string;
package/index.js CHANGED
@@ -5,6 +5,8 @@ const defaultSettings = {
5
5
  adjective: true,
6
6
  color: true,
7
7
  noun: true,
8
+ randomOrder: false,
9
+ separator: '-',
8
10
  number: {
9
11
  min: 0,
10
12
  max: 999,
@@ -38,15 +40,15 @@ function generateId(userSettings = {}) {
38
40
  }
39
41
 
40
42
  function getAdjectives() {
41
- return dictionaries[lang].adjectives;
43
+ return settings.dictionary && settings.dictionary.adjectives ? settings.dictionary.adjectives : {};
42
44
  }
43
45
 
44
46
  function getColors() {
45
- return dictionaries[lang].colors;
47
+ return settings.dictionary && settings.dictionary.colors ? settings.dictionary.colors : {};
46
48
  }
47
49
 
48
50
  function getNouns() {
49
- return dictionaries[lang].nouns;
51
+ return settings.dictionary && settings.dictionary.nouns ? settings.dictionary.nouns : {};
50
52
  }
51
53
 
52
54
  const parts = [];
@@ -75,9 +77,11 @@ function generateId(userSettings = {}) {
75
77
  parts.push(formattedNumber);
76
78
  }
77
79
 
78
- return parts.join('-');
79
- }
80
+ if (settings.randomOrder) {
81
+ parts.sort(() => Math.random() - 0.5);
82
+ }
80
83
 
81
- console.log(generateId())
84
+ return parts.join(`${settings.separator}`);
85
+ }
82
86
 
83
87
  module.exports = generateId;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "human-ids",
3
- "version": "1.0.8",
3
+ "version": "1.0.10",
4
4
  "description": "A library to generate human readable IDs",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -2,12 +2,14 @@ const generateId = require('../index');
2
2
 
3
3
  describe('generateId', () => {
4
4
  test('should return a unique ID', () => {
5
- const testLength = 1000;
5
+ const testLength = 1000000;
6
6
  const settings = {
7
7
  lang: 'en',
8
8
  adjective: true,
9
9
  color: true,
10
10
  noun: true,
11
+ randomOrder: true,
12
+ separator: '-',
11
13
  number: {
12
14
  min: 0,
13
15
  max: 9999,
@@ -20,6 +22,8 @@ describe('generateId', () => {
20
22
  // Generate testLength IDs
21
23
  for (let i = 0; i < testLength; i++) {
22
24
  const id = generateId(settings);
25
+
26
+ console.log(`${i} ID: ${id}`);
23
27
  ids.add(id);
24
28
  }
25
29