-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
99 lines (86 loc) · 2.43 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// const dayjs = require("dayjs");
const moment = require("moment");
const source = require("./source.json");
/**
* Olah nik untuk mengetahui apakah valid atau tidak
*
* @param {number} nik
* @return {boolean}
* @private
*/
function isValid (nik) {
const result = parse(nik);
return result.valid;
}
/**
* Olah nik sehingga menghasilkan nilai valid, jenis kelamin, tanggal lahir, provinsi, kabupaten_kota, kecamatan, kodepos, kode unik
*
* @param {number} nik
* @return {object}
* @private
*/
function parse (nik) {
let resp = {
nik: nik,
valid: true,
};
try {
let data = {
jenis_kelamin: "LAKI-LAKI",
tanggal_lahir: "",
provinsi: "",
kabupaten_kota: "",
kecamatan: "",
kodepos: "",
};
const tmpNIK = nik.toString();
if(typeof nik != "number") {
throw new Error("NIK harus dalam bentuk angka");
}
if(tmpNIK.length != 16) {
throw new Error("Jumlah karakter NIK tidak sesuai");
}
let tglLahir = tmpNIK.substring(6, 8);
let blnLahir = tmpNIK.substring(8, 10);
let thnLahir = tmpNIK.substring(10, 12);
// proses pencarian jenis kelamin
tglLahir > 40 && (data.jenis_kelamin = "PEREMPUAN");
// proses pencarian tanggal lahir
const curThn = new Date().getFullYear().toString().substring(-2);
if(data.jenis_kelamin == "PEREMPUAN") {
tglLahir -= 40;
tglLahir = tglLahir.toString().length > 1 ? tglLahir : `0${tglLahir}`;
}
thnLahir = ( thnLahir < curThn ? `20${thnLahir}` : `19${thnLahir}`);
const tglLahirFull = `${thnLahir}-${blnLahir}-${tglLahir}`;
if(!moment(tglLahirFull).isValid()) {
throw new Error("Tanggal Lahir tidak sesuai");
}
data.tanggal_lahir = tglLahirFull;
// proses pencarian wilayah
const foundDistrict = source.some(prov =>
prov.kabupaten_kota.some(kabkot =>
kabkot.kecamatan.some(kec => {
const check = kec.kode === tmpNIK.substring(0, 6);
if(check) {
data.provinsi = prov.nama;
data.kabupaten_kota = kabkot.nama;
data.kecamatan = kec.nama;
data.kodepos = kec.kodepos;
}
return check;
})));
if(!foundDistrict) {
throw new Error("Wilayah tidak ditemukan");
};
resp = {...resp, ...data};
} catch (error) {
resp.valid = false;
resp.message = error.message;
}
return resp;
}
module.exports = {
parse,
isValid
};