Skip to content
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
26 changes: 26 additions & 0 deletions src/cl/rut.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,38 @@ describe('cl/rut', () => {
expect(result.isValid && result.compact).toEqual('125319092');
});

// The number is normally printed with the thousands separators, which is
// also what format() emits.
it('validate:12.531.909-2', () => {
const result = validate('12.531.909-2');

expect(result.isValid && result.compact).toEqual('125319092');
});

it('validate:CL 7.727.230-5', () => {
const result = validate('CL 7.727.230-5');

expect(result.isValid && result.compact).toEqual('77272305');
});

it('validate:format:76086428-5', () => {
const result = validate(format('76086428-5'));

expect(result.isValid && result.compact).toEqual('760864285');
});

it('validate:12531909-3', () => {
const result = validate('12531909-3');

expect(result.error).toBeInstanceOf(InvalidChecksum);
});

it('validate:12.531.909-3', () => {
const result = validate('12.531.909-3');

expect(result.error).toBeInstanceOf(InvalidChecksum);
});

it('validate:76086A28-5', () => {
const result = validate('76086A28-5');

Expand Down
2 changes: 1 addition & 1 deletion src/cl/rut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { strings, weightedSum } from '../util';
import { Validator, ValidateReturn } from '../types';

function clean(input: string): ReturnType<typeof strings.cleanUnicode> {
const [v, err] = strings.cleanUnicode(input, ' -');
const [v, err] = strings.cleanUnicode(input, ' -.');

if (err) {
return ['', err];
Expand Down
27 changes: 27 additions & 0 deletions src/se/personnummer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@ describe('se/personnummer', () => {
},
);

// The separator is only written between the birth date and the birth number,
// it is not part of the number (Folkbokföringslagen 1991:481 §18), so the
// bare 10 and 12 digit forms are valid too.
test.each([
['8803200016', '880320-0016'],
['8112289874', '811228-9874'],
['6709199530', '670919-9530'],
['7010632391', '701063-2391'], // coordination number
['119001022384', '900102+2384'], // 4digit year, no hyphen
])('validate:%s', (given, want) => {
const result = validate(given);

expect(result.isValid && result.compact).toEqual(want);
});

it('validate:12345678', () => {
const result = validate('12345678');

Expand All @@ -49,4 +64,16 @@ describe('se/personnummer', () => {

expect(result.error).toBeInstanceOf(InvalidChecksum);
});

it('validate:8803200018', () => {
const result = validate('8803200018');

expect(result.error).toBeInstanceOf(InvalidChecksum);
});

it('validate:198803200018', () => {
const result = validate('198803200018');

expect(result.error).toBeInstanceOf(InvalidChecksum);
});
});
11 changes: 10 additions & 1 deletion src/se/personnummer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,16 @@ function clean(input: string): ReturnType<typeof strings.cleanUnicode> {
return [value, err];
}

const [a, b, c] = strings.splitAt(value, -5, -4);
// A 10 or 12 digit number is written without the separator, so put the
// default one back. '-' is only replaced by '+' the year someone turns 100,
// which the bare form cannot express.
const separated =
(value.length === 10 || value.length === 12) &&
!'-+'.includes(value[value.length - 5])
? `${value.slice(0, -4)}-${value.slice(-4)}`
: value;

const [a, b, c] = strings.splitAt(separated, -5, -4);

return [`${a.replace(/[-+]/g, '')}${b}${c}`, null];
}
Expand Down
19 changes: 19 additions & 0 deletions src/uy/cedula.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@ describe('uy/cedula', () => {
expect(result.isValid && result.compact).toEqual('12345672');
});

// The cédula is printed as a.bcd.efg-h, which is also what format() emits.
it('validate:1.234.567-2', () => {
const result = validate('1.234.567-2');

expect(result.isValid && result.compact).toEqual('12345672');
});

it('validate:format:12345672', () => {
const result = validate(format('12345672'));

expect(result.isValid && result.compact).toEqual('12345672');
});

it('validate:1121123', () => {
const result = validate('1121123');

Expand All @@ -25,4 +38,10 @@ describe('uy/cedula', () => {

expect(result.error).toBeInstanceOf(InvalidChecksum);
});

it('validate:1.234.567-3', () => {
const result = validate('1.234.567-3');

expect(result.error).toBeInstanceOf(InvalidChecksum);
});
});
2 changes: 1 addition & 1 deletion src/uy/cedula.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { strings, weightedSum } from '../util';
import { Validator, ValidateReturn } from '../types';

function clean(input: string): ReturnType<typeof strings.cleanUnicode> {
return strings.cleanUnicode(input, ' -/');
return strings.cleanUnicode(input, ' -/.');
}

const impl: Validator = {
Expand Down
18 changes: 18 additions & 0 deletions src/uy/nie.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ describe('uy/nie', () => {
expect(result.isValid && result.compact).toEqual('912345670');
});

it('validate:91.234.567-0', () => {
const result = validate('91.234.567-0');

expect(result.isValid && result.compact).toEqual('912345670');
});

it('validate:format:912345670', () => {
const result = validate(format('912345670'));

expect(result.isValid && result.compact).toEqual('912345670');
});

it('validate:1121123', () => {
const result = validate('1121123');

Expand All @@ -25,4 +37,10 @@ describe('uy/nie', () => {

expect(result.error).toBeInstanceOf(InvalidChecksum);
});

it('validate:91.234.567-3', () => {
const result = validate('91.234.567-3');

expect(result.error).toBeInstanceOf(InvalidChecksum);
});
});
2 changes: 1 addition & 1 deletion src/uy/nie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { strings, weightedSum } from '../util';
import { Validator, ValidateReturn } from '../types';

function clean(input: string): ReturnType<typeof strings.cleanUnicode> {
return strings.cleanUnicode(input, ' -');
return strings.cleanUnicode(input, ' -.');
}

const impl: Validator = {
Expand Down
Loading