ngx-nic-parser
Version:
A calculator of Date of birth and gender using NIC in SriLanka using Angular
230 lines (220 loc) • 11.6 kB
JavaScript
import * as i0 from '@angular/core';
import { Injectable, EventEmitter, Component, Input, Output, NgModule } from '@angular/core';
import * as i1 from '@angular/forms';
import { FormGroup, FormControl, Validators, ReactiveFormsModule, FormsModule } from '@angular/forms';
import * as i2 from '@angular/common';
import { CommonModule } from '@angular/common';
class NgxNicParserService {
constructor() { }
}
NgxNicParserService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0, type: NgxNicParserService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
NgxNicParserService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0, type: NgxNicParserService, providedIn: 'root' });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0, type: NgxNicParserService, decorators: [{
type: Injectable,
args: [{
providedIn: 'root'
}]
}], ctorParameters: function () { return []; } });
var Month;
(function (Month) {
Month[Month["JANUARY"] = 1] = "JANUARY";
Month[Month["FEBRUARY"] = 2] = "FEBRUARY";
Month[Month["MARCH"] = 3] = "MARCH";
Month[Month["APRIL"] = 4] = "APRIL";
Month[Month["MAY"] = 5] = "MAY";
Month[Month["JUNE"] = 6] = "JUNE";
Month[Month["JULY"] = 7] = "JULY";
Month[Month["AUGUST"] = 8] = "AUGUST";
Month[Month["SEPTEMBER"] = 9] = "SEPTEMBER";
Month[Month["OCTOBER"] = 10] = "OCTOBER";
Month[Month["NOVEMBER"] = 11] = "NOVEMBER";
Month[Month["DECEMBER"] = 12] = "DECEMBER";
})(Month || (Month = {}));
class BirthMonthAndDate {
constructor(day, monthName, month, year, gender, weekDay) {
this.day = day;
this.monthName = monthName;
this.month = month;
this.year = year;
this.gender = gender;
this.weekDay = weekDay;
}
}
class Response {
constructor(status, response) {
this.status = status;
this.response = response;
}
}
class NgxNicParserComponent {
constructor() {
this.id = '';
this.title = '';
this.requiredMessage = '';
this.patternErrorMessage = '';
this.placeholder = '';
this.clearNICNumber = '';
this.checkDob = new EventEmitter();
this.nicForm = new FormGroup({
nic: new FormControl('', [Validators.required, Validators.pattern("^([0-9]{9}[x|X|v|V]|[0-9]{12})")])
});
}
get nicNumber() {
return this.nicForm.get('nic');
}
ngOnInit() {
}
nicParser() {
if (this.nicForm.valid) {
let $nicNumber = this.nicForm.value.nic;
if ($nicNumber.length != 10 && $nicNumber.length != 12) {
this.checkDob.emit(new Response({ code: 404, message: 'Invalid Nic Number' }));
}
else if ($nicNumber.length == 10 && !($nicNumber.substr(0, 9))) {
this.checkDob.emit(new Response({ code: 404, message: 'Invalid Nic Number' }));
}
else {
let $year = this.getBirthYear($nicNumber);
let $dayText = this.getNumberOfDatesToDob($nicNumber);
const $gender = this.getGender($dayText);
$dayText = $dayText > 500 ? $dayText - 500 : $dayText;
const $obj = this.getBirthMonthAndDate($dayText);
const $dayName = this.getWeekDay($year, $obj.month, $obj.day);
const $res = Object.assign(Object.assign({}, $obj), { year: $year, gender: $gender, weekDay: $dayName });
this.checkDob.emit(new Response({ code: 200, message: '' }, $res));
}
}
}
getBirthYear($nicNumber) {
if ($nicNumber.length == 10) {
return Number("19" + $nicNumber.substr(0, 2));
}
else {
return Number($nicNumber.substr(0, 4));
}
}
getBirthMonthAndDate($dayText) {
if ($dayText < 1 && $dayText > 366) {
this.checkDob.emit(new Response({ code: 404, message: 'Invalid Nic Number' }));
}
else {
let b = this.isLeapYear();
if ($dayText > b[10]) {
return this.setBirthMonthAndDate(($dayText - b[10]), Month[Month.DECEMBER], Month.DECEMBER);
}
else if ($dayText > b[9]) {
return this.setBirthMonthAndDate(($dayText - b[9]), Month[Month.NOVEMBER], Month.NOVEMBER);
}
else if ($dayText > b[8]) {
return this.setBirthMonthAndDate(($dayText - b[8]), Month[Month.OCTOBER], Month.OCTOBER);
}
else if ($dayText > b[7]) {
return this.setBirthMonthAndDate(($dayText - b[7]), Month[Month.SEPTEMBER], Month.SEPTEMBER);
}
else if ($dayText > b[6]) {
return this.setBirthMonthAndDate(($dayText - b[6]), Month[Month.AUGUST], Month.AUGUST);
}
else if ($dayText > b[5]) {
return this.setBirthMonthAndDate(($dayText - b[5]), Month[Month.JULY], Month.JULY);
}
else if ($dayText > b[4]) {
return this.setBirthMonthAndDate(($dayText - b[4]), Month[Month.JUNE], Month.JUNE);
}
else if ($dayText > b[3]) {
return this.setBirthMonthAndDate(($dayText - b[3]), Month[Month.MAY], Month.MAY);
}
else if ($dayText > b[2]) {
return this.setBirthMonthAndDate(($dayText - b[2]), Month[Month.APRIL], Month.APRIL);
}
else if ($dayText > b[1]) {
return this.setBirthMonthAndDate(($dayText - b[1]), Month[Month.MARCH], Month.MARCH);
}
else if ($dayText < 32) {
return this.setBirthMonthAndDate(($dayText), Month[Month.JANUARY], Month.JANUARY);
}
else if ($dayText > 31) {
return this.setBirthMonthAndDate(($dayText - 31), Month[Month.FEBRUARY], Month.FEBRUARY);
}
}
}
setBirthMonthAndDate(day, monthName, monthNumber) {
return new BirthMonthAndDate(day, monthName, monthNumber);
}
getNumberOfDatesToDob($nicNumber) {
if ($nicNumber.length == 10) {
return parseInt($nicNumber.substr(2, 3));
}
else {
return parseInt($nicNumber.substr(4, 3));
}
}
getGender($numberOfDays) {
if ($numberOfDays > 500) {
return "Female";
}
else {
return "Male";
}
}
isLeapYear() {
return [31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366];
}
getWeekDay($year, $month, $day) {
return new Date($year, $month, $day).toLocaleString('en-us', { weekday: 'long' });
}
}
NgxNicParserComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0, type: NgxNicParserComponent, deps: [], target: i0.ɵɵFactoryTarget.Component });
NgxNicParserComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "12.0.0", version: "12.0.5", type: NgxNicParserComponent, selector: "ngx-nic-parser", inputs: { id: "id", title: "title", requiredMessage: "requiredMessage", patternErrorMessage: "patternErrorMessage", placeholder: "placeholder", clearNICNumber: "clearNICNumber" }, outputs: { checkDob: "checkDob" }, ngImport: i0, template: "<form [formGroup]=\"nicForm\">\r\n <div class=\"form-group\">\r\n <label for=\"{{id}}\">{{title}}</label>\r\n <input placeholder=\"{{placeholder}}\"\r\n type=\"text\" id=\"{{id}}\" name=\"{{id}}\"\r\n class=\"form-control\" [formControl]=\"nicNumber\"\r\n (keydown.enter)=\"nicParser()\">\r\n <div *ngIf=\"nicNumber?.touched && nicNumber?.invalid\" class=\"error-message\">\r\n <div *ngIf=\"nicNumber?.errors?.required\">{{requiredMessage}}</div>\r\n <div *ngIf=\"nicNumber?.errors?.pattern\">{{patternErrorMessage}}</div>\r\n </div>\r\n </div>\r\n</form>\r\n", styles: [".error-message{color:red;font-size:12px}\n"], directives: [{ type: i1.ɵNgNoValidate, selector: "form:not([ngNoForm]):not([ngNativeValidate])" }, { type: i1.NgControlStatusGroup, selector: "[formGroupName],[formArrayName],[ngModelGroup],[formGroup],form:not([ngNoForm]),[ngForm]" }, { type: i1.FormGroupDirective, selector: "[formGroup]", inputs: ["formGroup"], outputs: ["ngSubmit"], exportAs: ["ngForm"] }, { type: i1.DefaultValueAccessor, selector: "input:not([type=checkbox])[formControlName],textarea[formControlName],input:not([type=checkbox])[formControl],textarea[formControl],input:not([type=checkbox])[ngModel],textarea[ngModel],[ngDefaultControl]" }, { type: i1.NgControlStatus, selector: "[formControlName],[ngModel],[formControl]" }, { type: i1.FormControlDirective, selector: "[formControl]", inputs: ["disabled", "formControl", "ngModel"], outputs: ["ngModelChange"], exportAs: ["ngForm"] }, { type: i2.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0, type: NgxNicParserComponent, decorators: [{
type: Component,
args: [{
selector: 'ngx-nic-parser',
templateUrl: './ngx-nic-parser.component.html',
styleUrls: ['./ngx-nic-parser.component.css'],
}]
}], propDecorators: { id: [{
type: Input
}], title: [{
type: Input
}], requiredMessage: [{
type: Input
}], patternErrorMessage: [{
type: Input
}], placeholder: [{
type: Input
}], clearNICNumber: [{
type: Input
}], checkDob: [{
type: Output
}] } });
class NgxNicParserModule {
}
NgxNicParserModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0, type: NgxNicParserModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
NgxNicParserModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0, type: NgxNicParserModule, declarations: [NgxNicParserComponent], imports: [CommonModule,
ReactiveFormsModule, FormsModule], exports: [NgxNicParserComponent] });
NgxNicParserModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0, type: NgxNicParserModule, imports: [[
CommonModule,
ReactiveFormsModule, FormsModule
]] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0, type: NgxNicParserModule, decorators: [{
type: NgModule,
args: [{
declarations: [
NgxNicParserComponent
],
imports: [
CommonModule,
ReactiveFormsModule, FormsModule
],
exports: [NgxNicParserComponent]
// schemas: [CUSTOM_ELEMENTS_SCHEMA],
}]
}] });
/*
* Public API Surface of ngx-nic-parser
*/
/**
* Generated bundle index. Do not edit.
*/
export { NgxNicParserComponent, NgxNicParserModule, NgxNicParserService };
//# sourceMappingURL=ngx-nic-parser.js.map