Skip to content

Commit 9bccc4e

Browse files
author
Dmitry Ovsyanko
committed
OKPO8
1 parent 9d34fa0 commit 9bccc4e

File tree

7 files changed

+69
-10
lines changed

7 files changed

+69
-10
lines changed

__tests__/isOKPO8.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const {isOKPO8, randomOKPO8} = require ('..')
2+
3+
test ('basic', () => {
4+
5+
expect (() => isOKPO8 ('1111111111')).toThrow ()
6+
7+
expect (isOKPO8 ('47296611')).toBeUndefined ()
8+
9+
expect (isOKPO8 ('60419873')).toBeUndefined ()
10+
11+
expect (isOKPO8 (randomOKPO8 ())).toBeUndefined ()
12+
13+
})

index.js

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const Check = require ('./lib/Check')
22
const {OGRN_13, OGRN_15} = require ('./lib/Horner')
33
const {INN_10, INN_12_2, INN_12_1} = require ('./lib/INN')
44
const SNILS = require ('./lib/SNILS')
5+
const {OKPO_8} = require ('./lib/OKPO')
56
class KPP extends Check {constructor () {super (9)}}
67

78
module.exports = {
@@ -21,6 +22,9 @@ module.exports = {
2122
isKPP : str => new KPP ().verify (str),
2223
randomKPP : () => new KPP ().randomValue (),
2324

25+
isOKPO8 : str => new OKPO_8 ().verify (str),
26+
randomOKPO8: () => new OKPO_8 ().random (),
27+
2428
isINN12: str => {
2529
new INN_12_1 ().verify (str.slice (0, 11))
2630
new INN_12_2 ().verify (str)

lib/Check.js

+14-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ module.exports = class {
66
this.checkSumLength = checkSumLength
77
this.totalLength = totalLength
88

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

1113
}
1214

@@ -48,17 +50,23 @@ module.exports = class {
4850

4951
return b.toString ()
5052

51-
}
53+
}
5254

53-
checkSum (str) {
55+
adjustSum () {
5456

55-
this.sum = 0
57+
this.sum %= this.modulus2
58+
59+
}
60+
61+
checkSum (str) {
5662

5763
this.process (str)
5864

59-
const {sum, modulus} = this; delete this.sum
65+
this.sum %= this.modulus1
66+
67+
if (this.sum >= this.modulus2) this.adjustSum (str)
6068

61-
return String (sum % (modulus + 1) % modulus).padStart (this.checkSumLength, '0')
69+
return String (this.sum).padStart (this.checkSumLength, '0')
6270

6371
}
6472

lib/Horner.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Horner extends Check {
1818

1919
}
2020

21-
const horner = length => class OGRN_15 extends Horner {constructor () {super (length)}}
21+
const horner = length => class extends Horner {constructor () {super (length)}}
2222

2323
module.exports = {
2424
OGRN_13: horner (13),

lib/OKPO.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const ScalarProduct = require ('./ScalarProduct')
2+
3+
const COEF = new Uint8Array ([1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9])
4+
5+
class OKPO extends ScalarProduct {
6+
7+
constructor (valueLength) {
8+
9+
super (COEF.slice (0, valueLength))
10+
11+
this.scalarProduct2 = new ScalarProduct (COEF.slice (2, valueLength + 2))
12+
13+
}
14+
15+
adjustSum (sum) {
16+
17+
const {scalarProduct2} = this
18+
19+
scalarProduct2.process (sum)
20+
21+
this.sum = scalarProduct2.sum % this.modulus1
22+
23+
}
24+
25+
}
26+
27+
28+
const okpo = valueLength => class extends OKPO {constructor () {super (valueLength)}}
29+
30+
module.exports = {
31+
32+
OKPO_8 : okpo (7),
33+
34+
}

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ru-codes",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"description": "ИНН, КПП и т. п.",
55
"main": "index.js",
66
"files": ["/lib", "/badges"],

0 commit comments

Comments
 (0)