Skip to content

Commit c680f46

Browse files
author
Dmitry Ovsyanko
committed
random +options.pre
1 parent 0d2b4ac commit c680f46

File tree

7 files changed

+24
-20
lines changed

7 files changed

+24
-20
lines changed

__tests__/isKPP.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ test ('basic', () => {
88
expect (() => isKPP ('7714010?1')).toThrow ()
99

1010
expect (isKPP ('771401001')).toBeUndefined ()
11+
12+
expect (isKPP (randomKPP ())).toBeUndefined ()
13+
expect (randomKPP ({pre: ['77', '50']})).toMatch (/^(50|77)/)
1114

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

1416
})

index.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,25 @@ class KPP extends Check {constructor () {super (9)}}
88
module.exports = {
99

1010
isSNILS : str => new SNILS ().verify (str),
11-
randomSNILS : options => new SNILS ().random (options),
11+
randomSNILS : opt => new SNILS ().random (opt),
1212

1313
isOGRN15 : str => new OGRN_15 ().verify (str),
14-
randomOGRN15: () => new OGRN_15 ().random (),
14+
randomOGRN15: opt => new OGRN_15 ().random (opt),
1515

1616
isOGRN13 : str => new OGRN_13 ().verify (str),
17-
randomOGRN13: () => new OGRN_13 ().random (),
17+
randomOGRN13: opt => new OGRN_13 ().random (opt),
1818

1919
isINN10 : str => new INN_10 ().verify (str),
20-
randomINN10 : () => new INN_10 ().random (),
20+
randomINN10 : opt => new INN_10 ().random (opt),
2121

2222
isKPP : str => new KPP ().verify (str),
23-
randomKPP : () => new KPP ().randomValue (),
23+
randomKPP : opt => new KPP ().randomValue (opt),
2424

2525
isOKPO8 : str => new OKPO_8 ().verify (str),
26-
randomOKPO8: () => new OKPO_8 ().random (),
26+
randomOKPO8: opt => new OKPO_8 ().random (opt),
2727

2828
isOKPO10 : str => new OKPO_10 ().verify (str),
29-
randomOKPO10: () => new OKPO_10 ().random (),
29+
randomOKPO10: opt => new OKPO_10 ().random (opt),
3030

3131
isINN12: str => {
3232
new INN_12_1 ().verify (str.slice (0, 11))

lib/Check.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,15 @@ module.exports = class {
4141

4242
}
4343

44-
randomValue () {
44+
randomValue (options = {}) {
4545

46-
const {valueLength} = this, b = Buffer.alloc (this.valueLength)
46+
const {pre} = options, prefix = pre ? pre [Math.floor (pre.length * Math.random ())] : ''
47+
48+
const length = this.valueLength - prefix.length, b = Buffer.alloc (length)
4749

48-
for (let i = 0; i < valueLength; i ++) b [i] = 48 + Math.floor (10 * Math.random ())
50+
for (let i = 0; i < length; i ++) b [i] = 48 + Math.floor (10 * Math.random ())
4951

50-
return b.toString ()
52+
return prefix + b.toString ()
5153

5254
}
5355

@@ -95,9 +97,9 @@ module.exports = class {
9597

9698
}
9799

98-
random () {
100+
random (options = {}) {
99101

100-
const v = this.randomValue ()
102+
const v = this.randomValue (options)
101103

102104
return this.appendCheckSum (v)
103105

lib/OKPO.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ class OKPO extends ScalarProduct {
2929

3030
}
3131

32-
random () {
32+
random (options) {
3333

3434
while (true) {
3535

36-
const v = super.random ()
36+
const v = super.random (options)
3737

3838
if (v.charCodeAt (0) !== CH_ZERO) return v
3939

lib/SNILS.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ module.exports = class extends ScalarProduct {
2626

2727
random (options = {}) {
2828

29-
const result = super.random ()
29+
const result = super.random (options)
3030

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

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.3",
3+
"version": "1.0.4",
44
"description": "ИНН, КПП и т. п.",
55
"main": "index.js",
66
"files": ["/lib", "/badges"],

0 commit comments

Comments
 (0)