From 3b091a3f2bc44e54117198ca2dc210ed5fcd3dc1 Mon Sep 17 00:00:00 2001 From: Michal Michalski Date: Tue, 30 Jul 2019 12:08:57 +0200 Subject: [PATCH] [tizen] Add copy ctor to tizen.Bundle. This will simplify the implementation of API methods which need to accept both plain json object and tizen.Bundle. In example: function sendCommand(..., data) { var d = new tizen.Bundle(data); } instead of manually having to check if data is a tizen.Bundle or object. [Verification] Added new test case for this constructor, all existing tests passes. Signed-off-by: Michal Michalski Change-Id: I4c880dbf8889c88bb20a4029b5cbb8fa2941eb1d --- src/tizen/js/ut/bundle_ut.js | 15 +++++++++++++++ src/tizen/tizen_api.js | 26 ++++++++++++++++++-------- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/tizen/js/ut/bundle_ut.js b/src/tizen/js/ut/bundle_ut.js index f9c66d8..4c5f9f2 100644 --- a/src/tizen/js/ut/bundle_ut.js +++ b/src/tizen/js/ut/bundle_ut.js @@ -7,6 +7,19 @@ function TestDefaultCtor() { assertEqual(0, counter, 'bundle should be empty'); } +function TestCopyCtor() { + var src = new tizen.Bundle({ + key1: 'value', + key2: ['value1', 'value2'], + }); + + var dest = new tizen.Bundle(src); + + assertEqual(Object.keys(src.data).length, Object.keys(dest.data).length, "number of entries is different"); + assertEqual(src.data.key1, dest.data.key1, "key1 entry has different value"); + assertArrayEqual(src.data.key2, dest.data.key2, "key2 entry has different value"); +} + function TestJsonCtor() { var json = { key1: 'value', @@ -237,6 +250,7 @@ function TestToString() { var testcases = [ TestDefaultCtor, TestJsonCtor, + TestCopyCtor, TestSetter, TestGetters, TestTypeOf, @@ -244,3 +258,4 @@ var testcases = [ TestToJson, TestToString ]; + diff --git a/src/tizen/tizen_api.js b/src/tizen/tizen_api.js index 5dfb159..44b911b 100644 --- a/src/tizen/tizen_api.js +++ b/src/tizen/tizen_api.js @@ -507,19 +507,29 @@ function getValueType(value) { } } -exports.Bundle = function(json) { +function Bundle(arg) { xwalk.utils.validator.isConstructorCall(this, exports.Bundle); this.data = {}; + var json = {}; - if (xwalk.utils.type.isObject(json)) { - forEachOwnProperty( - json, - function(key, value) { - this.set(key, value); - }.bind(this) - ); + // copy constructor + if (arg instanceof tizen.Bundle) { + // perform deep copy of the other bundle. + json = JSON.parse(arg.toString()); + } + // json to bundle conversion + else if (xwalk.utils.type.isObject(json)) { + json = arg; } + + forEachOwnProperty( + json, + function(key, value) { + this.set(key, value); + }.bind(this) + ); }; +exports.Bundle = Bundle; exports.Bundle.prototype.constructor = exports.Bundle; exports.Bundle.prototype.set = function(key, value) { -- 2.7.4