Skip to content

Commit

Permalink
Merge pull request #4 from x3ro/x3ro/module-structure-mod
Browse files Browse the repository at this point in the history
Restructuring methods into AssociativeArray.prototype
  • Loading branch information
Josep M. Bach committed Jun 26, 2012
2 parents 2ac4903 + a4f3b7d commit 45535ab
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 60 deletions.
125 changes: 65 additions & 60 deletions lib/assoc.js
@@ -1,75 +1,80 @@
module.exports = function (object) { function AssociativeArray(object) {

this.data = [];
var data = [], this._merge_object_in_array(object, this.data, 'push');

};
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);
}
}
}
};


merge_object_in_array(object, data, 'push'); AssociativeArray.prototype = {


this.assoc = function (key) { assoc: function (key) {
var i; var i;
for (i = 0; i < data.length; i += 1) { for (i = 0; i < this.data.length; i += 1) {
if (key === data[i][0]) { if (key === this.data[i][0]) {
return data[i][1]; return this.data[i][1];
} }
} }
}; },


this.rassoc = function (value) { rassoc: function (value) {
var i; var i;
for (i = 0; i < data.length; i += 1) { for (i = 0; i < this.data.length; i += 1) {
if (value === data[i][1]) { if (value === this.data[i][1]) {
return data[i][0]; return this.data[i][0];
} }
} }
}; },


this.push = function (obj) { pop: function () {
merge_object_in_array(obj, data, 'push'); return this.data.pop();
}; },


this.pop = function () { shift: function () {
data.pop(); return this.data.shift();
}; },


this.unshift = function (obj) { push: function (obj) {
merge_object_in_array(obj, data, 'unshift'); this._merge_object_in_array(obj, this.data, 'push');
}; },


this.shift = function () { unshift: function (obj) {
data.shift(); 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;
13 changes: 13 additions & 0 deletions test/assoc_test.js
Expand Up @@ -84,6 +84,19 @@ testosterone
assert.equal(array.rassoc('b'), 'a'); 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 () { .run(function () {
require('util').print('done!'); require('util').print('done!');
}); });

0 comments on commit 45535ab

Please sign in to comment.