Installation

npm i --save @halloverden/number-utilities-ts

Test & build

Test with

npm run test

Build with

npm run build

International Utilities

Misc. International Utilities

checkDate(year: number, month: number, day: number): boolean

Takes a date, and returns true if the date actually exists. Returns false otherwise.

getRandomNumberFromRange(min: number, max: number): number

Returns a random number from the given range of numbers.

zeroPadding(value: string, length = 2): string

This function returns a zero padded string with the length specified. If the length of the incoming value is longer than or equal to the specified length, the value is returned unmodified.

International Mod11 Utilities

getMod11ControlDigit(input: string, weights: number[], acceptedRemainders = [0], onRemainder1 = '-'): string

Calculates a single control digit from the given input.

getMod11ControlDigits(input: string, numberOfDigits: number, throwIfOne = true): string[]

Runs the getMod11ControlDigit the given number of times, and returns an array of control digits.

getDefaultMod11WeightsForLength(length: number): number[]

Returns an array of numbers containing the default weights for mod11. The length of the array is based on the given length. The given length represents the length of the number to work with.

International Luhn Utilities

getLuhnControlDigit(input: string): number

Returns a single control digit for the given input.

getLuhnControlDigits(input: string, numberOfDigits: number): number[]

Returns an array of control digits for the given input.

checkLuhnControlDigit(input: string): boolean

Returns true if the last digit of the given string is a valid luhn control digit for the first part of the given string.

checkLuhnControlDigits(input: string, numberOfDigits: number): boolean

Returns true if the given number of digits from the end of the given input are valid luhn control numbers.

Country Specific Utilities

Norway đŸ‡ŗ🇴

Functions

generateEveryNinForDate(date: Date): NinListEntity

Returns a NinListEntity containing all valid NINs for the given date.

getRandomNinForDate(date: Date, type: NinTypeEnum = NinTypeEnum.TYPE_REGULAR_NIN): NinEntity

Returns a NinEntity with a random NIN for the given date and NIN type.

getIndRangesFromYear(year: number, type: NinTypeEnum = NinTypeEnum.TYPE_REGULAR_NIN): IndRange[]

Returns the individual number ranges that are valid for the given year and the given NIN type.

getNinIndividualNumberRangeMappingTypesFromInd(ind: number, type: NinTypeEnum = NinTypeEnum.TYPE_REGULAR_NIN): NinIndividualNumberRangeMapping[]

Returns individual number ranges that are valid for the given individual number.

getYearRangesFromInd(ind: number, type: NinTypeEnum = NinTypeEnum.TYPE_REGULAR_NIN): YearRange[]

Returns the range of years that are valid for a given individual number.

getBirthDateFromNinEntity(ninEntity: NinEntity): Date

Returns a Date instance based on the given NinEntity.

getDayFromNinEntity(ninEntity: NinEntity): number

Returns the day from the NIN in the given NinEntity.

getMonthFromNinEntity(ninEntity: NinEntity): number

Returns the month from the NIN in the given NinEntity.

getYearFromNinEntity(ninEntity: NinEntity): number

Returns the year from the NIN in the given NinEntity.

getYearFromNin(nin: string): number

Returns the year from the NIN in the given NIN.

isDNumberPattern(nin: string): boolean

Returns true if the given NIN follows the D-number pattern.

isFemaleNinPattern(nin: string): boolean

Returns true if the given NIN is follows the pattern of a NIN for a female.

isHNumberPattern(nin: string): boolean

Returns true if the given NIN is follows H-number pattern.

isMaleNinPattern(nin: string): boolean

Returns true if the given NIN is follows the pattern of a NIN for a male.

isNinPattern(nin: string): boolean

Returns true if the given NIN follows the NIN pattern.

isNprSyntheticNinPattern(nin: string): boolean

Returns true if the given NIN follows the Norwegian National Population Registry synthetic NIN pattern.

isRegularNinPattern(nin: string): boolean

Returns true if the given NIN follows the regular pattern. This function checks that the given nin is NOT a D-number, NOT an H-number, ang NOT an NPR synthetic NIN.

Types

type IndRange {...}

Represents a range of individual numbers.

class NinEntity {...}

The NinEntity is a utility class for representing a NIN. It expects a valid nin, and will throw an error if the given nin is invalid. If you want to validate a nin without trying to create an instance of NinEntity, use the validateNin function instead.


try {
  let nin = new NinEntity('12345678912');
} catch (e) {
  // invalid nin
}
          

The class has a couple of utility methods:

  • type (getter) - Returns the NinType
  • nin (getter) - Returns the NIN as a string
  • isRegularNin(): boolean - See: isRegularNinPattern(nin: string): boolean
  • isDNumber(): boolean - See: isDNumberPattern(nin: string): boolean
  • isHNumber(): boolean - See: isHNumberPattern(nin: string): boolean
  • isNprSyntheticNin(): boolean - See: isNprSyntheticNinPattern(nin: string): boolean
  • isMaleNinPattern(): boolean - See: isMaleNinPattern(nin: string): boolean
  • isFemaleNinPattern(): boolean - See: isFemaleNinPattern(nin: string): boolean
type NinIndividualNumberRangeMapping {...}

Intersection of IndRage & YearRange.

class NinList {...}

Keeps two lists of NINs. One for female, and one for male NINs.

enum NinType {...}

An enum representing the different NIN types.

type YearRange {...}

Represents a range of years.

Validators

validateAccountNumber(accountNumber: string): boolean

Returns true if the given account number is valid.

ℹī¸

Keep in mind that some Norwegian account numbers are also valid Norwegian NINs. You might want to handle this in your application.

validateKidNumber(kidNumber: string): boolean

Validates the given KID number.

⚠ī¸

NB! This function is incomplete. Please read the source code before using it.

validateNin(nin: string): boolean

Returns true if the given nin is valid.

validateOrganizationNumber(orgNumber: string): boolean

Returns true if the given organization number is valid.

Contribution Guidelines

PRs are very welcome. Official documentation for implementations might be hard to come by, but links are greatly appreciated. For any questions regarding any aspect of the library, feel free to open an issue.