human-ids 1.0.1 → 1.0.3
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 +8 -0
- package/index.d.ts +19 -0
- package/index.js +58 -46
- package/package.json +1 -1
- package/test-report.html +1 -1
- package/tests/index.test.js +6 -7
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file.
|
|
|
4
4
|
|
|
5
5
|
## [Unreleased]
|
|
6
6
|
|
|
7
|
+
## [1.0.3] - 2023-07-07
|
|
8
|
+
|
|
9
|
+
### Added
|
|
10
|
+
- Added default settings
|
|
11
|
+
## [1.0.2] - 2023-07-07
|
|
12
|
+
|
|
13
|
+
### Changed
|
|
14
|
+
- Refactor into function
|
|
7
15
|
## [1.0.1] - 2023-07-07
|
|
8
16
|
|
|
9
17
|
### Added
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
declare module 'human-ids' {
|
|
2
|
+
export interface IdGeneratorSettings {
|
|
3
|
+
lang: string;
|
|
4
|
+
adjective?: boolean;
|
|
5
|
+
color?: boolean;
|
|
6
|
+
noun?: boolean;
|
|
7
|
+
number?: {
|
|
8
|
+
min?: number;
|
|
9
|
+
max?: number;
|
|
10
|
+
completeWithZeros?: boolean;
|
|
11
|
+
sets?: number;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export default class IdGenerator {
|
|
16
|
+
constructor(settings?: IdGeneratorSettings);
|
|
17
|
+
generateId(): string;
|
|
18
|
+
}
|
|
19
|
+
}
|
package/index.js
CHANGED
|
@@ -1,71 +1,83 @@
|
|
|
1
1
|
const dictionaries = require('./dictionaries');
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
}
|
|
3
|
+
const defaultSettings = {
|
|
4
|
+
lang: 'en',
|
|
5
|
+
adjective: true,
|
|
6
|
+
color: true,
|
|
7
|
+
noun: true,
|
|
8
|
+
number: {
|
|
9
|
+
min: 0,
|
|
10
|
+
max: 999,
|
|
11
|
+
sets: 1,
|
|
12
|
+
completeWithZeros: false,
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
function generateId(userSettings = {}) {
|
|
17
|
+
const settings = Object.assign({}, defaultSettings, userSettings);
|
|
18
|
+
const lang = dictionaries[settings.lang] ? settings.lang : dictionaries[settings.lang?.split('-')[0]] ? settings.lang?.split('-')[0] : 'en';
|
|
19
|
+
const useAdjective = settings.adjective || false;
|
|
20
|
+
const useColor = settings.color || false;
|
|
21
|
+
const useNoun = settings.noun || false;
|
|
22
|
+
const numberMin = settings.number && settings.number.min !== undefined ? settings.number.min : 0;
|
|
23
|
+
const numberMax = settings.number && settings.number.max !== undefined ? settings.number.max : 999;
|
|
24
|
+
const numberSets = settings.number && settings.number.sets || 1;
|
|
25
|
+
const completeWithZeros = settings.number && settings.number.completeWithZeros || false;
|
|
14
26
|
|
|
15
|
-
getRandomNumber(min, max) {
|
|
27
|
+
function getRandomNumber(min, max) {
|
|
16
28
|
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
17
29
|
}
|
|
18
30
|
|
|
19
|
-
formatNumber(number) {
|
|
20
|
-
if (
|
|
21
|
-
const maxNumberLength =
|
|
31
|
+
function formatNumber(number) {
|
|
32
|
+
if (completeWithZeros) {
|
|
33
|
+
const maxNumberLength = numberMax.toString().length;
|
|
22
34
|
return number.toString().padStart(maxNumberLength, '0');
|
|
23
35
|
} else {
|
|
24
36
|
return number.toString();
|
|
25
37
|
}
|
|
26
38
|
}
|
|
27
39
|
|
|
28
|
-
getAdjectives() {
|
|
29
|
-
return dictionaries[
|
|
40
|
+
function getAdjectives() {
|
|
41
|
+
return dictionaries[lang].adjectives;
|
|
30
42
|
}
|
|
31
43
|
|
|
32
|
-
getColors() {
|
|
33
|
-
return dictionaries[
|
|
44
|
+
function getColors() {
|
|
45
|
+
return dictionaries[lang].colors;
|
|
34
46
|
}
|
|
35
47
|
|
|
36
|
-
getNouns() {
|
|
37
|
-
return dictionaries[
|
|
48
|
+
function getNouns() {
|
|
49
|
+
return dictionaries[lang].nouns;
|
|
38
50
|
}
|
|
39
51
|
|
|
40
|
-
|
|
41
|
-
const parts = [];
|
|
42
|
-
|
|
43
|
-
if (this.useAdjective) {
|
|
44
|
-
const adjectives = Object.values(this.getAdjectives());
|
|
45
|
-
const adjective = adjectives[Math.floor(Math.random() * adjectives.length)];
|
|
46
|
-
parts.push(adjective);
|
|
47
|
-
}
|
|
52
|
+
const parts = [];
|
|
48
53
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
+
if (useAdjective) {
|
|
55
|
+
const adjectives = Object.values(getAdjectives());
|
|
56
|
+
const adjective = adjectives[Math.floor(Math.random() * adjectives.length)];
|
|
57
|
+
parts.push(adjective);
|
|
58
|
+
}
|
|
54
59
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
+
if (useColor) {
|
|
61
|
+
const colors = Object.values(getColors());
|
|
62
|
+
const color = colors[Math.floor(Math.random() * colors.length)];
|
|
63
|
+
parts.push(color);
|
|
64
|
+
}
|
|
60
65
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
+
if (useNoun) {
|
|
67
|
+
const nouns = Object.values(getNouns());
|
|
68
|
+
const noun = nouns[Math.floor(Math.random() * nouns.length)];
|
|
69
|
+
parts.push(noun);
|
|
70
|
+
}
|
|
66
71
|
|
|
67
|
-
|
|
72
|
+
for (let i = 0; i < numberSets; i++) {
|
|
73
|
+
const number = getRandomNumber(numberMin, numberMax);
|
|
74
|
+
const formattedNumber = formatNumber(number);
|
|
75
|
+
parts.push(formattedNumber);
|
|
68
76
|
}
|
|
77
|
+
|
|
78
|
+
return parts.join('-');
|
|
69
79
|
}
|
|
70
80
|
|
|
71
|
-
|
|
81
|
+
console.log(generateId())
|
|
82
|
+
|
|
83
|
+
module.exports = generateId;
|
package/package.json
CHANGED
package/test-report.html
CHANGED
|
@@ -257,4 +257,4 @@ header {
|
|
|
257
257
|
font-size: 1rem;
|
|
258
258
|
padding: 0 0.5rem;
|
|
259
259
|
}
|
|
260
|
-
</style></head><body><div class="jesthtml-content"><header><h1 id="title">Test Report</h1></header><div id="metadata-container"><div id="timestamp">Started: 2023-07-07
|
|
260
|
+
</style></head><body><div class="jesthtml-content"><header><h1 id="title">Test Report</h1></header><div id="metadata-container"><div id="timestamp">Started: 2023-07-07 12:23:52</div><div id="summary"><div id="suite-summary"><div class="summary-total">Suites (1)</div><div class="summary-passed ">1 passed</div><div class="summary-failed summary-empty">0 failed</div><div class="summary-pending summary-empty">0 pending</div></div><div id="test-summary"><div class="summary-total">Tests (1)</div><div class="summary-passed ">1 passed</div><div class="summary-failed summary-empty">0 failed</div><div class="summary-pending summary-empty">0 pending</div></div></div></div><div id="suite-1" class="suite-container"><input id="collapsible-0" type="checkbox" class="toggle" checked="checked"/><label for="collapsible-0"><div class="suite-info"><div class="suite-path">/Users/jcmujica/Code/jc/human-ids/tests/index.test.js</div><div class="suite-time">0.185s</div></div></label><div class="suite-tests"><div class="test-result passed"><div class="test-info"><div class="test-suitename">generateId</div><div class="test-title">should return a unique ID</div><div class="test-status">passed</div><div class="test-duration">0.005s</div></div></div></div></div></div></body></html>
|
package/tests/index.test.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
const
|
|
1
|
+
const generateId = require('../index');
|
|
2
2
|
|
|
3
|
-
describe('
|
|
4
|
-
test('
|
|
5
|
-
const testLength =
|
|
3
|
+
describe('generateId', () => {
|
|
4
|
+
test('should return a unique ID', () => {
|
|
5
|
+
const testLength = 1000;
|
|
6
6
|
const settings = {
|
|
7
7
|
lang: 'en',
|
|
8
8
|
adjective: true,
|
|
@@ -15,12 +15,11 @@ describe('IdGenerator', () => {
|
|
|
15
15
|
}
|
|
16
16
|
};
|
|
17
17
|
|
|
18
|
-
const idGenerator = new IdGenerator(settings);
|
|
19
18
|
const ids = new Set();
|
|
20
19
|
|
|
21
|
-
// Generate
|
|
20
|
+
// Generate testLength IDs
|
|
22
21
|
for (let i = 0; i < testLength; i++) {
|
|
23
|
-
const id =
|
|
22
|
+
const id = generateId(settings);
|
|
24
23
|
ids.add(id);
|
|
25
24
|
}
|
|
26
25
|
|