forked from alitaheri/material-ui-pickers-jalali-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
221 lines (168 loc) · 5.6 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
'use strict';
var Moment = require('moment');
var iMoment = require('moment-hijri');
var extendMoment = require('moment-range').extendMoment;
var moment = extendMoment(Moment);
var arWeekDays = {
weekdays : 'الأحد_الإثنين_الثلاثاء_الأربعاء_الخميس_الجمعة_السبت'.split('_'),
weekdaysShort : 'أحد_إثنين_ثلاثاء_أربعاء_خميس_جمعة_سبت'.split('_'),
weekdaysMin : 'ح_ن_ث_ر_خ_ج_س'.split('_')
}
moment.updateLocale('ar-sa', arWeekDays);
var symbolMap = {
1: '۱',
2: '۲',
3: '۳',
4: '٤',
5: '۵',
6: '٦',
7: '۷',
8: '۸',
9: '۹',
0: '۰',
};
function parse(value, format) {
return iMoment(value, format);
}
function toIMoment(date) {
return iMoment(date ? date.clone() : undefined);
}
var Utils = function utils() { };
Utils.prototype.toIMoment = toIMoment;
Utils.prototype.date = Utils.prototype.parse = parse;
Utils.prototype.isValid = function isValid(date) {
return date.isValid();
};
Utils.prototype.isNull = function isNull(date) {
return date.parsingFlags().nullInput;
};
Utils.prototype.isEqual = function isEqual(value, comparing) {
return parse(value).isSame(comparing);
};
Utils.prototype.isAfter = function isAfter(date, value) {
return date.isAfter(value);
};
Utils.prototype.isBefore = function isBefore(date, value) {
return date.isBefore(value);
};
Utils.prototype.isAfterDay = function isAfterDay(date, value) {
return date.isAfter(value, 'day');
};
Utils.prototype.isBeforeDay = function isBeforeDay(date, value) {
return date.isBefore(value, 'day');
};
Utils.prototype.isBeforeYear = function isBeforeYear(date, value) {
return date.iYear() < value.iYear();
};
Utils.prototype.isAfterYear = function isAfterYear(date, value) {
return date.iYear() > value.iYear();
};
Utils.prototype.startOfDay = function startOfDay(date) {
return date.startOf('day');
};
Utils.prototype.endOfDay = function endOfDay(date) {
return date.endOf('day');
};
Utils.prototype.format = function format(date, formatString) {
return date.format(formatString);
};
Utils.prototype.formatNumber = function formatNumber(num) {
return (num || '').replace(/\d/g, function (match) {
return symbolMap[match];
}).replace(/,/g, '،');
};
Utils.prototype.getMeridiemText = function getMeridiemText(ampm) {
return ampm === 'am' ? toIMoment().hours(2).format('A') : toIMoment().hours(14).format('A');
};
Utils.prototype.addDays = function addDays(date, count) {
return count < 0 ? date.clone().subtract(Math.abs(count), 'days') : date.clone().add(count, 'days');
};
Utils.prototype.isSameDay = function isSameDay(date, comparing) {
return date.isSame(comparing, 'day');
};
Utils.prototype.getHours = function getHours(date) {
return date.get('hours');
};
Utils.prototype.setHours = function setHours(date, value) {
return date.clone().hours(value);
};
Utils.prototype.getMinutes = function getMinutes(date) {
return date.get('minutes');
};
Utils.prototype.setMinutes = function setMinutes(date, value) {
return date.clone().minutes(value);
};
Utils.prototype.getMonth = function getMonth(date) {
return date.iMonth();
};
Utils.prototype.getStartOfMonth = function getStartOfMonth(date) {
return date.clone().startOf('iMonth');
};
Utils.prototype.getNextMonth = function getNextMonth(date) {
return date.clone().add(1, 'iMonth');
};
Utils.prototype.getPreviousMonth = function getPreviousMonth(date) {
return date.clone().subtract(1, 'iMonth');
};
Utils.prototype.getYear = function getYear(date) {
return date.iYear();
};
Utils.prototype.setYear = function setYear(date, year) {
return date.clone().iYear(year);
};
Utils.prototype.mergeDateAndTime = function mergeDateAndTime(date, time) {
return this.setMinutes(this.setHours(date, this.getHours(time)), this.getMinutes(time));
};
Utils.prototype.getDiff = function getDiff(date, comparing) {
return date.diff(comparing);
}
Utils.prototype.getWeekdays = function getWeekdays() {
return [0, 1, 2, 3, 4, 5, 6].map(function (dayOfWeek) {
return toIMoment().weekday(dayOfWeek).format('dd');
});
};
Utils.prototype.getWeekArray = function getWeekArray(date) {
var start = toIMoment(date).startOf('iMonth').startOf('week');
var end = toIMoment(date).endOf('iMonth').endOf('week');
var weeks = Array.from(moment.range(start, end).by('week'));
var nestedWeeks = [];
weeks.forEach(function (week) {
var end = week.clone().endOf('week');
nestedWeeks.push(Array.from(moment.range(week, end).by('day')).map(toIMoment));
});
return nestedWeeks;
};
Utils.prototype.getYearRange = function getYearRange(start, end) {
var startDate = parse(start);
var endDate = parse(end);
var years = [];
var current = startDate;
while (current.isBefore(endDate)) {
years.push(current);
current = current.clone().add(1, 'year');
}
return years;
};
Utils.prototype.getCalendarHeaderText = function getCalendarHeaderText(date) {
return date.format('iMMMM iYYYY');
};
Utils.prototype.getYearText = function getYearText(date) {
return date.format('iYYYY');
};
Utils.prototype.getDatePickerHeaderText = function getDatePickerHeaderText(date) {
return date.format('ddd, iMMM iD');
};
Utils.prototype.getDateTimePickerHeaderText = function getDateTimePickerHeaderText(date) {
return date.format('iMMM iD');
};
Utils.prototype.getDayText = function getDayText(date) {
return date.format('iD');
};
Utils.prototype.getHourText = function getHourText(date, ampm) {
return date.format(ampm ? 'hh' : 'HH');
};
Utils.prototype.getMinuteText = function getMinuteText(date) {
return date.format('mm');
};
Utils['default'] = Utils;
module.exports = Utils;