summaryrefslogtreecommitdiff
path: root/framework/Web/Javascripts/extended/util.js
diff options
context:
space:
mode:
authorwei <>2006-01-16 02:43:30 +0000
committerwei <>2006-01-16 02:43:30 +0000
commitca47a8c7fd5eb9f34ac00a2f1a843859d6123dd8 (patch)
tree436215c674c4ef68335bf9b3cfe1387236d762ff /framework/Web/Javascripts/extended/util.js
parent2d6584b56f0c52686f868c4c7dafc44db0f7c5cf (diff)
Diffstat (limited to 'framework/Web/Javascripts/extended/util.js')
-rw-r--r--framework/Web/Javascripts/extended/util.js95
1 files changed, 95 insertions, 0 deletions
diff --git a/framework/Web/Javascripts/extended/util.js b/framework/Web/Javascripts/extended/util.js
index 1e1fad69..7cc836f4 100644
--- a/framework/Web/Javascripts/extended/util.js
+++ b/framework/Web/Javascripts/extended/util.js
@@ -88,3 +88,98 @@ function isElement(o, strict) {
*/
function isList(o) { return o && isObject(o) && (isArray(o) || o.item) }
+
+if(!Prado) var Prado = {};
+
+Prado.Util = {}
+
+/**
+ * Pad a number with zeros from the left.
+ * @param integer number
+ * @param integer total string length
+ * @return string zero padded number
+ */
+Prado.Util.pad = function(number, X)
+{
+ X = (!X ? 2 : X);
+ number = ""+number;
+ while (number.length < X)
+ number = "0" + number;
+ return number;
+}
+
+/**
+ * Convert a string into integer, returns null if not integer.
+ * @param {string} the string to convert to integer
+ * @type {integer|null} null if string does not represent an integer.
+ */
+Prado.Util.toInteger = function(value)
+{
+ var exp = /^\s*[-\+]?\d+\s*$/;
+ if (value.match(exp) == null)
+ return null;
+ var num = parseInt(value, 10);
+ return (isNaN(num) ? null : num);
+}
+
+/**
+ * Convert a string into a double/float value. <b>Internationalization
+ * is not supported</b>
+ * @param {string} the string to convert to double/float
+ * @param {string} the decimal character
+ * @return {float|null} null if string does not represent a float value
+ */
+Prado.Util.toDouble = function(value, decimalchar)
+{
+ decimalchar = undef(decimalchar) ? "." : decimalchar;
+ var exp = new RegExp("^\\s*([-\\+])?(\\d+)?(\\" + decimalchar + "(\\d+))?\\s*$");
+ var m = value.match(exp);
+ if (m == null)
+ return null;
+ 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 currency value
+ * @param {string} the grouping character, default is ","
+ * @param {int} number of decimal digits
+ * @param {string} the decimal character, default is "."
+ * @type {float|null} the currency value as float.
+ */
+Prado.Util.toCurrency = function(value, groupchar, digits, decimalchar)
+{
+ groupchar = undef(groupchar) ? "," : groupchar;
+ decimalchar = undef(decimalchar) ? "." : decimalchar;
+ digits = undef(digits) ? 2 : digits;
+
+ var exp = new RegExp("^\\s*([-\\+])?(((\\d+)\\" + groupchar + ")*)(\\d+)"
+ + ((digits > 0) ? "(\\" + decimalchar + "(\\d{1," + digits + "}))?" : "")
+ + "\\s*$");
+ var m = value.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);
+}
+
+/**
+ * Trim the value, if the value is undefined, empty string is return.
+ * @param {string} string to be trimmed.
+ * @type {string} trimmed string.
+ */
+Prado.Util.trim = function(value)
+{
+ if(!isString(value)) return "";
+ return value.replace(/^\s+|\s+$/g, "");
+}