Skip to content

Commit 5591554

Browse files
author
Dmitry Ovsyanko
committed
+mustBeEqual
1 parent fa91288 commit 5591554

File tree

4 files changed

+22
-18
lines changed

4 files changed

+22
-18
lines changed

index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module.exports = {
1818
isINN10 : str => new INN_10 ().verify (str),
1919
randomINN10 : () => new INN_10 ().random (),
2020

21-
isKPP : str => new KPP ().process (str),
21+
isKPP : str => new KPP ().verify (str),
2222
randomKPP : () => new KPP ().randomValue (),
2323

2424
isINN12: str => {

lib/Check.js

+18-14
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,15 @@ module.exports = class {
2424

2525
}
2626

27-
process (str, checkAllDigits) {
27+
process (str) {
2828

29-
if (typeof str !== 'string') this.raise ('Arg 1 must be string', {code: 'type', type: typeof str})
30-
31-
const length = checkAllDigits ? this.totalLength : this.valueLength
32-
33-
if (length !== str.length) this.raise ('Invalid length', {code: 'length', tobe: length, asis: str.length})
34-
35-
for (let i = 0; i < length; i ++) {
29+
const {valueLength} = this; for (let i = 0; i < valueLength; i ++) {
3630

3731
let dec = str.charCodeAt (i); if ((dec & 16) !== 16) this.raise ('Not a digit', {code: 'char', pos: i, value: str.charAt (i)})
3832

3933
dec &= 15; if (dec > 9) this.raise ('Not a digit', {code: 'char', pos: i, value: str.charAt (i)})
4034

41-
if (i < this.valueLength) this.processDigit (i, dec)
35+
this.processDigit (i, dec)
4236

4337
}
4438

@@ -54,11 +48,11 @@ module.exports = class {
5448

5549
}
5650

57-
checkSum (str, checkAllDigits = false) {
51+
checkSum (str) {
5852

5953
this.sum = 0
6054

61-
this.process (str, checkAllDigits)
55+
this.process (str)
6256

6357
const {sum} = this; delete this.sum
6458

@@ -68,13 +62,23 @@ module.exports = class {
6862

6963
}
7064

65+
mustBeEqual (asis, tobe, code) {
66+
67+
if (tobe === asis) return
68+
69+
this.raise ('Invalid ' + code, {code, tobe, asis})
70+
71+
}
72+
7173
verify (str) {
7274

73-
const tobe = this.checkSum (str, true), asis = str.slice (this.valueLength)
75+
this.mustBeEqual (typeof str, 'string', 'type')
7476

75-
if (tobe === asis) return
77+
this.mustBeEqual (str.length, this.totalLength, 'length')
78+
79+
const checkSum = this.checkSum (str); if (this.checkSumLength === 0) return
7680

77-
this.raise ('Wrong checksum', {code: 'checksum', tobe, asis})
81+
this.mustBeEqual (str.slice (this.valueLength), checkSum, 'checksum')
7882

7983
}
8084

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.1",
3+
"version": "1.0.2",
44
"description": "ИНН, КПП и т. п.",
55
"main": "index.js",
66
"files": ["/lib", "/badges"],

0 commit comments

Comments
 (0)