Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add "includeBrackets" config to control whether brackets are included in the random ID #4

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/random.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import {

/**
* generate random HKID
* @param config - config object
* @param config.includeBrackets - whether to include brackets
* @returns string of random HKID
*/
export function random(): string {
export function random(config: any): string {
// generate random HKID
// decide how many leading characters to generate
const leadingLettersLength = Math.random() > 0.5 ? 1 : 2;
Expand All @@ -31,5 +33,11 @@ export function random(): string {
} else {
checkDigit = remainder.toString();
}

// return HKID with no brackets if config.includeBrackets is explicitly set to false
if (config.hasOwnProperty('includeBrackets' && !config.includeBrackets)) {
return `${leadingLetters}${numbers}${checkDigit}`;
}
// return HKID with brackets by default
return `${leadingLetters}${numbers}(${checkDigit})`;
}
22 changes: 22 additions & 0 deletions test/random.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,26 @@ describe ('test random function', () => {
validate(result);
})
})

describe('config.includeBrackets', () => {
it.each([
{ includeBrackets: true },
{},
])
('should return HKID with brackets', (config) => {
// Act
const result = random(config);
// Assert
expect(result).toMatch(/\([A-Z0-9]\)/);
})

it('should return HKID without brackets', () => {
// Arrange
const config = { includeBrackets: false };
// Act
const result = random(config);
// Assert
expect(result).toMatch(/[A-Z0-9]/);
})
})
})