108 lines
3.4 KiB
JavaScript
108 lines
3.4 KiB
JavaScript
// https://tc39.github.io/ecma262/#sec-array.prototype.includes
|
|
if(!Array.prototype.includes) {
|
|
Object.defineProperty(Array.prototype, 'includes', {
|
|
value: function(searchElement, fromIndex) {
|
|
|
|
// 1. Let O be ? ToObject(this value).
|
|
if (this == null) {
|
|
throw new TypeError('"this" is null or not defined');
|
|
}
|
|
|
|
let o = Object(this);
|
|
|
|
// 2. Let len be ? ToLength(? Get(O, "length")).
|
|
let len = o.length >>> 0;
|
|
|
|
// 3. If len is 0, return false.
|
|
if (len === 0) {
|
|
return false;
|
|
}
|
|
|
|
// 4. Let n be ? ToInteger(fromIndex).
|
|
// (If fromIndex is undefined, this step produces the value 0.)
|
|
let n = fromIndex | 0;
|
|
|
|
// 5. If n ≥ 0, then
|
|
// a. Let k be n.
|
|
// 6. Else n < 0,
|
|
// a. Let k be len + n.
|
|
// b. If k < 0, let k be 0.
|
|
let k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
|
|
|
|
// 7. Repeat, while k < len
|
|
while (k < len) {
|
|
// a. Let elementK be the result of ? Get(O, ! ToString(k)).
|
|
// b. If SameValueZero(searchElement, elementK) is true, return true.
|
|
// c. Increase k by 1.
|
|
// NOTE: === provides the correct "SameValueZero" comparison needed here.
|
|
if (o[k] === searchElement) {
|
|
return true;
|
|
}
|
|
k++;
|
|
}
|
|
|
|
// 8. Return false
|
|
return false;
|
|
}
|
|
});
|
|
}
|
|
|
|
//http://stackoverflow.com/questions/5306680/move-an-array-element-from-one-array-position-to-another
|
|
if(!Array.prototype.move) {
|
|
Array.prototype.move = function (old_index, new_index) {
|
|
if (new_index >= this.length) {
|
|
let k = new_index - this.length;
|
|
while ((k--) + 1) {
|
|
this.push(undefined);
|
|
}
|
|
}
|
|
this.splice(new_index, 0, this.splice(old_index, 1)[0]);
|
|
return this; // for testing purposes
|
|
};
|
|
}
|
|
|
|
//My own implementation to work around Javascript's shitty naming and support for collection operations.
|
|
if(!Array.prototype.remove) {
|
|
Array.prototype.remove = function(item) {
|
|
let index = this.indexOf(item);
|
|
|
|
if(index != -1) {
|
|
this.splice(index, 1);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
|
|
//My own implementation to work around Javascript's shitty naming and support for collection operations.
|
|
// index is optional
|
|
if(!Array.prototype.add) {
|
|
Array.prototype.add = function(item, index) {
|
|
if(index == undefined || isNaN(index) || index >= this.length) return this.push(item);
|
|
else return this.splice(index, 0, item);
|
|
}
|
|
}
|
|
|
|
//My own implementation to work around Javascript's shitty naming and support for collection operations.
|
|
// Sorts a contiguous section of the array.
|
|
// Index is the index of the first element to be sorted (inclusive).
|
|
// Length is the number of elements of the array to be sorted (must be >= 2). If the length + index is greater than the array length then it will be adjusted to the end of the array.
|
|
// All other invalid inputs will result in no sorting action taken and no error.
|
|
if(!Array.prototype.partialSort) {
|
|
Array.prototype.partialSort = function(index, length, compareFunction) {
|
|
if(index >= 0 && length >= 2 && index <= (this.length - 2)) {
|
|
//Adjust the length so it doesn't over-run the array. This is the only error correction we will perform.
|
|
if(index + length > this.length) length = this.length - index;
|
|
|
|
//Shallow copy of the data in the segment to be sorted.
|
|
let sorted = this.slice(index, length + index);
|
|
|
|
sorted.sort(compareFunction);
|
|
|
|
//Put the sorted array elements back into the array.
|
|
for(let i = index, j = 0; i <= length; i++, j++) {
|
|
this[i] = sorted[j];
|
|
}
|
|
}
|
|
}
|
|
} |