-
Notifications
You must be signed in to change notification settings - Fork 0
/
string-helper.js
42 lines (39 loc) · 1.28 KB
/
string-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
module.exports = {
makeid: function makeid(length) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for (var i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
},
isValidJson: function validateJson(text) {
if (/^[\],:{}\s]*$/.test(text.replace(/\\["\\\/bfnrtu]/g, '@').
replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').
replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
return true;
} else {
return false;
}
},
fixJson: function fixJson(text) {
// preserve newlines, etc - use valid JSON
text = //text.replace(/\\n/g, "\\n")
text.replace(/\\'/g, "\\'")
.replace(/\\"/g, '\\"')
.replace(/\\&/g, "\\&")
.replace(/\\r/g, "\\r")
.replace(/\\t/g, "\\t")
.replace(/\\b/g, "\\b")
.replace(/\\f/g, "\\f");
// remove non-printable and other non-valid JSON chars
text = text.replace(/[\u0000-\u0019]+/g, "");
// wrap key values with double quotes
text = text.replace(/([\w]+):/g, '"$1":')
.replace(/:([a-zA-Z_]+)/g, ':"$1"')
.replace(/:([\d]+)/g, function (m, num) { return ':' + parseFloat(num) })
.replace(/:([[{])/g, ':$1');
return text;
}
}