I am building a web interface for configuring YAML files and therefore need to parse YAML to JSON and back to YAML. The node-module yaml-js does quite a good job converting my YAML to JSON but when I convert back to YAML the files end up huge and not really human-readable as duplicates are not saved as anchors and references. Am I doing something wrong?
Here is a minimal version of what I would like to achieve:
const yaml = require("js-yaml");
const json = {
constants: {
person: {
name: "july",
id: 2
}
},
worldObject: {
name: "july",
id: 2
},
worldArray: [
{
name: "july",
id: 2
},
{
name: "july",
id: 2
}
]
};
const jsonToYaml = async () => {
const output = yaml.safeDump(json);
console.log(output);
};
jsonToYaml();
Expected output
constants:
person: &person
name: july
id: 2
worldObject: *person
worldArray:
- *person
- *person
Alternative: https://codesandbox.io/s/small-pine-0w4ze?file=/src/index.js
Huge thanks in advance! Theo