Extend table module to handle formatting

This commit is contained in:
Juan Cruz Viotti 2014-11-19 12:12:08 -04:00
parent e0448357df
commit 9130281862
3 changed files with 123 additions and 8 deletions

View File

@ -21,11 +21,11 @@ exports.list = authHooks.failIfNotLoggedIn ->
exports.info = authHooks.failIfNotLoggedIn (id) ->
applicationModel.get(id).then (application) ->
console.log("ID: #{application.id}")
console.log("Name: #{application.app_name}")
console.log("Device Type: #{device.getDisplayName(application.device_type)}")
console.log("Git Repository: #{application.git_repository}")
console.log("Commit: #{application.commit}")
console.log table.vertical application, (application) ->
application.device_type = device.getDisplayName(application.device_type)
delete application.device
return application
, [ 'ID', 'Name', 'Device Type', 'Git Repository', 'Commit' ]
.catch (error) ->
throw error

View File

@ -35,10 +35,40 @@ exports.prepareObject = (object) ->
return object
exports.processTableContents = (contents, map) ->
# Allows us to simplify the algorithm by not
# concerning about different input types
if not _.isArray(contents)
contents = [ contents ]
contents = _.map(contents, map or _.identity)
contents = _.map(contents, exports.prepareObject)
return contents
isRealObject = (object) ->
return false if _.isArray(object) or _.isFunction(object)
return _.isObject(object)
exports.getDefaultContentsOrdering = (contents) ->
return if _.isEmpty(contents)
firstContentEntry = _.first(contents)
return if not isRealObject(firstContentEntry)
return _.keys(firstContentEntry)
# TODO: Maybe there is a (sane) way to test this, given
# that the result is not automatically printed by cliff?
exports.horizontal = (contents, map, ordering, colours) ->
contents = _.map(contents, map or _.noop)
contents = _.map(contents, exports.prepareObject)
ordering ?= _.keys(_.first(contents))
contents = exports.processTableContents(contents, map)
ordering ?= exports.getDefaultContentsOrdering(contents)
return cliff.stringifyObjectRows(contents, ordering, colours)
exports.vertical = (contents, map, ordering) ->
contents = exports.processTableContents(contents, map)
ordering ?= exports.getDefaultContentsOrdering(contents)
result = []
for item in contents
for next in ordering
result.push("#{next}: #{item[next]}")
return result.join('\n')

View File

@ -1,4 +1,5 @@
expect = require('chai').expect
_ = require('lodash')
table = require('./table')
OBJECTS =
@ -32,6 +33,10 @@ OBJECTS =
value: 'ok'
nested:
$data: [ 1, 2, 3 ]
valid:
one: 'one'
two: 'two'
three: 'three'
describe 'Table:', ->
@ -60,3 +65,83 @@ describe 'Table:', ->
it 'should not remove not empty arrays', ->
object = { array: [ 1, 2, 3 ] }
expect(table.prepareObject(object)).to.deep.equal(object)
describe '#processTableContents()', ->
checkIfArray = (input) ->
result = table.processTableContents(input, _.identity)
expect(result).to.be.an.instanceof(Array)
it 'should always return an array', ->
checkIfArray(OBJECTS.basic)
checkIfArray([ OBJECTS.basic ])
checkIfArray([ 'contents' ])
it 'should be able to manipulate the contents', ->
result = table.processTableContents { hey: 'there' }, (item) ->
item.hey = 'yo'
return item
expect(result).to.deep.equal([ hey: 'yo' ])
it 'should get rid of keys not starting with letters', ->
result = table.processTableContents(OBJECTS.basic, _.identity)
expect(result).to.deep.equal [
{
hello: 'world'
Hey: 'there'
}
]
it 'should allow a null/undefined map function without corrupting the data', ->
for map in [ null, undefined ]
result = table.processTableContents([ OBJECTS.valid ], map)
expect(result).to.deep.equal([ OBJECTS.valid ])
describe '#getDefaultContentsOrdering()', ->
it 'should return undefined if no contents', ->
expect(table.getDefaultContentsOrdering()).to.be.undefined
it 'should return undefined if contents is empty', ->
expect(table.getDefaultContentsOrdering([])).to.be.undefined
it 'should return undefined if contents is not an array of objects', ->
inputs = [
[ 1, 2, 3 ]
[ '1', '2', '3' ]
[ _.identity ]
]
for input in inputs
expect(table.getDefaultContentsOrdering(input)).to.be.undefined
it 'should return an array containing all the object keys', ->
result = table.getDefaultContentsOrdering([ OBJECTS.valid ])
console.log result
for key, value of OBJECTS.valid
expect(result.indexOf(key)).to.not.equal(-1)
describe '#vertical()', ->
it 'should return a string respecting the ordering', ->
ordering = [ 'one', 'two', 'three' ]
result = table.vertical(OBJECTS.valid, null, ordering).split('\n')
expected = [
'one: one'
'two: two'
'three: three'
]
expect(result).to.deep.equal(expected)
it 'should be able to print everything without explicit ordering', ->
result = table.vertical(OBJECTS.valid, null).split('\n')
expected = [
'one: one'
'two: two'
'three: three'
]
for line in expected
expect(result.indexOf(line)).to.not.equal(-1)