koblas / stdnum-js

A JavaScript library to provide functions to handle, parse and validate standard numbers.
MIT License
38 stars 26 forks source link

Swedish Personal Identity Numbers - optional -+ signs #97

Closed teebu closed 11 months ago

teebu commented 1 year ago

https://swedish.identityinfo.net/personalidentitynumber

Pattern
The official version of a Swedish Personal Identity Number (defined in law) follow this pattern:

YYMMDD-BBBC
YYMMDD+BBBC
The delimiter is - (hyphen) by default but changes to + (plus) the year you turn 100. See details below.
It's quite common to leave the hyphen out, like this:

YYMMDDBBBC
When stored or handled in a structured way, the law states you should handle it with the full length of the year, like this:

YYYYMMDDBBBC
koblas commented 1 year ago

Totally understand this, but trying to figure out what the issue is here.

What I think you're saying is that the format() method for personnummer does not return the correct values for 4 digit year values. This would then apply to the compact version that is returned by the validate() method as well.

Just checking before I break something

koblas commented 12 months ago

Does this look right, this is the test matrix I just built.

  test.each([
    ['8803200017', '880320-0017'], // no hyphen assume young person
    ['880320-0017', '880320-0017'], // standard format
    ['880320+0017', '880320+0017'], // standard format, person over 100
    ['19200901-0000', '200901+0000'], // 4digit year, hypen that's useless person over 100
    ['192009010000', '200901+0000'], // 4digit year, no hyphen, person over 100
    ['20200901-0000', '200901-0000'], // 4digit year, hyphen
    ['202009010000', '200901-0000'], // 4digit year, no hyphen
  ])('format:%s', (given, want) => {