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
1
const die = ( s , o ) => {
41
2
42
3
const err = Error ( s )
@@ -83,15 +44,15 @@ const randomString = length => {
83
44
84
45
}
85
46
86
- const is1011 = ( str , COEF ) => {
47
+ const is1110 = ( str , COEF ) => {
87
48
88
49
const tobe = scalarProduct ( COEF , str ) % 11 % 10 , asis = digit ( str , COEF . length - 1 )
89
50
90
51
if ( tobe !== asis ) die ( 'Wrong checksum' , { code : 'checksum' , tobe, asis} )
91
52
92
53
}
93
54
94
- const random1011 = COEF => {
55
+ const random1110 = COEF => {
95
56
96
57
const
97
58
length = COEF . length - 1 ,
@@ -101,50 +62,49 @@ const random1011 = COEF => {
101
62
102
63
}
103
64
104
- module . exports = {
65
+ const ex = {
105
66
106
67
digit,
107
68
scalarProduct,
108
69
109
- isINN12 : str => {
110
-
111
- {
112
-
113
- const tobe = scalarProduct ( COEF_INN_12_1 , str ) % 11 % 10 , asis = digit ( str , 10 )
70
+ }
114
71
115
- if ( tobe !== asis ) die ( 'Wrong checksum' , { code : 'checksum' , tobe, asis, phase : 1 } )
116
-
117
- }
72
+ {
118
73
119
- {
120
-
121
- const tobe = scalarProduct ( COEF_INN_12_2 , str ) % 11 % 10 , asis = digit ( str , 11 )
74
+ const COEF_INN_12_1 = new Uint8Array ( [ 7 , 2 , 4 , 10 , 3 , 5 , 9 , 4 , 6 , 8 , 0 , 0 ] )
75
+ const COEF_INN_12_2 = new Uint8Array ( [ 3 , 7 , 2 , 4 , 10 , 3 , 5 , 9 , 4 , 6 , 8 , 0 ] )
122
76
123
- if ( tobe !== asis ) die ( 'Wrong checksum' , { code : 'checksum' , tobe, asis, phase : 2 } )
124
-
125
- }
126
-
127
- } ,
77
+ ex . isINN12 = str => {
78
+
79
+ is1110 ( str . slice ( 0 , 11 ) , COEF_INN_12_1 . slice ( 0 , 11 ) )
80
+ is1110 ( str , COEF_INN_12_2 )
128
81
129
- isKPP : str => { scalarProduct ( COEF_KPP , str ) } ,
130
- randomKPP : ( ) => randomString ( COEF_KPP . length ) ,
82
+ }
131
83
132
- isINN10 : str => is1011 ( str , COEF_INN_10 ) ,
133
- randomINN10 : ( ) => random1011 ( COEF_INN_10 ) ,
84
+ ex . randomINN12 = ( ) => {
134
85
135
- isOGRN13 : str => is1011 ( str , COEF_OGRN_13 ) ,
136
- randomOGRN13 : ( ) => random1011 ( COEF_OGRN_13 ) ,
86
+ let no = randomString ( 10 )
87
+ no += scalarProduct ( COEF_INN_12_1 . slice ( 0 , 10 ) , no ) % 11 % 10
88
+ no += scalarProduct ( COEF_INN_12_2 . slice ( 0 , 11 ) , no ) % 11 % 10
89
+ return no
137
90
138
- isOGRN15 : str => is1011 ( str , COEF_OGRN_15 ) ,
139
- randomOGRN15 : ( ) => random1011 ( COEF_OGRN_15 ) ,
140
-
141
- isSNILS : str => {
91
+ }
92
+
93
+ }
94
+
95
+ {
96
+
97
+ const COEF_SNILS = new Uint8Array ( [ 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 , 0 , 0 ] )
98
+ const CH_HYPHEN = '-' . charCodeAt ( 0 )
99
+ const CH_SPACE = ' ' . charCodeAt ( 0 )
100
+
101
+ ex . isSNILS = str => {
142
102
143
103
if ( str . length === 14 ) {
144
104
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 } )
105
+ for ( const [ pos , code ] of [ [ 3 , CH_HYPHEN ] , [ 7 , CH_HYPHEN ] , [ 11 , CH_SPACE ] ] )
106
+ if ( str . charCodeAt ( pos ) !== code )
107
+ die ( 'Wrong format' , { code : 'format' , pos} )
148
108
149
109
str = str . slice ( 0 , 3 ) + str . slice ( 4 , 7 ) + str . slice ( 8 , 11 ) + str . slice ( 12 )
150
110
@@ -154,9 +114,9 @@ module.exports = {
154
114
155
115
if ( tobe !== asis ) die ( 'Wrong checksum' , { code : 'checksum' , tobe, asis} )
156
116
157
- } ,
117
+ }
158
118
159
- randomSNILS : ( options = { } ) => {
119
+ ex . randomSNILS = ( options = { } ) => {
160
120
161
121
const
162
122
length = COEF_SNILS . length - 2 ,
@@ -166,6 +126,35 @@ module.exports = {
166
126
167
127
return ! options . format ? result : result . slice ( 0 , 3 ) + '-' + result . slice ( 3 , 6 ) + '-' + result . slice ( 6 , 9 ) + ' ' + result . slice ( 9 )
168
128
169
- } ,
129
+ }
130
+
131
+ }
132
+
133
+ {
134
+
135
+ const COEF_OGRN_15 = [ 1e13 , 1e12 , 1e11 , 1e10 , 1e9 , 1e8 , 1e7 , 1e6 , 1e5 , 1e4 , 1e3 , 100 , 10 , 1 , 0 ]
136
+
137
+ ex . isOGRN15 = str => is1110 ( str , COEF_OGRN_15 )
138
+ ex . randomOGRN15 = ( ) => random1110 ( COEF_OGRN_15 )
139
+
140
+ {
141
+ const COEF_OGRN_13 = COEF_OGRN_15 . slice ( 2 )
142
+ ex . isOGRN13 = str => is1110 ( str , COEF_OGRN_13 )
143
+ ex . randomOGRN13 = ( ) => random1110 ( COEF_OGRN_13 )
144
+ }
145
+
146
+ }
147
+
148
+ {
149
+ const COEF_INN_10 = new Uint8Array ( [ 2 , 4 , 10 , 3 , 5 , 9 , 4 , 6 , 8 , 0 ] )
150
+ ex . isINN10 = str => is1110 ( str , COEF_INN_10 )
151
+ ex . randomINN10 = ( ) => random1110 ( COEF_INN_10 )
152
+ }
153
+
154
+ {
155
+ const COEF_KPP = new Uint8Array ( [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] )
156
+ ex . isKPP = str => { scalarProduct ( COEF_KPP , str ) }
157
+ ex . randomKPP = ( ) => randomString ( COEF_KPP . length )
158
+ }
170
159
171
- }
160
+ module . exports = ex
0 commit comments