Skip to content

Commit

Permalink
Merge pull request #30 from dknight/version-2.0.1
Browse files Browse the repository at this point in the history
Fixed issues #28, added src dir, added constants for gender.
  • Loading branch information
dknight authored Jul 13, 2022
2 parents b5e82de + a428d5e commit ee0cee2
Show file tree
Hide file tree
Showing 11 changed files with 149 additions and 124 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
tags
9 changes: 5 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
SRC := ./src
DEST := ./dest
ESBUILD := ./node_modules/.bin/esbuild
ESLINT := ./node_modules/.bin/eslint
Expand All @@ -6,7 +7,7 @@ JEST := ./node_modules/.bin/jest
all: build lint test

isikukood:
$(ESBUILD) isikukood.js --global-name=ik --format=iife\
$(ESBUILD) $(SRC)/isikukood.js --global-name=ik --format=iife\
--footer:js="window.Isikukood=ik.Isikukood;"\
--outfile=$(DEST)/isikukood.js

Expand All @@ -16,17 +17,17 @@ isikukood.min.js:
--outfile=$(DEST)/isikukood.min.js

isikukood.mjs:
$(ESBUILD) isikukood.js --format=esm\
$(ESBUILD) $(SRC)/isikukood.js --format=esm\
--outfile=$(DEST)/isikukood.mjs

isikukood.cjs.js:
$(ESBUILD) isikukood.js --format=cjs\
$(ESBUILD) $(SRC)/isikukood.js --format=cjs\
--outfile=$(DEST)/isikukood.cjs.js

build: isikukood isikukood.min.js isikukood.mjs isikukood.cjs.js

lint:
$(ESLINT) isikukood.js
$(ESLINT) $(SRC)/isikukood.js

test:
$(call lint)
Expand Down
34 changes: 31 additions & 3 deletions __tests__/isikukood.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,35 @@ describe('Generated Isikukood', () => {
it(code + ' should be valid', () => {
let ik = new Isikukood(code);
expect(ik.validate()).toBe(true);
})
});
});
});

describe('It should generate female code (#28)', () => {
Array(200)
.fill(null)
.map((_) => Isikukood.generate({
gender: Isikukood.GENDER.FEMALE
}))
.forEach(code => {
it('should be female code', () => {
let ik = new Isikukood(code);
expect(ik.getGender()).toBe(Isikukood.GENDER.FEMALE);
});
});
});

