Esanders has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/195971

Change subject: Provide oo.unique for remove duplicates from arrays
......................................................................

Provide oo.unique for remove duplicates from arrays

Change-Id: I471e6ada2a548af65a74486a9c17cfb4471001fd
---
M src/core.js
M tests/unit/core.test.js
2 files changed, 43 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/oojs/core refs/changes/71/195971/1

diff --git a/src/core.js b/src/core.js
index 68bda16..f96e83b 100644
--- a/src/core.js
+++ b/src/core.js
@@ -422,6 +422,21 @@
 };
 
 /**
+ * Return the unique values of an array, removing duplicates
+ *
+ * @param {Array} a Array
+ * @return {Array} Unique vales in array
+ */
+oo.unique = function ( arr ) {
+       return arr.reduce( function ( previous, current ) {
+               if ( previous.indexOf( current ) === -1 ) {
+                       previous.push( current );
+               }
+               return previous;
+       }, [] );
+};
+
+/**
  * Compute the union (duplicate-free merge) of a set of arrays.
  *
  * Arrays values must be convertable to object keys (strings).
diff --git a/tests/unit/core.test.js b/tests/unit/core.test.js
index 33eefa4..80cb7ee 100644
--- a/tests/unit/core.test.js
+++ b/tests/unit/core.test.js
@@ -1056,6 +1056,34 @@
                } );
        }
 
+       QUnit.test( 'unique', 4, function ( assert ) {
+
+               assert.deepEqual(
+                       oo.unique( [] ),
+                       [],
+                       'Empty'
+               );
+
+               assert.deepEqual(
+                       oo.unique( [ 'a', 'b', 'a' ] ),
+                       [ 'a', 'b' ],
+                       'Simple string duplication'
+               );
+
+               assert.deepEqual(
+                       oo.unique( [ 3, 3, 2, 4, 3, 1, 2, 1, 1, 2 ] ),
+                       [ 3, 2, 4, 1 ],
+                       'Simple number duplication'
+               );
+
+               assert.deepEqual(
+                       oo.unique( [ 1, 2, 3 ] ),
+                       [ 1, 2, 3 ],
+                       'No duplication'
+               );
+
+       } );
+
        QUnit.test( 'simpleArrayUnion', 5, function ( assert ) {
 
                assert.deepEqual(

-- 
To view, visit https://gerrit.wikimedia.org/r/195971
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I471e6ada2a548af65a74486a9c17cfb4471001fd
Gerrit-PatchSet: 1
Gerrit-Project: oojs/core
Gerrit-Branch: master
Gerrit-Owner: Esanders <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to