Skip to content

Commit a7de99f

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

File tree

3 files changed

+67
-76
lines changed

3 files changed

+67
-76
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

+64-75
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)
@@ -83,15 +44,15 @@ const randomString = length => {
8344

8445
}
8546

86-
const is1011 = (str, COEF) => {
47+
const is1110 = (str, COEF) => {
8748

8849
const tobe = scalarProduct (COEF, str) % 11 % 10, asis = digit (str, COEF.length - 1)
8950

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

9253
}
9354

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

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

10263
}
10364

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

10667
digit,
10768
scalarProduct,
10869

109-
isINN12: str => {
110-
111-
{
112-
113-
const tobe = scalarProduct (COEF_INN_12_1, str) % 11 % 10, asis = digit (str, 10)
70+
}
11471

115-
if (tobe !== asis) die ('Wrong checksum', {code: 'checksum', tobe, asis, phase: 1})
116-
117-
}
72+
{
11873

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])
12276

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)
12881

129-
isKPP: str => {scalarProduct (COEF_KPP, str)},
130-
randomKPP: () => randomString (COEF_KPP.length),
82+
}
13183

132-
isINN10: str => is1011 (str, COEF_INN_10),
133-
randomINN10: () => random1011 (COEF_INN_10),
84+
ex.randomINN12 = () => {
13485

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
13790

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 => {
142102

143103
if (str.length === 14) {
144104

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

149109
str = str.slice (0, 3) + str.slice (4, 7) + str.slice (8, 11) + str.slice (12)
150110

@@ -154,9 +114,9 @@ module.exports = {
154114

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

157-
},
117+
}
158118

159-
randomSNILS: (options = {}) => {
119+
ex.randomSNILS = (options = {}) => {
160120

161121
const
162122
length = COEF_SNILS.length - 2,
@@ -166,6 +126,35 @@ module.exports = {
166126

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

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+
}
170159

171-
}
160+
module.exports = ex

0 commit comments

Comments
 (0)