ot-apollo domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home3/achieve/public_html/knowledgebase/wp-includes/functions.php on line 6131// convert string to proper case
String.prototype.toProperCase = function () {
return this.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
};
Usage:
'your name'.toProperCase(); Output: 'Your Name'
// convert string to snake case
String.prototype.toSnakeCase = function () {
return this.replace(/(.)([A-Z])/g, "$1_$2").toLowerCase();
};
Usage:
'YourName'.toSnakeCase(); Output: 'your_name'
// check if string is a valid date
String.prototype.isDate = function () {
var dateFormat;
if (toString.call(this) === '[object Date]') {
return true;
}
if (typeof this.replace === 'function') {
this.replace(/^\s+|\s+$/gm, '');
}
dateFormat = /(^\d{1,4}[\.|\\/|-]\d{1,2}[\.|\\/|-]\d{1,4})(\s*(?:0?[1-9]:[0-5]|1(?=[012])\d:[0-5])\d\s*[ap]m)?$/;
if (dateFormat.test(this)) {
return !!new Date(this).getTime();
}
return false;
};
Usage:
'12-03-1990'.isDate(); Output: true
// check if string is a time
String.prototype.isTime = function () {
var isValid = /^([0-1]?[0-9]|2[0-3]):([0-5][0-9])(:[0-5][0-9])?$/.test(this);
return isValid;
};
Usage:
'14:36'.isTime(); Output: true
// check if string is a time
String.prototype.isDateTime = function () {
var date = this.split(" ");
if (date[0].isDate() && date[1].isTime()) {
return true;
}
return false;
};
Usage:
'20-10-1990 14:36'.isDateTime(); Output: true
// getting month long name from Date object
Date.prototype.getMonthName = function(lang) {
lang = lang && (lang in Date.locale) ? lang : 'en';
return Date.locale[lang].month_names[this.getMonth()];
};
// getting month short name from Date object
Date.prototype.getMonthNameShort = function(lang) {
lang = lang && (lang in Date.locale) ? lang : 'en';
return Date.locale[lang].month_names_short[this.getMonth()];
};
Date.locale = {
en: {
month_names: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
month_names_short: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
}
};
Usage:
var current_month = new Date().getMonthName(lang); var current_short_name = new Date().getMonthNameShort(lang);
lang parameter is optional
Array.prototype.contains = function(obj) {
var i = this.length;
while (i--) {
if (this[i] === obj) {
return true;
}
}
return false;
}
Usage:
['Red', 'Yellow', 'Green', 'Blue'].contains('Green');
Output: true
Array.prototype.random = function () {
return this[Math.floor((Math.random()*this.length))];
}
Usage:
[1, 2, 3, 4].random();
Storage.prototype.setObject = function(key, value) {
this.setItem(key, JSON.stringify(value));
}
Usage:
localStorage.setObject('numbers', [1, 2, 3, 4]);
Storage.prototype.getObject = function(key) {
var value = this.getItem(key);
return value && JSON.parse(value);
}
Usage:
localStorage.getObject('numbers');