You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ag...@apache.org on 2012/11/05 18:54:05 UTC
[2/4] js commit: Add util methods for array.indexOf and array.remove.
Add util methods for array.indexOf and array.remove.
Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/commit/c3517e77
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/tree/c3517e77
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/diff/c3517e77
Branch: refs/heads/master
Commit: c3517e7703ab9a1335db8a3d0974b045ae3107e5
Parents: 3ae88d1
Author: Andrew Grieve <ag...@chromium.org>
Authored: Sun Nov 4 17:35:28 2012 -0800
Committer: Andrew Grieve <ag...@chromium.org>
Committed: Mon Nov 5 12:53:37 2012 -0500
----------------------------------------------------------------------
lib/common/utils.js | 24 ++++++++++++++++++++++++
test/test.utils.js | 33 +++++++++++++++++++++++++++++++++
2 files changed, 57 insertions(+), 0 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/c3517e77/lib/common/utils.js
----------------------------------------------------------------------
diff --git a/lib/common/utils.js b/lib/common/utils.js
index 60afb02..0861446 100644
--- a/lib/common/utils.js
+++ b/lib/common/utils.js
@@ -32,6 +32,30 @@ utils.defineGetter = function(obj, key, func) {
}
};
+utils.arrayIndexOf = function(a, item) {
+ if (a.indexOf) {
+ return a.indexOf(item);
+ }
+ var len = a.length;
+ for (var i = 0; i < len; ++i) {
+ if (a[i] == item) {
+ return i;
+ }
+ }
+ return -1;
+};
+
+/**
+ * Returns whether the item was found in the array.
+ */
+utils.arrayRemove = function(a, item) {
+ var index = utils.arrayIndexOf(a, item);
+ if (index != -1) {
+ a.splice(index, 1);
+ }
+ return index != -1;
+};
+
/**
* Returns an indication of whether the argument is an array or not
*/
http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/c3517e77/test/test.utils.js
----------------------------------------------------------------------
diff --git a/test/test.utils.js b/test/test.utils.js
index 093ae65..555501a 100644
--- a/test/test.utils.js
+++ b/test/test.utils.js
@@ -22,6 +22,39 @@
describe("utils", function () {
var utils = require('cordova/utils');
+ describe("utils.arrayIndexOf", function() {
+ it("should return -1 when not found", function() {
+ expect(utils.arrayIndexOf([1,2,3], 4)).toBe(-1);
+ });
+ it("should return 0 for first item", function() {
+ expect(utils.arrayIndexOf([1,2,3], 1)).toBe(0);
+ });
+ it("should return 2 for last item", function() {
+ expect(utils.arrayIndexOf([1,2,3], 3)).toBe(2);
+ });
+ it("should return index of first occurance", function() {
+ expect(utils.arrayIndexOf([1,2,1], 1)).toBe(0);
+ });
+ });
+
+ describe("utils.arrayRemove", function() {
+ it("should return true when removed.", function() {
+ var a = [1, 2, 3];
+ expect(utils.arrayRemove(a, 2)).toBe(true);
+ expect(a).toEqual([1, 3]);
+ });
+ it("should return false when item was not there.", function() {
+ var a = [1, 2, 3];
+ expect(utils.arrayRemove(a, 4)).toBe(false);
+ expect(a).toEqual([1, 2, 3]);
+ });
+ it("should remove only first occurance", function() {
+ var a = [1, 2, 1];
+ expect(utils.arrayRemove(a, 1)).toBe(true);
+ expect(a).toEqual([2, 1]);
+ });
+ });
+
describe("when cloning", function () {
it("can clone an array", function () {
var orig = [1, 2, 3, {four: 4}, "5"];