Skip to content

Commit

Permalink
Merge pull request #122 from sskogen/main
Browse files Browse the repository at this point in the history
Support Swedish Coordination numbers ("Samordningsnummer")
  • Loading branch information
koblas authored Nov 16, 2024
2 parents ecace41 + 5c845ab commit d22fccd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/se/personnummer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ describe('se/personnummer', () => {
expect(result.isValid && result.compact).toEqual('880320-0016');
});

// Coordination number ("Samordningsnummer")
it('validate:701063-2391', () => {
const result = validate('701063-2391');

expect(result.isValid && result.compact).toEqual('701063-2391');
});

test.each(['811228-9874', '670919-9530', '11900102-2384'])(
'validate:%s',
value => {
Expand Down
15 changes: 12 additions & 3 deletions src/se/personnummer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ function formatImpl(input: string): string {
const [value] = clean(input);

let front,
back,
sep = '-';
back,
sep = '-';

if (value.length === 12 || value.length === 13) {
const [yyyy, mm, dd] = strings.splitAt(value, 0, 4, 6, 8);
Expand Down Expand Up @@ -122,6 +122,15 @@ const impl: Validator = {

yyyymmdd = `${century}${yymmdd}`;
}

// Adjust for coordination numbers ("Samordningsnummer")
// https://skatteverket.se/servicelankar/otherlanguages/inenglishengelska/individualsandemployees/coordinationnumbers
const day = parseInt(yyyymmdd.substring(6, 8), 10);
if (day > 60) {
yyyymmdd =
yyyymmdd.substring(0, 6) + (day - 60).toString().padStart(2, '0');
}

if (!isValidDateCompactYYYYMMDD(yyyymmdd, true)) {
return { isValid: false, error: new exceptions.InvalidComponent() };
}
Expand All @@ -140,4 +149,4 @@ const impl: Validator = {
};

export const { name, localName, abbreviation, validate, format, compact } =
impl;
impl;

0 comments on commit d22fccd

Please sign in to comment.