-
Notifications
You must be signed in to change notification settings - Fork 2
/
values.js
66 lines (62 loc) · 2.25 KB
/
values.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
var valueUtils = {
numberRegex: /^\s*[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?\s*$/,
uriRegex: /^([a-z][-+.a-z0-9]{0,11}):\/\/(.+)/i,
lowercaseAboutValue: function(str) {
var match = this.uriRegex.exec(str);
if (match) {
var scheme = match[1];
var postScheme = match[2];
var userpass = '';
var hostport = '';
var rest = '';
var slash = postScheme.indexOf('/');
if (slash > -1) {
rest = postScheme.slice(slash);
var hierarchicalPart = postScheme.slice(0, slash);
}
else {
var hierarchicalPart = postScheme;
}
if (hierarchicalPart){
var at = hierarchicalPart.indexOf('@');
if (at > -1){
userpass = hierarchicalPart.slice(0, at + 1);
hostport = hierarchicalPart.slice(at + 1);
}
else {
hostport = hierarchicalPart;
}
}
return scheme.toLowerCase() + '://' + userpass + hostport.toLowerCase() + rest;
}
else {
return str.toLowerCase();
}
},
truncateAbout: function(about, maxLen) {
// Return a shortened form of 'about' of length at most maxLen, suitable
// for display.
if (valueUtils.isLink(about)) {
// Chop off useless https?:// prefix.
var match = about.indexOf('://');
if (match > -1 && match < 6) {
about = about.slice(match + 3);
}
}
if (about.length > maxLen) {
about = about.slice(0, maxLen - 3) + '...';
}
return about;
},
quoteAbout: function(s) {
// Quote an about value to make it suitable for use in a
// fluiddb/about = " ... " query.
return s.replace(/\\/g, '\\\\').replace(/\"/g, '\"');
},
isLink: function(str) {
// Return true if str looks like an https?:// link. Don't allow < or >
// to appear, as a simple form of preventing html tags (like <script>)
// tricking us into thinking they're normal links.
return /^(https?|file):\/\/[^\<\>]+$/i.test(str);
}
};