mirror of
https://github.com/balena-io/balena-cli.git
synced 2025-04-13 14:12:57 +00:00
Implement table module
This commit is contained in:
parent
77d84103f6
commit
e390efd46e
lib/table
35
lib/table/table.coffee
Normal file
35
lib/table/table.coffee
Normal file
@ -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
|
58
lib/table/table.spec.coffee
Normal file
58
lib/table/table.spec.coffee
Normal file
@ -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'
|
Loading…
x
Reference in New Issue
Block a user