JQuery/JavaScript Extension Methods
JavaScript Extension JS
Source
//~~Prototype===========================================================================================================================
//[--String--]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~>
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~>
String.prototype.J_Trim = function () {
return this.replace(/^\s+|\s+$/g, "");
};
String.prototype.J_IsFilled = function () {
return this && this.length > 0 ? true : false;
}
String.prototype.J_IsEmpty = function () {
return !this.IsFilled();
}
String.prototype.J_PadLeft = function (len, c) {
var s = this;
c = c || '0';
while (s.length < len) s = c + s;
return s;
};
String.prototype.J_Format = function () {
var args = arguments;
return this.replace(/{(\d+)}/g, function (match, number) {
return typeof args[number] != 'undefined'
? args[number]
: match
;
});
};
String.prototype.J_EndsWith = function (suffix) {
return this.indexOf(suffix, this.length - suffix.length) !== -1;
};
String.prototype.J_TryParseInt = function () {
var result = 0;
if (this !== null) {
if (this.length > 0) {
if (!isNaN(this)) {
result = parseInt(this);
}
}
}
return result;
};
String.prototype.J_TryParseFloat = function () {
var result = 0;
if (this !== null) {
if (this.length > 0) {
if (!isNaN(this)) {
result = parseFloat(this);
}
}
}
return result;
};
String.prototype.J_TryParseBool = function () {
var result = false;
if (this !== null) {
if (this.length > 0) {
if (!isNaN(this)) {
if (this.toLowerCase() == "true" || this.toLowerCase() == "yes" || this.toLowerCase() == "1") {
result = true;
}
}
}
}
return result;
};
//[--Number--]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~>
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~>
Number.prototype.J_IsEven = function () {
return this % 2 == 0;
};
Number.prototype.J_IsOdd = function () {
return this % 2 == 1;
};
Number.prototype.J_Randomize = function (limit) {
var a = Math.pow(10, String(limit).length);
var readyNumber = Math.floor(Math.random() * a);
if (readyNumber >= limit) {
return randomize(limit);
} else {
return readyNumber;
}
};
Number.prototype.J_ToCurrency = function (noFractions, currencySymbol, decimalSeparator, thousandsSeparator) {
var n, startAt, intLen;
if (currencySymbol == null) currencySymbol = "$";
if (decimalSeparator == null) decimalSeparator = ".";
if (thousandsSeparator == null) thousandsSeparator = ",";
n = this.round(noFractions ? 0 : 2, true, decimalSeparator);
intLen = n.length - (noFractions ? 0 : 3);
if ((startAt = intLen % 3) == 0) startAt = 3;
for (var i = 0, len = Math.ceil(intLen / 3) - 1; i < len; i++) n = n.insertAt(i * 4 + startAt, thousandsSeparator);
return currencySymbol + n;
};
Number.prototype.J_ToInteger = function (thousandsSeparator) {
var n, startAt, intLen;
if (thousandsSeparator == null) thousandsSeparator = ",";
n = this.round(0, true);
intLen = n.length;
if ((startAt = intLen % 3) == 0) startAt = 3;
for (var i = 0, len = Math.ceil(intLen / 3) - 1; i < len; i++) n = n.insertAt(i * 4 + startAt, thousandsSeparator);
return n;
};
Number.prototype.J_Round = function (decimals, returnAsString, decimalSeparator) {
//Supports 'negative' decimals, e.g. myNumber.round(-3) rounds to the nearest thousand
var n, factor, breakPoint, whole, frac;
if (!decimals) decimals = 0;
factor = Math.pow(10, decimals);
n = (this.valueOf() + ""); //To get the internal value of an Object, use the valueOf() method
if (!returnAsString) return Math.round(n * factor) / factor;
if (!decimalSeparator) decimalSeparator = ".";
if (n == 0) return "0." + ((factor + "").substr(1));
breakPoint = (n = Math.round(n * factor) + "").length - decimals;
whole = n.substr(0, breakPoint);
if (decimals > 0) {
frac = n.substr(breakPoint);
if (frac.length < decimals) frac = (Math.pow(10, decimals - frac.length) + "").substr(1) + frac;
return whole + decimalSeparator + frac;
} else return whole + ((Math.pow(10, -decimals) + "").substr(1));
};
//[--Boolean--]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~>
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~>
Boolean.prototype.J_XOR = function (bool2) {
var bool1 = this.valueOf();
return (bool1 == true && bool2 == false) || (bool2 == true && bool1 == false);
//return (bool1 && !bool2) || (bool2 && !bool1);
};
//[--Array--]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~>
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~>
Array.prototype.J_Contains = function (value) {
for (var i = 0; i < this.length; i++) {
if (this[i] == value) return true;
}
return false;
};
Array.prototype.J_Union = function (a2, compareFunction) {
return this.concat(a2 ? a2 : null).removeDuplicates(compareFunction);
};
Array.prototype.J_Subtract = function (a2, compareFunction) {
if (!compareFunction) compareFunction = null;
var a1 = this.removeDuplicates(compareFunction);
if (!a2) return a1;
var a2 = a2.removeDuplicates(compareFunction);
var len2 = a2.length;
if (compareFunction) {
for (var i = 0; i < a1.length; i++) {
var src = a1[i], found = false, src;
for (var j = 0; j < len2 && compareFunction(src2 = a2[j], src) != 1; j++) if (compareFunction(src, src2) == 0) { found = true; break; }
if (found) a1.splice(i--, 1);
}
} else {
for (var i = 0; i < a1.length; i++) {
var src = a1[i], found = false, src;
for (var j = 0; (j < len2) && (src >= (src2 = a2[j])) ; j++) if (src2 == src) { found = true; break; }
if (found) a1.splice(i--, 1);
}
}
return a1;
};
Array.prototype.J_Intersect = function (a2, compareFunction) {
if (!compareFunction) compareFunction = null;
var a1 = this.removeDuplicates(compareFunction);
if (!a2) return a1;
var a2 = a2.removeDuplicates(compareFunction);
var len2 = a2.length;
if (len2 < a1.length) {
var c = a2; a2 = a1; a1 = c; c = null;
len2 = a2.length;
}
if (compareFunction) {
for (var i = 0; i < a1.length; i++) {
var src = a1[i], found = false, src;
for (var j = 0; j < len2 && compareFunction(src2 = a2[j], src) != 1; j++) if (compareFunction(src, src2) == 0) { found = true; break; }
if (!found) a1.splice(i--, 1);
}
} else {
for (var i = 0; i < a1.length; i++) {
var src = a1[i], found = false, src;
for (var j = 0; (j < len2) && (src >= (src2 = a2[j])) ; j++) if (src2 == src) { found = true; break; }
if (!found) a1.splice(i--, 1);
}
}
return a1;
};
Array.prototype.J_RemoveDuplicates = function (compareFunction) {
if (!compareFunction) compareFunction = null;
var a1 = this.concat().sort(compareFunction);
if (compareFunction) {
for (var i = 0; i < a1.length; i++) {
var src = a1[i];
for (var j = i + 1; j < a1.length && compareFunction(a1[j], src) == 0; j++) { }
if (j - 1 > i) a1.splice(i + 1, j - i - 1);
}
} else {
for (var i = 0; i < a1.length; i++) {
var src = a1[i];
for (var j = i + 1; j < a1.length && a1[j] == src; j++) { }
if (j - 1 > i) a1.splice(i + 1, j - i - 1);
}
}
return a1;
};
Array.prototype.J_Exclusion = function (a2, compareFunction) {
if (!compareFunction) compareFunction = null;
var a1 = this.removeDuplicates(compareFunction);
if (!a2) return a1;
return a1.subtract(a2, compareFunction).concat(a2.subtract(a1, compareFunction)).sort(compareFunction);
};
Array.prototype.J_UnSortedUnion = function (a2, compareFunction) {
if (!compareFunction) compareFunction = null;
return this.concat(a2 ? a2 : null).unsortedRemoveDuplicates(compareFunction);
};
Array.prototype.J_UnSortedSubtract = function (a2, compareFunction) {
if (!compareFunction) compareFunction = null;
var a1 = this.unsortedRemoveDuplicates(compareFunction);
if (!a2) return a1;
var subtrahend = a2.unsortedRemoveDuplicates(compareFunction);
if (compareFunction) {
for (var i = 0; i < a1.length; i++) {
var src = a1[i];
for (var j = 0, len = subtrahend.length; j < len; j++) if (compareFunction(subtrahend[j], src) == 0) { a1.splice(i--, 1); break; }
}
} else {
for (var i = 0; i < a1.length; i++) {
var src = a1[i];
for (var j = 0, len = subtrahend.length; j < len; j++) if (subtrahend[j] == src) { a1.splice(i--, 1); break; }
}
}
return a1;
};
Array.prototype.J_UnSortedIntersect = function (a2, compareFunction) {
if (!compareFunction) compareFunction = null;
if (!a2) return this.unsortedRemoveDuplicates(compareFunction);
var a1 = this;
var len2 = a2.length;
a1 = a1.unsortedRemoveDuplicates(compareFunction);
if (compareFunction) {
for (var i = 0; i < a1.length; i++) {
var src = a1[i];
for (var j = 0; j < len2; j++) if (compareFunction(a2[j], src) == 0) break;
if (j == len2) a1.splice(i--, 1);
}
} else {
for (var i = 0; i < a1.length; i++) {
var src = a1[i];
for (var j = 0; j < len2; j++) if (a2[j] == src) break;
if (j == len2) a1.splice(i--, 1);
}
}
return a1;
};
Array.prototype.J_UnSortedRemoveDuplicates = function (compareFunction) {
var a1 = this.concat();
if (compareFunction) {
for (var i = 0; i < a1.length; i++) {
var src = a1[i];
for (var j = i + 1; j < a1.length; j++) if (compareFunction(a1[j], src) == 0) a1.splice(j, 1);
}
} else {
for (var i = 0; i < a1.length; i++) {
var src = a1[i];
for (var j = i + 1; j < a1.length; j++) if (a1[j] == src) a1.splice(j--, 1);
}
}
return a1;
};
Array.prototype.J_UnSortedExclusion = function (a2, compareFunction) {
if (!compareFunction) compareFunction = null;
var a1 = this.unsortedRemoveDuplicates(compareFunction);
if (!a2) return a1;
return a1.unsortedSubtract(a2, compareFunction).concat(a2.unsortedSubtract(a1, compareFunction)).sort(compareFunction);
};
//[--Date--]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~>
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~>
Date.prototype.J_GetFullDay = function () {
return this.DAYNAMES[this.getDay()];
};
Date.prototype.J_GetDayAbbr = function () {
return this.getFullDay().slice(0, 3);
};
Date.prototype.J_GetFullMonth = function () {
return this.MONTHNAMES[this.getMonth()];
};
Date.prototype.J_GetMonthAbbr = function () {
return this.getFullMonth().slice(0, 3);
};
Date.prototype.J_To12HourTimeString = function () {
var h = this.getHours();
var m = "0" + this.getMinutes();
var s = "0" + this.getSeconds();
var ap = "am";
if (h >= 12) {
ap = "pm";
if (h >= 13)
h -= 12;
} else if (h == 0)
h = 12;
h = "0" + h;
return h.slice(-2) + ":" +
m.slice(-2) + ":" +
s.slice(-2) + " " + ap;
};
Date.prototype.J_To24HourTimeString = function () {
var h = "0" + this.getHours();
var m = "0" + this.getMinutes();
var s = "0" + this.getSeconds();
return h.slice(-2) + ":" + m.slice(-2) + ":" + s.slice(-2);
};
Date.prototype.J_CustomFormat = function (formatString) {
var YYYY, YY, MMMM, MMM, MM, M, DDDD, DDD, DD, D, hhh, hh, h, mm, m, ss, s, ampm, dMod, th;
YY = ((YYYY = this.getFullYear()) + "").substr(2, 2);
MM = (M = this.getMonth() + 1) < 10 ? ('0' + M) : M;
MMM = (MMMM = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"][M - 1]).substr(0, 3);
DD = (D = this.getDate()) < 10 ? ('0' + D) : D;
DDD = (DDDD = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"][this.getDay()]).substr(0, 3);
th = (D >= 10 && D <= 20) ? 'th' : ((dMod = D % 10) == 1) ? 'st' : (dMod == 2) ? 'nd' : (dMod == 3) ? 'rd' : 'th';
formatString = formatString.replace("#YYYY#", YYYY).replace("#YY#", YY).replace("#MMMM#", MMMM).replace("#MMM#", MMM).replace("#MM#", MM).replace("#M#", M).replace("#DDDD#", DDDD).replace("#DDD#", DDD).replace("#DD#", DD).replace("#D#", D).replace("#th#", th);
h = (hhh = this.getHours());
if (h == 0) h = 24;
if (h > 12) h -= 12;
hh = h < 10 ? ('0' + h) : h;
ampm = hhh < 12 ? 'am' : 'pm';
mm = (m = this.getMinutes()) < 10 ? ('0' + m) : m;
ss = (s = this.getSeconds()) < 10 ? ('0' + s) : s;
return formatString.replace("#hhh#", hhh).replace("#hh#", hh).replace("#h#", h).replace("#mm#", mm).replace("#m#", m).replace("#ss#", ss).replace("#s#", s).replace("#ampm#", ampm);
};
Date.prototype.J_Lastday = function () {
var d = new Date(this.getFullYear(), this.getMonth() + 1, 0);
return d.getDate();
};
Date.prototype.J_GetDaysBetween = function (d) {
d = d.copy();
d.setHours(this.getHours(), this.getMinutes(), this.getSeconds(), this.getMilliseconds());
var diff = d.getTime() - this.getTime();
return (diff) / this.msPERDAY;
};
Date.prototype.J_AddDays = function (d) {
this.setDate(this.getDate() + d);
};
Date.prototype.J_AddWeeks = function (w) {
this.addDays(w * 7);
};
Date.prototype.J_AddMonths = function (m) {
var d = this.getDate();
this.setMonth(this.getMonth() + m);
if (this.getDate() < d)
this.setDate(0);
};
Date.prototype.J_AddYears = function (y) {
var m = this.getMonth();
this.setFullYear(this.getFullYear() + y);
if (m < this.getMonth()) {
this.setDate(0);
}
};
Date.prototype.J_GetAge = function () {
var today = new Date();
return this.getYearsBetween(today).toFixed(2);
};
Date.prototype.J_GetYearsBetween = function (d) {
var months = this.getMonthsBetween(d);
return months / 12;
};
//Date.prototype.J_getMonthsBetween = function(d) {
// var sDate, eDate;
// var d1 = this.getFullYear() * 12 + this.getMonth();
// var d2 = d.getFullYear() * 12 + d.getMonth();
// var sign;
// var months = 0;
// if (this == d) {
// months = 0;
// } else if (d1 == d2) { //same year and month
// months = (d.getDate() - this.getDate()) / this.lastday();
// } else {
// if (d1 < d2) {
// sDate = this;
// eDate = d;
// sign = 1;
// } else {
// sDate = d;
// eDate = this;
// sign = -1;
// }
// var sAdj = sDate.lastday() - sDate.getDate();
// var eAdj = eDate.getDate();
// var adj = (sAdj + eAdj) / sDate.lastday() - 1;
// months = Math.abs(d2 - d1) + adj;
// months = (months * sign)
// }
// return months;
//};
Date.prototype.J_SameDayEachWeek = function (day, date) {
var aDays = new Array();
var eDate, nextDate, adj;
if (this > date) {
eDate = this;
nextDate = date.copy();
} else {
eDate = date;
nextDate = this.copy();
}
adj = (day - nextDate.getDay() + 7) % 7;
nextDate.setDate(nextDate.getDate() + adj);
while (nextDate < eDate) {
aDays[aDays.length] = nextDate.copy();
nextDate.setDate(nextDate.getDate() + 7);
}
return aDays;
};
Date.prototype.J_GetWeekNumber = function () {
var date = this;
date = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()));
var IsoDayOfWeek = date.getDay() == 0 ? 7 : date.getDay(); // Sunday = 7
date.setDate(date.getDate() + 4 - IsoDayOfWeek); // Change to nearest Thursday
var DayOfYear = (date.getTime() - Date.UTC(date.getFullYear(), 0, 1)) / 864e5;
var week = Math.floor(DayOfYear / 7) + 1;
return week;
};
Date.prototype.J_GetDayName = function (language) {
var days = [], supportetLanguages = ["da-DK", "en-US"];
/*
Danish is the standard
Note that this line uses a containsmetode from the Array object,
which are not in the native code of javascript.
See my post about extending the array object for more information.
*/
language = language && supportetLanguages.contains(language) ? language : "da-DK";
//Danish
days["da-DK"] = [];
days["da-DK"][0] = "Søndag";
days["da-DK"][1] = "Mandag";
days["da-DK"][2] = "Tirsdag";
days["da-DK"][3] = "Onsdag";
days["da-DK"][4] = "Torsdag";
days["da-DK"][5] = "Fredag";
days["da-DK"][6] = "Lørdag";
//English
days["en-US"] = [];
days["en-US"][0] = "Sunday";
days["en-US"][1] = "Monday";
days["en-US"][2] = "Tuesday";
days["en-US"][3] = "Wednesday";
days["en-US"][4] = "Thursday";
days["en-US"][5] = "Friday";
days["en-US"][6] = "Saturday";
return days[language][this.getDay()];
};
Date.prototype.J_GetMonthName = function (language) {
var months = [], supportetLanguages = ["da-DK", "en-US"];
/*
Danish is the standard
Note that this line uses a containsmetode from the Array object,
which are not in the native code of javascript.
See my post about extending the array object for more information.
*/
language = language && supportetLanguages.contains(language) ? language : "da-DK";
//Dansk
months["da-DK"] = [];
months["da-DK"][0] = "Januar";
months["da-DK"][1] = "Februar";
months["da-DK"][2] = "Marts";
months["da-DK"][3] = "April";
months["da-DK"][4] = "Maj";
months["da-DK"][5] = "Juni";
months["da-DK"][6] = "Juli";
months["da-DK"][7] = "August";
months["da-DK"][8] = "September";
months["da-DK"][9] = "Oktober";
months["da-DK"][10] = "November";
months["da-DK"][11] = "December";
//English
months["en-US"] = [];
months["en-US"][0] = "January";
months["en-US"][1] = "February";
months["en-US"][2] = "March";
months["en-US"][3] = "April";
months["en-US"][4] = "May";
months["en-US"][5] = "June";
months["en-US"][6] = "July";
months["en-US"][7] = "August";
months["en-US"][8] = "September";
months["en-US"][9] = "October";
months["en-US"][10] = "November";
months["en-US"][11] = "December";
return months[language][this.getMonth()];
};
//[--Object--]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~>
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~>
Object.Extend = function (destination, source) {
for (var property in source)
destination[property] = source[property];
return destination;
};
Object.DeepExtend = function (destination, source) {
for (var property in source) {
if (typeof source[property] === "object") {
destination[property] = destination[property] || {};
arguments.callee(destination[property], source[property]);
} else {
destination[property] = source[property];
}
}
return destination;
};
Example
none
Author: Keyur Panchal
Submitted on: 3 nov. 2016
Language: JavaScript
Type: String, Number, Date, Array, Object
Views: 4041