From 9eef6cc42fe1bfa1e5be1a1f0b1dab07acf142c8 Mon Sep 17 00:00:00 2001 From: Kamil Piechaczek Date: Thu, 2 Mar 2017 12:05:03 +0100 Subject: [PATCH 1/5] Feature: Added default configuration prameter for Config. --- src/config.js | 7 ++++++- tests/config.js | 34 ++++++++++++++++++++++++++++++---- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/config.js b/src/config.js index ed13e5a..8c172e2 100644 --- a/src/config.js +++ b/src/config.js @@ -18,7 +18,7 @@ export default class Config { * * @param {Object} [configurations] The initial configurations to be set. */ - constructor( configurations ) { + constructor( configurations, defaultConfigurations ) { /** * Store for the whole configuration. * @@ -31,6 +31,11 @@ export default class Config { if ( configurations ) { this._setObjectToTarget( this._config, configurations ); } + + // Set default configuration. + if ( defaultConfigurations ) { + this._setObjectToTarget( this._config, defaultConfigurations, true ); + } } /** diff --git a/tests/config.js b/tests/config.js index 1e8defb..f3189b4 100644 --- a/tests/config.js +++ b/tests/config.js @@ -23,7 +23,7 @@ describe( 'Config', () => { } ); } ); - describe( 'constructor', () => { + describe( 'constructor()', () => { it( 'should set configurations', () => { expect( config.get( 'creator' ) ).to.equal( 'inline' ); expect( config.get( 'language' ) ).to.equal( 'pl' ); @@ -41,9 +41,35 @@ describe( 'Config', () => { // No error should be thrown. config = new Config(); } ); + + it( 'should set default parameters', () => { + const defaultConfig = { + foo: 1, + bar: 2, + }; + + config = new Config( {}, defaultConfig ); + + expect( config.get( 'foo' ) ).to.equal( 1 ); + expect( config.get( 'bar' ) ).to.equal( 2 ); + } ); + + it( 'passed parameters should override default parameters', () => { + const defaultConfig = { + foo: 1, + bar: 2, + }; + + config = new Config( { + foo: 10, + }, defaultConfig ); + + expect( config.get( 'foo' ) ).to.equal( 10 ); + expect( config.get( 'bar' ) ).to.equal( 2 ); + } ); } ); - describe( 'set', () => { + describe( 'set()', () => { it( 'should set configurations when passing objects', () => { config.set( { option1: 1, @@ -158,7 +184,7 @@ describe( 'Config', () => { } ); } ); - describe( 'define', () => { + describe( 'define()', () => { it( 'should set configurations when passing objects', () => { config.set( { option1: 1, @@ -252,7 +278,7 @@ describe( 'Config', () => { } ); } ); - describe( 'get', () => { + describe( 'get()', () => { it( 'should retrieve a configuration', () => { expect( config.get( 'creator' ) ).to.equal( 'inline' ); } ); From 9bd0d4be1936030f1bb8c0cf79e10517094d525f Mon Sep 17 00:00:00 2001 From: Kamil Piechaczek Date: Thu, 2 Mar 2017 13:02:54 +0100 Subject: [PATCH 2/5] Internal: Used "Config.define" method to set default parameters in Config. --- src/config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/config.js b/src/config.js index 8c172e2..d487378 100644 --- a/src/config.js +++ b/src/config.js @@ -34,7 +34,7 @@ export default class Config { // Set default configuration. if ( defaultConfigurations ) { - this._setObjectToTarget( this._config, defaultConfigurations, true ); + this.define( defaultConfigurations ); } } From 4783f3815531cf109266d6c701be548267f3261b Mon Sep 17 00:00:00 2001 From: Kamil Piechaczek Date: Thu, 2 Mar 2017 13:23:16 +0100 Subject: [PATCH 3/5] Tests: Checking whether the Config class handles with nested objects as default parameters. --- tests/config.js | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/tests/config.js b/tests/config.js index f3189b4..deaa6ef 100644 --- a/tests/config.js +++ b/tests/config.js @@ -67,6 +67,46 @@ describe( 'Config', () => { expect( config.get( 'foo' ) ).to.equal( 10 ); expect( config.get( 'bar' ) ).to.equal( 2 ); } ); + + it( 'should work with deeper objects', () => { + const defaultConfig = { + a: { + first: 1, + second: 2 + }, + 'b.foo.first': 1, + 'b.foo.second': 2 + }; + + const parameters = { + 'a.third': 3, + b: { + foo: { + first: 3, + third: 1 + } + }, + custom: 'foo' + }; + + config = new Config( parameters, defaultConfig ); + + expect( config.get( 'a' ) ).to.deep.equal( { + first: 1, + second: 2, + third: 3 + } ); + + expect( config.get( 'b' ) ).to.have.key( 'foo' ); + + expect( config.get( 'b.foo' ) ).to.deep.equal( { + first: 3, + second: 2, + third: 1 + } ); + + expect( config.get( 'custom' ) ).to.equal( 'foo' ); + } ); } ); describe( 'set()', () => { From 2e7eab76ecaf2870bdd37ce13dfd96ac76fc462b Mon Sep 17 00:00:00 2001 From: Kamil Piechaczek Date: Thu, 2 Mar 2017 13:44:45 +0100 Subject: [PATCH 4/5] Tests: Used fully objects instead of shorter strings. --- tests/config.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/tests/config.js b/tests/config.js index deaa6ef..5963171 100644 --- a/tests/config.js +++ b/tests/config.js @@ -74,12 +74,18 @@ describe( 'Config', () => { first: 1, second: 2 }, - 'b.foo.first': 1, - 'b.foo.second': 2 + b: { + foo: { + first: 1, + second: 2, + } + } }; const parameters = { - 'a.third': 3, + a: { + third: 3 + }, b: { foo: { first: 3, From 1134922158872f05f99463430e9d057ebc6109d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotrek=20Koszuli=C5=84ski?= Date: Thu, 2 Mar 2017 13:53:32 +0100 Subject: [PATCH 5/5] API docs and made the defaults to be set first. --- src/config.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/config.js b/src/config.js index d487378..2f6138f 100644 --- a/src/config.js +++ b/src/config.js @@ -16,7 +16,8 @@ export default class Config { /** * Creates an instance of the {@link ~Config} class. * - * @param {Object} [configurations] The initial configurations to be set. + * @param {Object} [configurations] The initial configurations to be set. Usually, provided by the user. + * @param {Object} [defaultConfigurations] The default configurations. Usually, provided by the system. */ constructor( configurations, defaultConfigurations ) { /** @@ -27,15 +28,15 @@ export default class Config { */ this._config = {}; - // Set initial configuration. - if ( configurations ) { - this._setObjectToTarget( this._config, configurations ); - } - // Set default configuration. if ( defaultConfigurations ) { this.define( defaultConfigurations ); } + + // Set initial configuration. + if ( configurations ) { + this._setObjectToTarget( this._config, configurations ); + } } /**