Skip to content

Commit 0afabaa

Browse files
author
Dmitry Ovsyanko
committed
+isntCadNum
1 parent 4363f4b commit 0afabaa

5 files changed

Lines changed: 42 additions & 33 deletions

File tree

__tests__/isCadNum.js

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,36 @@
1-
const {isCadNum, randomCadNum} = require ('..')
1+
const {isCadNum, isntCadNum, randomCadNum} = require ('..')
22

33
const RE = /^\d{2}:\d{2}:\d{6,7}:\d{1,}$/
44

55
test ('basic', () => {
66

77
expect (() => isCadNum ()).toThrow ()
8-
expect (() => isCadNum (123)).toThrow ()
98

10-
expect (() => isCadNum ('47:14:120300:')).toThrow ()
11-
expect (() => isCadNum ('47:14:12030:814')).toThrow ()
12-
expect (() => isCadNum ('47:14:12030011:814')).toThrow ()
9+
for (const v of [
10+
'47:14:120300:',
11+
'47:14:12030:814',
12+
'47:14:12030011:814',
13+
'47-14-120300-814',
14+
'47!14:120300:814',
15+
'47:14-1203001:814',
16+
'47:14:12030O:814',
17+
'47:14:1203001:81A',
18+
]) {
1319

14-
expect (() => isCadNum ('47-14-120300-814')).toThrow ()
15-
16-
expect (() => isCadNum ('47!14:120300:814')).toThrow ()
17-
expect (() => isCadNum ('47:14-1203001:814')).toThrow ()
20+
expect (isntCadNum (v)).toBeTruthy ()
21+
expect (() => isCadNum (v)).toThrow ()
1822

19-
expect (() => isCadNum ('47:14:12030O:814')).toThrow ()
20-
expect (() => isCadNum ('47:14:1203001:81A')).toThrow ()
23+
}
2124

22-
expect (isCadNum ('47:14:120300:814')).toBeUndefined ()
23-
expect (isCadNum ('47:14:1203001:814')).toBeUndefined ()
25+
for (const v of [
26+
'47:14:120300:814',
27+
'47:14:1203001:814',
28+
]) {
29+
30+
expect (isntCadNum (v)).toBeFalsy ()
31+
expect (isCadNum (v)).toBeUndefined ()
32+
33+
}
2434

2535
for (const pre of [undefined, '', '47', '47:', '47:14', '47:14', '47:14:120300', '47:14:1203001', '47:14:120300:', '47:14:1203001:']) {
2636

index.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ const CadNum = require ('./lib/CadNum'), cadNum = new CadNum ()
88
const {OKPO_8, OKPO_10} = require ('./lib/OKPO')
99
class KPP extends Check {constructor () {super (9)}}
1010

11+
const raise = (a) => {
12+
if (!a) return
13+
const [message, o] = a
14+
const err = new Error (message)
15+
for (const k in o) err [k] = o [k]
16+
throw err
17+
}
18+
1119
module.exports = {
1220

1321
isSNILS : str => new SNILS ().verify (str),
@@ -43,7 +51,8 @@ module.exports = {
4351
isBankCard : str => new BankCard ().verify (str),
4452
randomBankCard : opt => new BankCard ().random (opt),
4553

46-
isCadNum : str => cadNum.verify (str),
54+
isntCadNum : str => cadNum.verify (str),
55+
isCadNum : str => raise (cadNum.verify (str)),
4756
randomCadNum : opt => cadNum.random (opt),
4857

4958
}

lib/CadNum.js

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,25 @@ const CH_9 = '9'.charCodeAt (0)
44

55
module.exports = class {
66

7-
raise (s, o) {
8-
9-
const err = Error (s)
10-
11-
for (const k in o) err [k] = o [k]
12-
13-
throw err
14-
15-
}
16-
177
verify (str) {
188

19-
const type = typeof str; if (type !== 'string') this.raise (`CadNum must be of type string`, {str, type, code: 'type'})
9+
const type = typeof str; if (type !== 'string') return [`CadNum must be of type string`, {str, type, code: 'type'}]
2010

21-
const {length} = str; if (length < 14) this.raise (`CadNum must be at least 14 charaters long`, {str, asis: length, code: 'length'})
11+
const {length} = str; if (length < 14) return [`CadNum must be at least 14 charaters long`, {str, asis: length, code: 'length'}]
2212

2313
const lastColonIndex = str.lastIndexOf (':')
2414

2515
switch (lastColonIndex) {
2616

2717
case -1:
28-
this.raise (`Invalid CadNum "${str}": colon not found`, {str, pos: lastColonIndex, code: 'format'})
18+
return [`Invalid CadNum "${str}": colon not found`, {str, pos: lastColonIndex, code: 'format'}]
2919

3020
case 12:
3121
case 13:
3222
break
3323

3424
default:
35-
this.raise (`Invalid CadNum "${str}": the last colon found at position ${lastColonIndex}`, {str, pos: lastColonIndex, code: 'format'})
25+
return [`Invalid CadNum "${str}": the last colon found at position ${lastColonIndex}`, {str, pos: lastColonIndex, code: 'format'}]
3626

3727
}
3828

@@ -44,11 +34,11 @@ module.exports = class {
4434

4535
case 2:
4636
case 5:
47-
if (c !== CH_COLON) this.raise (`Invalid CadNum "${str}": not a colon at position ${pos}`, {str, pos, code: 'char'})
37+
if (c !== CH_COLON) return [`Invalid CadNum "${str}": not a colon at position ${pos}`, {str, pos, code: 'char'}]
4838
break
4939

5040
default:
51-
if (c < CH_0 || c > CH_9) this.raise (`Invalid CadNum "${str}": not a digit at position ${pos}`, {str, pos, code: 'char'})
41+
if (c < CH_0 || c > CH_9) return [`Invalid CadNum "${str}": not a digit at position ${pos}`, {str, pos, code: 'char'}]
5242

5343
}
5444

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ru-codes",
3-
"version": "1.0.7",
3+
"version": "1.0.8",
44
"description": "Проверка и генерация ИНН, КПП и т. п.",
55
"main": "index.js",
66
"files": ["/lib", "/badges"],

0 commit comments

Comments
 (0)