Skip to content

YAML parser that lets you write back the YAML file later without loosing the file structure (comments, field order, etc..)

Notifications You must be signed in to change notification settings

costinsin/yaml-transmute

Repository files navigation

Yaml Transmute Logo

Build Status

Yaml Transmute

If you need to change the content of a YAML file, without losing the key order or field comments, this is the right package for you.

While parsing, yaml-transmute builds a context (key order + comments) that is returned to the caller and can be later used to stringify the original object that was slightly modified.

This package is based on Eemeli's YAML npm package.

Example

import { parse, stringify, YAMLContext } from "yaml-transmute";

const [obj, ctx]: [unknown, YAMLContext] = parse(`# Comment
foo:
  bar: a # Inline comment
  baz: b
qux: 0
`);

console.log(obj); // { foo: {bar: "a", baz: "b" }, qux: 0 }

// Create a new object, similar to the original one
const newObj = { foo: { bar: 1 }, qux: true, quux: false };

// Stringify the new object, using the context gathered from `parse`
const stringifiedYaml = stringify(newObj, ctx);

console.log(stringifiedYaml);
// # Comment
// foo:
//   bar: 1 # Inline comment
// qux: true
// quux: false

About

YAML parser that lets you write back the YAML file later without loosing the file structure (comments, field order, etc..)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published