Skip to content

Commit 4d74d03

Browse files
committed
Refactor ODS code generation to be more JS, and less python
1 parent 07fc662 commit 4d74d03

1 file changed

Lines changed: 21 additions & 25 deletions

File tree

artillery/notify_entrypoint.mjs

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,27 @@ const logger = pino()
77

88
const NUM_ODS_CODES = 10000
99

10-
// make a list of 10k ODS codes, using the known-valid ones as a seed
11-
const fullOdsCodes = (() => {
12-
const set = new Set(allowedOdsCodes)
13-
const codes = [...allowedOdsCodes]
14-
15-
const letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
16-
const numbers = "0123456789"
17-
18-
while (codes.length < NUM_ODS_CODES) {
19-
let c = ""
20-
for (let i = 0; i < 2; i++) {
21-
c += letters.charAt(Math.floor(Math.random() * letters.length))
22-
}
23-
for (let i = 0; i < 3; i++) {
24-
c += numbers.charAt(Math.floor(Math.random() * numbers.length))
25-
}
26-
27-
if (!set.has(c)) {
28-
set.add(c)
29-
codes.push(c)
30-
}
31-
}
32-
33-
return codes
34-
})()
10+
const LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
11+
const DIGITS = "0123456789";
12+
13+
const randomChar = (chars) => chars[Math.floor(Math.random() * chars.length)];
14+
15+
/** Generate one two-letter, three-digit ODS code, e.g. "AB123" */
16+
const generateOdsCode = () =>
17+
`${randomChar(LETTERS)}${randomChar(LETTERS)}${randomChar(DIGITS)}${randomChar(DIGITS)}${randomChar(DIGITS)}`;
18+
19+
function buildFullOdsCodes(targetCount, seedCodes) {
20+
const codes = new Set(seedCodes);
21+
22+
while (codes.size < targetCount) {
23+
codes.add(generateOdsCode());
24+
}
25+
26+
return Array.from(codes);
27+
}
28+
29+
/** The complete list of 10k ODS codes */
30+
const fullOdsCodes = buildFullOdsCodes(NUM_ODS_CODES, allowedOdsCodes);
3531

3632
function computeCheckDigit(nhsNumber) {
3733
const factors = [10,9,8,7,6,5,4,3,2]

0 commit comments

Comments
 (0)