Skip to content

Commit e29a07d

Browse files
author
Dmitry Ovsyanko
committed
+Luhn
1 parent 265fd09 commit e29a07d

File tree

5 files changed

+49
-3
lines changed

5 files changed

+49
-3
lines changed

__tests__/isBankCard.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const {isBankCard} = require ('..')
2+
3+
test ('basic', () => {
4+
5+
expect (isBankCard ('5062821234567892')).toBeUndefined ()
6+
expect (() => isBankCard ('5062821734567892')).toThrow ()
7+
8+
9+
})

index.js

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const {OGRN_13, OGRN_15} = require ('./lib/Horner')
33
const {INN_10, INN_12_2, INN_12_1} = require ('./lib/INN')
44
const SNILS = require ('./lib/SNILS')
55
const BankAcct = require ('./lib/BankAcct')
6+
const Luhn = require ('./lib/Luhn')
67
const {OKPO_8, OKPO_10} = require ('./lib/OKPO')
78
class KPP extends Check {constructor () {super (9)}}
89

@@ -38,4 +39,6 @@ module.exports = {
3839
isBankAcct : (str, bic) => new BankAcct ().verify (str, bic),
3940
randomBankAcct: (bic, opt) => new BankAcct ().random (bic, opt),
4041

42+
isBankCard : str => new Luhn (16).verify (str)
43+
4144
}

lib/Luhn.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const Check = require ('./Check')
2+
3+
module.exports = class extends Check {
4+
5+
constructor (totalLength) {
6+
7+
super (totalLength, 1)
8+
9+
this.modulus1 = 10
10+
11+
}
12+
13+
process (str) {
14+
15+
this.isToDouble = (this.totalLength & 1) === 0
16+
17+
super.process (str)
18+
19+
this.sum = 10 - (this.sum % 10)
20+
21+
}
22+
23+
processDigit (pos, digit) {
24+
25+
if (this.isToDouble) digit <<= 1
26+
27+
if (digit > 9) digit -= 9
28+
29+
this.sum += digit
30+
31+
this.isToDouble = !this.isToDouble
32+
}
33+
34+
}

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ru-codes",
3-
"version": "1.0.4",
3+
"version": "1.0.5",
44
"description": "ИНН, КПП и т. п.",
55
"main": "index.js",
66
"files": ["/lib", "/badges"],

0 commit comments

Comments
 (0)