Skip to content

Commit

Permalink
more tests and adding exclude keys feature
Browse files Browse the repository at this point in the history
  • Loading branch information
thlorenz committed Sep 16, 2013
1 parent ec7254c commit 910d9e4
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
9 changes: 6 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@ var go = module.exports = function (obj, opts) {
opts = opts || {};
var maxValueLength = Math.min(opts.maxValueLength, Infinity)
, maxKeyLength = Math.min(opts.maxKeyLength, Infinity)
, exclude = opts.exclude || [ 'function' ];
, excludeTypes = opts.excludeTypes || [ 'function' ]
, excludeKeys = opts.excludeKeys || [];

var rows = Object.keys(obj).reduce(reducer, []);

function reducer (acc, k) {
if (~excludeKeys.indexOf(k)) return acc;

var val = obj[k];
var valType = typeof val;
if (~exclude.indexOf(valType)) return acc;
if (~excludeTypes.indexOf(valType)) return acc;

var s;

if (valType === 'function') s = 'function';
if (valType === 'function') s = '[function]';
else if (valType === 'undefined') s = 'undefined';
else if (val === null) s = 'null';
else {
Expand Down
59 changes: 57 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,62 @@ test('\ntext-table package with max key and value length', function (t) {
})


test('\nprocess - to prove that it doen\'t choke on circular data structures', function (t) {

test('\ndoesn\'t choke on circular data structures', function (t) {
var obj = {
foo: {
bar: { prim: 1 }
},
shoe: 'sock'
}
obj.foo.bar.obj = obj;

var res = tabject(obj);
t.equal(res, [
'foo [object Object]'
, 'shoe "sock"'
].join('\n')
, 'falls back to toString since JSON.stringify fails'
)
t.end()
})

test('\nfunctions are excluded by default', function (t) {
var obj = { foo: 'bar', fn: function () { } };
var res = tabject(obj)
t.equal(res, 'foo "bar"');
t.end()
})

test('\nfunctions are included when excludeTypes is set to empty', function (t) {
var obj = { foo: 'bar', fn: function () { } };
var res = tabject(obj, { excludeTypes: [] })
t.equal(res, [
'foo "bar"'
, 'fn [function]'
].join('\n'))
t.end()
})

test('\nstrings are excluded when excludeTypes is set to [ string ]', function (t) {
var obj = { foo: 'bar', fn: function () { } };
var res = tabject(obj, { excludeTypes: [ 'string' ] })
t.equal(res, 'fn [function]')
t.end()
})

test('\nby default all keys are included', function (t) {
var obj = { key1: 1, key2: 2 }
var res = tabject(obj)
t.equal(res, [
'key1 1'
, 'key2 2'
].join('\n'))
t.end()
})

test('\nkey1 is excluded when I excludeKeys is set to [ key1 ]', function (t) {
var obj = { key1: 1, key2: 2 }
var res = tabject(obj, { excludeKeys: [ 'key1' ] })
t.equal(res, 'key2 2')
t.end()
})

0 comments on commit 910d9e4

Please sign in to comment.