-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #86 from koblas/mk_edb
fix: added Macedonia EDB
- Loading branch information
Showing
5 changed files
with
155 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { validate, format } from './edb'; | ||
import { InvalidLength, InvalidChecksum } from '../exceptions'; | ||
|
||
describe('mk/edb', () => { | ||
it('format:МК 4020990116747', () => { | ||
const result = format('МК 4020990116747'); | ||
|
||
expect(result).toEqual('4020990116747'); | ||
}); | ||
|
||
it('format:MK4057009501106', () => { | ||
const result = format('MK4057009501106'); | ||
|
||
expect(result).toEqual('4057009501106'); | ||
}); | ||
|
||
it('fvalidate:MK4030000375897', () => { | ||
const result = validate('MK4030000375897'); | ||
|
||
expect(result.isValid && result.compact).toEqual('4030000375897'); | ||
}); | ||
|
||
test.each([ | ||
'МК 4020990116747', | ||
'MK4057009501106', | ||
'4057018541765', | ||
'4057019547848', | ||
'4061018502090', | ||
'4064012500591', | ||
'4065015500653', | ||
'4069011500670', | ||
'4071016500495', | ||
])('validate:%s', value => { | ||
const result = validate(value); | ||
|
||
expect(result.isValid).toEqual(true); | ||
}); | ||
|
||
it('validate:405701954784', () => { | ||
const result = validate('405701954784'); | ||
|
||
expect(result.error).toBeInstanceOf(InvalidLength); | ||
}); | ||
|
||
it('validate:4057019547840', () => { | ||
const result = validate('4057019547840'); | ||
|
||
expect(result.error).toBeInstanceOf(InvalidChecksum); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/** | ||
* ЕДБ (Едниствен Даночен Број, North Macedonia tax number). | ||
* | ||
* This number consists of 13 digits, sometimes with an additional "MK" prefix. | ||
* | ||
* Source | ||
* http://www.ujp.gov.mk/en | ||
* | ||
* ENTITY/PERSON | ||
*/ | ||
|
||
import * as exceptions from '../exceptions'; | ||
import { strings } from '../util'; | ||
import { Validator, ValidateReturn } from '../types'; | ||
import { weightedSum } from '../util/checksum'; | ||
|
||
function clean(input: string): ReturnType<typeof strings.cleanUnicode> { | ||
// ASCII and Cyrillic prefixes | ||
return strings.cleanUnicode(input, ' -', ['MK', 'МК']); | ||
} | ||
|
||
// const validRe = /^[PCGQV]{1}00[A-Z0-9]{8}$/; | ||
|
||
// const ALPHABET = '0123456789X'; | ||
|
||
const impl: Validator = { | ||
name: 'NAME', | ||
localName: 'NAME', | ||
abbreviation: 'EDB', | ||
|
||
compact(input: string): string { | ||
const [value, err] = clean(input); | ||
|
||
if (err) { | ||
throw err; | ||
} | ||
|
||
return value; | ||
}, | ||
|
||
format(input: string): string { | ||
const [value] = clean(input); | ||
|
||
return value; | ||
}, | ||
|
||
validate(input: string): ValidateReturn { | ||
const [value, error] = clean(input); | ||
|
||
if (error) { | ||
return { isValid: false, error }; | ||
} | ||
if (value.length !== 13) { | ||
return { isValid: false, error: new exceptions.InvalidLength() }; | ||
} | ||
|
||
if (!strings.isdigits(value)) { | ||
return { isValid: false, error: new exceptions.InvalidFormat() }; | ||
} | ||
// if (!validRe.test(value)) { | ||
// return { isValid: false, error: new exceptions.InvalidFormat() }; | ||
// } | ||
|
||
const [front, check] = strings.splitAt(value, 12); | ||
|
||
const sum = | ||
(11 - | ||
weightedSum(front, { | ||
weights: [7, 6, 5, 4, 3, 2], | ||
modulus: 11, | ||
})) % | ||
11; | ||
|
||
if (String(sum % 10) !== check) { | ||
return { isValid: false, error: new exceptions.InvalidChecksum() }; | ||
} | ||
|
||
return { | ||
isValid: true, | ||
compact: value, | ||
isIndividual: false, | ||
isCompany: false, | ||
}; | ||
}, | ||
}; | ||
|
||
export const { name, localName, abbreviation, validate, format, compact } = | ||
impl; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * as jmbg from './jmbg'; | ||
export * as edb from './edb'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters