@@ -24,21 +24,15 @@ module.exports = class {
24
24
25
25
}
26
26
27
- process ( str , checkAllDigits ) {
27
+ process ( str ) {
28
28
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 ++ ) {
36
30
37
31
let dec = str . charCodeAt ( i ) ; if ( ( dec & 16 ) !== 16 ) this . raise ( 'Not a digit' , { code : 'char' , pos : i , value : str . charAt ( i ) } )
38
32
39
33
dec &= 15 ; if ( dec > 9 ) this . raise ( 'Not a digit' , { code : 'char' , pos : i , value : str . charAt ( i ) } )
40
34
41
- if ( i < this . valueLength ) this . processDigit ( i , dec )
35
+ this . processDigit ( i , dec )
42
36
43
37
}
44
38
@@ -54,11 +48,11 @@ module.exports = class {
54
48
55
49
}
56
50
57
- checkSum ( str , checkAllDigits = false ) {
51
+ checkSum ( str ) {
58
52
59
53
this . sum = 0
60
54
61
- this . process ( str , checkAllDigits )
55
+ this . process ( str )
62
56
63
57
const { sum} = this ; delete this . sum
64
58
@@ -68,13 +62,23 @@ module.exports = class {
68
62
69
63
}
70
64
65
+ mustBeEqual ( asis , tobe , code ) {
66
+
67
+ if ( tobe === asis ) return
68
+
69
+ this . raise ( 'Invalid ' + code , { code, tobe, asis} )
70
+
71
+ }
72
+
71
73
verify ( str ) {
72
74
73
- const tobe = this . checkSum ( str , true ) , asis = str . slice ( this . valueLength )
75
+ this . mustBeEqual ( typeof str , 'string' , 'type' )
74
76
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
76
80
77
- this . raise ( 'Wrong checksum' , { code : 'checksum' , tobe , asis } )
81
+ this . mustBeEqual ( str . slice ( this . valueLength ) , checkSum , 'checksum' )
78
82
79
83
}
80
84
0 commit comments