/
ajaxsolr.support.js
76 lines (69 loc) · 2 KB
/
ajaxsolr.support.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
// $Id$
/**
* Strip whitespace from the beginning and end of a string.
*
* @returns {String} The trimmed string.
*/
String.prototype.trim = function () {
return this.replace(/^ +/, '').replace(/ +$/, '');
};
/**
* A utility method for escaping HTML tag characters.
* <p>From Ruby on Rails.</p>
*
* @returns {String} The escaped string.
*/
String.prototype.htmlEscape = function () {
return this.replace(/"/g, '"').replace(/>/g, '>').replace(/</g, '<').replace(/&/g, '&');
};
/**
* Escapes the string without affecting existing escaped entities.
* <p>From Ruby on Rails.</p>
*
* @returns {String} The escaped string
*/
String.prototype.escapeOnce = function () {
return this.replace(/"/g, '"').replace(/>/g, '>').replace(/</g, '<').replace(/&(?!([a-zA-Z]+|#\d+);)/g, '&');
};
/**
* <p>From Ruby on Rails.</p>
*
* @see http://www.w3.org/TR/html4/types.html#type-name
*/
String.prototype.sanitizeToId = function () {
return this.replace(/\]/g, '').replace(/[^-a-zA-Z0-9:.]/g, '_');
};
/**
* Does the string end with the specified <tt>suffix</tt>?
* <p>From Ruby on Rails.</p>
*
* @param {String} suffix The specified suffix.
* @returns {Boolean}
*/
String.prototype.endsWith = function (suffix) {
return this.substring(this.length - suffix.length) == suffix;
};
/**
* Does the string start with the specified <tt>prefix</tt>?
* <p>From Ruby on Rails.</p>
*
* @param {String} prefix The speficied prefix.
* @returns {Boolean}
*/
String.prototype.startsWith = function (prefix) {
return this.substring(0, prefix.length) == prefix;
};
/**
* Equivalent to PHP's two-argument version of strtr.
*
* @see http://php.net/manual/en/function.strtr.php
* @param {Object} replacePairs An associative array in the form: {'from': 'to'}
* @returns {String} A translated copy of the string.
*/
String.prototype.strtr = function (replacePairs) {
var str = this;
for (var from in replacePairs) {
str = str.replace(new RegExp(from, 'g'), replacePairs[from]);
}
return str;
};