293 lines
8.8 KiB
TypeScript
293 lines
8.8 KiB
TypeScript
import HttpStatus from "../interfaces/http-status";
|
|
import HttpError from "../interfaces/http-error";
|
|
class Extension {
|
|
|
|
public static EvaluationResult(value: string) {
|
|
switch (value) {
|
|
case "PENDING":
|
|
return "รอดำเนินการ";
|
|
case "PASSED":
|
|
return "ผ่านการประเมิน";
|
|
case "NOTPASSED":
|
|
return "ไม่ผ่านการประเมิน";
|
|
case "EXCELLENT":
|
|
return "ดีเด่น";
|
|
case "VERY_GOOD":
|
|
return "ดีมาก";
|
|
case "GOOD":
|
|
return "ดี";
|
|
case "FAIR":
|
|
return "พอใช้";
|
|
case "IMPROVEMENT":
|
|
return "ต้องปรับปรุง";
|
|
default:
|
|
return "";
|
|
}
|
|
}
|
|
|
|
public static textPoint(val: number | undefined) {
|
|
if (val == undefined) val = -1;
|
|
if (val >= 0 && val <= 60) return "ต้องปรับปรุง";
|
|
if (val >= 60 && val <= 69) return "พอใช้";
|
|
if (val >= 70 && val <= 79) return "ดี";
|
|
if (val >= 80 && val <= 89) return "ดีมาก";
|
|
if (val >= 90 && val <= 100) return "ดีเด่น";
|
|
else return "-";
|
|
}
|
|
|
|
public static ToThaiMonth(value: number) {
|
|
switch (value) {
|
|
case 1:
|
|
return "มกราคม";
|
|
case 2:
|
|
return "กุมภาพันธ์";
|
|
case 3:
|
|
return "มีนาคม";
|
|
case 4:
|
|
return "เมษายน";
|
|
case 5:
|
|
return "พฤษภาคม";
|
|
case 6:
|
|
return "มิถุนายน";
|
|
case 7:
|
|
return "กรกฎาคม";
|
|
case 8:
|
|
return "สิงหาคม";
|
|
case 9:
|
|
return "กันยายน";
|
|
case 10:
|
|
return "ตุลาคม";
|
|
case 11:
|
|
return "พฤศจิกายน";
|
|
case 12:
|
|
return "ธันวาคม";
|
|
default:
|
|
return "";
|
|
}
|
|
}
|
|
|
|
public static ToThaiShortMonth(value: number) {
|
|
switch (value) {
|
|
case 1:
|
|
return "ม.ค.";
|
|
case 2:
|
|
return "ก.พ.";
|
|
case 3:
|
|
return "มี.ค.";
|
|
case 4:
|
|
return "เม.ย.";
|
|
case 5:
|
|
return "พ.ค.";
|
|
case 6:
|
|
return "มิ.ย.";
|
|
case 7:
|
|
return "ก.ค.";
|
|
case 8:
|
|
return "ส.ค.";
|
|
case 9:
|
|
return "ก.ย.";
|
|
case 10:
|
|
return "ต.ค.";
|
|
case 11:
|
|
return "พ.ย.";
|
|
case 12:
|
|
return "ธ.ค.";
|
|
default:
|
|
return "";
|
|
}
|
|
}
|
|
|
|
public static ToThaiYear(value: number) {
|
|
if (value < 2400) return value + 543;
|
|
else return value;
|
|
}
|
|
|
|
public static ToCeYear(value: number) {
|
|
if (value >= 2400) return value - 543;
|
|
else return value;
|
|
}
|
|
|
|
public static ToThaiNumber(value: string) {
|
|
let arabicNumbers = "0123456789";
|
|
let thaiNumbers = "๐๑๒๓๔๕๖๗๘๙";
|
|
let result = "";
|
|
for (let digit of value) {
|
|
let index = arabicNumbers.indexOf(digit);
|
|
if (index >= 0) {
|
|
result += thaiNumbers[index];
|
|
} else {
|
|
result += digit;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public static ToThaiFullDate(value: Date) {
|
|
let yy = value.getFullYear() < 2400 ? value.getFullYear() + 543 : value.getFullYear();
|
|
return (
|
|
"วันที่ " +
|
|
value.getDate() +
|
|
" เดือน " +
|
|
Extension.ToThaiMonth(value.getMonth() + 1) +
|
|
" พ.ศ. " +
|
|
yy
|
|
);
|
|
}
|
|
|
|
public static ToThaiFullDate2(value: Date) {
|
|
let yy = value.getFullYear() < 2400 ? value.getFullYear() + 543 : value.getFullYear();
|
|
return (
|
|
value.getDate() +
|
|
" " +
|
|
Extension.ToThaiMonth(value.getMonth() + 1) +
|
|
" " +
|
|
yy
|
|
);
|
|
}
|
|
|
|
public static ToThaiShortDate(value: Date) {
|
|
let yy = value.getFullYear() < 2400 ? value.getFullYear() + 543 : value.getFullYear();
|
|
return (
|
|
"วันที่ " +
|
|
value.getDate() +
|
|
" " +
|
|
Extension.ToThaiShortMonth(value.getMonth() + 1) +
|
|
" " +
|
|
yy.toString().slice(-2)
|
|
);
|
|
}
|
|
|
|
public static ToThaiShortDate_noPrefix(value: Date) {
|
|
let yy = value.getFullYear() < 2400 ? value.getFullYear() + 543 : value.getFullYear();
|
|
return (
|
|
value.getDate() +
|
|
" " +
|
|
Extension.ToThaiShortMonth(value.getMonth() + 1) +
|
|
" " +
|
|
yy.toString().slice(-2)
|
|
);
|
|
}
|
|
|
|
public static ToThaiShortDate_monthYear(value: Date) {
|
|
let yy = value.getFullYear() < 2400 ? value.getFullYear() + 543 : value.getFullYear();
|
|
return value.getDate() + " เดือน " + Extension.ToThaiMonth(value.getMonth() + 1) + " พ.ศ. " + yy;
|
|
}
|
|
|
|
public static sumObjectValues(array: any, propertyName: any) {
|
|
let sum = 0;
|
|
for (let i = 0; i < array.length; i++) {
|
|
if (array[i][propertyName] !== undefined) {
|
|
sum += array[i][propertyName];
|
|
}
|
|
}
|
|
return sum;
|
|
}
|
|
|
|
public static CheckCitizen(value: string) {
|
|
let citizen = value
|
|
if (citizen.length !== 13) {
|
|
throw new HttpError(HttpStatus.NOT_FOUND, "กรุณากรอกข้อมูลรหัสบัตรประจำตัวประชาชนให้ครบ 13 หลัก",);
|
|
}
|
|
const citizenIdDigits = citizen.toString().split("").map(Number);
|
|
const cal =
|
|
citizenIdDigits[0] * 13 +
|
|
citizenIdDigits[1] * 12 +
|
|
citizenIdDigits[2] * 11 +
|
|
citizenIdDigits[3] * 10 +
|
|
citizenIdDigits[4] * 9 +
|
|
citizenIdDigits[5] * 8 +
|
|
citizenIdDigits[6] * 7 +
|
|
citizenIdDigits[7] * 6 +
|
|
citizenIdDigits[8] * 5 +
|
|
citizenIdDigits[9] * 4 +
|
|
citizenIdDigits[10] * 3 +
|
|
citizenIdDigits[11] * 2;
|
|
const calStp2 = cal % 11;
|
|
let chkDigit = 11 - calStp2;
|
|
if (chkDigit === 10) {
|
|
chkDigit = 1;
|
|
}
|
|
else if (chkDigit === 11) {
|
|
chkDigit = chkDigit % 10;
|
|
}
|
|
|
|
if (citizenIdDigits[12] !== chkDigit) {
|
|
throw new HttpError(HttpStatus.NOT_FOUND, "ข้อมูลรหัสบัตรประจำตัวประชาชนไม่ถูกต้อง");
|
|
}
|
|
return citizen;
|
|
}
|
|
|
|
public static CalculateGovAge(appointDate: Date, plusYear: number = 0, subtractYear: number = 0): number {
|
|
if (appointDate == null || appointDate == undefined) return 0
|
|
const now = new Date();
|
|
if (now.getMonth() - appointDate.getMonth() >= 6) {
|
|
return (now.getFullYear() - appointDate.getFullYear()) + 1 + plusYear - subtractYear;
|
|
}
|
|
else {
|
|
return (now.getFullYear() - appointDate.getFullYear()) + plusYear - subtractYear;
|
|
}
|
|
}
|
|
|
|
public static CalculateAge(appointDate: Date, plusYear: number = 0, subtractYear: number = 0) {
|
|
let currentDate = new Date().getTime();
|
|
let appointDateTime = new Date(appointDate).getTime();
|
|
let ageInMilliseconds = currentDate - appointDateTime;
|
|
let ageInDays = ageInMilliseconds / (1000 * 60 * 60 * 24);
|
|
let years = Math.floor(ageInDays / 365.25) + plusYear - subtractYear;
|
|
return years;
|
|
}
|
|
|
|
public static CalculateAgeStrV2(date: Date, plusYear: number = 0, subtractYear: number = 0) {
|
|
if (date == null || date == undefined) return ""
|
|
const currentDate = new Date();
|
|
if (date > currentDate) {
|
|
throw new Error("วันเกิดต้องไม่มากกว่าวันที่ปัจจุบัน");
|
|
}
|
|
|
|
let years = currentDate.getFullYear() - date.getFullYear();
|
|
let months = 0;
|
|
let days = 0;
|
|
|
|
if (currentDate.getMonth() < date.getMonth() || (currentDate.getMonth() === date.getMonth() && currentDate.getDate() < date.getDate())) {
|
|
years--;
|
|
months = 12 - date.getMonth() + currentDate.getMonth();
|
|
|
|
if (currentDate.getDate() < date.getDate()) {
|
|
months--;
|
|
let lastMonthDays = 0;
|
|
if (currentDate.getMonth() === 0) {
|
|
lastMonthDays = new Date(currentDate.getFullYear() - 1, 11, 0).getDate();
|
|
}
|
|
else {
|
|
lastMonthDays = new Date(currentDate.getFullYear(), currentDate.getMonth(), 0).getDate();
|
|
days = lastMonthDays - date.getDate() + currentDate.getDate();
|
|
}
|
|
}
|
|
else {
|
|
days = currentDate.getDate() - date.getDate();
|
|
}
|
|
}
|
|
else {
|
|
months = currentDate.getMonth() - date.getMonth();
|
|
|
|
if (currentDate.getDate() < date.getDate()) {
|
|
months--;
|
|
let lastMonthDays = 0;
|
|
if (currentDate.getMonth() === 0) {
|
|
lastMonthDays = new Date(currentDate.getFullYear() - 1, 11, 0).getDate();
|
|
}
|
|
else {
|
|
lastMonthDays = new Date(currentDate.getFullYear(), currentDate.getMonth(), 0).getDate();
|
|
days = lastMonthDays - date.getDate() + currentDate.getDate();
|
|
}
|
|
}
|
|
else {
|
|
days = currentDate.getDate() - date.getDate();
|
|
}
|
|
}
|
|
years += plusYear - subtractYear;
|
|
return `${years} ปี ${months} เดือน ${days} วัน`;
|
|
}
|
|
}
|
|
|
|
export default Extension;
|