mirror of
https://github.com/pebble-dev/clay.git
synced 2026-09-06 00:57:21 -04:00
Change the ClayConfig.getSettings() method to convert data types to be compatible with sendAppMessage
This commit is contained in:
@@ -158,7 +158,7 @@ function ClayConfig(settings, config, $rootContainer, meta) {
|
|||||||
self.getSettings = function() {
|
self.getSettings = function() {
|
||||||
_checkBuilt('getSettings');
|
_checkBuilt('getSettings');
|
||||||
_.eachObj(_itemsByAppKey, function(appKey, item) {
|
_.eachObj(_itemsByAppKey, function(appKey, item) {
|
||||||
_settings[appKey] = item.get();
|
_settings[appKey] = utils.prepareForAppMessage(item.get());
|
||||||
});
|
});
|
||||||
return _settings;
|
return _settings;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -17,3 +17,39 @@ module.exports.updateProperties = function(obj, descriptor) {
|
|||||||
Object.defineProperty(obj, prop, 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;
|
||||||
|
};
|
||||||
|
|||||||
@@ -108,7 +108,12 @@ describe('ClayConfig', function() {
|
|||||||
{label: 'label-1', value: 'val-1'},
|
{label: 'label-1', value: 'val-1'},
|
||||||
{label: 'label-2', value: 'val-2'}
|
{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,
|
||||||
true,
|
true,
|
||||||
@@ -120,16 +125,19 @@ describe('ClayConfig', function() {
|
|||||||
assert.deepEqual(clayConfig.getSettings(), {
|
assert.deepEqual(clayConfig.getSettings(), {
|
||||||
test1: 'default val',
|
test1: 'default val',
|
||||||
test2: 'val-2',
|
test2: 'val-2',
|
||||||
test3: 0
|
test3: 0,
|
||||||
|
test4: []
|
||||||
});
|
});
|
||||||
|
|
||||||
clayConfig.getItemByAppKey('test1').set('val-1');
|
clayConfig.getItemByAppKey('test1').set('val-1');
|
||||||
clayConfig.getItemByAppKey('test3').set(true);
|
clayConfig.getItemByAppKey('test3').set(true);
|
||||||
|
clayConfig.getItemByAppKey('test4').set(['cb-1', 'cb-3']);
|
||||||
|
|
||||||
assert.deepEqual(clayConfig.getSettings(), {
|
assert.deepEqual(clayConfig.getSettings(), {
|
||||||
test1: 'val-1',
|
test1: 'val-1',
|
||||||
test2: 'val-2',
|
test2: 'val-2',
|
||||||
test3: 1
|
test3: 1,
|
||||||
|
test4: ['cb-1', 0, 'cb-3', 0]
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -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);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user