Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akhileshdarjee/ed0546cfaa3d0c352db82a7906d71bcc to your computer and use it in GitHub Desktop.
Save akhileshdarjee/ed0546cfaa3d0c352db82a7906d71bcc to your computer and use it in GitHub Desktop.
Javascript useful functions/protoytpes
// 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();});
};
// convert string to snake case
String.prototype.toSnakeCase = function () {
return this.replace(/(.)([A-Z])/g, "$1_$2").toLowerCase();
};
// 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;
};
// 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;
};
// 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;
};
// check if the string is a url/link
String.prototype.isURL = function() {
var pattern = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
return pattern.test(this);
}
// create slug from string
String.prototype.slugify = function() {
str = this.replace(/^\s+|\s+$/g, ''); // trim
str = str.toLowerCase();
// remove accents, swap ñ for n, etc
var from = "ÁÄÂÀÃÅČÇĆĎÉĚËÈÊẼĔȆÍÌÎÏŇÑÓÖÒÔÕØŘŔŠŤÚŮÜÙÛÝŸŽáäâàãåčçćďéěëèêẽĕȇíìîïňñóöòôõøðřŕšťúůüùûýÿžþÞĐđßÆa·/_,:;";
var to = "aaaaaacccdeeeeeeeeiiiinnoooooorrstuuuuuyyzaaaaaacccdeeeeeeeeiiiinnooooooorrstuuuuuyyzbBDdBAa------";
for (var i=0, l=from.length ; i<l ; i++) {
str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
}
str = str.replace(/[^a-z0-9 -]/g, '') // remove invalid chars
.replace(/&/g, '-and-') // replace '&' with 'and'
.replace(/\s+/g, '-') // collapse whitespace and replace by -
.replace(/-+/g, '-') // collapse dashes
.replace(/[^\w\-]+/g, '') // remove all non-word chars
.replace(/\-\-+/g, '-') // replace multiple '-' with single '-'
.replace(/^-+/, '') // Trim - from start of text
.replace(/-+$/, ''); // Trim - from end of text
return str;
}
// Prototyping for getting month long name and short name
Date.prototype.getMonthName = function(lang) {
lang = lang && (lang in Date.locale) ? lang : 'en';
return Date.locale[lang].month_names[this.getMonth()];
};
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']
}
};
// check if array contains element
Array.prototype.contains = function(obj) {
var i = this.length;
while (i--) {
if (this[i] === obj) {
return true;
}
}
return false;
}
// get random element from array
Array.prototype.random = function () {
return this[Math.floor((Math.random()*this.length))];
}
// get object from localstorage
Storage.prototype.getObject = function(key) {
var value = this.getItem(key);
return value && JSON.parse(value);
}
// set object in localstorage
Storage.prototype.setObject = function(key, value) {
this.setItem(key, JSON.stringify(value));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment