-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
47 lines (38 loc) · 1.05 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const {ceil, floor} = Math;
const toInt = s => Number.parseInt(s, 10);
const sumDigits = int => {
let sum = 0;
while (int) {
sum += int % 10;
int = floor(int / 10);
}
return sum;
};
const doubleAlternate = (value, index) => {
let pos = index ?? value.length - 1;
let sum = 0;
let alt = true;
for (; pos >= 0; pos--) {
const cur = toInt(value.charAt(pos));
sum += alt ? sumDigits(cur * 2) : cur;
alt = !alt;
}
return sum;
};
export function npiValid(id, prefix = '80840') {
const prefixRegex = /^80\d{3}$/;
const npiRegex = /^\d{10}$/;
const usPrefixSum = 24; // Precalculated value for 80840 (United States)
const npi = id?.toString() ?? '';
const pre = prefix?.toString() ?? '';
if (!npiRegex.test(npi) || !prefixRegex.test(pre)) {
return false;
}
const check = toInt(npi.charAt(9));
const npiSum = doubleAlternate(npi, 8);
const preSum = pre === '80840' ? usPrefixSum : doubleAlternate(pre, 3);
const total = npiSum + preSum;
const totalCeil = ceil(total / 10) * 10;
return totalCeil - total === check;
}
export default npiValid;