Skip to content

Instantly share code, notes, and snippets.

@Witiko
Created September 6, 2013 11:01
Show Gist options
  • Save Witiko/6462383 to your computer and use it in GitHub Desktop.
Save Witiko/6462383 to your computer and use it in GitHub Desktop.
A set of useful functions I've created over the years of my javascript programming
/*
Compatible Now! ~ Vít Novotný 2009 - 2011
Requires:
* Ecma5.js
*/
(function(Array, String, undefined) {
Date.timeZone = -new Date().getTimezoneOffset() / 60;
Number.prototype.times = function(f, bind) {
var index = this;
if(/\./.test(index) || index <= 0) return;
while(index-- > 0) f.call(bind);
};
Boolean.prototype.xor = function(boolean) {
var that = this.valueOf(),
bool = boolean.valueOf();
return (that && !bool) || (!that && bool);
};
Boolean.prototype.bitify = function() {
return this.valueOf()?1:0;
};
Boolean.random = function() {
return !Math.round(Math.random());
};
Function.prototype.attach = function() { // The returned function ignores all passed arguments
var boundFunction = this,
boundArguments = Array.from(arguments);
return function() {
return boundFunction.apply(
this, boundArguments
);
};
};
Function.prototype.partial = function() { // The arguments passed to the returned function
var boundFunction = this, // are concaterated with the bound arguments
boundArguments = Array.from(arguments);
return function() {
return boundFunction.apply(
this, boundArguments.concat(Array.from(arguments))
);
};
};
(function(global) {
Function.prototype.async = function(context) {
var args = arguments, func = this; global.setTimeout(function() {
func.call.apply(func, args);
}, 1);
};
})(this);
if(!("reverse" in String.prototype))
String.prototype.reverse = function() {
return String.split(this, "").reverse().join("");
};
Array.prototype.remove = function(index) {
Array.splice(this, index, 1);
return this.length;
};
Array.prototype.removeValue = function(value, from, all) {
var from = from || 0, that = this, index;
if(all) {
(index = Array.gather(this, value, from)).forEach(function(value, index) {
Array.remove(that, value - index);
}); return this.length;
} return (index = Array.indexOf(this, value, from)) !== -1?
Array.remove( this, index):this.length;
};
Array.prototype.rRemoveValue = function(value, from, all) {
var from = from || 0, that = this, index;
if(all) {
(index = Array.rGather(this, value, from)).forEach(function(value, index) {
Array.remove(that, value - index);
}); return this.length;
} return (index = Array[value instanceof RegExp?"search":"indexOf"](this, value, from)) !== -1?
Array.remove(this, index):this.length;
};
Array.prototype.equals = function(arr, maxDepth) {
maxDepth = maxDepth === true?Infinity:(!maxDepth?0:maxDepth);
var args = arguments;
return maxDepth-- !== -1 && this.length === arr.length?
Array.every(this, function(item, index) {
if(item === arr[index]) return true;
return item instanceof Array &&
arr[index] instanceof Array?
args.callee.call(item, arr[index], maxDepth):false;
}):false;
};
Array.prototype.shuffle = function() {
var i = this.length;
while(i) Array.swap(this, Math.floor(i-- * Math.random()), i);
return this;
};
Array.prototype.random = function() {
var l; return this[
(l = this.length) <= 1?
0:Math.ceil(
Math.random() * l
) - 1
];
};
Array.prototype.pick = function() {
var result, random, l;
if((l = this.length) === 1) {
result = this[0];
this.length = 0;
} else {
random = Math.ceil(
Math.random() * l
) - 1;
result = this[random];
Array.remove(this, random);
} return result;
};
Array.prototype.last = function() {
return this[this.length - 1];
};
Array.prototype.empty = function() {
this.length = 0;
};
Array.prototype.uPush = function() {
var arr = this; Array.forEach(arguments, function(item) {
if(!Array.contains(arr, item)) Array.push(arr, item);
}); return arr.length;
};
Array.prototype.uConcat = function() {
return Array.prototype.concat.apply(
this, arguments
).unique();
};
Array.prototype.associate = function(values) {
var obj = {}; Array.forEach(this, function(name, index) {
obj[name] = values[index];
}); return obj;
};
Array.prototype.numberOf = function(needle, from) {
from = !from || from < 0?0:from;
for(var num = 0; from = Array.indexOf(this, needle, from) + 1; num++);
return num;
};
String.prototype.numberOf = function(needle, from) {
return Array.prototype.rNumberOf.call(
String.isString(this)?this:this.toString(), needle, from
);
};
Array.prototype.rNumberOf = function(needle, from) {
from = !from || from < 0?0:from;
for(var num = 0,
func = Array[needle instanceof RegExp?"search":"indexOf"];
from = func(this, needle, from) + 1; num++); return num;
};
Array.prototype.unique = function() {
Array.forEach(this, function(item, i, arr) {
Array.removeValue(arr, item, i + 1, true);
}); return this;
};
Array.prototype.rotate =
Array.prototype.rotateRight = function() {
Array.unshift(this, Array.pop(this));
};
Array.prototype.rotateLeft = function() {
Array.push(this, Array.shift(this));
};
Array.prototype.swap = function(a, b) {
var tmp = this[a];
this[a] = this[b];
this[b] = tmp;
};
Array.prototype.swapValues = function(a, b) {
var aPos = this.indexOf(a),
bPos = this.indexOf(b);
this[aPos] = b; this[bPos] = a;
};
Array.prototype.copy = function() {
return Array.slice(this, 0);
};
Array.prototype.count = function() {
var values = [],
amounts = [];
Array.forEach(this, function(value, index, array) {
if(Array.contains(values, value)) return;
values.push(value);
amounts.push(Array.numberOf(array, value));
}); return values.map(function(value, index) {
return [value, amounts[index]];
}).sort(function(a, b) {
return b[1] - a[1];
});
};
Array.prototype.densify = function() {
var i = 0, l = this.length;
while(i !== l)
if(this[i] === undefined) {
Array.remove(this, i); l--;
} else i++;
};
Array.prototype.contains = function(needle) {
return Array.indexOf(this, needle) !== -1;
};
String.prototype.contains = function(needle) {
return needle instanceof RegExp?
needle.test(this):String.indexOf(this, needle) !== -1;
};
Array.prototype.rContains = function(needle) {
return Array[needle instanceof RegExp?
"search":"indexOf"](this, needle) !== -1;
};
Array.prototype.gather = function(needle, from) {
from = !from || from < 0?0:from;
var indexes = [];
while((from = Array.indexOf(this, needle, from)) !== -1) {
indexes.push(from++);
} return indexes;
};
Array.prototype.rGather = String.prototype.gather = function(needle, from) {
from = !from || from < 0?0:from;
var indexes = [],
func = Array[needle instanceof RegExp?"search":"indexOf"];
while((from = func(this, needle, from)) !== -1) {
indexes.push(from++);
} return indexes;
};
String.prototype.search = function(needle, from) {
from = !from || from < 0?0:from;
if(!(needle instanceof RegExp)) return String.indexOf(this, needle, from);
for(var char; char = String.charAt(this, from); from++)
if(needle.test(char)) return from; return -1;
};
Array.prototype.search = function(needle, from) {
from = !from || from < 0?0:from;
if(!(needle instanceof RegExp)) return Array.indexOf(this, needle, from);
for(var l = this.length; from < l; from++)
if(needle.test(this[from])) return from; return -1;
};
Object.deassociate = function(obj) {
var keys = [],
vals = [];
for(var key in obj) {
keys.push( key );
vals.push(obj[key]);
} return {
keys: keys,
values: vals
};
};
Array.from = function(arrayLike) {
return Array.slice(arrayLike, 0);
};
Array.create = function(obj) {
var dimensions = obj.dimensions.length,
amount = obj.dimensions[0],
fill = obj.fill,
array = new Array(amount),
callee = arguments.callee, newObj, index;
if(dimensions === 1) {
if("fill" in obj)
for(index = 0; index !== amount; index++)
array[index] = fill;
} else {
newObj = {
dimensions: Array.slice(obj.dimensions, 1)
}; if("fill" in obj) newObj.fill = fill;
for(index = 0; index !== amount; index++)
array[index] = callee(newObj);
} return array;
};
(function(shorthands, local) {
for(var obj in shorthands) {
(function(obj, arr) {
arr.forEach(function(name) {
if(!(obj[name] instanceof Function))
obj[name] = function(context) {
return obj.prototype[name].call.apply(
obj.prototype[name], arguments);
};
});
})(local[obj], shorthands[obj])
}
})({
Array: ["numberOf", "rNumberOf", "associate", "search", "gather", "rGather", "rContains", "contains", "densify", "copy", "swap", "swapValues", "rotate", "rotateLeft", "rotateRight", "unique", "rNumberOf", "numberOf", "uConcat", "uPush", "empty", "last", "pick", "random", "shuffle", "rRemoveValue", "removeValue", "count", "remove"],
String: ["search", "contains", "reverse"]
}, {
Array: Array,
String: String
});
})(Array, String);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment