Skip to content

Instantly share code, notes, and snippets.

@sebastianvirlan
Last active December 26, 2018 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebastianvirlan/049f321a5797088f4b8301f370dad3e6 to your computer and use it in GitHub Desktop.
Save sebastianvirlan/049f321a5797088f4b8301f370dad3e6 to your computer and use it in GitHub Desktop.
Ruby Array Methods in Javascript
/*** Custom RArray class ***/
class RArray extends Array {
// Returns an array containing the items for which the given closure is not true.
reject (callback) {
return this.filter((val) => {
return !callback(val);
});
}
// Returns an array containing all elements of array for which the given closure returns a true value.
select (callback) {
return this.filter(callback);
}
//Invokes the given closure once for each element of self.
collect (callback) {
return this.map(callback);
}
// Same as forEach, but passes the index of the element instead of the element itself.
eachIndex (callback) {
return this.forEach((value, index) => {
callback(index);
});
}
// Returns true if self contains no elements.
empty () {
return this.length === 0;
}
// Returns the first element, or the first n elements, of the array. If the array is empty, the first form returns null, and the second form returns an empty array.
first (n) {
if (this.length === 0 && !n) return null;
return this.slice(0, n || 1);
}
// Returns the last element, or the last n elements, of the array. If the array is empty, the first form returns null, and the second form returns an empty array.
last (n) {
if (this.length === 0 && !n) return null;
return this.slice(-n || -1);
}
// Returns an array by removing duplicate values in self.
uniq () {
return this.filter((value, index, self) => self.indexOf(value) === index);
}
}
/*** Through Prototype Polution ***/
// Returns an array containing the items for which the given closure is not true.
Array.prototype.reject = function (callback) {
return this.filter((val) => {
return !callback(val);
})
}
// Returns an array containing all elements of array for which the given closure returns a true value.
Array.prototype.select = function (callback) {
return this.filter(callback);
}
//Invokes the given closure once for each element of self.
Array.prototype.collect = function (callback) {
return this.map(callback);
}
// Same as forEach, but passes the index of the element instead of the element itself.
Array.prototype.eachIndex = function (callback) {
return this.forEach((value, index) {
callback(index);
});
}
// Returns true if self contains no elements.
Array.prototype.empty = function () {
return this.length === 0;
}
// Returns the first element, or the first n elements, of the array. If the array is empty, the first form returns null, and the second form returns an empty array.
Array.prototype.first = function (n) {
if (this.length === 0 && !n) return null;
return this.slice(0, n || 1);
}
// Returns the last element, or the last n elements, of the array. If the array is empty, the first form returns null, and the second form returns an empty array.
Array.prototype.last = function (n) {
if (this.length === 0 && !n) return null;
return this.slice(-n || -1);
}
// Returns an array by removing duplicate values in self.
Array.prototype.uniq = function () {
return this.filter((value, index, self) => self.indexOf(value) === index);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment