Skip to content

Commit fe7942a

Browse files
author
Dmitry Ovsyanko
committed
+randomINN12
1 parent 4e06fa6 commit fe7942a

File tree

3 files changed

+65
-61
lines changed

3 files changed

+65
-61
lines changed

__tests__/isINN12.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
const {isINN12} = require ('..')
1+
const {isINN12, randomINN12} = require ('..')
22

33
test ('basic', () => {
44

55
expect (() => isINN12 ('423543534553')).toThrow ()
66
expect (() => isINN12 ('635277570473')).toThrow ()
77

88
expect (isINN12 ('635277570478')).toBeUndefined ()
9+
expect (isINN12 (randomINN12 ())).toBeUndefined ()
910

1011
})

__tests__/isKPP.js

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const {isKPP, randomKPP} = require ('..')
33
test ('basic', () => {
44

55
expect (() => isKPP ('7714010011')).toThrow ()
6+
expect (() => isKPP ('7714010O1')).toThrow ()
67

78
expect (isKPP ('771401001')).toBeUndefined ()
89

index.js

+62-60
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,3 @@
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-
401
const die = (s, o) => {
412

423
const err = Error (s)
@@ -91,7 +52,7 @@ const is1011 = (str, COEF) => {
9152

9253
}
9354

94-
const random1011 = COEF => {
55+
const random1110 = COEF => {
9556

9657
const
9758
length = COEF.length - 1,
@@ -101,12 +62,19 @@ const random1011 = COEF => {
10162

10263
}
10364

104-
module.exports = {
65+
const ex = {
10566

10667
digit,
10768
scalarProduct,
10869

109-
isINN12: str => {
70+
}
71+
72+
{
73+
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])
76+
77+
ex.isINN12 = str => {
11078

11179
{
11280

@@ -124,27 +92,32 @@ module.exports = {
12492

12593
}
12694

127-
},
95+
}
96+
97+
ex.randomINN12 = () => {
12898

129-
isKPP: str => {scalarProduct (COEF_KPP, str)},
130-
randomKPP: () => randomString (COEF_KPP.length),
99+
let no = randomString (10)
100+
no += scalarProduct (COEF_INN_12_1.slice (0, 10), no) % 11 % 10
101+
no += scalarProduct (COEF_INN_12_2.slice (0, 11), no) % 11 % 10
102+
return no
131103

132-
isINN10: str => is1011 (str, COEF_INN_10),
133-
randomINN10: () => random1011 (COEF_INN_10),
104+
}
105+
106+
}
134107

135-
isOGRN13: str => is1011 (str, COEF_OGRN_13),
136-
randomOGRN13: () => random1011 (COEF_OGRN_13),
108+
{
137109

138-
isOGRN15: str => is1011 (str, COEF_OGRN_15),
139-
randomOGRN15: () => random1011 (COEF_OGRN_15),
140-
141-
isSNILS: str => {
110+
const COEF_SNILS = new Uint8Array ([9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0])
111+
const CH_HYPHEN = '-'.charCodeAt (0)
112+
const CH_SPACE = ' '.charCodeAt (0)
113+
114+
ex.isSNILS = str => {
142115

143116
if (str.length === 14) {
144117

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})
118+
for (const [pos, code] of [[3, CH_HYPHEN], [7, CH_HYPHEN], [11, CH_SPACE]])
119+
if (str.charCodeAt (pos) !== code)
120+
die ('Wrong format', {code: 'format', pos})
148121

149122
str = str.slice (0, 3) + str.slice (4, 7) + str.slice (8, 11) + str.slice (12)
150123

@@ -154,9 +127,9 @@ module.exports = {
154127

155128
if (tobe !== asis) die ('Wrong checksum', {code: 'checksum', tobe, asis})
156129

157-
},
130+
}
158131

159-
randomSNILS: (options = {}) => {
132+
ex.randomSNILS = (options = {}) => {
160133

161134
const
162135
length = COEF_SNILS.length - 2,
@@ -166,6 +139,35 @@ module.exports = {
166139

167140
return !options.format ? result : result.slice (0, 3) + '-' + result.slice (3, 6) + '-' + result.slice (6, 9) + ' ' + result.slice (9)
168141

169-
},
142+
}
143+
144+
}
145+
146+
{
147+
148+
const COEF_OGRN_15 = [1e13, 1e12, 1e11, 1e10, 1e9, 1e8, 1e7, 1e6, 1e5, 1e4, 1e3, 100, 10, 1, 0]
149+
150+
ex.isOGRN15 = str => is1011 (str, COEF_OGRN_15)
151+
ex.randomOGRN15 = () => random1110 (COEF_OGRN_15)
152+
153+
{
154+
const COEF_OGRN_13 = COEF_OGRN_15.slice (2)
155+
ex.isOGRN13 = str => is1011 (str, COEF_OGRN_13)
156+
ex.randomOGRN13 = () => random1110 (COEF_OGRN_13)
157+
}
158+
159+
}
160+
161+
{
162+
const COEF_INN_10 = new Uint8Array ([2, 4, 10, 3, 5, 9, 4, 6, 8, 0])
163+
ex.isINN10 = str => is1011 (str, COEF_INN_10)
164+
ex.randomINN10 = () => random1110 (COEF_INN_10)
165+
}
166+
167+
{
168+
const COEF_KPP = new Uint8Array ([0, 0, 0, 0, 0, 0, 0, 0, 0])
169+
ex.isKPP = str => {scalarProduct (COEF_KPP, str)}
170+
ex.randomKPP = () => randomString (COEF_KPP.length)
171+
}
170172

171-
}
173+
module.exports = ex

0 commit comments

Comments
 (0)