Repository navigation
Expand file tree
/
Copy pathscripts.js
More file actions
152 lines (130 loc) · 4.76 KB
/
Copy pathscripts.js
File metadata and controls
152 lines (130 loc) · 4.76 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
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
/* time periods in milliseconds */
var noOfMillisecondsInSecond = 1000,
noOfMillisecondsInMinute = 60 * noOfMillisecondsInSecond,
noOfMillisecondsInHour = 60 * noOfMillisecondsInMinute,
noOfMillisecondsInDay = 24 * noOfMillisecondsInHour,
noOfMillisecondsInWeek = 7 * noOfMillisecondsInDay;
var dateformat;
function processInput() {
dateformat = $('input[name=month-order]:checked').val()
$('#results').empty();
var input = $('#input').val().trim().split('\n');
for (var i=0; i < input.length; i++) {
processLine(input[i]);
};
}
function processLine(line) {
line = normalise(line);
var numOfDates = 0;
var placeholders = line.match(/(today|now)/g);
if (placeholders) {
for (var i=0; i < placeholders.length; i++) {
numOfDates++;
line = line.replace(placeholders[i], new Date().getTime())
}
}
var dates = line.match(/[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}/g);
if (dates) {
for (var i=0; i < dates.length; i++) {
numOfDates++;
line = line.replace(dates[i], Date.parse(dates[i]));
};
};
var additionArray = line.split(" + ");
var millisecondSum = 0;
for (var i=0; i < additionArray.length; i++) {
millisecondSum += parseInt(additionArray[i]);
};
var result;
if (numOfDates == 1) {
result = new Date(millisecondSum);
} else {
result = formatTime(millisecondSum);
};
$('#results').append('<li><span title="'+line+'">'+result+'</span></li>');
}
function normalise(line) {
line = convertToMilliseconds(line, line.match(/[0-9]+\s?(?:weeks|week|wks|wk)/g), noOfMillisecondsInWeek);
line = convertToMilliseconds(line, line.match(/[0-9]+\s?(?:days|day)/g), noOfMillisecondsInDay);
line = convertToMilliseconds(line, line.match(/[0-9]+\s?(?:hours|hour|hrs|hr)/g), noOfMillisecondsInHour);
line = convertToMilliseconds(line, line.match(/[0-9]+\s?(?:minutes|minute|min)/g), noOfMillisecondsInMinute);
line = convertToMilliseconds(line, line.match(/[0-9]+\s?(?:seconds|second|sec)/g), noOfMillisecondsInSecond);
line = line.replace(' plus ', ' + ')
line = line.replace(' minus ', ' - ')
line = line.replace(' - ', ' + -')
return line;
}
function convertToMilliseconds(line, match, multiplier) {
if (match) {
for (var i=0; i < match.length; i++) {
var num = match[i].match(/[0-9]+/)[0] * multiplier;
line = line.replace(match[i], num);
};
};
return line;
}
function formatTime(milliseconds) {
var isPositive = (milliseconds >= 0);
var formattedTime = isPositive ? '+' : '-';
milliseconds = Math.abs(milliseconds);
if (milliseconds >= noOfMillisecondsInWeek) {
var noOfWeeks = Math.floor(milliseconds / noOfMillisecondsInWeek);
formattedTime = formattedTime + " " + noOfWeeks + " " + (noOfWeeks == 1 ? 'week' : 'weeks');
milliseconds -= noOfWeeks * noOfMillisecondsInWeek;
}
if (milliseconds >= noOfMillisecondsInDay) {
var noOfDays = Math.floor(milliseconds / noOfMillisecondsInDay);
formattedTime = formattedTime + " " + noOfDays + " " + (noOfDays == 1 ? 'day' : 'days');
milliseconds -= noOfDays * noOfMillisecondsInDay;
}
if (milliseconds >= noOfMillisecondsInHour) {
var noOfHours = Math.floor(milliseconds / noOfMillisecondsInHour);
formattedTime = formattedTime + " " + noOfHours + " " + (noOfHours == 1 ? 'hour' : 'hours');
milliseconds -= noOfHours * noOfMillisecondsInHour;
}
if (milliseconds >= noOfMillisecondsInMinute) {
var noOfMinutes = Math.floor(milliseconds / noOfMillisecondsInMinute);
formattedTime = formattedTime + " " + noOfMinutes + " " + (noOfMinutes == 1 ? 'minute' : 'minutes');
milliseconds -= noOfMinutes * noOfMillisecondsInMinute;
}
if (milliseconds >= noOfMillisecondsInSecond) {
var noOfSeconds = Math.floor(milliseconds / noOfMillisecondsInSecond);
formattedTime = formattedTime + " " + noOfSeconds + " " + (noOfSeconds == 1 ? 'second' : 'seconds');
milliseconds -= noOfSeconds * noOfMillisecondsInSecond;
}
return formattedTime;
}
// stolen from http://stackoverflow.com/a/7180862
/*
$.fn.setCursorPosition = function(position){
if(this.length == 0) return this;
return $(this).setSelection(position, position);
}
$.fn.setSelection = function(selectionStart, selectionEnd) {
if(this.length == 0) return this;
input = this[0];
if (input.createTextRange) {
var range = input.createTextRange();
range.collapse(true);
range.moveEnd('character', selectionEnd);
range.moveStart('character', selectionStart);
range.select();
} else if (input.setSelectionRange) {
input.focus();
input.setSelectionRange(selectionStart, selectionEnd);
}
return this;
}
$.fn.focusEnd = function(){
this.setCursorPosition(this.val().length);
}
*/
$(document).ready(function(){
processInput();
setInterval(processInput, 5*noOfMillisecondsInSecond);
$('#input').focus().keypress(function(event){
if (event.which == 13) {
processInput();
}
});
});