-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
37cff9a
commit 5f0632c
Showing
9 changed files
with
119 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,30 @@ | ||
import { ResultMatcher } from "../../utils/resultMatcher.js"; | ||
import { LoadingException } from "../exception.js"; | ||
import { flattenTree } from "../utils.js"; | ||
|
||
/** @type {import("../types.js").FormatHandler} */ | ||
export const JsonHandler = { | ||
fileExtensions: ["json"], | ||
load: async (filePath, content, locale) => { | ||
try { | ||
content = content.trim(); | ||
if (content.length === 0) return new Map(); | ||
const tree = JSON.parse(content); | ||
return flattenTree(tree); | ||
} catch (e) { | ||
if (!(e instanceof Error)) throw e; | ||
load: (filePath, content) => { | ||
content = content.trim(); | ||
if (content.length === 0) return new Map(); | ||
|
||
/** @param {Error} e */ | ||
const raiseLoadingException = e => { | ||
console.warn("Raising loading exception"); | ||
throw new LoadingException( | ||
`Could not parse JSON file ${filePath}: ${e.message}`, | ||
{ cause: e }, | ||
); | ||
} | ||
|
||
return new ResultMatcher(JSON.parse) | ||
.ok(flattenTree) | ||
.catch(SyntaxError, raiseLoadingException) | ||
.run(content); | ||
|
||
}, | ||
async setPath() { | ||
setPath() { | ||
throw new Error("Not implemented"); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { JsonHandler } from "./json"; | ||
import { LoadingException } from "../exception"; | ||
|
||
describe("JsonHandler", () => { | ||
it("parses a flat JSON file", () => { | ||
const json = '{ "key1": "value1", "key2": "value2" }'; | ||
const result = JsonHandler.load("test.json", json); | ||
|
||
const expected = new Map().set("key1", "value1").set("key2", "value2"); | ||
|
||
expect(result).toEqual(expected); | ||
}); | ||
|
||
it("parses an empty file as an empty dictionary", () => { | ||
const result = JsonHandler.load("test.json", ""); | ||
expect(result).toEqual(new Map()); | ||
}); | ||
|
||
it("parses a nested JSON file", () => { | ||
const json = | ||
'{ "common" : { "save": "Save", "cancel": "Cancel" }, "home": { "title": "Home" } }'; | ||
|
||
const result = JsonHandler.load("test.json", json); | ||
const expected = new Map() | ||
.set("common.save", "Save") | ||
.set("common.cancel", "Cancel") | ||
.set("home.title", "Home"); | ||
|
||
expect(result).toEqual(expected); | ||
}); | ||
|
||
it("throws LoadingException if the JSON is invalid", () => { | ||
const invalidJson = '{ "key1": "value1", "key2": "value2" '; | ||
const parseInvalid = () => { | ||
JsonHandler.load("test.json", invalidJson); | ||
}; | ||
|
||
expect(parseInvalid).toThrow(LoadingException); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,27 @@ | ||
import { load as loadYaml } from "js-yaml"; | ||
import { load as loadYaml, YAMLException } from "js-yaml"; | ||
import { flattenTree } from "../utils.js"; | ||
import { LoadingException } from "../exception.js"; | ||
import { ResultMatcher } from "../../utils/resultMatcher.js"; | ||
|
||
/** @type {import("../types.js").FormatHandler} */ | ||
export const YamlHandler = { | ||
fileExtensions: ["yaml", "yml"], | ||
load: async (filePath, content, locale) => { | ||
try { | ||
const tree = loadYaml(content, { | ||
filename: filePath, | ||
}); | ||
return flattenTree(tree); | ||
} catch (e) { | ||
if (!(e instanceof Error)) throw e; | ||
load: (filePath, content) => { | ||
|
||
/** @param {YAMLException} e */ | ||
const raiseLoadingException = (e) => { | ||
throw new LoadingException( | ||
`Could not parse YAML file ${filePath}: ${e.message}`, | ||
{ cause: e }, | ||
{ cause: e } | ||
); | ||
} | ||
|
||
return new ResultMatcher(loadYaml) | ||
.ok(flattenTree) | ||
.catch(YAMLException, raiseLoadingException) | ||
.run(content, { filename: filePath }); | ||
}, | ||
async setPath() { | ||
setPath() { | ||
throw new Error("Not implemented"); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters