Skip to content

Commit

Permalink
Merge pull request #95 from koblas/se_personnummer_fix
Browse files Browse the repository at this point in the history
fix: handle 13 digit personnummers
  • Loading branch information
koblas committed Aug 15, 2023
2 parents 08da80a + 9b6d39c commit e70c3b1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/se/personnummer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ describe('se/personnummer', () => {
expect(result.isValid && result.compact).toEqual('880320-0016');
});

test.each(['811228-9874', '670919-9530', '11900102-2384'])(
'validate:%s',
value => {
const result = validate(value);

expect(result.isValid).toEqual(true);
},
);

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

Expand Down
8 changes: 6 additions & 2 deletions src/se/personnummer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,14 @@ const impl: Validator = {
if (error) {
return { isValid: false, error };
}
if (value.length !== 11 && value.length !== 13) {
let a, b, c;
if (value.length === 11) {
[a, b, c] = strings.splitAt(value, -5, -4);
} else if (value.length === 13) {
[, a, b, c] = strings.splitAt(value, -11, -5, -4);
} else {
return { isValid: false, error: new exceptions.InvalidLength() };
}
const [a, b, c] = strings.splitAt(value, -5, -4);
if (!'-+'.includes(b)) {
return { isValid: false, error: new exceptions.InvalidFormat() };
}
Expand Down

0 comments on commit e70c3b1

Please sign in to comment.