Skip to content

Commit 5e99b33

Browse files
author
Dmitry Ovsyanko
committed
OKPO +leading zeroes
1 parent c3494e5 commit 5e99b33

10 files changed

+45
-13
lines changed

__tests__/isINN10.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ test ('basic', () => {
77
expect (isINN10 ('1111111117')).toBeUndefined ()
88
expect (isINN10 ('6449013711')).toBeUndefined ()
99

10-
expect (isINN10 (randomINN10 ())).toBeUndefined ()
10+
for (let i = 0; i < 1000; i ++) expect (isINN10 (randomINN10 ())).toBeUndefined ()
1111

1212
})

__tests__/isINN12.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ test ('basic', () => {
66
expect (() => isINN12 ('635277570473')).toThrow ()
77

88
expect (isINN12 ('635277570478')).toBeUndefined ()
9-
expect (isINN12 (randomINN12 ())).toBeUndefined ()
9+
for (let i = 0; i < 1000; i ++) expect (isINN12 (randomINN12 ())).toBeUndefined ()
1010

1111
})

__tests__/isKPP.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ test ('basic', () => {
99

1010
expect (isKPP ('771401001')).toBeUndefined ()
1111

12-
expect (isKPP (randomKPP ())).toBeUndefined ()
12+
for (let i = 0; i < 1000; i ++) expect (isKPP (randomKPP ())).toBeUndefined ()
1313

1414
})

__tests__/isOGRN13.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ test ('basic', () => {
77
expect (() => isOGRN13 ('1027700280938')).toThrow ()
88

99
expect (isOGRN13 ('1027700280937')).toBeUndefined ()
10-
expect (isOGRN13 (randomOGRN13 ())).toBeUndefined ()
10+
for (let i = 0; i < 1000; i ++) expect (isOGRN13 (randomOGRN13 ())).toBeUndefined ()
1111

1212
})

__tests__/isOGRN15.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ test ('basic', () => {
55
expect (() => isOGRN15 ('304500116000154')).toThrow ()
66

77
expect (isOGRN15 ('304500116000157')).toBeUndefined ()
8-
expect (isOGRN15 (randomOGRN15 ())).toBeUndefined ()
8+
for (let i = 0; i < 1000; i ++) expect (isOGRN15 (randomOGRN15 ())).toBeUndefined ()
99

1010
})

__tests__/isOKPO10.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ const {isOKPO10, randomOKPO10} = require ('..')
33
test ('basic', () => {
44

55
expect (() => isOKPO8 ('1194265277')).toThrow ()
6-
expect (() => isOKPO8 ('194265277')).toThrow ()
76

7+
expect (isOKPO10 ('194265277')).toBeUndefined ()
88
expect (isOKPO10 ('0194265277')).toBeUndefined ()
99

10-
expect (isOKPO10 (randomOKPO10 ())).toBeUndefined ()
10+
for (let i = 0; i < 1000; i ++) expect (isOKPO10 (randomOKPO10 ())).toBeUndefined ()
1111

1212
})

__tests__/isOKPO8.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const {isOKPO8, randomOKPO8} = require ('..')
22

3+
// 7675829...?!
4+
35
test ('basic', () => {
46

57
expect (() => isOKPO8 ('1111111111')).toThrow ()
@@ -8,6 +10,12 @@ test ('basic', () => {
810

911
expect (isOKPO8 ('60419873')).toBeUndefined ()
1012

11-
expect (isOKPO8 (randomOKPO8 ())).toBeUndefined ()
13+
for (let i = 0; i < 1000; i ++) {
14+
15+
const v = randomOKPO8 ()
16+
17+
expect (isOKPO8 (v)).toBeUndefined ()
18+
19+
}
1220

1321
})

__tests__/isSNILS.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ test ('basic', () => {
1010
expect (isSNILS ('58659591780')).toBeUndefined ()
1111
expect (isSNILS ('586-595-917 80')).toBeUndefined ()
1212

13-
for (let i = 0; i < 100; i ++) {
13+
for (let i = 0; i < 1000; i ++) {
1414
const snils = randomSNILS ()
1515
expect (snils).toHaveLength (11)
1616
expect (isSNILS (snils)).toBeUndefined ()
1717
}
1818

19-
for (let i = 0; i < 100; i ++) {
19+
for (let i = 0; i < 1000; i ++) {
2020
const snils = randomSNILS ({format: true})
2121
expect (snils).toHaveLength (14)
2222
expect (isSNILS (snils)).toBeUndefined ()

lib/Check.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ module.exports = class {
77
this.totalLength = totalLength
88

99
this.modulus2 = 10; for (let i = 1; i < checkSumLength; i ++) this.modulus2 *= 10
10-
this.modulus1 = this.modulus2 + 1
11-
this.sum = 0
10+
this.modulus1 = this.modulus2 + 1
1211

1312
}
1413

@@ -30,7 +29,7 @@ module.exports = class {
3029

3130
process (str) {
3231

33-
const {valueLength} = this; for (let i = 0; i < valueLength; i ++) {
32+
this.sum = 0; const {valueLength} = this; for (let i = 0; i < valueLength; i ++) {
3433

3534
let dec = str.charCodeAt (i); if ((dec & 16) !== 16) this.raise ('Not a digit', {code: 'char', pos: i, value: str.charAt (i)})
3635

lib/OKPO.js

+25
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const ScalarProduct = require ('./ScalarProduct')
2+
const CH_ZERO = '0'.charCodeAt (0)
23

34
const COEF = new Uint8Array ([1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9])
45

@@ -12,6 +13,12 @@ class OKPO extends ScalarProduct {
1213

1314
}
1415

16+
verify (str) {
17+
18+
super.verify (str.padStart (this.totalLength, '0'))
19+
20+
}
21+
1522
adjustSum (sum) {
1623

1724
const {scalarProduct2} = this
@@ -22,6 +29,24 @@ class OKPO extends ScalarProduct {
2229

2330
}
2431

32+
random () {
33+
34+
while (true) {
35+
36+
const v = super.random ()
37+
38+
if (v.length !== this.totalLength) continue
39+
40+
if (v.charCodeAt (0) !== CH_ZERO) return v
41+
42+
if (v.charCodeAt (1) !== CH_ZERO) return v.slice (1)
43+
44+
this.sum = 0
45+
46+
}
47+
48+
}
49+
2550
}
2651

2752
const okpo = valueLength => class extends OKPO {constructor () {super (valueLength)}}

0 commit comments

Comments
 (0)