fix: strip bom from json files. Closes #94

This commit is contained in:
Kim Biesbjerg 2019-07-08 15:11:56 +02:00
parent bc2bfac7d7
commit 7d1bcd2a80
2 changed files with 8 additions and 6 deletions

View File

@ -1,7 +1,8 @@
import { CompilerInterface } from './compiler.interface'; import { CompilerInterface } from './compiler.interface';
import { TranslationCollection } from '../utils/translation.collection'; import { TranslationCollection } from '../utils/translation.collection';
import { stripBOM } from '../utils/utils';
import * as flat from 'flat'; import { flatten } from 'flat';
export class JsonCompiler implements CompilerInterface { export class JsonCompiler implements CompilerInterface {
@ -20,9 +21,9 @@ export class JsonCompiler implements CompilerInterface {
} }
public parse(contents: string): TranslationCollection { public parse(contents: string): TranslationCollection {
let values: any = JSON.parse(contents); let values: any = JSON.parse(stripBOM(contents));
if (this.isNamespacedJsonFormat(values)) { if (this.isNamespacedJsonFormat(values)) {
values = flat.flatten(values); values = flatten(values);
} }
return new TranslationCollection(values); return new TranslationCollection(values);
} }

View File

@ -1,7 +1,8 @@
import { CompilerInterface } from './compiler.interface'; import { CompilerInterface } from './compiler.interface';
import { TranslationCollection } from '../utils/translation.collection'; import { TranslationCollection } from '../utils/translation.collection';
import { stripBOM } from '../utils/utils';
import * as flat from 'flat'; import { flatten, unflatten } from 'flat';
export class NamespacedJsonCompiler implements CompilerInterface { export class NamespacedJsonCompiler implements CompilerInterface {
@ -16,14 +17,14 @@ export class NamespacedJsonCompiler implements CompilerInterface {
} }
public compile(collection: TranslationCollection): string { public compile(collection: TranslationCollection): string {
const values: {} = flat.unflatten(collection.values, { const values: {} = unflatten(collection.values, {
object: true object: true
}); });
return JSON.stringify(values, null, this.indentation); return JSON.stringify(values, null, this.indentation);
} }
public parse(contents: string): TranslationCollection { public parse(contents: string): TranslationCollection {
const values: {} = flat.flatten(JSON.parse(contents)); const values: {} = flatten(JSON.parse(stripBOM(contents)));
return new TranslationCollection(values); return new TranslationCollection(values);
} }