Skip to content

Commit 4e06fa6

Browse files
author
Dmitry Ovsyanko
committed
is1011 extracted
1 parent e4ebc2d commit 4e06fa6

File tree

1 file changed

+27
-49
lines changed

1 file changed

+27
-49
lines changed

index.js

+27-49
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ const randomString = length => {
8383

8484
}
8585

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+
8694
const random1011 = COEF => {
8795

8896
const
@@ -98,33 +106,6 @@ module.exports = {
98106
digit,
99107
scalarProduct,
100108

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-
128109
isINN12: str => {
129110

130111
{
@@ -145,39 +126,36 @@ module.exports = {
145126

146127
},
147128

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),
155131

132+
isINN10: str => is1011 (str, COEF_INN_10),
133+
randomINN10: () => random1011 (COEF_INN_10),
156134

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),
162140

163-
},
141+
isSNILS: str => {
164142

165-
randomKPP: () => {
143+
if (str.length === 14) {
166144

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})
168148

169-
},
149+
str = str.slice (0, 3) + str.slice (4, 7) + str.slice (8, 11) + str.slice (12)
170150

171-
isKPP: str => {
151+
}
172152

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})
174156

175157
},
176158

177-
randomINN10: () => random1011 (COEF_INN_10),
178-
randomOGRN13: () => random1011 (COEF_OGRN_13),
179-
randomOGRN15: () => random1011 (COEF_OGRN_15),
180-
181159
randomSNILS: (options = {}) => {
182160

183161
const

0 commit comments

Comments
 (0)