Skip to content

Commit

Permalink
fix(references): tokens with a number value should be interpolated co…
Browse files Browse the repository at this point in the history
…rrectly (#825)
  • Loading branch information
jakobe authored Jun 2, 2022
1 parent 884b1b8 commit a2f7784
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
10 changes: 10 additions & 0 deletions __tests__/__json_files/interpolation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"a" : "test1 value",
"b" : 123,
"c": "{a} text after",
"d": "text before {a}",
"e": "text before {a} text after",
"f": "{b} text after",
"g": "text before {b}",
"h": "text before {b} text after"
}
9 changes: 8 additions & 1 deletion __tests__/__json_files/not_circular.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
"prop2" : { "value": "test2 value" },
"prop3" : { "value": "{prop1.value}" },
"prop4" : { "value": "{prop3.value}" },
"prop5" : { "value": 5 },
"prop6" : { "value": 6 },
"prop7" : { "value": "{prop5.value}" },
"prop8" : { "value": "{prop7.value}" },
"prop12" : { "value": "{prop1.value}, {prop2.value} and some extra stuff" },
"prop124" : { "value": "{prop1.value}, {prop2.value} and {prop4.value}" }
"prop124" : { "value": "{prop1.value}, {prop2.value} and {prop4.value}" },
"prop15" : { "value": "{prop1.value}, {prop5.value} and some extra stuff" },
"prop156" : { "value": "{prop1.value}, {prop5.value} and {prop6.value}" },
"prop1568" : { "value": "{prop1.value}, {prop5.value}, {prop6.value} and {prop8.value}" }
}
19 changes: 18 additions & 1 deletion __tests__/utils/resolveObject.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ describe('utils', () => {
var test = resolveObject( helpers.fileToJSON(__dirname + '/../__json_files/simple.json') );
expect(test).toHaveProperty('bar', 'bar');
});

it('should do simple interpolation for both strings and numbers', () => {
var test = resolveObject( helpers.fileToJSON(__dirname + '/../__json_files/interpolation.json') );
expect(test).toHaveProperty('c', 'test1 value text after');
expect(test).toHaveProperty('d', 'text before test1 value');
expect(test).toHaveProperty('e', 'text before test1 value text after');
expect(test).toHaveProperty('f', '123 text after');
expect(test).toHaveProperty('g', 'text before 123');
expect(test).toHaveProperty('h', 'text before 123 text after');
});

it('should do nested references', () => {
var obj = helpers.fileToJSON(__dirname + '/../__json_files/nested_references.json');
Expand Down Expand Up @@ -176,8 +186,15 @@ describe('utils', () => {
prop2: { value: 'test2 value' },
prop3: { value: 'test1 value' },
prop4: { value: 'test1 value' },
prop5: { value: 5 },
prop6: { value: 6 },
prop7: { value: 5 },
prop8: { value: 5 },
prop12: { value: 'test1 value, test2 value and some extra stuff' },
prop124: { value: 'test1 value, test2 value and test1 value' }
prop124: { value: 'test1 value, test2 value and test1 value' },
prop15 : { value: 'test1 value, 5 and some extra stuff' },
prop156 : { value: 'test1 value, 5 and 6' },
prop1568 : { value: 'test1 value, 5, 6 and 5' }
}));
});

Expand Down
8 changes: 6 additions & 2 deletions lib/utils/resolveObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ function compile_value(value, stack) {
}

if (typeof ref !== 'undefined') {
if (typeof ref === 'string') {
if (typeof ref === 'string' || typeof ref === 'number') {
to_ret = value.replace(match, ref);

// Recursive, therefore we can compute multi-layer variables like a = b, b = c, eventually a = c
Expand Down Expand Up @@ -149,8 +149,12 @@ function compile_value(value, stack) {
to_ret = compile_value( to_ret, stack );
}
}
// if evaluated value is a number and equal to the reference, we want to keep the type
if (typeof ref === 'number' && ref.toString() === to_ret) {
to_ret = ref;
}
} else {
// if evaluated value is not a string, we want to keep the type
// if evaluated value is not a string or number, we want to keep the type
to_ret = ref;
}
} else {
Expand Down

0 comments on commit a2f7784

Please sign in to comment.