forked from locutusjs/locutus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimized.js
More file actions
85 lines (85 loc) · 2.83 KB
/
Copy pathoptimized.js
File metadata and controls
85 lines (85 loc) · 2.83 KB
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
function strtotime(text, now) {
if(!text) {
return null
}
text = text.trim().replace(/\s{2,}/g, " ").replace(/[\t\r\n]/g, "").toLowerCase();
var parsed;
if(text === "now") {
return now === null || isNaN(now) ? (new Date).getTime() / 1E3 | 0 : now | 0
}else {
if(!isNaN(parse = Date.parse(text))) {
return parse / 1E3 | 0
}
}
if(text === "now") {
return(new Date).getTime() / 1E3
}else {
if(!isNaN(parsed = Date.parse(text))) {
return parsed / 1E3
}
}
var match = text.match(/^(\d{2,4})-(\d{2})-(\d{2})(?:\s(\d{1,2}):(\d{2})(?::\d{2})?)?(?:\.(\d+)?)?$/);
if(match) {
var year = match[1] >= 0 && match[1] <= 69 ? +match[1] + 2E3 : match[1];
return new Date(year, parseInt(match[2], 10) - 1, match[3], match[4] || 0, match[5] || 0, match[6] || 0, match[7] || 0) / 1E3
}
var date = now ? new Date(now * 1E3) : new Date;
var days = {"sun":0, "mon":1, "tue":2, "wed":3, "thu":4, "fri":5, "sat":6};
var ranges = {"yea":"FullYear", "mon":"Month", "day":"Date", "hou":"Hours", "min":"Minutes", "sec":"Seconds"};
function lastNext(type, range, modifier) {
var day = days[range];
if(typeof day !== "undefined") {
var diff = day - date.getDay();
if(diff === 0) {
diff = 7 * modifier
}else {
if(diff > 0 && type === "last") {
diff -= 7
}else {
if(diff < 0 && type === "next") {
diff += 7
}
}
}
date.setDate(date.getDate() + diff)
}
}
function process(val) {
var split = val.split(" ");
var type = split[0];
var range = split[1].substring(0, 3);
var typeIsNumber = /\d+/.test(type);
var ago = split[2] === "ago";
var num = (type === "last" ? -1 : 1) * (ago ? -1 : 1);
if(typeIsNumber) {
num *= parseInt(type, 10)
}
if(ranges.hasOwnProperty(range)) {
return date["set" + ranges[range]](date["get" + ranges[range]]() + num)
}else {
if(range === "wee") {
return date.setDate(date.getDate() + num * 7)
}
}
if(type === "next" || type === "last") {
lastNext(type, range, num)
}else {
if(!typeIsNumber) {
return false
}
}
return true
}
var regex = "([+-]?\\d+\\s" + "(years?|months?|weeks?|days?|hours?|min|minutes?|sec|seconds?" + "|sun\\.?|sunday|mon\\.?|monday|tue\\.?|tuesday|wed\\.?|wednesday" + "|thu\\.?|thursday|fri\\.?|friday|sat\\.?|saturday)|(last|next)\\s" + "(years?|months?|weeks?|days?|hours?|min|minutes?|sec|seconds?" + "|sun\\.?|sunday|mon\\.?|monday|tue\\.?|tuesday|wed\\.?|wednesday" + "|thu\\.?|thursday|fri\\.?|friday|sat\\.?|saturday))(\\sago)?";
match = text.match(new RegExp(regex, "gi"));
if(!match) {
return false
}
for(var i = 0, len = match.length;i < len;i++) {
if(!process(match[i])) {
return false
}
}
return date.getTime() / 1E3
}
;