Transform Swedish Personal Numbers into Multiple Formats
This document explains how to transform a Swedish personal number into four commonly used formats:
YYYYMMDDXXXX
(long format, no hyphen)YYYYMMDD-XXXX
(long format with hyphen)YYMMDDXXXX
(short format, no hyphen)YYMMDD-XXXX
(short format with hyphen)
The solution processes the input using a script within PhenixID Authentication Services (PAS). It works independently of the input format, handling all standard variations of Swedish personal numbers.
Purpose
Swedish personal numbers are used in different formats depending on the system or context, including:
- 12-digit formats (
YYYYMMDDXXXX
,YYYYMMDD-XXXX
) for modern systems and government records. - 10-digit formats (
YYMMDDXXXX
,YYMMDD-XXXX
) for older systems or simplified representations.
This solution ensures that any given input is consistently normalized and all four formats are generated for compatibility with various systems.
How It Works
Input Formats
The configuration accepts the following Swedish personal number formats:
YYYYMMDDXXXX
(e.g.,199901011234
)YYYYMMDD-XXXX
(e.g.,19990101-1234
)YYMMDDXXXX
(e.g.,9901011234
)YYMMDD-XXXX
(e.g.,990101-1234
)
Output Formats
The script generates the following standardized formats:
longFormatNoHyphen
: Full 12-digit format (e.g.,199901011234
).longFormatWithHyphen
: Full format with a hyphen (e.g.,19990101-1234
).shortFormatNoHyphen
: Compact 10-digit format (e.g.,9901011234
).shortFormatWithHyphen
: Compact format with a hyphen (e.g.,990101-1234
).
Example Pipe
Below is the full configuration of the pipe, which processes the input and generates all four formats. It includes an ItemCreateValve to initialize the item object.
{
"id": "d45a7c9e-1e7d-4f31-9c5b-6a8edecfc8a9",
"description": "Pipe transforming personal numbers to all standard formats",
"name": "Generate all personal number formats",
"enabled": "true",
"valves": [
{
"name": "ItemCreateValve",
"config": {
"item_id": "personalNumberItem"
}
},
{
"name": "PropertyAddValve",
"config": {
"name": "personalNumberInput",
"value": "{{request.username}}"
}
},
{
"name": "ScriptEvalValve",
"enabled": "true",
"config": {
"proceed_on_error": "false",
"mime_type": "application/javascript",
"script": "var pnr = flow.items().get(0).getPropertyValue('personalNumberInput').replace('-', ''); var longFormatNoHyphen = ''; var longFormatWithHyphen = ''; var shortFormatNoHyphen = ''; var shortFormatWithHyphen = ''; if (pnr.length === 12) { longFormatNoHyphen = pnr; longFormatWithHyphen = pnr.substring(0, 8) + '-' + pnr.substring(8); shortFormatNoHyphen = pnr.substring(2); shortFormatWithHyphen = pnr.substring(2, 8) + '-' + pnr.substring(8); } else if (pnr.length === 10) { var prefix = parseInt(pnr.substring(0, 2), 10) > (new Date().getFullYear() % 100) ? '19' : '20'; longFormatNoHyphen = prefix + pnr; longFormatWithHyphen = prefix + pnr.substring(0, 6) + '-' + pnr.substring(6); shortFormatNoHyphen = pnr; shortFormatWithHyphen = pnr.substring(0, 6) + '-' + pnr.substring(6); } flow.items().get(0).replaceProperty('longFormatNoHyphen', longFormatNoHyphen); flow.items().get(0).replaceProperty('longFormatWithHyphen', longFormatWithHyphen); flow.items().get(0).replaceProperty('shortFormatNoHyphen', shortFormatNoHyphen); flow.items().get(0).replaceProperty('shortFormatWithHyphen', shortFormatWithHyphen);"
}
}
]
}
Valve Descriptions
ItemCreateValve
- Purpose: Initializes an item object to store and process properties during the flow.
- Configuration:
item_id
: A unique identifier for the item (personalNumberItem
).
PropertyAddValve
- Purpose: Stores the input personal number from the request parameter
username
into the item propertypersonalNumberInput
.
ScriptEvalValve
- Purpose: Processes the personal number and generates all four standardized formats dynamically:
longFormatNoHyphen
:YYYYMMDDXXXX
longFormatWithHyphen
:YYYYMMDD-XXXX
shortFormatNoHyphen
:YYMMDDXXXX
shortFormatWithHyphen
:YYMMDD-XXXX
Sample Input and Output
Input Examples
199901011234
(12-digit, no hyphen)19990101-1234
(12-digit, with hyphen)9901011234
(10-digit, no hyphen)990101-1234
(10-digit, with hyphen)
Output Properties
For all inputs, the script generates the following outputs:
longFormatNoHyphen
:199901011234
longFormatWithHyphen
:19990101-1234
shortFormatNoHyphen
:9901011234
shortFormatWithHyphen
:990101-1234
Formatted and Commented Script
The following script processes the input personal number and generates four commonly used formats:
// Retrieve the input personal number from the item property and remove any hyphens
var pnr = flow.items().get(0).getPropertyValue('personalNumberInput').replace('-', '');
// Initialize variables to store the generated formats
var longFormatNoHyphen = ''; // Full 12-digit format: YYYYMMDDXXXX
var longFormatWithHyphen = ''; // Full format with hyphen: YYYYMMDD-XXXX
var shortFormatNoHyphen = ''; // Short 10-digit format: YYMMDDXXXX
var shortFormatWithHyphen = ''; // Short format with hyphen: YYMMDD-XXXX
// Check if the input is in the 12-digit format (YYYYMMDDXXXX)
if (pnr.length === 12) {
// Generate all formats based on the input
longFormatNoHyphen = pnr;
longFormatWithHyphen = pnr.substring(0, 8) + '-' + pnr.substring(8);
shortFormatNoHyphen = pnr.substring(2);
shortFormatWithHyphen = pnr.substring(2, 8) + '-' + pnr.substring(8);
}
// Check if the input is in the 10-digit format (YYMMDDXXXX)
else if (pnr.length === 10) {
// Determine the century prefix (19 or 20) based on the year
var prefix = parseInt(pnr.substring(0, 2), 10) > (new Date().getFullYear() % 100) ? '19' : '20';
// Generate all formats based on the input and calculated prefix
longFormatNoHyphen = prefix + pnr;
longFormatWithHyphen = prefix + pnr.substring(0, 6) + '-' + pnr.substring(6);
shortFormatNoHyphen = pnr;
shortFormatWithHyphen = pnr.substring(0, 6) + '-' + pnr.substring(6);
}
// Add the generated formats back to the item for use in subsequent valves
flow.items().get(0).replaceProperty('longFormatNoHyphen', longFormatNoHyphen);
flow.items().get(0).replaceProperty('longFormatWithHyphen', longFormatWithHyphen);
flow.items().get(0).replaceProperty('shortFormatNoHyphen', shortFormatNoHyphen);
flow.items().get(0).replaceProperty('shortFormatWithHyphen', shortFormatWithHyphen);
This solution provides a reliable and flexible method for working with Swedish personal numbers, ensuring compatibility with various systems while maintaining simplicity and scalability.