Skip to content

Commit

Permalink
Change the ClayConfig.getSettings() method to convert data types to b…
Browse files Browse the repository at this point in the history
…e compatible with sendAppMessage
  • Loading branch information
keegan-lillo committed Mar 7, 2016
1 parent d6659db commit 271a0d1
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/scripts/lib/clay-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ function ClayConfig(settings, config, $rootContainer, meta) {
self.getSettings = function() {
_checkBuilt('getSettings');
_.eachObj(_itemsByAppKey, function(appKey, item) {
_settings[appKey] = item.get();
_settings[appKey] = utils.prepareForAppMessage(item.get());
});
return _settings;
};
Expand Down
36 changes: 36 additions & 0 deletions src/scripts/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,39 @@ module.exports.updateProperties = function(obj, descriptor) {
Object.defineProperty(obj, prop, descriptor);
});
};

/**
* Converts the val into a type compatible with Pebble.sendAppMessage().
* - Strings will be returned without modification
* - Numbers will be returned without modification
* - Booleans will be converted to a 0 or 1
* - Arrays that contain strings will be split with a zero.
* eg: ['one', 'two'] becomes ['one', 0, 'two', 0]
* - Arrays that contain numbers will be returned without modification
* eg: [1, 2] becomes [1, 2]
* - Arrays that contain booleans will be converted to a 0 or 1
* eg: [true, false] becomes [1, 0]
* - Arrays must be single dimensional
* @param {number|string|boolean|Array} val
* @returns {number|string|Array}
*/
module.exports.prepareForAppMessage = function(val) {
var result;

if (typeof val === 'boolean') {
result = val ? 1 : 0;
} else if (Array.isArray(val)) {
result = [];
val.forEach(function(item) {
var itemConverted = module.exports.prepareForAppMessage(item);
result.push(itemConverted);
if (typeof itemConverted === 'string') {
result.push(0);
}
});
} else {
result = val;
}

return result;
};
14 changes: 11 additions & 3 deletions test/spec/lib/clay-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,12 @@ describe('ClayConfig', function() {
{label: 'label-1', value: 'val-1'},
{label: 'label-2', value: 'val-2'}
]},
{type: 'toggle', appKey: 'test3'}
{type: 'toggle', appKey: 'test3'},
{type: 'checkboxgroup', appKey: 'test4', options: [
{label: 'label-1', value: 'cb-1'},
{label: 'label-2', value: 'cb-2'},
{label: 'label-2', value: 'cb-3'}
]}
],
true,
true,
Expand All @@ -120,16 +125,19 @@ describe('ClayConfig', function() {
assert.deepEqual(clayConfig.getSettings(), {
test1: 'default val',
test2: 'val-2',
test3: 0
test3: 0,
test4: []
});

clayConfig.getItemByAppKey('test1').set('val-1');
clayConfig.getItemByAppKey('test3').set(true);
clayConfig.getItemByAppKey('test4').set(['cb-1', 'cb-3']);

assert.deepEqual(clayConfig.getSettings(), {
test1: 'val-1',
test2: 'val-2',
test3: 1
test3: 1,
test4: ['cb-1', 0, 'cb-3', 0]
});
});
});
Expand Down
30 changes: 30 additions & 0 deletions test/spec/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,34 @@ describe('Utils', function() {
);
});
});

describe('.prepareForAppMessage', function() {
it('converts an array correctly when array contains strings', function() {
assert.deepEqual(
utils.prepareForAppMessage(['one', 'two']),
['one', 0, 'two', 0]
);
});

it('converts an array correctly when array contains numbers', function() {
assert.deepEqual(utils.prepareForAppMessage([1, 2, 3]), [1, 2, 3]);
});

it('converts an array correctly when array contains booleans', function() {
assert.deepEqual(utils.prepareForAppMessage([true, false, true]), [1, 0, 1]);
});

it('converts booleans to ints', function() {
assert.strictEqual(utils.prepareForAppMessage(false), 0);
assert.strictEqual(utils.prepareForAppMessage(true), 1);
});

it('leaves strings alone', function() {
assert.strictEqual(utils.prepareForAppMessage('test'), 'test');
});

it('leaves numbers alone', function() {
assert.strictEqual(utils.prepareForAppMessage(123), 123);
});
});
});

0 comments on commit 271a0d1

Please sign in to comment.