-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
143 lines (128 loc) · 5.48 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const Validation = require( "./utills/inputValidation");
const formating = require("./utills/formats");
const prepration = require( "./prepration");
function ZaravandDate() {
// قالبهای تاریخی پیشفرض جهت تایین نوع ورودی
this.formats = formating.format;
//تابع تغییر قالب تاریخ
this.convert = function(date, convertKind, outputFormat = "YYYY/MM/DD") {
/**
* @date = تاریخ مورد نظر برای تغییر به شمسی یا میلادی
* @convertKind = نوع تبدیل --> به شمسی یا میلادی --> fa یا en
* @outputFormat = تایین فرمت خروجی
*/
// ارزشیابی دادههای ورودی از کاربر
if (!Validation.Validator(date, convertKind, outputFormat)) {
return null;
}
// findInputRegix= در صورتی که تاریخ ورودی با فرمت کارکتری ثبت شده یکسان باشد این مقدار صحیح میشود
// formatIndex_Input= شاخص آرایه برای فرمت ورودی
// findOutputFromat = در صورتی که فرمت خروجی با یکی از فرمت های ثبت شده یکسان باشد این مقدار صحیح میشود
// formatIndex_Output = شاخص آرایه برای فرمت خروجی
let findInputRegix = false,
formatIndex_Input = -1,
findOutputFromat = false,
formatIndex_Output = -1;
// محاسبات جهت تایین فرمت ورودی
for (let i = 0; i < this.formats.length; i++) {
if(this.formats[i].dRegix === false) continue;
if (this.formats[i].dRegix.test(date.trim())) {
findInputRegix = true;
formatIndex_Input = i;
break;
}
}
// محاسبات جهت تایین فرمت خروجی
for (let i = 0; i < this.formats.length; i++) {
if(this.formats[i].hRegix === false) continue;
if (this.formats[i].hRegix.test(outputFormat.trim())) {
findOutputFromat = true;
formatIndex_Output = i;
break;
}
}
// درصورتی که
// date
// ساختاری متناسب با فرمتهای ثبت شده داشته باشد
if (findInputRegix) {
// درصورتی که
// outputFormat
// در فرمتهای ثبت شده وجود داشته باشد
if (findOutputFromat) {
return prepration(
date,
convertKind,
this.formats[formatIndex_Input],
this.formats[formatIndex_Output],
outputFormat
);
}
// درصورتی که
// outputFormat
// در فرمتهای ثبت شده وجود نداشته باشد
else {
this.showErrors([
"فرمتهای موجود در برنامه، با فرمت وارد شده تناسب ندارد"
]);
this.showErrors(["فرمت خروجی شما = ", outputFormat]);
this.showErrors(["https://github.com/roozbehshafieebafti/zaravand-jallali-date"]);
return null;
}
}
// درصورتی که
// date
// ساختاری متناسب با فرمت ورودی نداشته باشد
else {
this.showErrors(["فرمتهای موجود با تاریخ وارد شده تناسب ندارد"]);
this.showErrors(["تاریخ وارد شده= ", date]);
this.showErrors(["https://github.com/roozbehshafieebafti/zaravand-jallali-date"]);
return null;
}
};
this.showErrors = errorArray => {
console.log("Zaravand Message :", ...errorArray);
};
}
// این تابع مضاعف، تبدیل تاریخ را انجام میدهد و فقط مقدار سال را بر میگرداند
ZaravandDate.prototype.year = function (date,convertKind) {
let _date = this.convert(date,convertKind,'raw');
if(_date.year !== undefined){
return _date.year;
}
}
// این تابع مضاعف، تبدیل تاریخ را انجام میدهد و فقط مقدار ماه را بر میگرداند
ZaravandDate.prototype.month = function (date,convertKind) {
let _date = this.convert(date,convertKind,'raw');
if(_date.month !== undefined){
return _date.month;
}
}
// این تابع مضاعف، تبدیل تاریخ را انجام میدهد و فقط مقدار نام ماه را بر میگرداند
ZaravandDate.prototype.Month = function (date,convertKind) {
let _date = this.convert(date,convertKind,'raw');
if(_date.Month !== undefined){
return _date.Month;
}
}
// این تابع مضاعف، تبدیل تاریخ را انجام میدهد و فقط مقدار روز را بر میگرداند
ZaravandDate.prototype.day = function (date,convertKind) {
let _date = this.convert(date,convertKind,'raw');
if(_date.day !== undefined){
return _date.day;
}
}
// این تابع مضاعف، تبدیل تاریخ را انجام میدهد و فقط مقدار روز هفته را بر میگرداند
ZaravandDate.prototype.week = function (date,convertKind) {
let _date = this.convert(date,convertKind,'raw');
if(_date.dweek !== undefined){
return _date.dweek;
}
}
// این تابع مضاعف، تبدیل تاریخ را انجام میدهد و فقط مقدار عددی تاریخ را برحسب میلی ثانیه بر میگرداند
ZaravandDate.prototype.numeric = function (date,convertKind) {
let _date = this.convert(date,convertKind,'raw');
if(_date.numeric !== undefined){
return _date.numeric;
}
}
module.exports = ZaravandDate;