aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorcyanos3 <[email protected]>2013-11-05 17:03:14 -0700
committercyanos3 <[email protected]>2013-11-05 17:03:14 -0700
commit229f9b9d04bab0d57d575b71f2f8a281e53a58ec (patch)
tree01624391d039bdef5e92403e8933be4a1fbce0b6 /lib
parent89124e9f023189688da1fc4433c5c63acfd49399 (diff)
downloadfaker-229f9b9d04bab0d57d575b71f2f8a281e53a58ec.tar.xz
faker-229f9b9d04bab0d57d575b71f2f8a281e53a58ec.zip
Refactor tree so that it will work with the module model. (Not working at all prior)
Diffstat (limited to 'lib')
-rw-r--r--lib/tree.js97
1 files changed, 47 insertions, 50 deletions
diff --git a/lib/tree.js b/lib/tree.js
index c35851cf..75e2d75f 100644
--- a/lib/tree.js
+++ b/lib/tree.js
@@ -1,67 +1,64 @@
var Faker = require('../index');
-var clone = function clone(obj) {
- if (obj == null || typeof(obj) != 'object')
- return obj;
-
- var temp = obj.constructor(); // changed
+var tree = {
- for (var key in obj) {
- temp[key] = clone(obj[key]);
- }
- return temp;
-};
+ clone: function clone(obj) {
+ if (obj == null || typeof(obj) != 'object')
+ return obj;
+ var temp = obj.constructor(); // changed
-var createTree = function (depth, width, obj) {
+ for (var key in obj) {
+ temp[key] = this.clone(obj[key]);
+ }
+ return temp;
+ },
- if (!obj) {
- throw {
- name: "ObjectError",
- message: "there needs to be an object passed in",
- toString: function () {
- return this.name + ": " + this.message
- }
- };
- }
+ createTree: function (depth, width, obj) {
+ if (!obj) {
+ throw {
+ name: "ObjectError",
+ message: "there needs to be an object passed in",
+ toString: function () {
+ return this.name + ": " + this.message
+ }
+ };
+ }
- if (width <= 0) {
- throw {
- name: "TreeParamError",
- message: "width must be greater than zero",
- toString: function () {
- return this.name + ": " + this.message
- }
- };
- }
+ if (width <= 0) {
+ throw {
+ name: "TreeParamError",
+ message: "width must be greater than zero",
+ toString: function () {
+ return this.name + ": " + this.message
+ }
+ };
+ }
- var newObj = clone(obj);
+ var newObj = this.clone(obj);
- for (var prop in newObj) {
- if (newObj.hasOwnProperty(prop)) {
- var value = null;
- if (newObj[prop] !== "__RECURSE__") {
- value = eval(newObj[prop]);
- }
- else {
- if (depth !== 0) {
- value = [];
- for (var i = 0; i < width; i++) {
- value.push(createTree(depth - 1, width, obj));
+ for (var prop in newObj) {
+ if (newObj.hasOwnProperty(prop)) {
+ var value = null;
+ if (newObj[prop] !== "__RECURSE__") {
+ value = eval(newObj[prop]);
+ }
+ else {
+ if (depth !== 0) {
+ value = [];
+ for (var i = 0; i < width; i++) {
+ value.push(this.createTree(depth - 1, width, obj));
+ }
}
}
- }
- newObj[prop] = value;
+ newObj[prop] = value;
+ }
}
+
+ return newObj;
}
- return newObj;
};
-var tree = {
- createTree: createTree
-};
-
-
-module.exports = tree;
+module.exports = tree; \ No newline at end of file