human-ids 1.0.1 → 1.0.2

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,10 @@ All notable changes to this project will be documented in this file.
4
4
 
5
5
  ## [Unreleased]
6
6
 
7
+ ## [1.0.2] - 2023-07-07
8
+
9
+ ### Changed
10
+ - Refactor into function
7
11
  ## [1.0.1] - 2023-07-07
8
12
 
9
13
  ### 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,67 @@
1
1
  const dictionaries = require('./dictionaries');
2
2
 
3
- class IdGenerator {
4
- constructor(settings = {}) {
5
- this.lang = dictionaries[settings.lang] ? settings.lang : dictionaries[settings.lang?.split('-')[0]] ? settings.lang?.split('-')[0] : 'en';
6
- this.useAdjective = settings.adjective || false;
7
- this.useColor = settings.color || false;
8
- this.useNoun = settings.noun || false;
9
- this.numberMin = settings.number && settings.number.min !== undefined ? settings.number.min : 0;
10
- this.numberMax = settings.number && settings.number.max !== undefined ? settings.number.max : 999;
11
- this.numberSets = settings.number && settings.number.sets || 1;
12
- this.completeWithZeros = settings.number && settings.number.completeWithZeros || false;
13
- }
3
+ function generateId(settings = {}) {
4
+ const lang = dictionaries[settings.lang] ? settings.lang : dictionaries[settings.lang?.split('-')[0]] ? settings.lang?.split('-')[0] : 'en';
5
+ const useAdjective = settings.adjective || false;
6
+ const useColor = settings.color || false;
7
+ const useNoun = settings.noun || false;
8
+ const numberMin = settings.number && settings.number.min !== undefined ? settings.number.min : 0;
9
+ const numberMax = settings.number && settings.number.max !== undefined ? settings.number.max : 999;
10
+ const numberSets = settings.number && settings.number.sets || 1;
11
+ const completeWithZeros = settings.number && settings.number.completeWithZeros || false;
14
12
 
15
- getRandomNumber(min, max) {
13
+ function getRandomNumber(min, max) {
16
14
  return Math.floor(Math.random() * (max - min + 1)) + min;
17
15
  }
18
16
 
19
- formatNumber(number) {
20
- if (this.completeWithZeros) {
21
- const maxNumberLength = this.numberMax.toString().length;
17
+ function formatNumber(number) {
18
+ if (completeWithZeros) {
19
+ const maxNumberLength = numberMax.toString().length;
22
20
  return number.toString().padStart(maxNumberLength, '0');
23
21
  } else {
24
22
  return number.toString();
25
23
  }
26
24
  }
27
25
 
28
- getAdjectives() {
29
- return dictionaries[this.lang].adjectives;
26
+ function getAdjectives() {
27
+ return dictionaries[lang].adjectives;
30
28
  }
31
29
 
32
- getColors() {
33
- return dictionaries[this.lang].colors;
30
+ function getColors() {
31
+ return dictionaries[lang].colors;
34
32
  }
35
33
 
36
- getNouns() {
37
- return dictionaries[this.lang].nouns;
34
+ function getNouns() {
35
+ return dictionaries[lang].nouns;
38
36
  }
39
37
 
40
- generateId() {
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
- }
38
+ const parts = [];
48
39
 
49
- if (this.useColor) {
50
- const colors = Object.values(this.getColors());
51
- const color = colors[Math.floor(Math.random() * colors.length)];
52
- parts.push(color);
53
- }
40
+ if (useAdjective) {
41
+ const adjectives = Object.values(getAdjectives());
42
+ const adjective = adjectives[Math.floor(Math.random() * adjectives.length)];
43
+ parts.push(adjective);
44
+ }
54
45
 
55
- if (this.useNoun) {
56
- const nouns = Object.values(this.getNouns());
57
- const noun = nouns[Math.floor(Math.random() * nouns.length)];
58
- parts.push(noun);
59
- }
46
+ if (useColor) {
47
+ const colors = Object.values(getColors());
48
+ const color = colors[Math.floor(Math.random() * colors.length)];
49
+ parts.push(color);
50
+ }
60
51
 
61
- for (let i = 0; i < this.numberSets; i++) {
62
- const number = this.getRandomNumber(this.numberMin, this.numberMax);
63
- const formattedNumber = this.formatNumber(number);
64
- parts.push(formattedNumber);
65
- }
52
+ if (useNoun) {
53
+ const nouns = Object.values(getNouns());
54
+ const noun = nouns[Math.floor(Math.random() * nouns.length)];
55
+ parts.push(noun);
56
+ }
66
57
 
67
- return parts.join('-');
58
+ for (let i = 0; i < numberSets; i++) {
59
+ const number = getRandomNumber(numberMin, numberMax);
60
+ const formattedNumber = formatNumber(number);
61
+ parts.push(formattedNumber);
68
62
  }
63
+
64
+ return parts.join('-');
69
65
  }
70
66
 
71
- module.exports = IdGenerator;
67
+ module.exports = generateId;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "human-ids",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "A library to generate human readable IDs",
5
5
  "main": "index.js",
6
6
  "scripts": {
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 11:15:36</div><div id="summary"><div id="suite-summary"><div class="summary-total">Suites (1)</div><div class="summary-passed summary-empty">0 passed</div><div class="summary-failed ">1 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 summary-empty">0 passed</div><div class="summary-failed ">1 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 warn">247.141s</div></div></label><div class="suite-tests"><div class="test-result failed"><div class="test-info"><div class="test-suitename">IdGenerator</div><div class="test-title">generateId should return a unique ID</div><div class="test-status">failed</div><div class="test-duration">246.948s</div></div></div></div></div></div></body></html>
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>
@@ -1,8 +1,8 @@
1
- const IdGenerator = require('../index');
1
+ const generateId = require('../index');
2
2
 
3
- describe('IdGenerator', () => {
4
- test('generateId should return a unique ID', () => {
5
- const testLength = 1000000;
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 1000 IDs
20
+ // Generate testLength IDs
22
21
  for (let i = 0; i < testLength; i++) {
23
- const id = idGenerator.generateId();
22
+ const id = generateId(settings);
24
23
  ids.add(id);
25
24
  }
26
25