forked from zuriby/Faker.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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__"
- Loading branch information
Showing
3 changed files
with
164 additions
and
0 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
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 | Diff line number | Diff line change |
---|---|---|
@@ -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; |
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
if (typeof module !== 'undefined') { | ||
var assert = require('assert'); | ||
var sinon = require('sinon'); | ||
var Faker = require('../index'); | ||
} | ||
|
||
describe("tree.js", function () { | ||
describe("createTree()", function () { | ||
|
||
var proto = { | ||
"firstname": "Faker.random.first_name()", | ||
"children": "__RECURSE__" | ||
}; | ||
|
||
it("requires the width to be at least one", function () { | ||
sinon.spy(Faker.Tree, 'createTree'); | ||
|
||
try { | ||
Faker.Tree.createTree(0, 0, {}); | ||
} | ||
catch (e) { | ||
} | ||
|
||
assert.ok(Faker.Tree.createTree.threw); | ||
|
||
Faker.Tree.createTree.restore(); | ||
}); | ||
|
||
it("requires that the object passed in should not be null", function () { | ||
sinon.spy(Faker.Tree, 'createTree'); | ||
|
||
try { | ||
Faker.Tree.createTree(1, 1, null); | ||
} | ||
catch (e) { | ||
} | ||
|
||
assert.ok(Faker.Tree.createTree.threw); | ||
|
||
Faker.Tree.createTree.restore(); | ||
|
||
}); | ||
|
||
it("can create a trivial tree with one node", function () { | ||
sinon.spy(Faker.random, 'first_name'); | ||
|
||
var tree = Faker.Tree.createTree(0, 1, proto); | ||
|
||
assert.ok(Faker.random.first_name.calledOnce); | ||
|
||
assert.ok(tree.children == null); | ||
|
||
Faker.random.first_name.restore(); | ||
}); | ||
|
||
it("can create a deep tree with one node at each level", function () { | ||
sinon.spy(Faker.random, 'first_name'); | ||
var tree = Faker.Tree.createTree(2, 1, proto); | ||
|
||
assert.ok(Faker.random.first_name.calledThrice); | ||
|
||
assert.ok(tree.firstname); | ||
assert.ok(tree.children[0].firstname); | ||
assert.ok(tree.children[0].children[0].firstname); | ||
|
||
Faker.random.first_name.restore(); | ||
}); | ||
|
||
it("can create a basic N-tree", function () { | ||
var n = 3; | ||
sinon.spy(Faker.random, 'first_name'); | ||
var tree = Faker.Tree.createTree(1, n, proto); | ||
|
||
assert.ok(Faker.random.first_name.callCount == 4); | ||
|
||
assert.ok(tree.firstname); | ||
assert.ok(tree.children[0].firstname); | ||
assert.ok(tree.children[1].firstname); | ||
assert.ok(tree.children[2].firstname); | ||
|
||
Faker.random.first_name.restore(); | ||
}); | ||
|
||
it("can create a full N-tree", function () { | ||
var n = 3; | ||
sinon.spy(Faker.random, 'first_name'); | ||
var tree = Faker.Tree.createTree(2, n, proto); | ||
|
||
assert.ok(Faker.random.first_name.callCount == 13); | ||
|
||
Faker.random.first_name.restore(); | ||
}); | ||
|
||
}); | ||
|
||
}); |