describe('It should generate male code (#28)', () => {
Array(200)
.fill(null)
.map((_) => Isikukood.generate({
gender: Isikukood.GENDER.MALE
}))
.forEach(code => {
it('should be female code', () => {
let ik = new Isikukood(code);
expect(ik.getGender()).toBe(Isikukood.GENDER.MALE);
});
});
});
});
Expand All @@ -58,7 +86,7 @@ describe('Isikukood 35703150220', () => {
expect(ik.validate()).toBe(true);
});
it('should return male as a gender', () => {
expect(ik.getGender()).toBe('male');
expect(ik.getGender()).toBe(Isikukood.GENDER.MALE);
});
it('should return age correctly', () => {
const correct_bd = new Date(1957, 2, 15);
Expand All @@ -84,7 +112,7 @@ describe('Isikukood 48709172756', () => {
expect(ik.validate()).toBe(false);
});
it('should return female as a gender', () => {
expect(ik.getGender()).toBe('female');
expect(ik.getGender()).toBe(Isikukood.GENDER.FEMALE);
});
it('should return correct age', () => {
var correct_bd = new Date(1987, 8, 17);
Expand Down
33 changes: 20 additions & 13 deletions dest/isikukood.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ class Isikukood {
set code(c) {
this._code = String(c);
}
static get GENDER() {
return {
MALE: "male",
FEMALE: "female",
UNKNOWN: "unknown"
};
}
getControlNumber(code) {
if (!code) {
code = this.code;
Expand Down Expand Up @@ -78,15 +85,15 @@ class Isikukood {
case "1":
case "3":
case "5":
retval = "male";
retval = this.constructor.GENDER.MALE;
break;
case "2":
case "4":
case "6":
retval = "female";
retval = this.constructor.GENDER.FEMALE;
break;
default:
retval = "unknown";
retval = this.constructor.GENDER.UNKNOWN;
}
return retval;
}
Expand Down Expand Up @@ -123,7 +130,7 @@ class Isikukood {
let y;
let m;
let d;
let gender = params.gender || Math.round(Math.random()) === 0 ? "male" : "female";
let gender = params.gender || (Math.round(Math.random()) === 0 ? this.GENDER.MALE : this.GENDER.FEMALE);
let personalId = "";
const hospitals = [
"00",
Expand All @@ -142,7 +149,7 @@ class Isikukood {
"70",
"95"
];
if (!(gender === "female" || gender === "male")) {
if (![this.GENDER.MALE, this.GENDER.FEMALE].includes(gender)) {
return "";
}
if (params.birthYear) {
Expand All @@ -161,21 +168,21 @@ class Isikukood {
var daysInMonth = new Date(y, m, 0).getDate();
d = Math.floor(Math.random() * daysInMonth) + 1;
}
if (gender === "male" && y >= 1800 && y <= 1899) {
if (gender === this.GENDER.MALE && y >= 1800 && y <= 1899) {
personalId += "1";
} else if (gender === "female" && y >= 1800 && y <= 1899) {
} else if (gender === this.GENDER.FEMALE && y >= 1800 && y <= 1899) {
personalId += "2";
} else if (gender === "male" && y >= 1900 && y <= 1999) {
} else if (gender === this.GENDER.MALE && y >= 1900 && y <= 1999) {
personalId += "3";
} else if (gender === "female" && y >= 1900 && y <= 1999) {
} else if (gender === this.GENDER.FEMALE && y >= 1900 && y <= 1999) {
personalId += "4";
} else if (gender === "male" && y >= 2e3 && y <= 2099) {
} else if (gender === this.GENDER.MALE && y >= 2e3 && y <= 2099) {
personalId += "5";
} else if (gender === "female" && y >= 2e3 && y <= 2099) {
} else if (gender === this.GENDER.FEMALE && y >= 2e3 && y <= 2099) {
personalId += "6";
} else if (gender === "male" && y >= 2100 && y <= 2199) {
} else if (gender === this.GENDER.MALE && y >= 2100 && y <= 2199) {
personalId += "7";
} else if (gender === "female" && y >= 2100 && y <= 2199) {
} else if (gender === this.GENDER.FEMALE && y >= 2100 && y <= 2199) {
personalId += "8";
} else {
return "";
Expand Down
33 changes: 20 additions & 13 deletions dest/isikukood.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ var ik = (() => {
set code(c) {
this._code = String(c);
}
static get GENDER() {
return {
MALE: "male",
FEMALE: "female",
UNKNOWN: "unknown"
};
}
getControlNumber(code) {
if (!code) {
code = this.code;
Expand Down Expand Up @@ -78,15 +85,15 @@ var ik = (() => {
case "1":
case "3":
case "5":
retval = "male";
retval = this.constructor.GENDER.MALE;
break;
case "2":
case "4":
case "6":
retval = "female";
retval = this.constructor.GENDER.FEMALE;
break;
default:
retval = "unknown";
retval = this.constructor.GENDER.UNKNOWN;
}
return retval;
}
Expand Down Expand Up @@ -123,7 +130,7 @@ var ik = (() => {
let y;
let m;
let d;
let gender = params.gender || Math.round(Math.random()) === 0 ? "male" : "female";
let gender = params.gender || (Math.round(Math.random()) === 0 ? this.GENDER.MALE : this.GENDER.FEMALE);
let personalId = "";
const hospitals = [
"00",
Expand All @@ -142,7 +149,7 @@ var ik = (() => {
"70",
"95"
];
if (!(gender === "female" || gender === "male")) {
if (![this.GENDER.MALE, this.GENDER.FEMALE].includes(gender)) {
return "";
}
if (params.birthYear) {
Expand All @@ -161,21 +168,21 @@ var ik = (() => {
var daysInMonth = new Date(y, m, 0).getDate();
d = Math.floor(Math.random() * daysInMonth) + 1;
}
if (gender === "male" && y >= 1800 && y <= 1899) {
if (gender === this.GENDER.MALE && y >= 1800 && y <= 1899) {
personalId += "1";
} else if (gender === "female" && y >= 1800 && y <= 1899) {
} else if (gender === this.GENDER.FEMALE && y >= 1800 && y <= 1899) {
personalId += "2";
} else if (gender === "male" && y >= 1900 && y <= 1999) {
} else if (gender === this.GENDER.MALE && y >= 1900 && y <= 1999) {
personalId += "3";
} else if (gender === "female" && y >= 1900 && y <= 1999) {
} else if (gender === this.GENDER.FEMALE && y >= 1900 && y <= 1999) {
personalId += "4";
} else if (gender === "male" && y >= 2e3 && y <= 2099) {
} else if (gender === this.GENDER.MALE && y >= 2e3 && y <= 2099) {
personalId += "5";
} else if (gender === "female" && y >= 2e3 && y <= 2099) {
} else if (gender === this.GENDER.FEMALE && y >= 2e3 && y <= 2099) {
personalId += "6";
} else if (gender === "male" && y >= 2100 && y <= 2199) {
} else if (gender === this.GENDER.MALE && y >= 2100 && y <= 2199) {
personalId += "7";
} else if (gender === "female" && y >= 2100 && y <= 2199) {
} else if (gender === this.GENDER.FEMALE && y >= 2100 && y <= 2199) {
personalId += "8";
} else {
return "";
Expand Down
2 changes: 1 addition & 1 deletion dest/isikukood.min.js

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

Loading

0 comments on commit ee0cee2

Please sign in to comment.