diff --git a/lib/table/table.coffee b/lib/table/table.coffee new file mode 100644 index 00000000..08809bd4 --- /dev/null +++ b/lib/table/table.coffee @@ -0,0 +1,35 @@ +_ = require('lodash') + +KEY_DISPLAY_MAP = + commit: 'Commit' + app_name: 'Name' + git_repository: 'Git Repository' + device_type: 'Device Type' + id: 'ID' + +startsWithLetter = (string) -> + firstLetter = _.first(string) + return /[a-z|A-Z]/.test(firstLetter) + +renameObjectKey = (object, key, newKey) -> + object[newKey] = object[key] + delete object[key] + +exports.prepareObject = (object) -> + object = _.omit object, (value, key) -> + return not startsWithLetter(key) + + for key, value of object + if _.isObject(value) + object[key] = exports.prepareObject(value) + + displayKey = KEY_DISPLAY_MAP[key] + if displayKey? + renameObjectKey(object, key, displayKey) + + object = _.omit object, (value, key) -> + + # For some reason, _.isEmpty returns true for numbers + return _.isEmpty(value) and not _.isNumber(value) + + return object diff --git a/lib/table/table.spec.coffee b/lib/table/table.spec.coffee new file mode 100644 index 00000000..5ca38071 --- /dev/null +++ b/lib/table/table.spec.coffee @@ -0,0 +1,58 @@ +expect = require('chai').expect +table = require('./table') + +OBJECTS = + application: + device: null + id: 162 + user: + __deferred: [] + __id: 24 + app_name: 'HelloResin' + git_repository: 'git@git.staging.resin.io:jviotti/helloresin.git' + commit: '1234' + device_type: 'raspberry-pi' + __metadata: + uri: '/ewa/application(162)' + basic: + hello: 'world' + Hey: 'there' + __private: true + _option: false + $data: [ 1, 2, 3 ] + recursive: + hello: 'world' + Hey: + __private: true + value: + $option: false + data: 'There' + _option: false + __something: + value: 'ok' + nested: + $data: [ 1, 2, 3 ] + +describe 'Table:', -> + + describe '#prepareObject()', -> + + it 'should get rid of keys not starting with letters', -> + expect(table.prepareObject(OBJECTS.basic)).to.deep.equal + hello: 'world' + Hey: 'there' + + it 'should get rid of keys not starting with letters recursively', -> + expect(table.prepareObject(OBJECTS.recursive)).to.deep.equal + hello: 'world' + Hey: + value: + data: 'There' + + it 'should do proper key renamings', -> + expect(table.prepareObject(OBJECTS.application)).to.deep.equal + ID: 162 + Name: 'HelloResin' + 'Git Repository': 'git@git.staging.resin.io:jviotti/helloresin.git' + Commit: '1234' + 'Device Type': 'raspberry-pi'