All files / src/lib business-id.ts

100% Statements 10/10
100% Branches 6/6
100% Functions 2/2
100% Lines 10/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21    4x       56x 26x   30x 210x     30x 30x 11x   19x 19x    
// The Finnish Business ID (Y-tunnus): seven digits, a hyphen and a check
// digit, e.g. "2345678-0".
const WEIGHTS = [7, 9, 10, 5, 8, 4, 2]
 
/** Whether the text is a Business ID in its strict form with a matching check digit. */
export function isValidBusinessId(text: string): boolean {
  if (!/^\d{7}-\d$/.test(text)) {
    return false
  }
  const sum = WEIGHTS.reduce(
    (total, weight, index) => total + weight * Number(text[index]),
    0,
  )
  const remainder = sum % 11
  if (remainder === 1) {
    return false
  }
  const check = remainder === 0 ? 0 : 11 - remainder
  return check === Number(text[8])
}