@remato/personal-code-to-birthday 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,40 @@
1
+ name: build
2
+
3
+ on:
4
+ pull_request:
5
+ push:
6
+ branches:
7
+ - main
8
+
9
+ jobs:
10
+ test:
11
+ runs-on: ubuntu-latest
12
+
13
+ steps:
14
+ - name: Checkout
15
+ uses: actions/checkout@v4
16
+
17
+ - name: Setup node
18
+ uses: actions/setup-node@v4
19
+ with:
20
+ node-version: '20'
21
+
22
+ - name: Install dependencies
23
+ run: npm install
24
+
25
+ - name: Check code format
26
+ run: npm run prettier
27
+
28
+ - name: Lint
29
+ run: npm run lint
30
+
31
+ - name: Test
32
+ run: npm run test
33
+
34
+ - name: Upload coverage reports to Codecov
35
+ uses: codecov/codecov-action@v4.0.1
36
+ with:
37
+ token: ${{ secrets.CODECOV_TOKEN }}
38
+
39
+ - name: Build
40
+ run: npm run build
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Remato
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # Personal Code to Birthday
2
+
3
+ [![Build](https://img.shields.io/github/actions/workflow/status/rematocorp/personal-code-to-birthday/ci.yml)](https://github.com/rematocorp/personal-code-to-birthday/actions/workflows/ci.yml)
4
+ [![Codecov](https://img.shields.io/codecov/c/github/rematocorp/personal-code-to-birthday?token=NDT35FM2LG&style=flat)](https://codecov.io/gh/rematocorp/personal-code-to-birthday)
5
+
6
+ Converts personal identification codes (or national ID numbers) from various countries into birthdate.
7
+
8
+ ### Install
9
+
10
+ ```bash
11
+ npm install @remato/personal-code-to-birthday
12
+ ```
13
+
14
+ ### How to use
15
+
16
+ ```ts
17
+ const birthday = personalCodeToBirthday('39309262855')
18
+
19
+ console.log(birthday) // outputs { day: 26, month: 9, year: 1993 }
20
+ ```
21
+
22
+ ### Countries supported
23
+
24
+ - 🇪🇪 Estonia
25
+ - 🇱🇻 Latvia
26
+ - 🇱🇹 Lithuania
27
+ - 🇫🇮 Finland
28
+ - 🇸🇪 Sweden
29
+ - 🇳🇴 Norway
30
+ - 🇩🇰 Denmark
31
+
32
+ Countries we would like to support (PRs are welcome):
33
+
34
+ - Iceland
35
+ - South Africa
36
+ - Romania
37
+ - Hungary
38
+ - Bulgaria
39
+ - North Macedonia
40
+ - Czech Republic
41
+ - Slovakia
42
+ - Poland
43
+ - South Korea
44
+ - Turkey
45
+ - Moldova
46
+ - Azerbaijan
47
+ - Russia
48
+ - Ukraine
package/index.test.ts ADDED
@@ -0,0 +1,79 @@
1
+ import personalCodeToBirthday from './index'
2
+
3
+ describe('valid codes', () => {
4
+ test('Estonia', () => {
5
+ expect(personalCodeToBirthday('39405280299')).toEqual({ day: 28, month: 5, year: 1994 })
6
+ expect(personalCodeToBirthday('60105280299')).toEqual({ day: 28, month: 5, year: 2001 })
7
+ })
8
+
9
+ test('Latvia', () => {
10
+ expect(personalCodeToBirthday('050393-12344')).toEqual({ day: 5, month: 3, year: 1993 })
11
+ expect(personalCodeToBirthday('050303-22344')).toEqual({ day: 5, month: 3, year: 2003 })
12
+ })
13
+
14
+ test('Lithuania', () => {
15
+ expect(personalCodeToBirthday('39905280217')).toEqual({ day: 28, month: 5, year: 1999 })
16
+ })
17
+
18
+ test('Finland', () => {
19
+ expect(personalCodeToBirthday('101085-7001')).toEqual({ day: 10, month: 10, year: 1985 })
20
+ expect(personalCodeToBirthday('101085+789W')).toEqual({ day: 10, month: 10, year: 1885 })
21
+ expect(personalCodeToBirthday('150752-308N')).toEqual({ day: 15, month: 7, year: 1952 })
22
+ expect(personalCodeToBirthday('010100A123D')).toEqual({ day: 1, month: 1, year: 2000 })
23
+ })
24
+
25
+ test('Sweden', () => {
26
+ expect(personalCodeToBirthday('199001011234')).toEqual({ day: 1, month: 1, year: 1990 })
27
+ })
28
+
29
+ test('Norway', () => {
30
+ expect(personalCodeToBirthday('01020352345')).toEqual({ day: 1, month: 2, year: 2003 })
31
+ })
32
+
33
+ test('Denmark', () => {
34
+ expect(personalCodeToBirthday('0503054567')).toEqual({ day: 5, month: 3, year: 2005 })
35
+ expect(personalCodeToBirthday('0503841235')).toEqual({ day: 5, month: 3, year: 1984 })
36
+ expect(personalCodeToBirthday('050384-1235')).toEqual({ day: 5, month: 3, year: 1984 })
37
+ expect(personalCodeToBirthday('0503844567')).toEqual({ day: 5, month: 3, year: 1984 })
38
+ })
39
+ })
40
+
41
+ describe('invalid personal code', () => {
42
+ test('Estonia', () => {
43
+ const result = personalCodeToBirthday('39913025555')
44
+ expect(result).toBeNull()
45
+ })
46
+
47
+ test('Latvia', () => {
48
+ expect(personalCodeToBirthday('123456-12345')).toBeNull()
49
+ })
50
+
51
+ test('Lithuania', () => {
52
+ expect(personalCodeToBirthday('99905280217')).toBeNull()
53
+ })
54
+
55
+ test('Finland', () => {
56
+ expect(personalCodeToBirthday('999999A123T')).toBeNull()
57
+ expect(personalCodeToBirthday('131052Z3087')).toBeNull()
58
+ expect(personalCodeToBirthday('321052A3087')).toBeNull()
59
+ })
60
+
61
+ test('Sweden', () => {
62
+ expect(personalCodeToBirthday('1990010123456')).toBeNull()
63
+ expect(personalCodeToBirthday('199013011234')).toBeNull()
64
+ expect(personalCodeToBirthday('199001341234')).toBeNull()
65
+ })
66
+
67
+ test('Norway', () => {
68
+ expect(personalCodeToBirthday('32129999999')).toBeNull()
69
+ })
70
+
71
+ test('Denmark', () => {
72
+ expect(personalCodeToBirthday('9999991234')).toBeNull()
73
+ })
74
+
75
+ test('unsupported personal code format', () => {
76
+ const result = personalCodeToBirthday('abcd1234')
77
+ expect(result).toBeNull()
78
+ })
79
+ })
package/index.ts ADDED
@@ -0,0 +1,192 @@
1
+ interface ParsedDate {
2
+ day: number
3
+ month: number
4
+ year: number
5
+ }
6
+
7
+ export default function personalCodeToBirthday(code: string): ParsedDate | null {
8
+ if (/^\d{11}$/.test(code)) {
9
+ // Could be Estonia, Lithuania, or Norway
10
+ const birthday = estonianLithuanianParser(code)
11
+ if (birthday) {
12
+ return birthday
13
+ }
14
+
15
+ return norwayParser(code)
16
+ } else if (/^\d{6}-\d{5}$/.test(code)) {
17
+ return latvianParser(code)
18
+ } else if (/^\d{6}[+-A]\d{3}[A-Za-z]$/.test(code)) {
19
+ return finlandParser(code)
20
+ } else if (/^\d{6}[+-A]\d{4}$/.test(code)) {
21
+ // Could be Finland or Denmark
22
+ const birthday = finlandParser(code)
23
+ if (birthday) {
24
+ return birthday
25
+ }
26
+
27
+ return denmarkParser(code)
28
+ } else if (/^\d{12}$/.test(code)) {
29
+ return swedenParser(code)
30
+ } else if (/^\d{10}$/.test(code)) {
31
+ return denmarkParser(code)
32
+ }
33
+
34
+ return null
35
+ }
36
+
37
+ // Estonia (EE) and Lithuanian (LT) Parser
38
+ function estonianLithuanianParser(code: string): ParsedDate | null {
39
+ const centuryCode = parseInt(code[0], 10)
40
+ const day = parseInt(code.slice(5, 7), 10)
41
+ const month = parseInt(code.slice(3, 5), 10)
42
+ let year = parseInt(code.slice(1, 3), 10)
43
+
44
+ if (centuryCode === 3 || centuryCode === 4) {
45
+ year += 1900
46
+ }
47
+ if (centuryCode === 5 || centuryCode === 6) {
48
+ year += 2000
49
+ }
50
+
51
+ if (!isValidDate(day, month, year)) {
52
+ return null
53
+ }
54
+
55
+ return { day, month, year }
56
+ }
57
+
58
+ // Latvia (LV) Parser
59
+ // Correct old school format is DDMMYY-CXXXX
60
+ function latvianParser(code: string): ParsedDate | null {
61
+ const day = parseInt(code.slice(0, 2), 10)
62
+ const month = parseInt(code.slice(2, 4), 10)
63
+ let year = parseInt(code.slice(4, 6), 10)
64
+ const centuryCode = parseInt(code[7], 10)
65
+
66
+ // Latvian codes are always from the 20th century, so add 1900
67
+ if (centuryCode === 2) {
68
+ year += 2000
69
+ } else {
70
+ year += 1900
71
+ }
72
+
73
+ // Validate the parsed date
74
+ if (!isValidDate(day, month, year)) {
75
+ return null
76
+ }
77
+
78
+ return { day, month, year }
79
+ }
80
+
81
+ // Finland (FI) Parser
82
+ function finlandParser(code: string): ParsedDate | null {
83
+ const day = parseInt(code.slice(0, 2), 10)
84
+ const month = parseInt(code.slice(2, 4), 10)
85
+ const yearSuffix = parseInt(code.slice(4, 6), 10)
86
+ const centuryMarker = code[6]
87
+
88
+ let year = yearSuffix
89
+
90
+ // Handle the century based on the marker
91
+ if (centuryMarker === '+') {
92
+ year += 1800
93
+ } else if (centuryMarker === '-') {
94
+ year += 1900
95
+ } else if (centuryMarker === 'A') {
96
+ year += 2000
97
+ }
98
+
99
+ // Validate the parsed date
100
+ if (!isValidDate(day, month, year)) {
101
+ return null
102
+ }
103
+
104
+ if (!isValidFinnishChecksum(code)) {
105
+ return null
106
+ }
107
+
108
+ return { day, month, year }
109
+ }
110
+
111
+ function isValidFinnishChecksum(code: string): boolean {
112
+ const validChars = '0123456789ABCDEFHJKLMNPRSTUVWXY'
113
+
114
+ const serial = code.slice(0, 6) + code.slice(7, 10)
115
+ const checksum = code[10]
116
+ const num = parseInt(serial, 10)
117
+ const remainder = num % 31
118
+ const expectedChecksum = validChars.charAt(remainder)
119
+
120
+ return expectedChecksum === checksum.toUpperCase()
121
+ }
122
+
123
+ // Sweden (SE) Parser
124
+ function swedenParser(code: string): ParsedDate | null {
125
+ const year = parseInt(code.slice(0, 4), 10)
126
+ const month = parseInt(code.slice(4, 6), 10)
127
+ const day = parseInt(code.slice(6, 8), 10)
128
+
129
+ if (!isValidDate(day, month, year)) {
130
+ return null
131
+ }
132
+
133
+ return { day, month, year }
134
+ }
135
+
136
+ // Norway (NO) Parser
137
+ function norwayParser(code: string): ParsedDate | null {
138
+ const day = parseInt(code.slice(0, 2), 10)
139
+ const month = parseInt(code.slice(2, 4), 10)
140
+ let year = parseInt(code.slice(4, 6), 10)
141
+
142
+ const individual = parseInt(code.slice(6, 9), 10)
143
+
144
+ // Determine century based on individual number and year
145
+ if (individual <= 499 || (individual >= 500 && year >= 40)) {
146
+ year += 1900
147
+ } else if (individual <= 999 && year <= 39) {
148
+ year += 2000
149
+ }
150
+
151
+ // Now validate the date after adjusting the century
152
+ if (!isValidDate(day, month, year)) {
153
+ return null
154
+ }
155
+
156
+ return { day, month, year }
157
+ }
158
+
159
+ // Denmark (DK) Parser
160
+ function denmarkParser(code: string): ParsedDate | null {
161
+ if (code.includes('-')) {
162
+ code = code.replace('-', '')
163
+ }
164
+
165
+ const day = parseInt(code.slice(0, 2), 10)
166
+ const month = parseInt(code.slice(2, 4), 10)
167
+ let year = parseInt(code.slice(4, 6), 10)
168
+ const centuryIndicator = parseInt(code.slice(6, 7), 10)
169
+
170
+ // Determine century based on the 7th digit and the year
171
+ if (centuryIndicator >= 0 && centuryIndicator <= 3) {
172
+ year += 1900
173
+ } else if (centuryIndicator >= 4 && centuryIndicator <= 9) {
174
+ if (year >= 0 && year <= 36) {
175
+ year += 2000
176
+ } else {
177
+ year += 1900
178
+ }
179
+ }
180
+
181
+ if (!isValidDate(day, month, year)) {
182
+ return null
183
+ }
184
+
185
+ return { day, month, year }
186
+ }
187
+
188
+ function isValidDate(day: number, month: number, year: number): boolean {
189
+ const date = new Date(year, month - 1, day) // month is zero-indexed in JS Date
190
+
191
+ return date.getFullYear() === year && date.getMonth() === month - 1 && date.getDate() === day
192
+ }
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "@remato/personal-code-to-birthday",
3
+ "version": "0.0.0",
4
+ "description": "Converts personal identification codes from various countries into birthdate",
5
+ "license": "MIT",
6
+ "main": "index.ts",
7
+ "scripts": {
8
+ "build": "ncc build index.ts",
9
+ "test": "jest",
10
+ "prettier": "prettier --list-different \"**/*.ts\"",
11
+ "lint": "eslint \"**/*.ts\""
12
+ },
13
+ "devDependencies": {
14
+ "@remato/eslint-config": "^1.14.0",
15
+ "@remato/prettier-config": "^1.0.0",
16
+ "@types/jest": "^29.5.14",
17
+ "@vercel/ncc": "^0.38.2",
18
+ "eslint": "^8.57.1",
19
+ "jest": "^29.7.0",
20
+ "prettier": "^3.3.3",
21
+ "ts-jest": "^29.2.5",
22
+ "typescript": "^5.6.3"
23
+ },
24
+ "jest": {
25
+ "preset": "ts-jest",
26
+ "testEnvironment": "node",
27
+ "moduleFileExtensions": [
28
+ "js",
29
+ "ts"
30
+ ],
31
+ "testMatch": [
32
+ "**/*.test.ts"
33
+ ],
34
+ "testPathIgnorePatterns": [
35
+ "/node_modules/",
36
+ "/dist/"
37
+ ],
38
+ "transform": {
39
+ "^.+\\.ts$": "ts-jest"
40
+ },
41
+ "collectCoverage": true,
42
+ "collectCoverageFrom": [
43
+ "index.ts"
44
+ ]
45
+ },
46
+ "eslintConfig": {
47
+ "extends": [
48
+ "@remato/eslint-config/typescript"
49
+ ]
50
+ }
51
+ }
@@ -0,0 +1 @@
1
+ module.exports = require("@remato/prettier-config");
package/tsconfig.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "$schema": "https://json.schemastore.org/tsconfig",
3
+ "compilerOptions": {
4
+ "target": "ES2022",
5
+ "module": "NodeNext",
6
+ "rootDir": "./",
7
+ "moduleResolution": "NodeNext",
8
+ "baseUrl": "./",
9
+ "sourceMap": true,
10
+ "outDir": "./dist",
11
+ "noImplicitAny": true,
12
+ "esModuleInterop": true,
13
+ "forceConsistentCasingInFileNames": true,
14
+ "strict": true,
15
+ "skipLibCheck": true,
16
+ "newLine": "lf"
17
+ },
18
+ "exclude": ["./node_modules"]
19
+ }