Skip to content

Commit

Permalink
feat: added static parse() method, refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
dknight committed May 19, 2023
1 parent 1bbfce9 commit a70dfbd
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 106 deletions.
74 changes: 18 additions & 56 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,62 +66,24 @@ const maleIk = Isikukood.generate({

## API

<table class="table1">
<tr>
<th>Method</th>
<th>Description</th>
<th>Arguments</th>
<th>Return</th>
</tr>
<tr>
<td>new Isikukood()</td>
<td>constructor</td>
<td>number|string</td>
<td>Isikukood</td>
</tr>
<tr>
<td>validate()</td>
<td>Validates personal ID</td>
<td>-</td>
<td>boolean</td>
</tr>
<tr>
<td>generate()</td>
<td>Static function generates a valid personal ID.</td>
<td>Object { gender: "male|female", birthDay: day, birthMonth: month, birthYear: year }.r Month are beginning from 1, eg. 1 is January, 2 is February etc.</td>
<td>string</td>
</tr>
<tr>
<td>getGender()</td>
<td>Get the gender of a person.</td>
<td>-</td>
<td>string</td>
</tr>
<tr>
<td>getBirthday()</td>
<td>Get the birthday of a person.</td>
<td>-</td>
<td>Date</td>
</tr>
<tr>
<td>getAge()</td>
<td>Get the birthday of a person in years.</td>
<td>-</td>
<td>number</td>
</tr>
<tr>
<td>getControlNumber()</td>
<td>Gets the control number of personal ID</td>
<td>-</td>
<td>number</td>
</tr>
<tr>
<td>parse()</td>
<td>Parses the code and return it's data as object.</td>
<td>-</td>
<td>object</td>
</tr>
</table>
### 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

Expand Down
29 changes: 27 additions & 2 deletions __tests__/isikukood.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Isikukood, {Gender} from '../index';
import Isikukood, {Gender, PersonalData} from '../index';
/**
* Isikukood test suite.
*
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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);
});
Expand All @@ -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', () => {
Expand Down
15 changes: 8 additions & 7 deletions dist/isikukood.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion dist/isikukood.esm.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a70dfbd

Please sign in to comment.