Class Luhn

Lets you easily generate and validate checksum values based on the Luhn mod-N algorithm

Hierarchy

  • Luhn

Constructors

Properties

dictionary: string = '0123456789AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'

Dictionary that contains all valid characters. Override it if you want to use additional/less characters

sensitive: boolean = false

Toggle if the class should be case sensitive

Methods

  • Looks up an index in the dictionary based on the given character

    Parameters

    • character: string

      The character you want to find in the dictionary

    • dictionary: string

    Returns number

    the index of the character in the dictionary

  • Filter out any characters that are not in the dictionary.

    Parameters

    • dictionary: string

    Returns ((character) => boolean)

    True if the character is in the dictionary

      • (character): boolean
      • Parameters

        • character: string

        Returns boolean

  • Generates a check character for the input string.

    Example

    Luhn.generate('foo') // -> {phrase: 'foo', checksum: '5'}
    Luhn.generate('FoO') // -> {phrase: 'foo', checksum: '5'}
    Luhn.generate('FoO', true) // -> {phrase: 'FoO', checksum: 'n'}

    Parameters

    • input: string

      The string that should have a check character

    • Optional sensitive: boolean

      Toggle if the function should be case-sensitive or not.

    Returns {
        checksum: string;
        phrase: string;
    }

    The filtered string and checksum character

    • checksum: string
    • phrase: string
  • Checks that the dictionary complies with the requirements, and returns the N value.

    Parameters

    • dictionary: string

    Returns number

    Length of the dictionary

  • Looks up a character in the dictionary based on it's index

    Parameters

    • codePoint: number

      The index you want to find in the dictionary

    • dictionary: string

    Returns string

    The character in the dictionary, at the given index

  • Parameters

    • dictionary: string

    Returns string

  • Higher-order function that returns a reducer that calculates the checksum

    Parameters

    • factor: 2 | 1
    • dictionary: string

    Returns ((sum, current) => number)

      • (sum, current): number
      • Reducer function that is applied on an array of characters in order to calculate a checksum.

        Parameters

        • sum: number

          current sum of the checksum

        • current: string

          current character that is being calculated

        Returns number

        the checksum

  • Validates if the given string, matches the calculated checksum. The last character in the string is considered the checksum.

    Example

    Luhn.validate('foo5') // -> {phrase: 'foo5', isValid: true}
    Luhn.validate('FoO5') // -> {phrase: 'foo5', isValid: true}
    Luhn.validate('FoOö5') // -> {phrase: 'foo5', isValid: true}
    Luhn.validate('FoO5', true) // -> {phrase: 'FoO5', isValid: false}

    Parameters

    • input: string

      The string that you want to check, including the check character in the end of the string

    • Optional sensitive: boolean

      Toggle if the function should be case-sensitive or not.

    Returns {
        isValid: boolean;
        phrase: string;
    }

    returns if the string is valid or not.

    • isValid: boolean
    • phrase: string

Generated using TypeDoc