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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
/**
* @class String extensions
*/
Object.extend(String.prototype,
{
/**
* @param {String} "left" to pad the string on the left, "right" to pad right.
* @param {Number} minimum string length.
* @param {String} character(s) to pad
* @return {String} padded character(s) on the left or right to satisfy minimum string length
*/
pad : function(side, len, chr) {
if (!chr) chr = ' ';
var s = this;
var left = side.toLowerCase()=='left';
while (s.length<len) s = left? chr + s : s + chr;
return s;
},
/**
* @param {Number} minimum string length.
* @param {String} character(s) to pad
* @return {String} padded character(s) on the left to satisfy minimum string length
*/
padLeft : function(len, chr) {
return this.pad('left',len,chr);
},
/**
* @param {Number} minimum string length.
* @param {String} character(s) to pad
* @return {String} padded character(s) on the right to satisfy minimum string length
*/
padRight : function(len, chr) {
return this.pad('right',len,chr);
},
/**
* @param {Number} minimum string length.
* @return {String} append zeros to the left to satisfy minimum string length.
*/
zerofill : function(len) {
return this.padLeft(len,'0');
},
/**
* @return {String} removed white spaces from both ends.
*/
trim : function() {
return this.replace(/^\s+|\s+$/g,'');
},
/**
* @return {String} removed white spaces from the left end.
*/
trimLeft : function() {
return this.replace(/^\s+/,'');
},
/**
* @return {String} removed white spaces from the right end.
*/
trimRight : function() {
return this.replace(/\s+$/,'');
},
/**
* Convert period separated function names into a function reference.
* e.g. "Prado.AJAX.Callback.Action.setValue".toFunction() will return
* the actual function Prado.AJAX.Callback.Action.setValue()
* @return {Function} the corresponding function represented by the string.
*/
toFunction : function()
{
var commands = this.split(/\./);
var command = window;
commands.each(function(action)
{
if(command[new String(action)])
command=command[new String(action)];
});
if(typeof(command) == "function")
return command;
else
{
if(typeof Logger != "undefined")
Logger.error("Missing function", this);
throw new Error ("Missing function '"+this+"'");
}
},
/**
* Convert a string into integer, returns null if not integer.
* @return {Number} null if string does not represent an integer.
*/
toInteger : function()
{
var exp = /^\s*[-\+]?\d+\s*$/;
if (this.match(exp) == null)
return null;
var num = parseInt(this, 10);
return (isNaN(num) ? null : num);
},
/**
* Convert a string into a double/float value. <b>Internationalization
* is not supported</b>
* @param {String} the decimal character
* @return {Double} null if string does not represent a float value
*/
toDouble : function(decimalchar)
{
if(this.length <= 0) return null;
decimalchar = decimalchar || ".";
var exp = new RegExp("^\\s*([-\\+])?(\\d+)?(\\" + decimalchar + "(\\d+))?\\s*$");
var m = this.match(exp);
if (m == null)
return null;
m[1] = m[1] || "";
m[2] = m[2] || "0";
m[4] = m[4] || "0";
var cleanInput = m[1] + (m[2].length>0 ? m[2] : "0") + "." + m[4];
var num = parseFloat(cleanInput);
return (isNaN(num) ? null : num);
},
/**
* Convert strings that represent a currency value (e.g. a float with grouping
* characters) to float. E.g. "10,000.50" will become "10000.50". The number
* of dicimal digits, grouping and decimal characters can be specified.
* <i>The currency input format is <b>very</b> strict, null will be returned if
* the pattern does not match</i>.
* @param {String} the grouping character, default is ","
* @param {Number} number of decimal digits
* @param {String} the decimal character, default is "."
* @type {Double} the currency value as float.
*/
toCurrency : function(groupchar, digits, decimalchar)
{
groupchar = groupchar || ",";
decimalchar = decimalchar || ".";
digits = typeof(digits) == "undefined" ? 2 : digits;
var exp = new RegExp("^\\s*([-\\+])?(((\\d+)\\" + groupchar + ")*)(\\d+)"
+ ((digits > 0) ? "(\\" + decimalchar + "(\\d{1," + digits + "}))?" : "")
+ "\\s*$");
var m = this.match(exp);
if (m == null)
return null;
var intermed = m[2] + m[5] ;
var cleanInput = m[1] + intermed.replace(
new RegExp("(\\" + groupchar + ")", "g"), "")
+ ((digits > 0) ? "." + m[7] : "");
var num = parseFloat(cleanInput);
return (isNaN(num) ? null : num);
},
/**
* Converts the string to a date by finding values that matches the
* date format pattern.
* @param string date format pattern, e.g. MM-dd-yyyy
* @return {Date} the date extracted from the string
*/
toDate : function(format)
{
return Date.SimpleParse(this, format);
}
});
|