Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: added Macedonia EDB #86

Merged
merged 1 commit into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ All country validators are in the "namespace" of the ISO country code.
| Luxembourgian | LU | TVA | Vat | taxe sur la valeur ajoutée |
| Latvian | LV | PVN | Person/Vat | Pievienotās vērtības nodokļa |
| Morocco | MA | ICE | Company | Identifiant Commun de l'Entreprise, Moroccan new registration number |
| Macedonia | MK | EDB | Vat | Едниствен Даночен Број, North Macedonia tax number |
| Macedonia | MK | JMBG | Person | Unique Master Citizen Number (Единствен матичен број на граѓанинот) |
| Monaco | MC | TVA | Vat | taxe sur la valeur ajoutée, Monacan VAT number |
| Moldavia | MD | IDNO | Vat | Moldavian VAT number |
Expand Down
50 changes: 50 additions & 0 deletions src/mk/edb.spec.ts
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);
});
});
88 changes: 88 additions & 0 deletions src/mk/edb.ts
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;
1 change: 1 addition & 0 deletions src/mk/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * as jmbg from './jmbg';
export * as edb from './edb';
18 changes: 15 additions & 3 deletions src/util/strings/clean.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// import PREFIXES from 'gb/nino-prefixes';
import * as exceptions from '../../exceptions';

// Map visually similar unicode values to ASCII
Expand Down Expand Up @@ -273,7 +274,7 @@ const mapped: Record<string, string> = {
export function cleanUnicode(
value: string,
deletechars = ' ',
stripPrefix?: string,
stripPrefix?: string | string[],
): [string, exceptions.InvalidFormat | null] {
if (typeof value !== 'string') {
return ['', new exceptions.InvalidFormat()];
Expand All @@ -286,8 +287,19 @@ export function cleanUnicode(
.join('')
.toLocaleUpperCase();

if (stripPrefix && cleaned.startsWith(stripPrefix)) {
return [cleaned.substr(stripPrefix.length), null];
if (stripPrefix && stripPrefix.length !== 0) {
let prefix;

if (Array.isArray(stripPrefix)) {
prefix = stripPrefix.find(p => cleaned.startsWith(p));
} else if (cleaned.startsWith(stripPrefix)) {
prefix = stripPrefix;
}

if (prefix !== undefined) {
return [cleaned.substring(prefix.length), null];
}
}

return [cleaned, null];
}