Last active
December 20, 2015 09:19
-
-
Save stfnhh/6107192 to your computer and use it in GitHub Desktop.
Useful Prototypes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Arrays */ | |
Array.prototype.each = function(callback) { | |
for (i=0; i < this.length; i++) { | |
callback(i, this[i]) | |
} | |
} | |
Array.prototype.first = function() { | |
return(this[0] || undefined); | |
}; | |
Array.prototype.last = function() { | |
if(this.length > 0) { | |
return(this[this.length - 1]); | |
} | |
return(undefined); | |
}; | |
Array.prototype.shuffle = function() { | |
var currentIndex = this.length, temporaryValue, randomIndex ; | |
while (0 !== currentIndex) { | |
randomIndex = Math.floor(Math.random() * currentIndex); | |
currentIndex -= 1; | |
temporaryValue = this[currentIndex]; | |
this[currentIndex] = this[randomIndex]; | |
this[randomIndex] = temporaryValue; | |
} | |
return this; | |
} | |
Array.prototype.max = function(array){ | |
return(Math.max.apply(Math, array)); | |
}; | |
Array.prototype.min = function(array){ | |
return(Math.min.apply(Math, array)); | |
}; | |
/* Objects */ | |
Object.prototype.each = function(callback) { | |
for(k in this) { | |
if(this.hasOwnProperty(k)) { | |
callback(k, this[k]); | |
} | |
} | |
} | |
/* Strings */ | |
String.prototype.truncate = function(n,useWordBoundary){ | |
var toLong = this.length>n, | |
s = toLong ? this.substr(0,n-1) : this; | |
s = useWordBoundary && toLong ? s.substr(0,s.lastIndexOf(' ')) : s; | |
return(toLong ? s + '…' : s); | |
}; | |
String.prototype.trim = function() { | |
return(this.replace(/^\s+|\s+$/g, '')); | |
}; | |
String.prototype.contains = function(t) { | |
return(this.indexOf(t) >= 0 ? true : false); | |
}; | |
String.prototype.linkify = function() { | |
var str = this.toString(); | |
str = str.replace(/(https?:\/\/[^\s]+)/gim, '<a href="$1">$1</a>', str); | |
str = str.replace(/([#|@][^\s]+)/gim, '<a href="http://twitter.com/$1">$1</a>', str); | |
return(str); | |
} | |
String.prototype.timeago = function(ref_date, date_formats, time_units) { | |
date_formats = date_formats || { | |
past: [ | |
{ ceiling: 60, text: "$seconds seconds ago" }, | |
{ ceiling: 3600, text: "$minutes minutes ago" }, | |
{ ceiling: 86400, text: "$hours hours ago" }, | |
{ ceiling: 2629744, text: "$days days ago" }, | |
{ ceiling: 31556926, text: "$months months ago" }, | |
{ ceiling: null, text: "$years years ago" } | |
], | |
future: [ | |
{ ceiling: 60, text: "in $seconds seconds" }, | |
{ ceiling: 3600, text: "in $minutes minutes" }, | |
{ ceiling: 86400, text: "in $hours hours" }, | |
{ ceiling: 2629744, text: "in $days days" }, | |
{ ceiling: 31556926, text: "in $months months" }, | |
{ ceiling: null, text: "in $years years" } | |
] | |
}; | |
//Time units must be be ordered largest -> smallest | |
time_units = time_units || [ | |
[31556926, 'years'], | |
[2629744, 'months'], | |
[86400, 'days'], | |
[3600, 'hours'], | |
[60, 'minutes'], | |
[1, 'seconds'] | |
]; | |
date = new Date(this); | |
ref_date = ref_date ? new Date(ref_date) : new Date(); | |
var seconds_difference = (ref_date - date) / 1000; | |
var tense = 'past'; | |
if (seconds_difference < 0) { | |
tense = 'future'; | |
seconds_difference = 0-seconds_difference; | |
} | |
function get_format() { | |
for (var i=0; i<date_formats[tense].length; i++) { | |
if (date_formats[tense][i].ceiling == null || seconds_difference <= date_formats[tense][i].ceiling) { | |
return(date_formats[tense][i]); | |
} | |
} | |
return(null); | |
} | |
function get_time_breakdown() { | |
var seconds = seconds_difference; | |
var breakdown = {}; | |
for(var i=0; i<time_units.length; i++) { | |
var occurences_of_unit = Math.floor(seconds / time_units[i][0]); | |
seconds = seconds - (time_units[i][0] * occurences_of_unit); | |
breakdown[time_units[i][1]] = occurences_of_unit; | |
} | |
return(breakdown); | |
} | |
function render_date(date_format) { | |
var breakdown = get_time_breakdown(); | |
var time_ago_text = date_format.text.replace(/\$(\w+)/g, function() { | |
return(breakdown[arguments[1]]); | |
}); | |
return(depluralize_time_ago_text(time_ago_text, breakdown)); | |
} | |
function depluralize_time_ago_text(time_ago_text, breakdown) { | |
for(var i in breakdown) { | |
if (breakdown[i] == 1) { | |
var regexp = new RegExp("\\b"+i+"\\b"); | |
time_ago_text = time_ago_text.replace(regexp, function() { | |
return(arguments[0].replace(/s\b/g, '')); | |
}); | |
} | |
} | |
return(time_ago_text); | |
} | |
return(render_date(get_format())); | |
} | |
/* Dates */ | |
Date.prototype.format = function (format) { | |
var date = this, | |
day = date.getDate(), | |
month = date.getMonth() + 1, | |
year = date.getFullYear(), | |
hours = date.getHours(), | |
minutes = date.getMinutes(), | |
seconds = date.getSeconds(); | |
if (!format) { | |
format = "yyyy-MM-dd"; | |
} | |
format = format.replace("MM", month.toString().replace(/^(\d)$/, '0$1')); | |
if (format.indexOf("yyyy") > -1) { | |
format = format.replace("yyyy", year.toString()); | |
} else if (format.indexOf("yy") > -1) { | |
format = format.replace("yy", year.toString().substr(2, 2)); | |
} | |
format = format.replace("dd", day.toString().replace(/^(\d)$/, '0$1')); | |
if (format.indexOf("t") > -1) { | |
if (hours > 11) { | |
format = format.replace("t", "pm"); | |
} else { | |
format = format.replace("t", "am"); | |
} | |
} | |
if (format.indexOf("HH") > -1) { | |
format = format.replace("HH", hours.toString().replace(/^(\d)$/, '0$1')); | |
} | |
if (format.indexOf("hh") > -1) { | |
if (hours > 12) { | |
hours -= 12; | |
} | |
if (hours === 0) { | |
hours = 12; | |
} | |
format = format.replace("hh", hours.toString().replace(/^(\d)$/, '0$1')); | |
} | |
if (format.indexOf("mm") > -1) { | |
format = format.replace("mm", minutes.toString().replace(/^(\d)$/, '0$1')); | |
} | |
if (format.indexOf("ss") > -1) { | |
format = format.replace("ss", seconds.toString().replace(/^(\d)$/, '0$1')); | |
} | |
return(format); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment