summaryrefslogtreecommitdiff
path: root/south-african-id-parser.js
diff options
context:
space:
mode:
authorJustin Wernick <justin@worthe-it.co.za>2023-08-04 21:57:35 +0200
committerJustin Wernick <justin@worthe-it.co.za>2023-08-07 10:41:44 +0200
commita0d4c3c3447e07ba66ce4d8882d051eafe2d4d28 (patch)
tree8fac49344785d4b90cd293c0c1f4585fdf5a01f3 /south-african-id-parser.js
parent6a158dd03c24fb7d7e66b7b93a4906702020ca22 (diff)
Add typescript types to the package
This doesn't change any internal workings, but makes this package easier to use by anyone using Typescript. Fix #12
Diffstat (limited to 'south-african-id-parser.js')
-rw-r--r--south-african-id-parser.js72
1 files changed, 40 insertions, 32 deletions
diff --git a/south-african-id-parser.js b/south-african-id-parser.js
index 983647a..75637bb 100644
--- a/south-african-id-parser.js
+++ b/south-african-id-parser.js
@@ -17,11 +17,11 @@
* Parsing result for a valid South African ID number.
*
* @typedef {Object} ValidIDParseResult
- * @property {bool} isValid - true
+ * @property {boolean} isValid - true
* @property {Date} dateOfBirth - The date of birth from the ID number.
- * @property {bool} isMale - The sex from the ID number - true if male, false if female.
- * @property {bool} isFemale - The sex from the ID number - true if female, false if male.
- * @property {bool} isSouthAfricanCitizen - Citizenship status from the ID
+ * @property {boolean} isMale - The sex from the ID number - true if male, false if female.
+ * @property {boolean} isFemale - The sex from the ID number - true if female, false if male.
+ * @property {boolean} isSouthAfricanCitizen - Citizenship status from the ID
* number, true if it indicates South African citizenship.
*/
@@ -29,7 +29,7 @@
* Parsing result for a invalid South African ID number.
*
* @typedef {Object} InvalidIDParseResult
- * @property {bool} isValid - false
+ * @property {boolean} isValid - false
*/
return {
@@ -64,7 +64,22 @@
* // isValid: false
* // }
*/
- parse: parse,
+ parse: function(idNumber) {
+ var isValid = validate(idNumber);
+ if (!isValid) {
+ return {
+ isValid: false
+ };
+ }
+
+ return {
+ isValid: isValid,
+ dateOfBirth: parseDateOfBirth(idNumber),
+ isMale: parseIsMale(idNumber),
+ isFemale: parseIsFemale(idNumber),
+ isSouthAfricanCitizen: parseIsSouthAfricanCitizen(idNumber)
+ };
+ },
/**
* Validates an ID number.
@@ -75,7 +90,7 @@
*
* @function
* @param {string} idNumber - The ID number to be validated.
- * @return {bool} True if the ID number is a valid South African ID number.
+ * @return {boolean} True if the ID number is a valid South African ID number.
*
* @example
* var saIdParser = require('south-african-id-parser');
@@ -84,7 +99,9 @@
*
* // valid === true
*/
- validate: validate,
+ validate: function(idNumber) {
+ return validate(idNumber);
+ },
/**
* Parses the date of birth out of an ID number.
@@ -119,7 +136,9 @@
*
* // dateOfBirth === new Date(1990, 0, 4)
*/
- parseDateOfBirth: parseDateOfBirth,
+ parseDateOfBirth: function(idNumber) {
+ return parseDateOfBirth(idNumber);
+ },
/**
* Parses the sex out of the ID number and returns true it is male.
@@ -129,7 +148,7 @@
*
* @function
* @param {string} idNumber - The ID number to be parsed.
- * @return {?bool} True if male, false if female. Returns undefined if the
+ * @return {?boolean} True if male, false if female. Returns undefined if the
* ID number is not a 13 digit number.
*
* @example
@@ -139,7 +158,9 @@
*
* // isMale === true
*/
- parseIsMale: parseIsMale,
+ parseIsMale: function(idNumber) {
+ return parseIsMale(idNumber);
+ },
/**
* Parses the sex out of the ID number and returns true it is female.
@@ -149,7 +170,7 @@
*
* @function
* @param {string} idNumber - The ID number to be parsed.
- * @return {?bool} True if female, false if male. Returns undefined if the
+ * @return {?boolean} True if female, false if male. Returns undefined if the
* ID number is not a 13 digit number.
* @example
* var saIdParser = require('south-african-id-parser');
@@ -158,7 +179,9 @@
*
* // isFemale === false
*/
- parseIsFemale: parseIsFemale,
+ parseIsFemale: function(idNumber) {
+ return parseIsFemale(idNumber);
+ },
/**
* Parses the citizenship status out of an ID number and returns true if it
@@ -169,7 +192,7 @@
*
* @function
* @param {string} idNumber - The ID number to be parsed.
- * @return {?bool} True if the ID number belongs to a South African
+ * @return {?boolean} True if the ID number belongs to a South African
* citizen. Returns undefined if the ID number is not a 13 digit number.
*
* @example
@@ -179,25 +202,10 @@
*
* // isSouthAfricanCitizen === true
*/
- parseIsSouthAfricanCitizen: parseIsSouthAfricanCitizen
- };
-
- function parse(idNumber) {
- var isValid = validate(idNumber);
- if (!isValid) {
- return {
- isValid: false
- };
+ parseIsSouthAfricanCitizen: function(idNumber) {
+ return parseIsSouthAfricanCitizen(idNumber);
}
-
- return {
- isValid: isValid,
- dateOfBirth: parseDateOfBirth(idNumber),
- isMale: parseIsMale(idNumber),
- isFemale: parseIsFemale(idNumber),
- isSouthAfricanCitizen: parseIsSouthAfricanCitizen(idNumber)
- };
- }
+ };
function validate(idNumber) {
if (!regexpValidate(idNumber) || !datePartValidate(idNumber) || !controlDigitValidate(idNumber)) {