2017-01-17 22:31:43 -08:00
// https://tc39.github.io/ecma262/#sec-array.prototype.includes
2017-05-09 13:51:26 -07:00
if ( ! Array . prototype . includes ) {
2017-01-17 22:31:43 -08:00
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' ) ;
}
2017-05-09 13:51:26 -07:00
let o = Object ( this ) ;
2017-01-17 22:31:43 -08:00
// 2. Let len be ? ToLength(? Get(O, "length")).
2017-05-09 13:51:26 -07:00
let len = o . length >>> 0 ;
2017-01-17 22:31:43 -08:00
// 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.)
2017-05-09 13:51:26 -07:00
let n = fromIndex | 0 ;
2017-01-17 22:31:43 -08:00
// 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.
2017-05-09 13:51:26 -07:00
let k = Math . max ( n >= 0 ? n : len - Math . abs ( n ) , 0 ) ;
2017-01-17 22:31:43 -08:00
// 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 ;
}
} ) ;
2017-05-09 13:51:26 -07:00
}
//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.
2017-10-08 08:56:15 -07:00
for ( let i = index , j = 0 ; j < length ; i ++ , j ++ ) {
2017-05-09 13:51:26 -07:00
this [ i ] = sorted [ j ] ;
}
}
}
2017-01-17 22:31:43 -08:00
}