JSFiddle - React, Tailwind, and code Playground
by xavierm02
JavaScript
var Name = {
create: function ( ) {
return Math.random( ).toString( );
}
};
( function ( ) {
var sorted = Name.create( );
var compare = Name.create( );
var prototypeOfArray = Array.prototype;
var defaultCompare = function ( a, b ) {
if ( a < b ) {
return -1;
} else if ( a > b ) {
return 1;
} else {
return 0;
}
};
// sort just stores the function used to sort
var sort = prototypeOfArray.sort;
prototypeOfArray.sort = function ( localCompare ) {
this[ compare ] = localCompare;
return sort.apply( this, arguments );
};
// append puts the item in the array just like push
// except it puts it where it would be after sort is called
// so that the array is still sorted
var slice = prototypeOfArray.slice;
var floor = Math.floor;
prototypeOfArray.append = function ( item ) {
var i;
var min = 0;
var max = this.length;
var comparison;
var localCompare = typeof compare === 'function' ? this[ compare ] : defaultCompare;
while ( max - min > 1 ) {
i = floor( ( min + max ) / 2 );
comparison = localCompare( item, this[ i ] );
if ( comparison < 0 ) {
max = i;
} else {
min = i;
}
}
i = min + 1;
this.splice( i, 0, item );
return this.length;
};
// indexOf is a bit optimized since if the array is ordered, it needs only ~ log2(length) tests
var indexOf = prototypeOfArray.indexOf;
prototypeOfArray.indexOf = function ( item ) {
if ( ! this.hasOwnProperty( compare ) ) {
return indexOf.apply( this, arguments );
}
var i;
var min = 0;
var max = this.length;
var comparison;
var localCompare = this[ compare ];
if ( typeof localCompare !== 'function' ) {
localCompare...