Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implements sortUnique

  • Loading branch information...
commit f09221d58da4620e43cf99ea59d58e9a1e500daa 1 parent cfb41d5
@deliminator deliminator authored
View
50 src/lib/util/lang.js
@@ -7,7 +7,55 @@
GENTICS = window.GENTICS || {};
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
(function(window, undefined) {
View
1  src/test/index.html
@@ -27,6 +27,7 @@ <h1 id="qunit-header">Aloha Editor Tests</h1>
<li><a href="unit/selection1.html">Selection 1</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/lang-tests.html">Lang Utils</a></li>
</ul>
<h2>Commands</h2>
<ul>
View
41 src/test/unit/lang-tests.html
@@ -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>
View
50 src/test/unit/lang-tests.js
@@ -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 ] );
+ });
+ });
+});
Please sign in to comment.
Something went wrong with that request. Please try again.