@@ -83,6 +83,14 @@ const randomString = length => {
83
83
84
84
}
85
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
+
86
94
const random1011 = COEF => {
87
95
88
96
const
@@ -98,33 +106,6 @@ module.exports = {
98
106
digit,
99
107
scalarProduct,
100
108
101
- isSNILS : str => {
102
-
103
- if ( str . length === 14 ) {
104
-
105
- if ( str . charCodeAt ( 3 ) !== 45 ) die ( 'Wrong format' , { code : 'format' , pos : 3 } )
106
- if ( str . charCodeAt ( 7 ) !== 45 ) die ( 'Wrong format' , { code : 'format' , pos : 7 } )
107
- if ( str . charCodeAt ( 11 ) !== 32 ) die ( 'Wrong format' , { code : 'format' , pos : 11 } )
108
-
109
- str = str . slice ( 0 , 3 ) + str . slice ( 4 , 7 ) + str . slice ( 8 , 11 ) + str . slice ( 12 )
110
-
111
- }
112
-
113
- const tobe = scalarProduct ( COEF_SNILS , str ) % 101 % 100 , asis = 10 * digit ( str , 9 ) + digit ( str , 10 )
114
-
115
- if ( tobe !== asis ) die ( 'Wrong checksum' , { code : 'checksum' , tobe, asis} )
116
-
117
- } ,
118
-
119
- isINN10 : str => {
120
-
121
- const tobe = scalarProduct ( COEF_INN_10 , str ) % 11 % 10 , asis = digit ( str , 9 )
122
-
123
- if ( tobe !== asis ) die ( 'Wrong checksum' , { code : 'checksum' , tobe, asis} )
124
-
125
- } ,
126
-
127
-
128
109
isINN12 : str => {
129
110
130
111
{
@@ -145,39 +126,36 @@ module.exports = {
145
126
146
127
} ,
147
128
148
- isOGRN13 : str => {
149
-
150
- const tobe = scalarProduct ( COEF_OGRN_13 , str ) % 11 % 10 , asis = digit ( str , 12 )
151
-
152
- if ( tobe !== asis ) die ( 'Wrong checksum' , { code : 'checksum' , tobe, asis} )
153
-
154
- } ,
129
+ isKPP : str => { scalarProduct ( COEF_KPP , str ) } ,
130
+ randomKPP : ( ) => randomString ( COEF_KPP . length ) ,
155
131
132
+ isINN10 : str => is1011 ( str , COEF_INN_10 ) ,
133
+ randomINN10 : ( ) => random1011 ( COEF_INN_10 ) ,
156
134
157
- isOGRN15 : str => {
158
-
159
- const tobe = scalarProduct ( COEF_OGRN_15 , str ) % 11 % 10 , asis = digit ( str , 14 )
160
-
161
- if ( tobe !== asis ) die ( 'Wrong checksum' , { code : 'checksum' , tobe , asis } )
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 ) ,
162
140
163
- } ,
141
+ isSNILS : str => {
164
142
165
- randomKPP : ( ) => {
143
+ if ( str . length === 14 ) {
166
144
167
- return randomString ( COEF_KPP . length )
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 } )
168
148
169
- } ,
149
+ str = str . slice ( 0 , 3 ) + str . slice ( 4 , 7 ) + str . slice ( 8 , 11 ) + str . slice ( 12 )
170
150
171
- isKPP : str => {
151
+ }
172
152
173
- scalarProduct ( COEF_KPP , str )
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} )
174
156
175
157
} ,
176
158
177
- randomINN10 : ( ) => random1011 ( COEF_INN_10 ) ,
178
- randomOGRN13 : ( ) => random1011 ( COEF_OGRN_13 ) ,
179
- randomOGRN15 : ( ) => random1011 ( COEF_OGRN_15 ) ,
180
-
181
159
randomSNILS : ( options = { } ) => {
182
160
183
161
const
0 commit comments