diff --git a/README.md b/README.md
index 7241e9c..ff0a245 100644
--- a/README.md
+++ b/README.md
@@ -66,62 +66,24 @@ const maleIk = Isikukood.generate({
## API
-
-
- Method |
- Description |
- Arguments |
- Return |
-
-
- new Isikukood() |
- constructor |
- number|string |
- Isikukood |
-
-
- validate() |
- Validates personal ID |
- - |
- boolean |
-
-
- generate() |
- Static function generates a valid personal ID. |
- Object { gender: "male|female", birthDay: day, birthMonth: month, birthYear: year }.r Month are beginning from 1, eg. 1 is January, 2 is February etc. |
- string |
-
-
- getGender() |
- Get the gender of a person. |
- - |
- string |
-
-
- getBirthday() |
- Get the birthday of a person. |
- - |
- Date |
-
-
- getAge() |
- Get the birthday of a person in years. |
- - |
- number |
-
-
- getControlNumber() |
- Gets the control number of personal ID |
- - |
- number |
-
-
- parse() |
- Parses the code and return it's data as object. |
- - |
- object |
-
-
+### Instance methods
+
+| Method | Description | Arguments | Return |
+| ------------------ | --------------------------------------------- | ---------------- | ------------ |
+| new Isikukood() | constructor | number \| string | Isikukood |
+| validate() | Validates personal ID | - | boolean |
+| generate() | Static function generates a valid personal ID | GenerateInput | string |
+| getGender() | Get the gender of a person | - | Gender |
+| getBirthday() | Get the birthday of a person | - | Date |
+| getAge() | Get the birthday of a person in years | - | number |
+| getControlNumber() | Gets the control number of personal ID | - | number |
+| parse() | Parses the code | - | PersonalData |
+
+### Static methods
+
+| Method | Description | Arguments | Return |
+| ------- | --------------- | ---------------- | ------------ |
+| parse() | Parses the code | number \| string | PersonalData |
## Development
diff --git a/__tests__/isikukood.test.ts b/__tests__/isikukood.test.ts
index 5ff205c..833bbcd 100644
--- a/__tests__/isikukood.test.ts
+++ b/__tests__/isikukood.test.ts
@@ -1,4 +1,4 @@
-import Isikukood, {Gender} from '../index';
+import Isikukood, {Gender, PersonalData} from '../index';
/**
* Isikukood test suite.
*
@@ -118,6 +118,19 @@ describe('Isikukood 35703150220', () => {
it('should have controlNumber 0', () => {
expect(ik.getControlNumber()).toEqual(0);
});
+
+ it('should parse the id', () => {
+ const expBD = new Date(1957, 2, 15);
+ const exp: PersonalData = {
+ gender: Gender.MALE,
+ age: getAge(expBD),
+ birthDay: expBD,
+ };
+ let got = Isikukood.parse(ik.code);
+ expect(got).toStrictEqual(exp);
+ got = ik.parse();
+ expect(got).toStrictEqual(exp);
+ });
});
describe('Isikukood 48709172756', () => {
@@ -166,7 +179,6 @@ describe('Isikukood 28709172754', () => {
describe('Isikukood 49200186017', () => {
const ik: Isikukood = new Isikukood('49200186017');
-
it('should not validate', () => {
expect(ik.validate()).toBe(false);
});
@@ -177,6 +189,19 @@ describe('Isikukood 60311213742 (#5)', () => {
it('should validate', () => {
expect(ik.validate()).toBe(true);
});
+
+ it('should parse the id', () => {
+ const expBD = new Date(2003, 10, 21);
+ const exp: PersonalData = {
+ gender: Gender.FEMALE,
+ age: getAge(expBD),
+ birthDay: expBD,
+ };
+ let got: PersonalData = Isikukood.parse(ik.code);
+ expect(got).toStrictEqual(exp);
+ got = ik.parse();
+ expect(got).toStrictEqual(exp);
+ });
});
describe('Isikukood as a number', () => {
diff --git a/dist/isikukood.esm.js b/dist/isikukood.esm.js
index d4a1da8..3314d8f 100644
--- a/dist/isikukood.esm.js
+++ b/dist/isikukood.esm.js
@@ -84,14 +84,15 @@ class Isikukood {
}
return new Date(year, month, day);
}
- parse(code = "") {
- if (!code) {
- code = this.code;
- }
+ parse() {
+ return Isikukood.parse(this.code);
+ }
+ static parse(code) {
+ const ik = new this(code);
const data = {
- gender: this.getGender(),
- birthDay: this.getBirthday(),
- age: this.getAge()
+ gender: ik.getGender(),
+ birthDay: ik.getBirthday(),
+ age: ik.getAge()
};
return data;
}
diff --git a/dist/isikukood.esm.min.js b/dist/isikukood.esm.min.js
index c7cb0d1..1a9af3c 100644
--- a/dist/isikukood.esm.min.js
+++ b/dist/isikukood.esm.min.js
@@ -1,3 +1,3 @@
"use strict";// isikukood.esm.js, https://github.com/dknight/Isikukood-js
-class u{constructor(t){this._code=String(t)}get code(){return this._code}set code(t){this._code=String(t)}getControlNumber(t=""){t||(t=this.code);const r=[1,2,3,4,5,6,7,8,9,1],i=[3,4,5,6,7,8,9,1,2,3];let o=0,n=0;for(let e=0;e<10;++e)n+=Number(t.charAt(e))*r[e];if(o=n%11,n=0,o===10){for(let e=0;e<10;++e)n+=Number(t.charAt(e))*i[e];o=n%11,o===10&&(o=0)}return o}validate(){if(this.code.charAt(0)==="0"||this.code.length!==11||this.getControlNumber()!==Number(this.code.charAt(10)))return!1;const r=Number(this.code.substring(1,3)),i=Number(this.code.substring(3,5)),o=Number(this.code.substring(5,7)),n=this.getBirthday();return r===n.getFullYear()%100&&n.getMonth()+1===i&&o===n.getDate()}getGender(){const t=this.code.charAt(0);let r;switch(t){case"1":case"3":case"5":r=s.MALE;break;case"2":case"4":case"6":r=s.FEMALE;break;default:r=s.UNKNOWN}return r}getAge(){return Math.floor((Date.now()-this.getBirthday().getTime())/(86400*1e3)/365.25)}getBirthday(){let t=Number(this.code.substring(1,3));const r=Number(this.code.substring(3,5).replace(/^0/,""))-1,i=Number(this.code.substring(5,7).replace(/^0/,"")),o=this.code.charAt(0);for(let n=1,e=1800;n<=8;n+=2,e+=100)[n,n+1].map(String).includes(o)&&(t+=e);return new Date(t,r,i)}parse(t=""){return t||(t=this.code),{gender:this.getGender(),birthDay:this.getBirthday(),age:this.getAge()}}static generate(t={}){let r,i,o;const n=t.gender||(Math.round(Math.random())===0?s.MALE:s.FEMALE);let e="";const l=["00","01","02","22","27","37","42","47","49","52","57","60","65","70","95"];if(![s.MALE,s.FEMALE].includes(n))return"";if(t.birthYear?r=t.birthYear:r=Math.round(Math.random()*100+1900+(new Date().getFullYear()-2e3)),t.birthMonth?i=t.birthMonth:i=Math.floor(Math.random()*12)+1,t.birthDay)o=t.birthDay;else{const a=new Date(r,i,0).getDate();o=Math.floor(Math.random()*a)+1}for(let a=1800,c=2;a<=2100;a+=100,c+=2)if(r>=a&&r