forked from espruino/BangleApps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
44 lines (44 loc) · 1.13 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
function escapeHtml(text) {
var map = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": '''
};
return text.replace(/[&<>"']/g, function(m) { return map[m]; });
}
function htmlToArray(collection) {
return [].slice.call(collection);
}
function htmlElement(str) {
var div = document.createElement('div');
div.innerHTML = str.trim();
return div.firstChild;
}
function httpGet(url) {
return new Promise((resolve,reject) => {
var oReq = new XMLHttpRequest();
oReq.addEventListener("load", () => {
if (oReq.status==200) resolve(oReq.responseText)
else reject(oReq.status+" - "+oReq.statusText);
});
oReq.addEventListener("error", () => reject());
oReq.addEventListener("abort", () => reject());
oReq.open("GET", url);
oReq.send();
});
}
function toJS(txt) {
return JSON.stringify(txt);
}
// callback for sorting apps
function appSorter(a,b) {
if (a.unknown || b.unknown)
return (a.unknown)? 1 : -1;
var sa = 0|a.sortorder;
var sb = 0|b.sortorder;
if (sa<sb) return -1;
if (sa>sb) return 1;
return (a.name==b.name) ? 0 : ((a.name<b.name) ? -1 : 1);
}