This repository was archived by the owner on Nov 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathutilities.js
167 lines (147 loc) · 4.41 KB
/
utilities.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
'use strict';
/* global Normalizer */
/* exported DateHelper, NumberHelper, HtmlHelper, StringHelper */
var DateHelper = {
todayStarted: function dh_todayStarted() {
var now = (new Date()).valueOf();
return this.getMidnight(now);
},
yesterdayStarted: function dh_yesterdayStarted() {
var now = (new Date()).valueOf();
var dayAgo = now - 86400000;
return this.getMidnight(dayAgo);
},
thisWeekStarted: function dh_thisWeekStarted() {
var now = new Date();
var dayOfTheWeek = now.getDay();
var firstDay = new Date(
now.getFullYear(),
now.getMonth(),
//getDay is zero based so if today
//is the start of the week it will not
//change the date. Also if we get
//into negative days the date object
//handles that too...
now.getDate() - dayOfTheWeek
);
return this.getMidnight(firstDay);
},
thisMonthStarted: function dh_thisMonthStarted() {
var now = new Date();
var firstDay = (new Date(
now.getFullYear(),
now.getMonth(),
1).valueOf()
);
return firstDay;
},
lastSixMonthsStarted: function dh_lastSixMonthsStarted() {
var now = new Date().valueOf();
var sixMonthsAgo = now - 2629743830 * 6;
return sixMonthsAgo;
},
thisYearStarted: function dh_thisYearStarted() {
var now = new Date();
var firstDay = (new Date(
now.getFullYear(),
0).valueOf()
);
return firstDay;
},
getMidnight: function dh_getMidnight(timestamp) {
var day = new Date(timestamp);
var midnight = (new Date(
day.getFullYear(),
day.getMonth(),
day.getDate(),
0).valueOf());
return midnight;
}
};
var NumberHelper = {
/**
* Pad a string representaiton of an integer with leading zeros
*
* @param {String} string String to pad.
* @param {Integer} len Desired length of output.
* @return {String} Padded string.
*/
zfill: function nh_zfill(string, len) {
var s = string;
while (s.length < len) {
s = '0' + s;
}
return s;
}
};
var StringHelper = {
fromUTF8: function ut_fromUTF8(str) {
var buf = new Uint8Array(str.length);
for (var i = 0; i < str.length; i++) {
buf[i] = str.charCodeAt(i);
}
return buf;
},
camelCase: function ut_camelCase(str) {
var rdashes = /-(.)/g;
return str.replace(rdashes, function replacer(str, p1) {
return p1.toUpperCase();
});
}
};
var HtmlHelper = {
createHighlightHTML: function ut_createHighlightHTML(text, terms) {
// We store here what positions in the text are going to be enclosed in
// highlight tags as boolean values. Positions in toHighlight correspond to
// positions in the text string.
var toHighlight = [];
var normalizedText = Normalizer.toAscii(text).toLowerCase();
terms.forEach(function(term) {
term = Normalizer.toAscii(term).toLowerCase();
var index = normalizedText.indexOf(term);
while (index >= 0) {
for(var i = 0, length = term.length; i < length; i++){
toHighlight[index + i] = true;
}
index = normalizedText.indexOf(term, index + term.length);
}
});
var fragment = document.createDocumentFragment();
for(var i = 0; i < text.length; i++){
if (!toHighlight[i]) {
fragment.appendChild(document.createTextNode(text[i]));
} else {
var term = '';
while (toHighlight[i]) {
term += text[i];
i++;
}
i--;
var mark = document.createElement('mark');
mark.textContent = term;
fragment.appendChild(mark);
}
}
return fragment;
},
escapeHTML: function ut_escapeHTML(str, escapeQuotes) {
var span = document.createElement('span');
span.textContent = str;
// Escape space for displaying multiple space in message.
span.innerHTML = span.innerHTML.replace(/\s/g, ' ');
if (escapeQuotes) {
return span.innerHTML.replace(/"/g, '"').replace(/'/g, '''); //"
}
return span.innerHTML;
},
// Import elements into context. The first argument
// is the context to import into, each subsequent
// argument is the id of an element to import.
// Elements can be accessed using the camelCased id
importElements: function importElements(context) {
var ids = [].slice.call(arguments, 1);
ids.forEach(function(id) {
context[StringHelper.camelCase(id)] = document.getElementById(id);
});
}
};