diff --git a/src/random.ts b/src/random.ts index ba8e48b..4d685ae 100644 --- a/src/random.ts +++ b/src/random.ts @@ -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; @@ -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})`; } \ No newline at end of file diff --git a/test/random.spec.ts b/test/random.spec.ts index 2d50d10..eb6be19 100644 --- a/test/random.spec.ts +++ b/test/random.spec.ts @@ -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]/); + }) + }) }) \ No newline at end of file