-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
175 lines (157 loc) · 3.35 KB
/
utils.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
const MONTHS = {
"january": 1,
"february": 2,
"march": 3,
"april": 4,
"may": 5,
"june": 6,
"july": 7,
"august": 8,
"september": 9,
"october": 10,
"november": 11,
"december": 12
}
const DAYS = {
"sunday": 0,
"monday": 1,
"tuesday": 2,
"wednesday": 3,
"thursday": 4,
"friday": 5,
"saturday": 6
}
const SEASONS = {
"spring": 0,
"summer": 1,
"autumn": 2,
"winter": 3
}
function next_mod(lookup, query, pointer) {
let i = lookup.indexOf(query.toLowerCase()) + 1 * pointer
i = i < 0 ? i + lookup.length : i
let next_index = i % lookup.length
return lookup[next_index]
}
function next_season(season, pointer) {
let lookup = ["spring", "summer", "autumn", "winter"]
return next_mod(lookup, season, pointer)
}
function next_date(date, pointer) {
date = new Number(date)
if (isNaN(date)) {
return false
}
return (date + pointer).toString()
}
function next_day(day, pointer) {
let lookup = [
"sunday",
"monday",
"tuesday",
"wednesday",
"thursday",
"friday",
"saturday"
]
return next_mod(lookup, day, pointer)
}
function next_month(month, pointer) {
let lookup = [
"january",
"february",
"march",
"april",
"may",
"june",
"july",
"august",
"september",
"october" ,
"november" ,
"december"
]
return next_mod(lookup, month, pointer)
}
function next_year(year, pointer) {
year = new Number(year)
if (isNaN(year)) {
return false
}
return (year + pointer).toString()
}
function could_be_hour(hour, width = null, hours12 = false) {
hour = new Number(hour)
if (isNaN(hour)) {
return false
}
return hour >= 0 && hour <= (hours12 ? 12 : 24) && (width == null || width > 0)
}
function could_be_minute(minute, width = null) {
minute = new Number(minute)
if (isNaN(minute)) {
return false
}
return minute >= 0 && minute <= 60 && (width == null || width <= 2)
}
function could_be_second(second, width = null) {
second = new Number(second)
if (isNaN(second)) {
return false
}
return second >= 0 && second <= 60 && (width == null || width <= 2)
}
function could_be_day(date) {
if (typeof day == 'string') {
day = day.toLowerCase()
return DAYS.hasOwnProperty(day)
} else {
return false
}
}
function could_be_date(date) {
date = new Number(date)
if (isNaN(date)) {
return false
}
return date >= 1 && date <= 31
}
function could_be_month(month, width = null) {
if (typeof month == 'string') {
month = month.toLowerCase()
if (MONTHS.hasOwnProperty(month)) {
month = MONTHS[month]
} else {
month = new Number(month)
}
}
if (isNaN(month)) {
return false
}
return month >= 1 && month <= 12 && (width == null || width <= 2)
}
function could_be_season(season) {
season = season.toLowerCase()
return SEASONS.hasOwnProperty(season)
}
function could_be_year(year, width = null) {
year = new Number(year)
if (isNaN(year)) {
return false
}
return year >= 0 && year <= 9999 && (width == null || width == 2 || width == 4)
}
module.exports = {
MONTHS: MONTHS,
DAYS: DAYS,
could_be_date: could_be_date,
could_be_day: could_be_day,
could_be_month: could_be_month,
could_be_season: could_be_season,
could_be_year: could_be_year,
next_day: next_day,
next_date: next_date,
next_month: next_month,
next_season: next_season,
next_year:next_year
}