Skip to content
This repository has been archived by the owner on Apr 9, 2024. It is now read-only.

Commit

Permalink
fixed an issue exposed by my line-by-line test of the function omissi…
Browse files Browse the repository at this point in the history
…on functionality that resulted in an incorrect prefix being used for the last item IF the object contained one or more functions and hideFunctions = true
  • Loading branch information
notatestuser committed Dec 24, 2012
1 parent 4d9ed74 commit b5d3c67
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 35 deletions.
69 changes: 48 additions & 21 deletions test/tree-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,29 @@ var treeify = require('../treeify'),

// - helper functions -----------------

function treeifyByLineGuts(args) {
var emitter = new events.EventEmitter(),
lineNum = 0;
args.push(function(line) {
emitter.emit('success', line);
emitter.emit('line ' + (++lineNum), line);
});
treeify.asLines.apply(this, args);
return emitter;
}

function treeifyByLine(obj) {
return function(showValues) {
var emitter = new events.EventEmitter(),
lineNum = 0;
treeify.asLines(obj, showValues, function(line) {
emitter.emit('success', line);
emitter.emit('line ' + (++lineNum), line);
});
return emitter;
};
var arguments = [ obj, showValues ];
return treeifyByLineGuts(arguments);
};
}

function treeifyByLineWithHideFunctionsArgument(obj) {
return function(showValues, hideFunctions) {
var arguments = [ obj, showValues, hideFunctions ];
return treeifyByLineGuts(arguments);
};
}

function treeifyEntirely(obj) {
Expand All @@ -29,7 +42,7 @@ function withValuesShown(showValues) {

function withValuesShownFunctionsHidden() {
return function(func){ return func(true, true) };

}

function is(content, arrayIndex) {
Expand Down Expand Up @@ -57,10 +70,10 @@ function checkLines(/* ... */) {
// - the beautiful test suite ---------

vows.describe('tree-test').addBatch({

'A tree created from an empty object': {
topic: {},

'when returned as a whole tree': {
topic: treeifyEntirely,

Expand All @@ -76,7 +89,7 @@ vows.describe('tree-test').addBatch({
},

'A tree created from a single-level object': {
topic: {
topic: {
apples: 'gala', // ├─ apples: gala
oranges: 'mandarin' // └─ oranges: mandarin
},
Expand All @@ -86,20 +99,20 @@ vows.describe('tree-test').addBatch({

'with values hidden': {
topic: withValuesShown(false),

'is two lines long': function(err, line) {
this.expect(2);
},
on: checkLines('├─ apples',
on: checkLines('├─ apples',
'└─ oranges')
},
'with values shown': {
topic: withValuesShown(true),

'is two lines long': function(err, line) {
this.expect(2);
},
on: checkLines('├─ apples: gala',
on: checkLines('├─ apples: gala',
'└─ oranges: mandarin')
}
},
Expand Down Expand Up @@ -143,7 +156,7 @@ vows.describe('tree-test').addBatch({
},

'A tree created from a multi-level object': {
topic: {
topic: {
oranges: { // ├─ oranges
'mandarin': { // │ └─ mandarin
clementine: null, // │ ├─ clementine
Expand All @@ -154,19 +167,19 @@ vows.describe('tree-test').addBatch({
apples: { // └─ apples
'gala': null, // ├─ gala
'pink lady': null // └─ pink lady
}
}
},

'when returned line-by-line': {
topic: treeifyByLine,

'with values hidden': {
topic: withValuesShown(false),

'is seven lines long': function(err, line) {
this.expect(7);
},
on: checkLines('├─ oranges',
on: checkLines('├─ oranges',
'│ └─ mandarin',
'│ ├─ clementine',
'│ └─ tangerine',
Expand Down Expand Up @@ -296,14 +309,28 @@ vows.describe('tree-test').addBatch({
Another:"stuff"
},

'when returned line-by-line': {
topic: treeifyByLineWithHideFunctionsArgument,

'with values shown, but functions hidden': {
topic: withValuesShownFunctionsHidden(),

'is two lines long': function(err, line) {
this.expect(2);
},
on: checkLines('├─ Friendly: stuff',
'└─ Another: stuff')
}
},

'when returned as a whole tree': {
topic: treeifyEntirely,

'with values shown, but functions hidden': {
topic: withValuesShownFunctionsHidden(),

'and split into an array of lines': {
topic: function(tree) {
topic: function(tree) {
console.error(tree);
return tree.split(/\n/g) },
'is a one liner output (with a following blank line)': function(lines) {
Expand Down
40 changes: 26 additions & 14 deletions treeify.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// https://github.com/notatestuser/treeify.js

(function() {

// namespacing
var Treeify;
if (typeof exports !== 'undefined') {
Expand All @@ -22,6 +22,22 @@
return str;
}

function filterKeys(obj, hideFunctions) {
var keys = [];
for (var branch in obj) {
// always exclude anything in the object's prototype
if (!obj.hasOwnProperty(branch)) {
continue;
}
// ... and hide any keys mapped to functions if we've been told to
if (hideFunctions && ((typeof obj[branch])==="function")) {
continue;
}
keys.push(branch);
}
return keys;
}

function growBranch(key, root, last, lastStates, showValues, hideFunctions, callback) {
var line = '', index = 0, lastKey, circular, lastStatesCopy = lastStates.slice(0);

Expand All @@ -37,31 +53,27 @@
}
});

// the prefix varies based on whether the key contains something to show and
// the prefix varies based on whether the key contains something to show and
// whether we're dealing with the last element in this collection
line += makePrefix(key, last) + key;

// append values and the circular reference indicator
showValues && typeof root !== 'object' && (line += ': ' + root);
circular && (line += ' (circular ref.)');

callback(line);
}

// can we descend into the next item?
if ( ! circular && typeof root === 'object') {
for (var branch in root) {
// always exclude anything in the object's prototype
if (!root.hasOwnProperty(branch)) {
continue;
}
if (hideFunctions&& ((typeof root[branch])==="function")) {
continue;
}
var keys = filterKeys(root, hideFunctions);
keys.forEach(function(branch){
// the last key is always printed with a different prefix, so we'll need to know if we have it
lastKey = ++index === keys.length;

// hold your breath for recursive action
lastKey = ++index === Object.keys(root).length;
growBranch(branch, root[branch], lastKey, lastStatesCopy, showValues, hideFunctions, callback);
}
});
}
};

Expand All @@ -86,6 +98,6 @@
});
return tree;
};


})();

0 comments on commit b5d3c67

Please sign in to comment.