From e6a6d63df030bf9b0e64fb6d3926639d82c62c08 Mon Sep 17 00:00:00 2001 From: Luciano Colosio Date: Tue, 1 Mar 2016 10:07:53 +0400 Subject: [PATCH] Enhancing params resolution: Now you can reference the same parameter multiple, allowing for more elastic string composition. --- reconfig.js | 4 +++- test.js | 18 ++++++++++++++++++ test/reconfig.js | 14 ++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 test.js diff --git a/reconfig.js b/reconfig.js index 20c21e7..98583dc 100644 --- a/reconfig.js +++ b/reconfig.js @@ -95,7 +95,9 @@ Reconfig.prototype.resolveReferences = function(value) { */ Reconfig.prototype.resolveParameters = function(value, parameters) { for (var property in parameters) { - value = (value) ? value.replace(':' + property, (parameters[property] || '')) : value; + if (value) { + value = value.replace(RegExp(':' + property, 'g'), (parameters[property] || '')); + } } return value; diff --git a/test.js b/test.js new file mode 100644 index 0000000..ee39a85 --- /dev/null +++ b/test.js @@ -0,0 +1,18 @@ +var rec = require('./reconfig.js'); +var c = { + root: { + a: 'valA', + b: { + c: '{{ root.a }}', + d: 'valD:paramD' + }, + e: 'valE:paramE', + f: 'valeF {{root.b.d}}' + } +}; + +var conf = new rec(c); + +console.log('conf: ', conf); +console.log('conf paramD: ', conf.get('root', {paramD: 'ddddddddddd'})); +console.log('conf app params: ', conf.get('root', {paramD: 'ddddddddddd', paramE: 'eeeeeeeeeeeeeeee'})); diff --git a/test/reconfig.js b/test/reconfig.js index 916202e..7252e3e 100644 --- a/test/reconfig.js +++ b/test/reconfig.js @@ -125,6 +125,20 @@ describe('Reconfig', function() { })); }); + it('should be able to render parmas in composite values', function() { + var values = { + a: { + b: 'hello :what!' + }, + c: '{{ a.b }}, :what' + }; + var config = new reconfig(values); + + assert('hello world!, world' === config.get('c', { + what: 'world' + })); + }); + it('should be able to handle self-referencing configs', function() { var values = { a: {