The Luhn Algorithm is a simple checksum formula used to validate a variety of identification numbers, such as credit card numbers, IMEI numbers, and others. This documentation provides an overview of the Luhn Algorithm implementation in PHP, including exa
npm install @designbycode/luhn-algorithmbash
pnpm add @designbycode/luhn-algorithm
`
#### Using npm
`bash
npm install @designbycode/luhn-algorithm
`
#### Using yarn
`bash
yarn add @designbycode/luhn-algorithm
`
Usage
To use the Luhn Algorithm plugin, simply import the LuhnAlgorithm class and call the desired function.
`typescript
import LuhnAlgorithm from '@designbycode/luhn-algorithm';
const number = "4532015112830366";
const isValid = LuhnAlgorithm.isValid(number);
console.log(isValid); // true
`
Functions
$3
isValid(number: string): boolean
The isValid function takes a string number as input and returns a boolean indicating whether the number is valid according to the Luhn algorithm.
`typescript
const number = "4532015112830366";
const isValid = LuhnAlgorithm.isValid(number);
console.log(isValid); // true
`
$3
generate(number: string): string
The generate function takes a string number as input and returns a new string with a check digit appended to the end. The check digit is calculated using the Luhn algorithm.
`typescript
const number = "45320151128303";
const generatedNumber = LuhnAlgorithm.generate(number);
console.log(generatedNumber); // "4532015112830366"
`
$3
getDigit(number: string): string
The getDigit function takes a string number as input and returns a string representing the check digit calculated using the Luhn algorithm.
`typescript
const number = "45320151128303";
const checkDigit = LuhnAlgorithm.getDigit(number);
console.log(checkDigit); // "6"
`
$3
withoutDigit(number: string): string
The withoutDigit function takes a string number as input and returns a new string with the last digit removed.
`typescript
const number = "4532015112830366";
const withoutDigit = LuhnAlgorithm.withoutDigit(number);
console.log(withoutDigit); // "45320151128303"
`
$3
validateAndSuggest(number: string): { isValid: boolean; suggestedDigit: string }
The validateAndSuggest function takes a string number as input and returns an object with two properties: isValid and suggestedDigit. The isValid property indicates whether the number is valid, according to the Luhn algorithm, and the
suggestedDigit property provides a suggested check digit if the number is invalid.
`typescript
const number = "45320151128303";
const result = LuhnAlgorithm.validateAndSuggest(number);
console.log(result); // { isValid: false, suggestedDigit: "6" }
``