From 07c57d335bb3f82cd7ac5b5e426dfb918668ebed Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Tue, 6 Jan 2015 14:12:11 -0300 Subject: [PATCH] Refactor table to get rid of map feature --- lib/actions/app.coffee | 4 ++-- lib/actions/device.coffee | 4 ++-- lib/actions/examples.coffee | 4 ++-- lib/actions/keys.coffee | 4 ++-- lib/errors/errors.spec.coffee | 4 ++-- lib/ui/widgets/table/table-helpers.coffee | 6 ++---- lib/ui/widgets/table/table-helpers.spec.coffee | 13 ------------- lib/ui/widgets/table/table.coffee | 8 ++++---- 8 files changed, 16 insertions(+), 31 deletions(-) diff --git a/lib/actions/app.coffee b/lib/actions/app.coffee index 397ffa56..a174e3cb 100644 --- a/lib/actions/app.coffee +++ b/lib/actions/app.coffee @@ -33,7 +33,7 @@ exports.list = permissions.user -> resin.models.application.getAll (error, applications) -> errors.handle(error) if error? - log.out ui.widgets.table.horizontal applications, _.identity, [ + log.out ui.widgets.table.horizontal applications, [ 'ID' 'Name' 'Device Display Name' @@ -45,7 +45,7 @@ exports.info = permissions.user (params) -> resin.models.application.get params.id, (error, application) -> errors.handle(error) if error? - log.out ui.widgets.table.vertical application, _.identity, [ + log.out ui.widgets.table.vertical application, [ 'ID' 'Name' 'Device Display Name' diff --git a/lib/actions/device.coffee b/lib/actions/device.coffee index 49210b7a..96655d85 100644 --- a/lib/actions/device.coffee +++ b/lib/actions/device.coffee @@ -13,7 +13,7 @@ exports.list = permissions.user (params, options) -> resin.models.device.getAllByApplication options.application, (error, devices) -> errors.handle(error) if error? - log.out ui.widgets.table.horizontal devices, _.identity, [ + log.out ui.widgets.table.horizontal devices, [ 'ID' 'Name' 'Device Display Name' @@ -27,7 +27,7 @@ exports.info = permissions.user (params) -> resin.models.device.get params.id, (error, device) -> errors.handle(error) if error? - log.out ui.widgets.table.vertical device, _.identity, [ + log.out ui.widgets.table.vertical device, [ 'ID' 'Name' 'Device Display Name' diff --git a/lib/actions/examples.coffee b/lib/actions/examples.coffee index e3f3d464..b9571957 100644 --- a/lib/actions/examples.coffee +++ b/lib/actions/examples.coffee @@ -20,7 +20,7 @@ exports.list = permissions.user -> example.author ?= 'Unknown' return example - log.out ui.widgets.table.horizontal examplesData, _.identity, [ + log.out ui.widgets.table.horizontal examplesData, [ 'ID' 'Display Name' 'Repository' @@ -38,7 +38,7 @@ exports.info = permissions.user (params) -> example.id = id example.author ?= 'Unknown' - log.out ui.widgets.table.vertical example, _.identity, [ + log.out ui.widgets.table.vertical example, [ 'ID' 'Display Name' 'Description' diff --git a/lib/actions/keys.coffee b/lib/actions/keys.coffee index 98c4ba02..674fdb43 100644 --- a/lib/actions/keys.coffee +++ b/lib/actions/keys.coffee @@ -13,14 +13,14 @@ exports.list = permissions.user -> resin.models.key.getAll (error, keys) -> errors.handle(error) if error? - log.out ui.widgets.table.horizontal keys, _.identity, [ 'ID', 'Title' ] + log.out ui.widgets.table.horizontal keys, [ 'ID', 'Title' ] exports.info = permissions.user (params) -> resin.models.key.get params.id, (error, key) -> errors.handle(error) if error? key.public_key = '\n' + _.str.chop(key.public_key, resin.settings.get('sshKeyWidth')).join('\n') - log.out(ui.widgets.table.vertical(key, _.identity, [ 'ID', 'Title', 'Public Key' ])) + log.out(ui.widgets.table.vertical(key, [ 'ID', 'Title', 'Public Key' ])) exports.remove = permissions.user (params, options) -> ui.patterns.remove 'key', options.yes, (callback) -> diff --git a/lib/errors/errors.spec.coffee b/lib/errors/errors.spec.coffee index 58fe303e..a9706ee3 100644 --- a/lib/errors/errors.spec.coffee +++ b/lib/errors/errors.spec.coffee @@ -62,7 +62,7 @@ describe 'Errors:', -> expect(logErrorStub).to.have.been.calledWith(error.stack) delete process.env.DEBUG - it.only 'should handle EISDIR', -> + it 'should handle EISDIR', -> error = new Error() error.code = 'EISDIR' error.path = 'hello' @@ -70,7 +70,7 @@ describe 'Errors:', -> expect(logErrorStub).to.have.been.calledOnce expect(logErrorStub).to.have.been.calledWith('File is a directory: hello') - it.only 'should handle ENOENT', -> + it 'should handle ENOENT', -> error = new Error() error.code = 'ENOENT' error.path = 'hello' diff --git a/lib/ui/widgets/table/table-helpers.coffee b/lib/ui/widgets/table/table-helpers.coffee index 4c4480ec..c69585a3 100644 --- a/lib/ui/widgets/table/table-helpers.coffee +++ b/lib/ui/widgets/table/table-helpers.coffee @@ -57,7 +57,7 @@ exports.prepareObject = (object) -> return object -exports.processTableContents = (contents, map) -> +exports.processTableContents = (contents) -> return if not contents? # Allows us to simplify the algorithm by not @@ -65,9 +65,7 @@ exports.processTableContents = (contents, map) -> if not _.isArray(contents) contents = [ contents ] - contents = _.map(contents, map or _.identity) - contents = _.map(contents, exports.prepareObject) - return contents + return _.map(contents, exports.prepareObject) isRealObject = (object) -> return false if _.isArray(object) or _.isFunction(object) diff --git a/lib/ui/widgets/table/table-helpers.spec.coffee b/lib/ui/widgets/table/table-helpers.spec.coffee index 95081336..9f59abe8 100644 --- a/lib/ui/widgets/table/table-helpers.spec.coffee +++ b/lib/ui/widgets/table/table-helpers.spec.coffee @@ -97,19 +97,6 @@ describe 'Table Helpers:', -> checkIfArray([ OBJECTS.basic ]) checkIfArray([ 'contents' ]) - it 'should be able to manipulate the contents', -> - result = tableHelpers.processTableContents { hey: 'there' }, (item) -> - item.hey = 'yo' - return item - - expect(result).to.deep.equal([ Hey: 'yo' ]) - - it 'should not call map if contents does not exist', -> - map = sinon.spy() - tableHelpers.processTableContents(null, map) - tableHelpers.processTableContents(undefined, map) - expect(map).to.not.have.been.called - it 'should get rid of keys not starting with letters', -> result = tableHelpers.processTableContents(OBJECTS.basic, _.identity) expect(result).to.deep.equal [ diff --git a/lib/ui/widgets/table/table.coffee b/lib/ui/widgets/table/table.coffee index 238a9445..737760e1 100644 --- a/lib/ui/widgets/table/table.coffee +++ b/lib/ui/widgets/table/table.coffee @@ -3,15 +3,15 @@ tableHelpers = require('./table-helpers') # 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) -> +exports.horizontal = (contents, ordering, colours) -> return if not contents? - contents = tableHelpers.processTableContents(contents, map) + contents = tableHelpers.processTableContents(contents) ordering = tableHelpers.normaliseOrdering(ordering, contents) return cliff.stringifyObjectRows(contents, ordering, colours) -exports.vertical = (contents, map, ordering) -> +exports.vertical = (contents, ordering) -> return if not contents? - contents = tableHelpers.processTableContents(contents, map) + contents = tableHelpers.processTableContents(contents) ordering = tableHelpers.normaliseOrdering(ordering, contents) # TODO: Add some kind of separator to make clear