From be54187b963d527348f6142f04e7d84c54af6ef2 Mon Sep 17 00:00:00 2001 From: cyanos3 Date: Tue, 5 Nov 2013 07:09:59 -0700 Subject: Add tree: createTree(depth, width, obj) where - depth is the distance from the root to the farthest node - width is the number of children to add to each node - each property/value pair in obj contains a value that will be evaluated to get the final result. The property that contains the array of child nodes should have the value of "__RECURSE__" --- lib/tree.js | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 lib/tree.js (limited to 'lib/tree.js') diff --git a/lib/tree.js b/lib/tree.js new file mode 100644 index 00000000..c35851cf --- /dev/null +++ b/lib/tree.js @@ -0,0 +1,67 @@ +var Faker = require('../index'); + +var clone = function clone(obj) { + if (obj == null || typeof(obj) != 'object') + return obj; + + var temp = obj.constructor(); // changed + + for (var key in obj) { + temp[key] = clone(obj[key]); + } + return temp; +}; + + +var 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 + } + }; + } + + var newObj = 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)); + } + } + } + + newObj[prop] = value; + } + } + + return newObj; +}; + +var tree = { + createTree: createTree +}; + + +module.exports = tree; -- cgit v1.2.3 From 229f9b9d04bab0d57d575b71f2f8a281e53a58ec Mon Sep 17 00:00:00 2001 From: cyanos3 Date: Tue, 5 Nov 2013 17:03:14 -0700 Subject: Refactor tree so that it will work with the module model. (Not working at all prior) --- lib/tree.js | 97 ++++++++++++++++++++++++++++++------------------------------- 1 file changed, 47 insertions(+), 50 deletions(-) (limited to 'lib/tree.js') 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 -- cgit v1.2.3 From 5aa4062dcabc2fdac1c93df09e71d702ec7ec81e Mon Sep 17 00:00:00 2001 From: cyanos3 Date: Tue, 5 Nov 2013 17:44:10 -0700 Subject: Tree: let the width parameter also be a function that returns an int --- lib/tree.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'lib/tree.js') diff --git a/lib/tree.js b/lib/tree.js index 75e2d75f..839b698c 100644 --- a/lib/tree.js +++ b/lib/tree.js @@ -25,6 +25,7 @@ var tree = { }; } + if (width <= 0) { throw { name: "TreeParamError", @@ -46,9 +47,19 @@ var tree = { else { if (depth !== 0) { value = []; - for (var i = 0; i < width; i++) { + var evalWidth = 1; + + if (typeof(width) == "function") { + evalWidth = width(); + } + else { + evalWidth = width; + } + + for (var i = 0; i < evalWidth; i++) { value.push(this.createTree(depth - 1, width, obj)); } + } } @@ -61,4 +72,4 @@ var tree = { }; -module.exports = tree; \ No newline at end of file +module.exports = tree; -- cgit v1.2.3 From c8f71cd072b74c66693cae927bdaf9a97ee15c1f Mon Sep 17 00:00:00 2001 From: cyanos3 Date: Tue, 5 Nov 2013 20:10:31 -0700 Subject: My take on separating male and female names. Tests passing, Coverage restored. --- lib/tree.js | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) (limited to 'lib/tree.js') diff --git a/lib/tree.js b/lib/tree.js index 839b698c..e2fd44b6 100644 --- a/lib/tree.js +++ b/lib/tree.js @@ -18,10 +18,7 @@ var tree = { if (!obj) { throw { name: "ObjectError", - message: "there needs to be an object passed in", - toString: function () { - return this.name + ": " + this.message - } + message: "there needs to be an object passed in" }; } @@ -29,10 +26,7 @@ var tree = { if (width <= 0) { throw { name: "TreeParamError", - message: "width must be greater than zero", - toString: function () { - return this.name + ": " + this.message - } + message: "width must be greater than zero" }; } -- cgit v1.2.3