Permalink
Browse files

First commit.

  • Loading branch information...
0 parents commit 4d710cd9a2f4d3a74d55f5f2fffe47a37637761c @joseprio committed Apr 22, 2012
Showing with 2,378 additions and 0 deletions.
  1. +1 −0 Arrays-min.js
  2. +211 −0 Arrays.js
  3. +35 −0 README.md
  4. +120 −0 test/ArraysTests.js
  5. +235 −0 test/qunit/qunit.css
  6. +1,757 −0 test/qunit/qunit.js
  7. +19 −0 test/test.html

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
211 Arrays.js
@@ -0,0 +1,211 @@
+/*!
+ * Arrays Libary v1.0
+ * http://joseprio.com/
+ *
+ * Copyright 2012, Josep del Rio
+ * Licensed under the MIT license.
+ *
+ *
+ * Date: Mon Apr 15 12:23:00 2012 +0100
+ *
+ * New functions:
+ *
+ * Arrays.create(m, n, ...)
+ * Creates a new multidimensional array of dimensions m*n*...
+ *
+ * Arrays.isArray(a)
+ * Returns true if a is an array, false otherwise
+ *
+ * Java equivalent function support:
+ *
+ * static int binarySearch(Object[] a, int fromIndex, int toIndex, Object key)
+ * static int binarySearch(Object[] a, Object key)
+ * static boolean equals(Object[] a, Object[] a2)
+ * static void fill(Object[] a, int fromIndex, int toIndex, Object val)
+ * static void fill(Object[] a, Object val)
+ * static void sort(Object[] a)
+ * static void sort(Object[] a, Comparator c)
+ * static void sort(Object[] a, int fromIndex, int toIndex)
+ * static void sort(Object[] a, int fromIndex, int toIndex, Comparator c)
+ * static String toString(Object[] a)
+ * static boolean deepEquals(Object[] a1, Object[] a2)
+ * static String deepToString(Object[] a)
+ * static Object[] copyOf(Object[] original, int newLength)
+ * static Object[] copyOfRange(Object[] original, int from, int to)
+ *
+ * Unimplemented:
+ * static int binarySearch(Object[] a, Object key, Comparator c)
+ * static int hashCode(Object[] a)
+ * static int deepHashCode(Object[] a)
+ * static List asList(Object[] a)
+ *
+ *
+ */
+var Arrays = {
+ sort: function() {
+ var target = arguments[0];
+ var sorter = null;
+
+ if (arguments.length == 2) {
+ sorter = arguments[1];
+ } else if (arguments.length == 4) {
+ sorter = arguments[3];
+ } else {
+ sorter = function(a, b) { if (a < b) return -1; if (a > b) return 1; return 0 };
+ }
+
+ if (arguments.length >= 3) {
+ // We have fromIndex and toIndex
+ var subtarget = Arrays.copyOfRange(target, arguments[1], arguments[2]);
+ subtarget.sort(sorter);
+
+ for (var c=arguments[1];c<=arguments[2];c++) {
+ target[c] = subtarget[c-arguments[1]];
+ }
+ } else {
+ target.sort(sorter);
+ }
+ },
+
+ create: function() {
+ // Create a multidimensional array
+ var total = arguments[0];
+ var result = new Array(total);
+ if (arguments.length > 1) {
+ var newArgs = [];
+ for (var c=1;c<arguments.length;c++) {
+ newArgs[c-1] = arguments[c];
+ }
+ for (var c=0;c<total;c++) {
+ result[c] = Arrays.create.apply(null, newArgs);
+ }
+ }
+ return result;
+ },
+
+ fill: function(target, value) {
+ // We fill the array with certain value
+ // If it's a multidimensional array, we fill them too
+
+ var fromIndex = 0;
+ var toIndex = target.length-1;
+
+ // Support fromIndex/toIndex
+ if (arguments.length > 2) {
+ fromIndex = arguments[1];
+ toIndex = arguments[2];
+ value = arguments[3];
+ }
+
+ for (var c=fromIndex;c<=toIndex;c++) {
+ if (Arrays.isArray(target[c])) {
+ Arrays.fill(target[c], value);
+ } else {
+ target[c] = value;
+ }
+ }
+ },
+
+ toString: function(target) {
+ return "[" + target.join(", ") + "]";
+ },
+
+ // TODO: handle arrays that contain themselves; when detected, use "[...]"
+ deepToString: function(target) {
+ var output = "[";
+ for (var c=0;c<target.length;c++) {
+ if (c > 0) {
+ output += ", ";
+ }
+ if (Arrays.isArray(target[c])) {
+ output += Arrays.deepToString(target[c]);
+ } else {
+ output += target[c];
+ }
+ }
+
+ output += "]";
+
+ return output;
+ },
+
+ isArray: Array.isArray || function(a) {
+ // Use Array.isArray if present; if not, we need to check the string value
+ return (Object.prototype.toString.call(a) === '[object Array]');
+ },
+
+ binarySearch: function(a, fromIndex, toIndex, key) {
+ // Binary search can be called in two ways: with from/to indexes or without
+ if (arguments.length < 4) {
+ key = fromIndex;
+ fromIndex = 0;
+ toIndex = a.length-1;
+ }
+ if (toIndex < fromIndex) {
+ // Not found
+ }
+
+ var pos = Math.floor((toIndex + fromIndex) / 2);
+ if (a[pos] === key) {
+ return pos;
+ } else if (fromIndex >= toIndex) {
+ // Failed to find it
+ return -Math.abs(toIndex)-1;
+ } else if (a[pos] > key) {
+ return Arrays.binarySearch(a, fromIndex, pos-1, key);
+ } else {
+ return Arrays.binarySearch(a, pos+1, toIndex, key);
+ }
+ },
+
+ copyOf: function(original, newLength) {
+ var newArray;
+
+ // copyOf can be called with newLength or without, so check for it
+ if (typeof newLength == "undefined") {
+ newLength = original.length;
+ }
+
+ if (newLength < original.length) {
+ newArray = original.slice(0, newLength);
+ } else {
+ newArray = original.slice(0);
+ newArray.length = newLength;
+ }
+
+ return newArray;
+ },
+
+ copyOfRange: function(original, from, to) {
+ return original.slice(from, to+1);
+ },
+
+ equals: function(a1, a2) {
+ if (a1.length != a2.length) return false;
+
+ var isEquals = true;
+
+ for (var c=0;(c<a2.length)&&isEquals;c++) {
+ isEquals = (a1[c] === a2[c]);
+ }
+
+ return isEquals;
+ },
+
+ // TODO: make sure we don't enter an infinite array contains itself
+ deepEquals: function(a1, a2) {
+ if (a1.length != a2.length) return false;
+
+ var isEquals = true;
+
+ for (var c=0;(c<a1.length)&&isEquals;c++) {
+ if (Arrays.isArray(a1[c]) && Arrays.isArray(a2[c])) {
+ isEquals = Arrays.deepEquals(a1[c], a2[c]);
+ } else {
+ isEquals = (a1[c] === a2[c]);
+ }
+ }
+
+ return isEquals;
+ }
+};
@@ -0,0 +1,35 @@
+Arrays.js
+=========
+
+This library implements an API similar to the Java Arrays helper class; it's
+purpose is to ease the transition from Java to JavaScript, and at the same time
+offer better array handling.
+
+New functions:
+`Arrays.create(m, n, ...)`
+Creates a new multidimensional array of dimensions m**n**...
+
+`Arrays.isArray(a)`
+Returns true if a is an array, false otherwise
+
+Functions equivalent to Java:
+`static int binarySearch(Object[] a, int fromIndex, int toIndex, Object key)`
+`static int binarySearch(Object[] a, Object key)`
+`static boolean equals(Object[] a, Object[] a2)`
+`static void fill(Object[] a, int fromIndex, int toIndex, Object val)`
+`static void fill(Object[] a, Object val)`
+`static void sort(Object[] a)`
+`static void sort(Object[] a, Comparator c)`
+`static void sort(Object[] a, int fromIndex, int toIndex)`
+`static void sort(Object[] a, int fromIndex, int toIndex, Comparator c)`
+`static String toString(Object[] a)`
+`static boolean deepEquals(Object[] a1, Object[] a2)`
+`static String deepToString(Object[] a)`
+`static Object[] copyOf(Object[] original, int newLength)`
+`static Object[] copyOfRange(Object[] original, int from, int to)`
+
+Unimplemented functions:
+`static int binarySearch(Object[] a, Object key, Comparator c)`
+`static int hashCode(Object[] a)`
+`static int deepHashCode(Object[] a)`
+`static List asList(Object[] a)`
@@ -0,0 +1,120 @@
+test('create()', function() {
+ var data = [ 2, 3, 4, 2];
+ var target = Arrays.create(data[0], data[1], data[2], data[3]);
+
+ for (var c=0; c<data.length; c++) {
+ ok(target.length == data[c], 'Dimension #' + (c+1) + ' matches size');
+ target = target[0];
+ }
+});
+
+test('isArray()', function() {
+ ok(!Arrays.isArray("string"), 'String is not array');
+ ok(!Arrays.isArray({}), 'Object is not array');
+ ok(!Arrays.isArray(-4), 'Number is not array');
+ ok(!Arrays.isArray(true), 'Boolean is not array');
+ ok(Arrays.isArray([]), 'Empty array is array');
+ ok(Arrays.isArray([1,2,3]), 'Non-empty array is array');
+});
+
+test('toString()', function() {
+ var emptyArray = [];
+ var numberArray = [1,2,3,4];
+ var stringArray = ["a","b","c","d"];
+
+ equal(Arrays.toString(emptyArray), "[]", "Empty array toString is correct");
+ equal(Arrays.toString(numberArray), "[1, 2, 3, 4]", "Number array toString is correct");
+ equal(Arrays.toString(stringArray), "[a, b, c, d]", "String array toString is correct");
+});
+
+test('deepEquals()', function() {
+ var a1 = [[1,2,3,4],[5,6,7,8],[9,10,11,12]];
+ var a2 = [[1,2,3,4],[5,6,7,8],[9,10,11,12]];
+
+ ok(Arrays.deepEquals(a1,a2),"The arrays are deeply equal");
+
+ a2[1][1] = 0;
+
+ ok(!Arrays.deepEquals(a1,a2),"The arrays are not deeply equal");
+});
+
+test('deepToString()', function() {
+ var testArray = [[1,2,3,4],[5,6,7,8],[9,10,11,12]];
+ var expectedResult = "[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]";
+
+ equal(Arrays.deepToString(testArray), expectedResult,"deepToString generated expected result");
+});
+
+test('copyOf()', function() {
+ var testArray = [1,2,3,4,5,6];
+ var copyArray = [1,2,3,4,5,6];
+ var shortArray = [1,2,3,4];
+
+ ok(Arrays.deepEquals(copyArray, Arrays.copyOf(testArray, testArray.length)), "copyOf generated correct output");
+ ok(Arrays.deepEquals(shortArray, Arrays.copyOf(testArray, testArray.length-2)), "copyOf generated correct output");
+});
+
+test('copyOfRange()', function() {
+ var testArray = [1,2,3,4,5,6];
+ var copyArray = [2,3,4,5];
+
+ ok(Arrays.deepEquals(copyArray, Arrays.copyOfRange(testArray, 1, testArray.length-2)), "copyOfRange generated correct output");
+});
+
+test('fill()', function() {
+ var testArray = new Array(5);
+ Arrays.fill(testArray, 7);
+
+ for (var c=0;c<testArray.length;c++) {
+ equal(testArray[c], 7, 'Element ' + c + ' in array is filled correctly');
+ }
+
+ Arrays.fill(testArray, 1, testArray.length - 2, 5);
+
+ for (var c=1;c<testArray.length-1;c++) {
+ equal(testArray[c], 5, 'Element ' + c + ' in array is partial-filled correctly');
+ }
+});
+
+test('sort()', function() {
+ var unordered = [ 9, 1, 4, 2, 6, 5, 4 ];
+ var ordered = [ 1, 2, 4, 4, 5, 6, 9 ];
+
+ var unorderedCopy = Arrays.copyOf(unordered);
+ Arrays.sort(unorderedCopy);
+
+ ok(Arrays.deepEquals(unorderedCopy, ordered), 'Sorted array is ordered' );
+
+ var inverse = [ 9, 6, 5, 4, 4, 2, 1 ];
+ unorderedCopy = Arrays.copyOf(unordered);
+
+ Arrays.sort(unorderedCopy, function(a, b) { if (a > b) return -1; if (a < b) return 1; return 0 });
+
+ ok(Arrays.deepEquals(unorderedCopy, inverse), 'Sorted array with comparator function is correctly ordered' );
+
+ var partial = [ 9, 1, 2, 4, 5, 6, 4 ];
+ unorderedCopy = Arrays.copyOf(unordered);
+
+ Arrays.sort(unorderedCopy, 1, partial.length-2);
+
+ ok(Arrays.deepEquals(unorderedCopy, partial), 'Partial sorting is ordered');
+
+ var partialInverse = [ 9, 6, 5, 4, 2, 1, 4 ];
+ unorderedCopy = Arrays.copyOf(unordered);
+
+ Arrays.sort(unorderedCopy, 1, partial.length-2, function(a, b) { if (a > b) return -1; if (a < b) return 1; return 0 });
+
+ ok(Arrays.deepEquals(unorderedCopy, partialInverse), 'Partial sorting with comparator function is ordered');
+});
+
+test('binarySearch()', function() {
+ var testArray = [ 1, 2, 4, 5, 6, 9 ];
+
+ equal(Arrays.binarySearch(testArray, 4), 2, 'Found element in correct position');
+ equal(Arrays.binarySearch(testArray, 9), 5, 'Found element in correct position');
+ equal(Arrays.binarySearch(testArray, 1), 0, 'Found element in correct position');
+ ok(Arrays.binarySearch(testArray, 11) < 0, 'Not found non existing element');
+ ok(Arrays.binarySearch(testArray, 3) < 0, 'Not found non existing element');
+ ok(Arrays.binarySearch(testArray, 0) < 0, 'Not found non existing element');
+});
+
Oops, something went wrong.

0 comments on commit 4d710cd

Please sign in to comment.