Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from x3ro/x3ro/module-structure-mod
Restructuring methods into AssociativeArray.prototype
- Loading branch information
Showing
2 changed files
with
78 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters