@@ -84,8 +84,10 @@ export function validateOrganizationNumber(
8484 return / ^ \d { 10 } $ / . test ( value ) ;
8585}
8686
87+ type NationalIdentityNumberFormat = 'short' | 'long' ;
8788type NationalIdenityNumberOptions = ValidatorOptions & {
88- format ?: [ 'long' , 'short' ] ;
89+ /** By default, both formats are allowed */
90+ format ?: NationalIdentityNumberFormat ;
8991} ;
9092
9193/**
@@ -111,25 +113,25 @@ export function validateNationalIdentityNumber(
111113 value = stripFormatting ( value ) ;
112114 }
113115
114- // this allows us to handle both YYYYMMDD and YYMMDD when extracting the date
115- const offset = value . length === 12 ? 2 : 0 ;
116+ const isLongFormat = value . length === 12 ;
116117
117118 // when verifying the value, we must always use the short format.
118119 // because the long format would generate a different checksum
119- const isValid = mod10 ( offset ? value . substring ( 2 ) : value ) ;
120+ const isValid = mod10 ( isLongFormat ? value . substring ( 2 ) : value ) ;
120121 if ( ! isValid ) {
121122 return false ;
122123 }
123124
125+ // this allows us to handle both YYYYMMDD and YYMMDD when extracting the date
126+ const offset = isLongFormat ? 2 : 0 ;
124127 // copy/inspiration from NAV https://github.com/navikt/fnrvalidator/blob/77e57f0bc8e3570ddc2f0a94558c58d0f7259fe0/src/validator.ts#L108
125128 let year = Number ( value . substring ( 0 , 2 + offset ) ) ;
126129 const month = Number ( value . substring ( 2 + offset , 4 + offset ) ) ;
127130 let day = Number ( value . substring ( 4 + offset , 6 + offset ) ) ;
128131
129132 // 1900 isn't a leap year, but 2000 is. Since JS two digits years to the Date constructor is an offset from the year 1900
130133 // we need to special handle that case. For other cases it doesn't really matter if the year is 1925 or 2025.
131- // if (options.format === 'short' && year === 0) {
132- if ( value . length === 10 && year === 0 ) {
134+ if ( ! isLongFormat && year === 0 ) {
133135 year = 2000 ;
134136 }
135137
0 commit comments