|
| 1 | +import type { ValidatorOptions } from './types'; |
| 2 | +import { mod11, stripFormatting } from './utils'; |
| 3 | + |
| 4 | +type PostalCodeOptions = ValidatorOptions; |
| 5 | + |
| 6 | +/** |
| 7 | + * Validates that the input value is a Norwegian postal (zip) code. |
| 8 | + * @example |
| 9 | + * ``` |
| 10 | + * validatePostalCode('0000') // => true |
| 11 | + * ``` |
| 12 | + */ |
| 13 | +export function validatePostalCode( |
| 14 | + value: string, |
| 15 | + options: PostalCodeOptions = {}, |
| 16 | +): boolean { |
| 17 | + if (options.allowFormatting) { |
| 18 | + // biome-ignore lint/style/noParameterAssign: |
| 19 | + value = stripFormatting(value); |
| 20 | + } |
| 21 | + |
| 22 | + return /^\d{4}$/.test(value); |
| 23 | +} |
| 24 | + |
| 25 | +type PhoneNumberOptions = ValidatorOptions & { |
| 26 | + /** |
| 27 | + * Whether it should be a mobile number |
| 28 | + * @default false |
| 29 | + */ |
| 30 | + mobileOnly?: boolean; |
| 31 | +}; |
| 32 | + |
| 33 | +/** |
| 34 | + * Validates that the input value is a Norwegian phone number. |
| 35 | + * |
| 36 | + * Supports mobile only validation. |
| 37 | + * @example |
| 38 | + * ``` |
| 39 | + * validatePhoneNumber('00000000') // => true |
| 40 | + * validatePhoneNumber('90000000', { mobileOnly: true }) // => true |
| 41 | + * ``` |
| 42 | + */ |
| 43 | +export function validatePhoneNumber( |
| 44 | + value: string, |
| 45 | + options: PhoneNumberOptions = {}, |
| 46 | +): boolean { |
| 47 | + if (options.allowFormatting) { |
| 48 | + // biome-ignore lint/style/noParameterAssign: |
| 49 | + value = stripFormatting(value); |
| 50 | + } |
| 51 | + |
| 52 | + const isPhoneNumber = /^\d{8}$/.test(value); |
| 53 | + |
| 54 | + if (options.mobileOnly) { |
| 55 | + // Norwegian mobile phone numbers start with 4 or 9 |
| 56 | + // See https://nkom.no/telefoni-og-telefonnummer/telefonnummer-og-den-norske-nummerplan/alle-nummerserier-for-norske-telefonnumre |
| 57 | + return isPhoneNumber && ['4', '9'].includes(value.charAt(0)); |
| 58 | + } |
| 59 | + |
| 60 | + return isPhoneNumber; |
| 61 | +} |
| 62 | + |
| 63 | +type OrganizationNumberOptions = ValidatorOptions; |
| 64 | + |
| 65 | +/** |
| 66 | + * Validates that the input value is a {@link https://www.brreg.no/om-oss/registrene-vare/om-enhetsregisteret/organisasjonsnummeret/ Norwegian organization number}. |
| 67 | + * @example |
| 68 | + * ``` |
| 69 | + * validateOrganizationNumber('000000000') // => true |
| 70 | + * ``` |
| 71 | + */ |
| 72 | +export function validateOrganizationNumber( |
| 73 | + value: string, |
| 74 | + options: PhoneNumberOptions = {}, |
| 75 | +): boolean { |
| 76 | + if (options.allowFormatting) { |
| 77 | + // biome-ignore lint/style/noParameterAssign: |
| 78 | + value = stripFormatting(value); |
| 79 | + } |
| 80 | + |
| 81 | + /** References: |
| 82 | + * https://www.brreg.no/om-oss/registrene-vare/om-enhetsregisteret/organisasjonsnummeret/ |
| 83 | + * https://no.wikipedia.org/wiki/Organisasjonsnummer |
| 84 | + */ |
| 85 | + return mod11(value, [3, 2, 7, 6, 5, 4, 3, 2]); |
| 86 | +} |
0 commit comments