I know this is probably long overdue, but here is a list of some of the more common JScript functions that I use, and hopefully you will find useful.
String.prototype.trim = function () {
return this.replace(/(^\s+)|(\s+$)/g, "");
}
Number.prototype.formatNumber = function (placesToTheRight) {
if (placesToTheRight == null) placesToTheRight = 0;
if (typeof placesToTheRight != "number") throw "Invalid Parameter: " + placesToTheRight;
if (placesToTheRight < 0) throw "Argument out of range: " + placesToTheRight;
/(-*\d+)(\.(\d*))*/.exec(String(this));
var wholeNumber = RegExp.$1;
var remainder = RegExp.$3;
switch (placesToTheRight) {
case 0:
return wholeNumber;
default:
var arr = new Array(placesToTheRight);
arr.initialize(0);
for (var i = 0; i < remainder.length && i < arr.length; i++)
arr[i] = remainder.charAt(i);
return wholeNumber + "." + arr.join("");
}
}
This is required for the formatNumber function.
Array.prototype.initialize = function (value) {
for (var i = 0; i < this.length; i++) {
this[i] = value;
}
}
Number.prototype.formatCurrency = function () {
return "$" + this.formatNumber(2);
}
Date.prototype.getMonthName = function() {
switch(this.getMonth()) {
case 1: return "January"; break;
case 2: return "February"; break;
case 3: return "March"; break;
case 4: return "April"; break;
case 5: return "May"; break;
case 6: return "June"; break;
case 7: return "July"; break;
case 8: return "August"; break;
case 9: return "September"; break;
case 10: return "October"; break;
case 11: return "November"; break;
case 12: return "December"; break;
default: return "";
}
}
Date.prototype.formatDate = function (type) {
switch (type) {
case 2: // dd mname, yyyy
return (this.getDate() + " " + this.getMonthName() + ", " + this.getFullYear());
case 3: //mname dd, yyyy
return (this.getMonthName() + " " + this.getDate() + ", " + this.getFullYear());
case 4: // mm/dd/yyyy hh:mm:ss AM|PM
return this.formatDate() + " " + this.formatDate (7);
case 5: // mm/dd/yyyy hh:mm AM|PM
return this.formatDate() + " " + this.formatDate (7);
case 6: // hh:mm:ss AM|PM
return this.formatDate(8) + ":" + this.getSeconds().zeroPad() + " " +
(this.getHours() > 12 ? "PM" : "AM");
case 7: // hh:mm AM|PM
return this.formatDate(8) + " " + (this.getHours() > 12 ? "PM" : "AM");
case 8: // hh:mm
return (this.getHours() % 12 == 0 ? 12 : this.getHours() % 12) + ":" +
this.getMinutes().zeroPad();
default : // mm/dd/yyyy
return (this.getMonth() + 1).zeroPad() + "/" + this.getDate().zeroPad() + "/" +
this.getFullYear();
}
}
This is required for the formatDate function.
Number.prototype.zeroPad = function() {
if (this < 10)
return "0" + this.toString();
return this.toString();
}
Date.prototype.toUsaString = function() {
return (ZeroPad(this.getMonth() + 1)) + "/" +
ZeroPad(this.getDate()) + "/" +
ZeroPad(this.getFullYear()) + " " +
ZeroPad(this.getHours()) + ":" +
ZeroPad(this.getMinutes()) + ":" +
ZeroPad(this.getSeconds());
}
String.prototype.formatPhone = function () {
var returnString = new String();
if (String(this).length == 7) {
returnString = String(this).substring(0,3) + "-" + String(this).substring(3,7);
} else if (String(this).length == 10) {
returnString = "(" + String(this).substring(0,3) + ")" + String.fromCharCode(160) +
String(this).substring(3,6) + "-" + String(this).substring(6,10);
} else {
returnString = this;
}
return returnString;
}
String.prototype.formatZip = function() {
strTmpZip = new String(this);
if (this.indexOf("-") == -1) {
if (this.length > 5) strTmpZip = this.substr(0,5) + "-" + this.substr(5);
}
return strTmpZip;
}
function SQLin (val) {
switch (typeof val) {
case "string":
if (val.toUpperCase() == "NULL") return "NULL";
if (val.toUpperCase() == "UNDEFINED") return "NULL";
val = val.replace(/\'/g, "''").replace(/(&)/g, "&"); //'
return "'" + val + "'";
case "boolean":
return val ? 1 : 0;
case "number":
return val;
case "date":
return "'" + val.toUsaString() + "'";
default:
return val == null ? "NULL" : SQLin(String(val));
}
}