-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
64 lines (56 loc) · 1.85 KB
/
index.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
var assert = require('assert')
module.exports = (function () {
var util = {}
var escapes = { '&': '&', '>': '>', '<': '<', '"': '"', "'": ''', '/': '/' }
util.mapify = function (list, key) {
assert(Array.isArray(list), ' list ' + list + ' must be an array')
assert(key, 'key must be present')
var map = {}
for (var i = 0; i < list.length; i++) {
assert(list[i][key], list[i] + ' does not contain ' + key)
map[list[i][key]] = list[i]
}
return map;
}
util.arrayMapify = function (list, key) {
assert(Array.isArray(list), 'list ' + list + ' must be an array')
assert(key, 'key must be present')
var map = {}
for (var i = 0; i < list.length; i++) {
assert(list[i][key], list[i] + ' does not contain ' + key)
map[list[i][key]] = map[list[i][key]] || []
map[list[i][key]].push(list[i])
}
return map;
}
util.fixed = function (number) {
return number.toFixed(10) - 0
}
util.delProps = function (obj, properties) {
var props = Array.isArray(properties) && properties || [properties]
var objs = Array.isArray(obj) && obj || [obj]
for (var i = 0; i < objs.length; i++) {
for (var j = 0; j < props.length; j++) {
delete objs[i][props[j]]
}
}
return Array.isArray(obj) && objs || objs[0]
}
util.unsanitizeString = function (string) {
var badChars = Object.keys(escapes)
for (var i = 0; i < badChars.length; i++) {
var re = new RegExp(escapes[badChars[i]], 'g')
string = string.replace(re, badChars[i])
}
return string
}
util.sanitizeString = function (string) {
var badChars = Object.keys(escapes)
for (var i = 0; i < badChars.length; i++) {
var re = new RegExp(badChars[i], 'g')
string = string.replace(re, escapes[badChars[i]])
}
return string
}
return util
})()