Skip to content

Instantly share code, notes, and snippets.

@FelipeBudinich
Last active August 29, 2015 14:00
Show Gist options
  • Save FelipeBudinich/11159797 to your computer and use it in GitHub Desktop.
Save FelipeBudinich/11159797 to your computer and use it in GitHub Desktop.
Array utilities plugin for ImpactJs
/*
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Copyright (C) 2014 Felipe Budinich <[email protected]>
Everyone is permitted to copy and distribute verbatim or modified
copies of this document and license.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
*/
/*global ig*/
ig.module(
'plugins.array'
).requires().defines(function () {
'use strict';
if (Array.prototype.copy) {
throw "Array method 'copy()' already defined elsewhere";
} else {
/**
* Returns a shallow copy of this array
*/
Array.prototype.copy = function () {
return this.slice(0);
};
}
if (Array.prototype.contains) {
throw "Array method 'contains()' already defined elsewhere";
} else {
/**
* Returns true if this array contains 'element', returns false otherwise
*/
Array.prototype.contains = function (element) {
if (this.indexOf(element) !== -1) {
return true;
} else {
return false;
}
};
}
if (Array.prototype.remove) {
throw "Array method 'remove()' already defined elsewhere";
} else {
/**
* Returns a copy of this array, removing the elements 'from' index 'to' index within it
*/
Array.prototype.remove = function (from, to) {
var copy = this.copy(),
rest = copy.slice((to || from) + 1 || copy.length);
copy.length = from < 0 ? copy.length + from : from;
copy.push.apply(copy, rest);
return copy;
};
}
if (Array.prototype.rotate) {
throw "Array method 'rotate()' already defined elsewhere";
} else {
/**
* Returns a copy of this array, rotated 'n' places, counterclockwise if 'n' is positive, clockwise otherwise
*/
Array.prototype.rotate = function (n) {
var copy = this.copy();
copy.unshift.apply(copy, copy.splice(n, copy.length));
return copy;
};
}
if (Array.prototype.first) {
throw "Array method 'first()' already defined elsewhere";
} else {
/**
* Returns a copy of this array, removing but the first 'n' elements from it
*/
Array.prototype.first = function (n) {
var copy = this.copy();
if (typeof n === 'undefined') {
copy.length = 1;
return copy;
} else if (n >= copy.length) {
return copy;
} else {
copy.length = n;
return copy;
}
};
}
if (Array.prototype.last) {
throw "Array method 'last()' already defined elsewhere";
} else {
/**
* Returns a copy of this array, removing but the last 'n' elements from it
*/
Array.prototype.last = function (n) {
var copy = this.copy();
if (typeof n === 'undefined') {
copy.remove(0, copy.length - 2);
return copy;
} else if (n >= copy.length) {
return copy;
} else {
copy.remove(0, copy.length - n - 1);
return copy;
}
};
}
if (Array.prototype.shuffle) {
throw "Array method 'shuffle()' already defined elsewhere";
} else {
/**
* Returns a copy of this array, sorting it's elements randomly
*/
Array.prototype.shuffle = function () {
var copy = this.copy();
return copy.sort(function () {
return Math.random() - 0.5;
});
};
}
if (Array.prototype.getAssociativeArrayLength) {
throw "Array method 'getAssociativeArrayLength()' already defined elsewhere";
} else {
/**
* Returns this associative array length
*/
Array.prototype.getAssociativeArrayLength = function () {
var length,
key;
for (key in this) {
if (this.hasOwnProperty(key)) {
length = length + 1;
}
}
return length;
};
}
if (Array.prototype.difference) {
throw "Array method 'difference()' already defined elsewhere";
} else {
/**
* Returns a copy of this array that contains the difference between source array with 'array'
*/
Array.prototype.difference = function (array) {
return this.filter(function (i) {
return (array.indexOf(i) < 0);
});
};
}
if (Array.prototype.intersection) {
throw "Array method 'intersection()' already defined elsewhere";
} else {
/**
* Returns a copy of this array that contains the intersection between source array with 'array'
*/
Array.prototype.intersection = function (array) {
return this.filter(function (i) {
return (array.indexOf(i) !== -1);
});
};
}
if (Array.prototype.union) {
throw "Array method 'union()' already defined elsewhere";
} else {
/**
* Returns a copy of this array that contains the union between source array with 'array', removing duplicates
*/
Array.prototype.union = function (array) {
var obj = {},
res = [],
i,
k;
for (i = this.length - 1; i >= 0; i = i - 1) {
obj[this[i]] = this[i];
}
for (i = array.length - 1; i >= 0; i = i - 1) {
obj[array[i]] = array[i];
}
for (k in obj) {
if (obj.hasOwnProperty(k)) {
res.push(obj[k]);
}
}
return res;
};
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment