Skip to content

Commit

Permalink
Fix replaceVariablesInObj bug
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinNovak committed Oct 13, 2021
1 parent f22522b commit b3543c5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
33 changes: 15 additions & 18 deletions src/utils/data-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,24 @@ export class DataUtils {
jsonValue: any,
variables: { [name: string]: string }
): any {
for (let key in jsonValue) {
// "for ... in" loops over all properties, including prototypes
// So we need to check if this property belong to only the object
if (!jsonValue.hasOwnProperty(key)) {
continue;
}

switch (typeof jsonValue[key]) {
case 'object': {
this.replaceVariablesInObj(jsonValue[key], variables);
break;
}
case 'string': {
jsonValue[key] = this.replaceVariables(jsonValue[key], variables);
break;
}
default: {
continue;
switch (typeof jsonValue) {
case 'object': {
for (let key in jsonValue) {
// "for ... in" loops over all properties, including prototypes
// So we need to check if this property belong to only the object
if (!jsonValue.hasOwnProperty(key)) {
continue;
}
jsonValue[key] = this.replaceVariablesInObj(jsonValue[key], variables);
}
break;
}
case 'string': {
jsonValue = this.replaceVariables(jsonValue, variables);
break;
}
}

return jsonValue;
}

Expand Down
7 changes: 7 additions & 0 deletions tests/unit/utils/data-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,12 @@ describe('DataUtils', (): void => {
'{"test":"HELLO","nested":{"test":"HELLO"},"array":["HELLO","HELLO"],"mixedArray":["HELLO",true,5],"inside":"Before HELLO After","number":5,"boolean":true,"null":null}'
);
});

it('String example', (): void => {
let myString = DataUtils.replaceVariablesInObj('Before {{MY_VARIABLE}} After', {
MY_VARIABLE: 'HELLO',
});
expect(myString).to.equal('Before HELLO After');
});
});
});

0 comments on commit b3543c5

Please sign in to comment.