zimbabwean-id-number-validator 1.0.1

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.
package/README.md ADDED
@@ -0,0 +1,61 @@
1
+ # Zimbabwean ID Number Validator
2
+
3
+ The ID numbers issued in Zimbabwe consist of either 11 or 12 alphanumeric characters. Each ID contains one alphabetic letter, while the remaining characters are numeric digits. Various systems may capture ID numbers in different formats, with the most common formats including:
4
+ - 082047823Q29
5
+ - 08-2047823-Q-29
6
+ - 08-2047823Q29
7
+
8
+ | ID | Description |
9
+ |------|---------------------------------------------------------------------------|
10
+ | 08 | District Code |
11
+ | 2047823 | 6 or 7 digit sequence|
12
+ | Q | Alphabetic letter |
13
+ | 29 | District Code |
14
+ -----------------------------------------------------------------------------------
15
+
16
+ This package uses regular expressions to validate the input against the established patterns. It also includes a lookup of District Codes to ensure the correctness of the ID number.
17
+ The three possible responses are as follows:
18
+ ```javascript
19
+ {
20
+ isIdNumberValid: false,
21
+ description: "id number must be in one of the formats: (082047823Q29), (08-2047823Q29), or (08-2047823-Q-29)"
22
+ },
23
+
24
+ {
25
+ isIdNumberValid: false,
26
+ description: "id number is invalid"
27
+ },
28
+
29
+ {
30
+ isIdNumberValid: true,
31
+ description: "id number is valid"
32
+ }
33
+
34
+ ```
35
+
36
+ District Codes used in the validation procedures were acquired from the following [source](https://ntjwg.uwazi.io/entity/seif769joed?file=15512639038111am2iqd21an.pdf&page=9).
37
+
38
+
39
+ ## Usage
40
+
41
+ Install the package using the following command:
42
+
43
+ ```bash
44
+ npm install zimbabwean-id-number-validator
45
+ ```
46
+
47
+ ### Usage In ES6
48
+
49
+ ```javascript
50
+ import validateIdNumber from 'zimbabwean-id-number-validator'
51
+ const validationResults = validateIdNumber("082047823Q21")
52
+ ```
53
+
54
+ ### Usage in CommonJS
55
+
56
+ ```javascript
57
+ const validateIdNumber = require('zimbabwean-id-number-validator');
58
+ const validationResults = validateIdNumber("082047823Q21")
59
+ ```
60
+
61
+
package/constants.js ADDED
@@ -0,0 +1,92 @@
1
+ const validLetters = {
2
+ 0: "Z",
3
+ 1: "A",
4
+ 2: "B",
5
+ 3: "C",
6
+ 4: "D",
7
+ 5: "E",
8
+ 6: "F",
9
+ 7: "G",
10
+ 8: "H",
11
+ 9: "J",
12
+ 10: "K",
13
+ 11: "L",
14
+ 12: "M",
15
+ 13: "N",
16
+ 14: "P",
17
+ 15: "Q",
18
+ 16: "R",
19
+ 17: "S",
20
+ 18: "T",
21
+ 19: "V",
22
+ 20: "W",
23
+ 21: "X",
24
+ 22: "Y",
25
+ }
26
+
27
+ const validDistrictCodes = [
28
+ "08",
29
+ "63",
30
+ "07",
31
+ "44",
32
+ "13",
33
+ "42",
34
+ "75",
35
+ "50",
36
+ "34",
37
+ "05",
38
+ "71",
39
+ "15",
40
+ "45",
41
+ "11",
42
+ "61",
43
+ "68",
44
+ "18",
45
+ "25",
46
+ "80",
47
+ "43",
48
+ "49",
49
+ "47",
50
+ "48",
51
+ "59",
52
+ "85",
53
+ "32",
54
+ "38",
55
+ "24",
56
+ "37",
57
+ "70",
58
+ "86",
59
+ "04",
60
+ "14",
61
+ "13",
62
+ "27",
63
+ "22",
64
+ "54",
65
+ "83",
66
+ "06",
67
+ "35",
68
+ "79",
69
+ "41",
70
+ "53",
71
+ "73",
72
+ "84",
73
+ "02",
74
+ "56",
75
+ "28",
76
+ "21",
77
+ "39",
78
+ "19",
79
+ "77",
80
+ "26",
81
+ "23",
82
+ "29",
83
+ "58",
84
+ "03",
85
+ "66",
86
+ "67",
87
+ ]
88
+
89
+ module.exports = {
90
+ validLetters,
91
+ validDistrictCodes
92
+ }
package/index.js ADDED
@@ -0,0 +1,47 @@
1
+ const { validLetters, validDistrictCodes } = require("./constants")
2
+
3
+ const idNumberRegex = /^\d{2}-?\d{6,7}-?[A-HJ-NP-Z]-?\d{2}$/g;
4
+
5
+ const validateIdNumber = (idNumber) => {
6
+ // initialize id validation results
7
+ const idValidationResults = {
8
+ isIdNumberValid: false,
9
+ description: "id number must be in one of the formats: (082047823Q29), (08-2047823Q29), or (08-2047823-Q-29)"
10
+ }
11
+
12
+ // check if the id number is formatted correctly
13
+ const isIdFormatCorrect = idNumberRegex.test(idNumber)
14
+
15
+ if (isIdFormatCorrect) {
16
+ // check if the id number is valid
17
+ // extract the letter and application centre district code
18
+ const idLetter = idNumber.match(/[A-Z]/)[0]
19
+ const applicationCentreCode = idNumber.slice(0, 2)
20
+
21
+ // replace all hyphens and split the id number into an array using the id letter
22
+ const idNumberArray = idNumber.replace(/-/g, "").split(idLetter)
23
+ const placeOfOriginCode = idNumberArray[1]
24
+
25
+ // convert the id number digits before the letter into a number
26
+ const idDigitsBeforeLetter = parseInt(idNumberArray[0])
27
+ const numericLetterValue = idDigitsBeforeLetter % 23 // represents a key in the validLetters object
28
+
29
+ if (
30
+ validDistrictCodes.includes(applicationCentreCode) &&
31
+ idLetter === validLetters[numericLetterValue] &&
32
+ validDistrictCodes.includes(placeOfOriginCode)
33
+ ) {
34
+ // update idValidationResults and set the id number to valid
35
+ idValidationResults["isIdNumberValid"] = true
36
+ idValidationResults["description"] = "id number is valid"
37
+ }
38
+ else {
39
+ // update the invalid id number's description
40
+ idValidationResults["description"] = "id number is invalid"
41
+ }
42
+ }
43
+
44
+ return idValidationResults
45
+ }
46
+
47
+ module.exports = validateIdNumber
package/package.json ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "name": "zimbabwean-id-number-validator",
3
+ "version": "1.0.1",
4
+ "description": "Zimbabwean national id number validator",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "author": "Wellington Mpofu",
10
+ "license": "ISC"
11
+ }