Skip to content

Instantly share code, notes, and snippets.

View zykadelic's full-sized avatar

Andreas Fransson zykadelic

View GitHub Profile
@zykadelic
zykadelic / array.count.js
Last active June 10, 2022 23:49
Count the frequency of an element within an array, returns length if the argument is undefined
if(!Array.prototype.count){
Array.prototype.count = function(el){
if(typeof(el) === "undefined"){
return this.length;
}else{
var count = 0;
for(i = 0; i < this.length; i++){
if(this[i] === el){
count++;
}
@zykadelic
zykadelic / pluralize.js
Last active December 12, 2015 07:39
Easily pluralize strings. Inspired by the Ruby on Rails helper (http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-pluralize).
function pluralize(count, singular, plural){
return (count || 0) + ' ' + (count == 1 ? singular : (plural || singular + 's'));
}