-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path090-StringExtensions.js
More file actions
97 lines (80 loc) · 2.66 KB
/
090-StringExtensions.js
File metadata and controls
97 lines (80 loc) · 2.66 KB
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
//console.debug("begin stringextensions.js");
//Extensions for strings
// Created by Michael J. Ryan - 2009-10-20
//empty string reference
String.empty = "";
//trim functionality -- see: http://blog.stevenlevithan.com/archives/faster-trim-javascript
if (typeof String.prototype.ltrim == "undefined")
String.prototype.ltrim = function() {
return this.replace(/^\s+/,"");
};
if (typeof String.prototype.rtrim == "undefined")
String.prototype.rtrim = function() {
var ws = /\s/;
var i = this.length;
while (ws.test(this.charAt(--i))){};
return this.slice(0, i+1);
};
if (typeof String.prototype.trim == "undefined")
String.prototype.trim = function() {
return this.ltrim().rtrim();
};
//convert anything to a trimmed string (converts null and undefined to String.empty)
if (typeof String.fromAny == "undefined")
String.fromAny = function(input) {
//localise the input
var ret = input;
//if ret is undefined or null, use an empty string
if (typeof(ret) == "undefined" || ret === null)
ret = "";
//ensure that objects are converted, and trim the result.
return ret.toString().trim();
};
//make sure there's a toLocaleLowerCase, use toLowerCase if needed
if (typeof String.prototype.toLocaleLowerCase == "undefined")
if (typeof(String.prototype.toLocaleLowerCase) != "function")
String.prototype.toLocaleLowerCase = String.prototype.toLowerCase;
//creates a trimmed string, lowercased for comparisons
if (typeof String.toCmp == "undefined")
String.toCmp = function(input) {
return String.fromAny(input).toLocaleLowerCase();
};
//case insensitive matching for strings
if (typeof String.equals == "undefined")
String.equals = function(s1, s2) {
return String.toCmp(s1) == String.toCmp(s2);
};
if (typeof String.prototype.equals == "undefined")
String.prototype.equals = function(s2) {
return String.equals(this, s2);
};
//search within a string, or array of strings
if (typeof String.contains == "undefined")
String.contains = function(s1, s2) {
//matching against an array
if (typeof(s1) == "array") {
var search = String.toCmp(s2);
for (var i=0; i<s1.length; i++) {
if (String.toCmp(s1[i]).indexOf(search) >= 0)
return true;
}
return false;
}
//matching against strings
return String.toCmp(s1).indexOf(String.toCmp(s2)) >= 0;
};
if (typeof String.prototype.contains == "undefined")
String.prototype.contains = function(s2) {
return String.contains(this, s2);
};
//reverse of contains
if (typeof String.inside == "undefined")
String.inside = function(s1, s2) {
return String.contains(s2, s1);
};
if (typeof String.prototype.inside == "undefined")
String.prototype.inside = function (s2) {
return String.inside(this, s2);
};
//console.debug("end stringextensions.js");