Skip to content

Commit

Permalink
Copy raw data before replacing
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinNovak committed Oct 15, 2021
1 parent 61134b2 commit cf668a2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/linguini.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export class Linguini {
* @returns The retrieved language file item.
*/
public getRaw(location: string, langCode: string, variables?: { [name: string]: string }): any {
let data = this.langDatas[langCode].data[location];
let data = JSON.parse(JSON.stringify(this.langDatas[langCode].data[location]));
if (variables) {
data = DataUtils.replaceVariablesInObj(data, variables);
}
Expand Down
29 changes: 10 additions & 19 deletions src/utils/data-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,32 @@ export class DataUtils {
return output;
}

public static replaceVariablesInObj(input: any, variables: { [name: string]: string }): any {
let output: any;
switch (typeof input) {
public static replaceVariablesInObj(
jsonValue: any,
variables: { [name: string]: string }
): any {
switch (typeof jsonValue) {
case 'object': {
output = JSON.parse(JSON.stringify(input));
break;
}
default: {
output = input;
break;
}
}

switch (typeof output) {
case 'object': {
for (let key in output) {
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 (!output.hasOwnProperty(key)) {
if (!jsonValue.hasOwnProperty(key)) {
continue;
}
output[key] = this.replaceVariablesInObj(output[key], variables);
jsonValue[key] = this.replaceVariablesInObj(jsonValue[key], variables);
}
break;
}
case 'string': {
output = this.replaceVariables(output, variables);
jsonValue = this.replaceVariables(jsonValue, variables);
break;
}
default: {
break;
}
}

return output;
return jsonValue;
}

public static flatten<T>(input: CategoryItems<T>): { [refName: string]: T } {
Expand Down

0 comments on commit cf668a2

Please sign in to comment.