UNPKG

can-queues

Version:

A light weight JavaScript task queue

82 lines (66 loc) 1.74 kB
// Taken from jQuery var hasDuplicate, sortInput, sortStable = true, indexOf = Array.prototype.indexOf; function sortOrder( a, b ) { // Flag for duplicate removal if ( a === b ) { hasDuplicate = true; return 0; } // Sort on method existence if only one input has compareDocumentPosition var compare = !a.compareDocumentPosition - !b.compareDocumentPosition; if ( compare ) { return compare; } // Calculate position if both inputs belong to the same document compare = ( a.ownerDocument || a ) === ( b.ownerDocument || b ) ? a.compareDocumentPosition( b ) : // Otherwise we know they are disconnected 1; // Disconnected nodes if ( compare & 1 ) { // Choose the first element that is related to our preferred document if ( a === document || a.ownerDocument === document && document.documentElement.contains(a) ) { return -1; } if ( b === document || b.ownerDocument === document && document.documentElement.contains(b) ) { return 1; } // Maintain original order return sortInput ? ( indexOf.call( sortInput, a ) - indexOf.call( sortInput, b ) ) : 0; } return compare & 4 ? -1 : 1; } function uniqueSort( results ) { var elem, duplicates = [], j = 0, i = 0; hasDuplicate = false; sortInput = !sortStable && results.slice( 0 ); results.sort( sortOrder ); if ( hasDuplicate ) { while ( ( elem = results[ i++ ] ) ) { if ( elem === results[ i ] ) { j = duplicates.push( i ); } } while ( j-- ) { results.splice( duplicates[ j ], 1 ); } } // Clear input after sorting to release objects // See https://github.com/jquery/sizzle/pull/225 sortInput = null; return results; } module.exports = { uniqueSort: uniqueSort, sortOrder: sortOrder };