Skip to content

Commit

Permalink
Implements sortUnique
Browse files Browse the repository at this point in the history
  • Loading branch information
deliminator committed Mar 7, 2012
1 parent cfb41d5 commit f09221d
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 1 deletion.
50 changes: 49 additions & 1 deletion src/lib/util/lang.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,7 +7,55 @@
GENTICS = window.GENTICS || {}; GENTICS = window.GENTICS || {};
GENTICS.Utils = GENTICS.Utils || {}; GENTICS.Utils = GENTICS.Utils || {};


define( 'util/lang', [], function(){} ); define( "util/lang", [], function(){

return {
/**
* Implements unique() using the browser's sort().
*
* @param array
* The array to sort and strip of duplicate values.
* @param compFunc
* A custom comparison function that accepts two values
* a and b from the given array and returns -1, 0, 1
* depending on whether a < b, a == b, a > b respectively.
* If no compFunc is provided, the algorithm will the
* browsers default sort behaviour and a loosely
* comparison to detect duplicates.
* @return
* A sorted array containing all values from the given array
* excluding duplicates.
*/
"sortUnique": function( array, compFunc ){
if ( 0 === array.length ) {
return [];
}

var sorted = array.slice();
if ( compFunc ) {
sorted.sort( compFunc );
} else {
sorted.sort();
}

var result = new Array( sorted.length );
var lastValue = sorted[ 0 ]; // array.length >= 1 checked above
result[ 0 ] = lastValue;

var j = 1;
var len = sorted.length;
for ( var i = 1; i < len; i++ ) {
var value = sorted[ i ];
// Use loosely typed comparsion if no compFunc is given
// to avoid sortUnique( [6, "6", 6] ) => [6, "6", 6]
if ( compFunc ? 0 !== compFunc( lastValue, value ) : lastValue != value ) {
lastValue = result[ j++ ] = value;
}
}
return result.slice(0, j);
}
};
} );


// Start Closure // Start Closure
(function(window, undefined) { (function(window, undefined) {
Expand Down
1 change: 1 addition & 0 deletions src/test/index.html
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ <h2>Core</h2>
<li><a href="unit/selection1.html">Selection 1</a></li> <li><a href="unit/selection1.html">Selection 1</a></li>
<li><a href="unit/selection2.html">Selection 2</a></li> <li><a href="unit/selection2.html">Selection 2</a></li>
<li><a href="unit/selection3.html">Selection 3</a></li> <li><a href="unit/selection3.html">Selection 3</a></li>
<li><a href="unit/lang-tests.html">Lang Utils</a></li>
</ul> </ul>
<h2>Commands</h2> <h2>Commands</h2>
<ul> <ul>
Expand Down
41 changes: 41 additions & 0 deletions src/test/unit/lang-tests.html
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,41 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Aloha Editor Lang Utils Test Suite</title>

<!-- include qunit -->
<link rel="stylesheet" href="../vendor/qunit.css" type="text/css"/>
<script type="text/javascript" src="../vendor/qunit.js"></script>
<script src="../vendor/jquery-1.6.1.js" ></script>

<script>
var Aloha = { jQuery: $ };
</script>
<script src="../../lib/aloha.js"></script>

<!-- include testswarm inject script -->
<script>
var url = window.location.search;
url = decodeURIComponent( url.slice( url.indexOf("swarmURL=") + 9 ) );
if ( url && url.indexOf("http") === 0 ) {
require(["http://testswarm.aloha-editor.org/js/inject.js?" + (new Date).getTime()], function() {});
}
</script>

</head>
<body>
<!-- include the tests -->
<script>
require( ['lang-tests'] );
</script>
<h1 id="qunit-header">Aloha Editor Lang Utils Test Suite</h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
<div id="qunit-fixture">
<div id="test-editable"></div>
</div>
</body>
</html>
50 changes: 50 additions & 0 deletions src/test/unit/lang-tests.js
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,50 @@
/*!
* This file is part of Aloha Editor
* Author & Copyright (c) 2010 Gentics Software GmbH, aloha@gentics.com
* Licensed unter the terms of http://www.aloha-editor.com/license.html
*/

define(
[],
function( ) {
"use strict";

Aloha.ready( function() {
var Lang = Aloha.require( "util/lang" );
var $ = Aloha.require( "aloha/jquery" );

module('sortUnique');

test('loose compare', function() {
var unique = Lang.sortUnique( [ 6, 3, 6, 3, "6", 3, "9", 9, 3, 2, 1 ] );
// Either numeric or string values for "6" and "9" may be chosen
for ( var i = 0; i < unique.length; i++ ) {
unique[ i ] = parseInt( unique[ i ] );
}
deepEqual( unique, [ 1, 2, 3, 6, 9 ] );
});

test('strict comparison', function() {
var unique = Lang.sortUnique( [ 6, 3, "6", 3, 6, 3, "9", 9, 3, 2, 1 ], function(a,b){
return typeof a < typeof b ? -1
: ( typeof a > typeof b ? 1
: ( a < b ? -1 : ( a > b ? 1 : 0 )));
});
deepEqual( unique, [ 1, 2, 3, 6, 9, "6", "9" ] );
});

test('comparator', function() {
var unique = Lang.sortUnique( [ 7, 6, 9, 6, 9, 8 ], function( a, b ) {
// Pretend 6 and 9 is equal
if ( a === 9 ) {
a = 6;
}
if ( b === 9 ) {
b = 6;
}
return a < b ? -1 : ( a === b ? 0 : +1 );
});
deepEqual( unique, [ 6, 7, 8 ] );
});
});
});

0 comments on commit f09221d

Please sign in to comment.