-
Notifications
You must be signed in to change notification settings - Fork 7
/
helper.js
156 lines (140 loc) · 3.74 KB
/
helper.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
const I18N = require('./I18N');
const _ = {
e(str) {
if (typeof str !== 'string') {
str = String(str);
}
return str.replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, ''');
},
t(value, vars = null) {
if (typeof I18N.translate === 'function') {
return I18N.translate(value, vars);
}
else {
return I18N.format(value, vars);
}
},
toNothing(label = null) {
if (label === null) {
label = _.t('n/a');
}
return `<i class="null">${label}</i>`;
},
toList(list, sort = false, formatter = null, ordered = null) {
let tag = ordered === true ? 'ol' : 'ul';
if (!Array.isArray(list)) {
list = [list];
}
if (sort) {
list = list.slice(0);
if (typeof sort === 'function') {
list.sort(sort);
}
else {
list.sort();
}
if (ordered === null) {
tag = 'ol';
}
}
if (typeof formatter === 'function') {
list = list.map(formatter);
}
if (list.length === 0) {
return _.toNothing();
}
else if (list.length === 1) {
return list[0];
}
else {
return `<${tag}><li>${list.join("</li><li>")}</li></${tag}>`;
}
},
toLink(url, title = "", rel = "", target = "_blank") {
if (!title) {
if (url.length > 50) {
title = url.replace(/^\w+:\/\/([^\/]+)((\/[^\/\?]+)*\/([^\/\?]+)(\?.*)?)?$/i, function(...x) {
if (x[4]) {
return x[1] + '/[…]/' + x[4]; // There are invisible zero-width whitespaces after and before the slashes. It allows breaking the link in the browser. Be careful when editing.
}
return x[1];
});
}
else {
title = url.replace(/^\w+:\/\//i, '');
}
}
return `<a href="${_.e(url)}" rel="${_.e(rel)}" target="${_.e(target)}">${_.e(title)}</a>`;
},
toObject(obj, formatter = null, keyFormatter = null, keyOrder = [], filter = null, path = []) {
let html = '<dl>';
let keys = Array.isArray(keyOrder) && keyOrder.length >= 2 ? keyOrder : Object.keys(obj);
for(let key of keys) {
if (!(key in obj) || (typeof filter === 'function' && path.length > 0 && !filter(path[0], path.concat([key]))) ) {
continue;
}
let label;
if (typeof keyFormatter === 'function') {
label = keyFormatter(key, obj);
}
else {
label = _.formatKey(key, true);
}
let value = obj[key];
if (typeof formatter === 'function') {
value = formatter(value, key, obj);
}
html += `<dt>${label}</dt><dd>${value}</dd>`;
}
html += `</dl>`;
return html;
},
abbrev(short, long) {
return `<abbr title="${_.e(long)}">${_.e(short)}</abbr>`;
},
isObject(obj) {
return (typeof obj === 'object' && obj === Object(obj) && !Array.isArray(obj));
},
formatKey(key, prefix = false) {
if (key.includes('/')) {
// Slashes are strong indicators for URIs or media types, don't format
return _.e(_.t(key));
}
if (prefix === false) {
key = key.replace(/^[\w-]+:/i, '');
}
let formatted = key.split(/[:_\-\s]/g).map(part => part.substr(0, 1).toUpperCase() + part.substr(1)).join(' ');
return _.e(_.t(formatted));
},
hexToUint8(hexString) {
if(hexString.length === 0 || hexString.length % 2 !== 0){
throw new Error(`The string "${hexString}" is not valid hex.`);
}
return new Uint8Array(hexString.match(/.{1,2}/g).map(byte => parseInt(byte, 16)));
},
uint8ToHex(bytes) {
return bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, '0'), '');
},
keysFromListOfObjects(objectList) {
return objectList.reduce(
(arr, o) => Object.keys(o).reduce(
(a, k) => {
if (a.indexOf(k) == -1) {
a.push(k);
}
return a;
},
arr
),
[]
);
},
unit(value, unit = '') {
if (typeof unit === 'string' && unit.length > 0) {
unit = _.t(unit);
return `${value} <span class="unit">${unit}</unit>`;
}
return value;
}
};
module.exports = _;