1
- const COEF_SNILS = new Uint8Array ( [ 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 , 0 , 0 ] )
2
- const COEF_INN_10 = new Uint8Array ( [ 2 , 4 , 10 , 3 , 5 , 9 , 4 , 6 , 8 , 0 ] )
3
- const COEF_KPP = new Uint8Array ( [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] )
4
- const COEF_INN_12_1 = new Uint8Array ( [ 7 , 2 , 4 , 10 , 3 , 5 , 9 , 4 , 6 , 8 , 0 , 0 ] )
5
- const COEF_INN_12_2 = new Uint8Array ( [ 3 , 7 , 2 , 4 , 10 , 3 , 5 , 9 , 4 , 6 , 8 , 0 ] )
6
- const COEF_OGRN_13 = [
7
- 100000000000 ,
8
- 10000000000 ,
9
- 1000000000 ,
10
- 100000000 ,
11
- 10000000 ,
12
- 1000000 ,
13
- 100000 ,
14
- 10000 ,
15
- 1000 ,
16
- 100 ,
17
- 10 ,
18
- 1 ,
19
- 0
20
- ]
21
-
22
- const COEF_OGRN_15 = [
23
- 10000000000000 ,
24
- 1000000000000 ,
25
- 100000000000 ,
26
- 10000000000 ,
27
- 1000000000 ,
28
- 100000000 ,
29
- 10000000 ,
30
- 1000000 ,
31
- 100000 ,
32
- 10000 ,
33
- 1000 ,
34
- 100 ,
35
- 10 ,
36
- 1 ,
37
- 0
38
- ]
39
-
40
- const die = ( s , o ) => {
41
-
42
- const err = Error ( s )
43
-
44
- for ( const k in o ) err [ k ] = o [ k ]
45
-
46
- throw err
47
-
48
- }
49
-
50
- const digit = ( str , pos ) => str . charCodeAt ( pos ) & 15
51
-
52
- const scalarProduct = ( coef , str ) => {
53
-
54
- if ( typeof str !== 'string' ) die ( 'Arg 1 must be string' , { code : 'type' , type : typeof str } )
55
-
56
- const { length} = coef ; if ( length !== str . length ) die ( 'Invalid length' , { code : 'length' , tobe : length , asis : str . length } )
57
-
58
- const buf = Buffer . from ( str )
59
-
60
- let s = 0 ; for ( let i = 0 ; i < length ; i ++ ) {
61
-
62
- let dec = buf [ i ] ; if ( ( dec & 16 ) !== 16 ) die ( 'Not a digit' , { code : 'char' , pos : i , value : str . charAt ( i ) } )
63
-
64
- dec &= 15 ; if ( dec > 9 ) die ( 'Not a digit' , { code : 'char' , pos : i , value : str . charAt ( i ) } )
65
-
66
- const k = coef [ i ] ; if ( k === 0 ) continue
67
-
68
- s += k * dec
69
-
70
- }
71
-
72
- return s
73
-
74
- }
75
-
76
- const randomString = length => {
77
-
78
- const b = Buffer . alloc ( length )
79
-
80
- for ( let i = 0 ; i < length ; i ++ ) b [ i ] = 48 + Math . floor ( 10 * Math . random ( ) )
81
-
82
- return b . toString ( )
83
-
84
- }
85
-
86
- const is1011 = ( str , COEF ) => {
87
-
88
- const tobe = scalarProduct ( COEF , str ) % 11 % 10 , asis = digit ( str , COEF . length - 1 )
89
-
90
- if ( tobe !== asis ) die ( 'Wrong checksum' , { code : 'checksum' , tobe, asis} )
91
-
92
- }
93
-
94
- const random1011 = COEF => {
95
-
96
- const
97
- length = COEF . length - 1 ,
98
- no = randomString ( length )
99
-
100
- return no + scalarProduct ( COEF . slice ( 0 , length ) , no ) % 11 % 10
101
-
102
- }
1
+ const Check = require ( './lib/Check' )
2
+ const { OGRN_13 , OGRN_15 } = require ( './lib/Horner' )
3
+ const { INN_10 , INN_12_2 , INN_12_1 } = require ( './lib/INN' )
4
+ const SNILS = require ( './lib/SNILS' )
5
+ class KPP extends Check { constructor ( ) { super ( 9 ) } }
103
6
104
7
module . exports = {
105
8
106
- digit ,
107
- scalarProduct ,
9
+ isSNILS : str => new SNILS ( ) . verify ( str ) ,
10
+ randomSNILS : options => new SNILS ( ) . random ( options ) ,
108
11
109
- isINN12 : str => {
110
-
111
- {
112
-
113
- const tobe = scalarProduct ( COEF_INN_12_1 , str ) % 11 % 10 , asis = digit ( str , 10 )
12
+ isOGRN15 : str => new OGRN_15 ( ) . verify ( str ) ,
13
+ randomOGRN15 : ( ) => new OGRN_15 ( ) . random ( ) ,
114
14
115
- if ( tobe !== asis ) die ( 'Wrong checksum' , { code : 'checksum' , tobe, asis, phase : 1 } )
116
-
117
- }
15
+ isOGRN13 : str => new OGRN_13 ( ) . verify ( str ) ,
16
+ randomOGRN13 : ( ) => new OGRN_13 ( ) . random ( ) ,
118
17
119
- {
18
+ isINN10 : str => new INN_10 ( ) . verify ( str ) ,
19
+ randomINN10 : ( ) => new INN_10 ( ) . random ( ) ,
120
20
121
- const tobe = scalarProduct ( COEF_INN_12_2 , str ) % 11 % 10 , asis = digit ( str , 11 )
122
-
123
- if ( tobe !== asis ) die ( 'Wrong checksum' , { code : 'checksum' , tobe, asis, phase : 2 } )
124
-
125
- }
126
-
127
- } ,
128
-
129
- isKPP : str => { scalarProduct ( COEF_KPP , str ) } ,
130
- randomKPP : ( ) => randomString ( COEF_KPP . length ) ,
131
-
132
- isINN10 : str => is1011 ( str , COEF_INN_10 ) ,
133
- randomINN10 : ( ) => random1011 ( COEF_INN_10 ) ,
134
-
135
- isOGRN13 : str => is1011 ( str , COEF_OGRN_13 ) ,
136
- randomOGRN13 : ( ) => random1011 ( COEF_OGRN_13 ) ,
137
-
138
- isOGRN15 : str => is1011 ( str , COEF_OGRN_15 ) ,
139
- randomOGRN15 : ( ) => random1011 ( COEF_OGRN_15 ) ,
140
-
141
- isSNILS : str => {
142
-
143
- if ( str . length === 14 ) {
144
-
145
- if ( str . charCodeAt ( 3 ) !== 45 ) die ( 'Wrong format' , { code : 'format' , pos : 3 } )
146
- if ( str . charCodeAt ( 7 ) !== 45 ) die ( 'Wrong format' , { code : 'format' , pos : 7 } )
147
- if ( str . charCodeAt ( 11 ) !== 32 ) die ( 'Wrong format' , { code : 'format' , pos : 11 } )
148
-
149
- str = str . slice ( 0 , 3 ) + str . slice ( 4 , 7 ) + str . slice ( 8 , 11 ) + str . slice ( 12 )
150
-
151
- }
152
-
153
- const tobe = scalarProduct ( COEF_SNILS , str ) % 101 % 100 , asis = 10 * digit ( str , 9 ) + digit ( str , 10 )
154
-
155
- if ( tobe !== asis ) die ( 'Wrong checksum' , { code : 'checksum' , tobe, asis} )
156
-
157
- } ,
158
-
159
- randomSNILS : ( options = { } ) => {
160
-
161
- const
162
- length = COEF_SNILS . length - 2 ,
163
- no = randomString ( length ) ,
164
- sum = scalarProduct ( COEF_SNILS . slice ( 0 , length ) , no ) % 101 % 100
165
- result = sum < 10 ? no + '0' + sum : no + sum
166
-
167
- return ! options . format ? result : result . slice ( 0 , 3 ) + '-' + result . slice ( 3 , 6 ) + '-' + result . slice ( 6 , 9 ) + ' ' + result . slice ( 9 )
21
+ isKPP : str => new KPP ( ) . process ( str ) ,
22
+ randomKPP : ( ) => new KPP ( ) . randomValue ( ) ,
168
23
24
+ isINN12 : str => {
25
+ new INN_12_1 ( ) . verify ( str . slice ( 0 , 11 ) )
26
+ new INN_12_2 ( ) . verify ( str )
169
27
} ,
28
+ randomINN12 : ( ) => new INN_12_2 ( ) . appendCheckSum ( new INN_12_1 ( ) . random ( ) )
170
29
171
30
}
0 commit comments