Skip to content

Commit

Permalink
fix issue yahoo#13 replace multiple tokens per value, if possible
Browse files Browse the repository at this point in the history
if a string value contains multiple $$tokens$$, replace them
all if they resolve to a value. todo: needs refactoring
  • Loading branch information
Isao Yagi committed May 7, 2013
1 parent dc004cb commit 3febd6d
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 3 deletions.
15 changes: 12 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
* See the accompanying LICENSE file for terms.
*/


/*jslint anon:true, node:true, nomen:true*/

'use strict';

var VERSION = '1.0.2',
DEFAULT = '*',
SEPARATOR = '/',
SUBMATCH = /\$\$([a-zA-Z0-9.-_]+)\$\$/;
SUBMATCH = /\$\$([\w.-_]+?)\$\$/,
SUBMATCHES = /\$\$([\w.-_]+?)\$\$/g;


//---------------------------------------------------------------
// UTILITY FUNCTIONS
Expand Down Expand Up @@ -64,6 +65,12 @@ function extract(bag, key, def) {
return cur;
}

function replacer(base) {
return function replaceCb(match, key) {
return extract(base, key, match);
};
}

//---------------------------------------------------------------
// OBJECT ORIENTED INTERFACE

Expand Down Expand Up @@ -254,6 +261,7 @@ Ycb.prototype = {
} else {
config[key] = '--YCB-SUBSTITUTION-ERROR--';
}

} else if (SUBMATCH.test(config[key])) {
// Test if the value is a "substitution" value
// We have a match so lets use it
Expand All @@ -276,7 +284,8 @@ Ycb.prototype = {
}
} else {
// If not it's just part of the whole value
config[key] = config[key].replace(sub[0], extract(base, find, null));
config[key] = config[key]
.replace(SUBMATCHES, replacer(base));
}
}
}
Expand Down
41 changes: 41 additions & 0 deletions tests/fixtures/subs-expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"key0": {
"key1": "value1",
"key2": "value2",
"key3": "value1",
"key4": "The value of key0.key2 is value2"
},
"key5": {
"key1": "value1",
"key2": "value2",
"key3": "value1",
"key4": "The value of key0.key2 is value2"
},
"key6": {
"key7": {
"key8": {
"key1": "value1",
"key2": "value2",
"key3": "value1",
"key4": "The value of key0.key2 is value2"
}
},
"key9": [1, 2, "The value of key0.key2 is value2", 3, 4]
},
"$$key0.key1$$": "--YCB-SUBSTITUTION-ERROR--",
"key10": {
"key11": {
"key1": "value1",
"key2": "value2",
"key3": "value1",
"key4": "The value of key0.key2 is value2"
}
},
"key11": ["a", "b", 1, 2, "The value of key0.key2 is value2", 3, 4, "c", "d"],
"key8": {
"key1": "value1",
"key2": "value2",
"key3": "value1",
"key4": "The value of key0.key2 is value2"
}
}
48 changes: 48 additions & 0 deletions tests/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,54 @@ cases = {
A.isTrue(config.key8.key4 === 'The value of key0.key2 is value2');
},

'test _applySubstitutions against ./fixtures/subs-expected.json': function() {
var config = require('./fixtures/substitutions.json'),
expected = require('./fixtures/subs-expected.json'),
ycb;

ycb = new libycb.Ycb([]);
ycb._applySubstitutions(config);
cmp(config, expected);
},


'test _applySubstitutions replaces': function() {
var ycb = new libycb.Ycb([]),
config,
expected;

config = {
key0: {
key1: 'value1',
key2: 'value2',
key3: '$$key0.key1$$',
key4: 'values of keys 1-3: $$key0.key1$$, $$key0.key2$$, $$key0.key3$$'
}
};

expected = {
key0: {
key1: 'value1',
key2: 'value2',
key3: 'value1',
key4: 'values of keys 1-3: value1, value2, value1'
}
};

ycb._applySubstitutions(config);
cmp(config, expected);
},

'test _applySubstitutions wrt ./fixtures/subs-expected.json': function() {
var config = require('./fixtures/substitutions.json'),
expected = require('./fixtures/subs-expected.json'),
ycb;

ycb = new libycb.Ycb([]);
ycb._applySubstitutions(config);
cmp(config, expected);
},


'test if we can use a simple config': function() {
var bundle, config;
Expand Down

0 comments on commit 3febd6d

Please sign in to comment.