Skip to content

Commit

Permalink
Merge pull request #24 from radiovisual/flatten-json
Browse files Browse the repository at this point in the history
feat: flatten json files
  • Loading branch information
radiovisual authored Sep 20, 2024
2 parents 2b8bb48 + c698ba7 commit e28f24f
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 8 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
},
"dependencies": {
"@formatjs/icu-messageformat-parser": "^2.7.8",
"chalk": "^4.0.0"
"chalk": "^4.0.0",
"flattie": "^1.1.1"
},
"keywords": [
"cli",
Expand Down
8 changes: 3 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,16 @@ import path from "node:path";
import chalk from "chalk";

import { runRules } from "./engine/rule-engine.ts";
import { Config } from "./types.js";
import type { Config } from "./types.js";
import { config } from "./config/default-config.ts";

const defaultConfig: Config = config;

const configPath = path.join(__dirname, "../keeli.config.json");

// Only start the routine running if the configuration file is found.
// Only start keeli if the keeli configuration file is found.
if (fs.existsSync(configPath)) {
const userConfig: Partial<Config> = JSON.parse(
fs.readFileSync(configPath, "utf8")
);
const userConfig: Config = JSON.parse(fs.readFileSync(configPath, "utf8"));

const config = { ...defaultConfig, ...userConfig };

Expand Down
74 changes: 74 additions & 0 deletions src/utils/file-helpers.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import type { TranslationFiles } from "../types";
import { flatten } from "./file-helpers";

describe("flatten", () => {
it("should flatten nested JavaScript objects", () => {
const nested = {
a: "hi",
b: {
a: null,
b: ["foo", "", null, "bar"],
d: "hello",
e: {
a: "yo",
b: undefined,
c: "sup",
d: 0,
f: [
{ foo: 123, bar: 123 },
{ foo: 465, bar: 456 },
],
},
},
c: "world",
} as unknown as TranslationFiles;

const expected = {
a: "hi",
"b.b.0": "foo",
"b.b.1": "",
"b.b.3": "bar",
"b.d": "hello",
"b.e.a": "yo",
"b.e.c": "sup",
"b.e.d": 0,
"b.e.f.0.foo": 123,
"b.e.f.0.bar": 123,
"b.e.f.1.foo": 465,
"b.e.f.1.bar": 456,
c: "world",
};

expect(flatten(nested)).toEqual(expected);
});

it("should flatten a parsed nested JSON object", () => {
const nested = JSON.parse(
JSON.stringify({
some: {
deeply: {
nested: {
message: "hello",
},
},
},
another: {
super: {
deeply: {
nested: {
message: "world",
},
},
},
},
})
) as TranslationFiles;

const expected = {
"some.deeply.nested.message": "hello",
"another.super.deeply.nested.message": "world",
};

expect(flatten(nested)).toEqual(expected);
});
});
11 changes: 9 additions & 2 deletions src/utils/file-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import path from "node:path";
import fs from "node:fs";
import { Config, TranslationFiles } from "../types";
import { flattie } from "flattie";
import type { Config, TranslationFiles } from "../types";

export function flatten(obj: TranslationFiles): Record<string, string> {
return flattie(obj, ".");
}

/**
* Import all of the translation files in the current project and bundle their data into a single object.
Expand Down Expand Up @@ -31,7 +36,9 @@ export function loadLanguageFiles(config: Config): TranslationFiles {

try {
// TODO: convert to JSON before parsing if the file is not JSON. https://github.com/radiovisual/keeli/issues/2
files[locale] = JSON.parse(fs.readFileSync(translatedFilePath, "utf8"));
files[locale] = flatten(
JSON.parse(fs.readFileSync(translatedFilePath, "utf8"))
);
} catch (err: unknown) {
console.error(
`There was an error trying to read the file at path: '${translatedFilePath}' for the locale: '${locale}'. Please ensure that this is a valid translation file and try again.`
Expand Down

0 comments on commit e28f24f

Please sign in to comment.