From 271a0d1cfd10045a7e2ad26cda278824264a4917 Mon Sep 17 00:00:00 2001 From: Keegan Date: Mon, 7 Mar 2016 12:58:46 +1100 Subject: [PATCH] Change the ClayConfig.getSettings() method to convert data types to be compatible with sendAppMessage --- src/scripts/lib/clay-config.js | 2 +- src/scripts/lib/utils.js | 36 ++++++++++++++++++++++++++++++++++ test/spec/lib/clay-config.js | 14 ++++++++++--- test/spec/lib/utils.js | 30 ++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 4 deletions(-) diff --git a/src/scripts/lib/clay-config.js b/src/scripts/lib/clay-config.js index 750e700..7de1379 100644 --- a/src/scripts/lib/clay-config.js +++ b/src/scripts/lib/clay-config.js @@ -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; }; diff --git a/src/scripts/lib/utils.js b/src/scripts/lib/utils.js index 02079f3..46ca729 100644 --- a/src/scripts/lib/utils.js +++ b/src/scripts/lib/utils.js @@ -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; +}; diff --git a/test/spec/lib/clay-config.js b/test/spec/lib/clay-config.js index 7aa2d4d..8275bab 100644 --- a/test/spec/lib/clay-config.js +++ b/test/spec/lib/clay-config.js @@ -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, @@ -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] }); }); }); diff --git a/test/spec/lib/utils.js b/test/spec/lib/utils.js index 11050b1..bf0bf5a 100644 --- a/test/spec/lib/utils.js +++ b/test/spec/lib/utils.js @@ -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); + }); + }); });