Skip to content

Commit

Permalink
fix(pull): replace null values with empty strings in nested keys
Browse files Browse the repository at this point in the history
fix #11
  • Loading branch information
Pegase745 committed Jul 17, 2020
1 parent 503e256 commit 2637354
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions src/models/MasterFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import { WTIProjectFile } from './types';

type Translations = {
[key: string]: string;
[key: string]: Translations | string;
};

const writeTranslations = (path: fs.PathLike, data: string) =>
Expand All @@ -23,6 +23,24 @@ const writeTranslations = (path: fs.PathLike, data: string) =>
});
});

const replaceNullValues = (data: Translations) => {
const newData: Translations = {};

Object.keys(data)
.sort()
.forEach(function (key) {
if (data[key] === null) {
newData[key] = '';
} else if (typeof data[key] === 'object') {
newData[key] = replaceNullValues(data[key] as Translations);
} else {
newData[key] = data[key];
}
});

return newData;
};

export class MasterFile {
private _masterFileId: number;

Expand Down Expand Up @@ -64,15 +82,10 @@ export class MasterFile {

const result = (await response.json()) as Translations;

const data: Translations = {};

Object.keys(result)
.sort()
.forEach(function (key) {
data[key] = result[key] === null ? '' : result[key];
});

await writeTranslations(file.name, JSON.stringify(data, null, 2));
await writeTranslations(
file.name,
JSON.stringify(replaceNullValues(result), null, 2)
);
} catch (err) {
console.error(String(err));
process.exit(1);
Expand Down

0 comments on commit 2637354

Please sign in to comment.