Skip to content

Instantly share code, notes, and snippets.

@nhalstead
Last active June 8, 2019 23:18
Show Gist options
  • Save nhalstead/281cda2e5bb88a8cb9f8ec9ef2093ef3 to your computer and use it in GitHub Desktop.
Save nhalstead/281cda2e5bb88a8cb9f8ec9ef2093ef3 to your computer and use it in GitHub Desktop.
This expansion pack adds some extra Functions that I tend to use in my programming, Works in Node and in Browser.
/**
* Adds some Capacity to run a foreach on an object
*
* @param {Function} Callback that will execute on every element
* @param {Mixed} Data to send to Every Call of the data
*/
Object.prototype.forEach = function(callback, extraData) {
Object.keys(this).forEach(function(key) {
var value = this[key];
callback(key, value, extraData);
});
}
/**
* Select a random Property from the Object.
*
* @link https://stackoverflow.com/a/52175028/5779200
* @return {Mixed} Selected Element
*/
Object.prototype.random = function() {
var objValues = Object.values(this);
return objValues[ Math.floor(Math.random() * objValues.length) ];
}
/**
* This will add the Capibility to remove duplicates
*
* @link https://stackoverflow.com/a/1584377/5779200
* @return {Array} The list with NO duplicates
*/
Array.prototype.unique = function() {
var a = this.concat();
for (var i = 0; i < a.length; ++i) {
for (var j = i + 1; j < a.length; ++j) {
if (a[i] === a[j])
a.splice(j--, 1);
}
}
return a;
};
/**
* Adds some merge Capibility to objects like arrayMerge in php but for Objects
* This is the Same thing as Object.assign BUT it does not overrite the
* object you are calling it on
*
* @param {Object} Another Object to Add to the Current Object
* @return {Object} The self + the Object to merge.
*/
Object.prototype.merge = function (objectToMerge) {
return Object.assign({}, this, objectToMerge);
};
/**
* Convert Object to an Array
* Returns just the array of the Values.
*
* @return {Array} Value of the Object
*/
Object.prototype.toArray = function() {
return Array.from(Object.keys(this), k=>this[k]);
};
/**
* Adds the ability to remove one element from an array.
* @param String Data to send to Every Call of the data
*
* @return {Object} Self
*/
Object.prototype.removeSelected = function (ObjectName) {
delete this[ObjectName];
return this;
}
/**
*
* @link https://stackoverflow.com/a/44376705/5779200
* @return {Array} Returns a new version of the Array.
*/
Array.prototype.unique1 = function() {
var a = [];
for (i = 0; i < this.length; i++) {
var current = this[i];
if (a.indexOf(current) < 0) a.push(current);
}
return a;
}
/**
* Faster but not supported by IE. (217.71 times faster using Set!)
*
* @return {Array} Returns a new version of the Array.
*/
Array.prototype.unique2 = function() {
return Array.from(new Set(this));
}
/**
* Compatibility Test built in to see if it can use
* a faster method.
*
* @return {Array} Returns a new version of the Array.
*/
Array.prototype.no_dups = function(){
if( typeof Set !== undefined ){
return this.unique2();
}
else {
return this.unique1();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment