Skip to content
Browse files

Merge pull request #4 from x3ro/x3ro/module-structure-mod

Restructuring methods into AssociativeArray.prototype
  • Loading branch information...
2 parents 2ac4903 + a4f3b7d commit 45535ab3f0cd658d270741e4be48e73757e62407 @txus committed Jun 26, 2012
Showing with 78 additions and 60 deletions.
  1. +65 −60 lib/assoc.js
  2. +13 −0 test/assoc_test.js
View
125 lib/assoc.js
@@ -1,75 +1,80 @@
-module.exports = function (object) {
-
- var data = [],
-
- unshift_or_push = function (obj, ary, action) {
- if (action === 'unshift') {
- ary.unshift(obj);
- } else {
- ary.push(obj);
- }
- },
-
- merge_object_in_array = function (obj, ary, action) {
- var i, temp = [], pair;
-
- if (obj instanceof Array) {
- for (i = 0; i < obj.length; i += 1) {
- if (obj[i] instanceof Array) {
- unshift_or_push(obj[i], ary, action);
- } else {
- temp.push(obj[i]);
- }
- }
- if (temp.length > 0) {
- unshift_or_push(temp, data, action);
- }
- } else {
- for (name in obj) {
- if (typeof obj[name] !== 'function') {
- pair = [name, obj[name]];
- unshift_or_push(pair, ary, action);
- }
- }
- }
- };
+function AssociativeArray(object) {
+ this.data = [];
+ this._merge_object_in_array(object, this.data, 'push');
+};
- merge_object_in_array(object, data, 'push');
+AssociativeArray.prototype = {
- this.assoc = function (key) {
+ assoc: function (key) {
var i;
- for (i = 0; i < data.length; i += 1) {
- if (key === data[i][0]) {
- return data[i][1];
+ for (i = 0; i < this.data.length; i += 1) {
+ if (key === this.data[i][0]) {
+ return this.data[i][1];
}
}
- };
+ },
- this.rassoc = function (value) {
+ rassoc: function (value) {
var i;
- for (i = 0; i < data.length; i += 1) {
- if (value === data[i][1]) {
- return data[i][0];
+ for (i = 0; i < this.data.length; i += 1) {
+ if (value === this.data[i][1]) {
+ return this.data[i][0];
}
}
- };
+ },
- this.push = function (obj) {
- merge_object_in_array(obj, data, 'push');
- };
+ pop: function () {
+ return this.data.pop();
+ },
- this.pop = function () {
- data.pop();
- };
+ shift: function () {
+ return this.data.shift();
+ },
- this.unshift = function (obj) {
- merge_object_in_array(obj, data, 'unshift');
- };
+ push: function (obj) {
+ this._merge_object_in_array(obj, this.data, 'push');
+ },
- this.shift = function () {
- data.shift();
- };
+ unshift: function (obj) {
+ this._merge_object_in_array(obj, this.data, 'unshift');
+ },
- this.data = data;
-};
+ // Private methods
+
+
+ _unshift_or_push: function (obj, ary, action) {
+ if (action === 'unshift') {
+ ary.unshift(obj);
+ } else {
+ ary.push(obj);
+ }
+ },
+
+ _merge_object_in_array: function (obj, ary, action) {
+ var i, temp = [], pair;
+
+ if (obj instanceof Array) {
+ for (i = 0; i < obj.length; i += 1) {
+ if (obj[i] instanceof Array) {
+ this._unshift_or_push(obj[i], ary, action);
+ } else {
+ temp.push(obj[i]);
+ }
+ }
+ if (temp.length > 0) {
+ this._unshift_or_push(temp, ary, action);
+ }
+ } else {
+ for (name in obj) {
+ if (typeof obj[name] !== 'function') {
+ pair = [name, obj[name]];
+ this._unshift_or_push(pair, ary, action);
+ }
+ }
+ }
+ }
+
+}
+
+module.exports = AssociativeArray;
View
13 test/assoc_test.js
@@ -84,6 +84,19 @@ testosterone
assert.equal(array.rassoc('b'), 'a');
})
+ .add('#pop should return the last element of the array', function () {
+ var array = new AssociativeArray([['a', 'b'], ['c', 'd']]);
+
+ assert.deepEqual(array.pop(), ['c', 'd']);
+ })
+
+ .add('#shift should return the first element of the array', function () {
+ var array = new AssociativeArray([['a', 'b'], ['c', 'd']]);
+
+ assert.deepEqual(array.shift(), ['a', 'b']);
+ })
+
+
.run(function () {
require('util').print('done!');
});

0 comments on commit 45535ab

Please sign in to comment.
Something went wrong with that request. Please try again.