Install: @travetto/yaml
npm install @travetto/yaml
# or
yarn add @travetto/yaml
In the desire to provide a minimal footprint, the framework provides a minimal YAML parser/serializer to handle standard configuration structure.
YamlUtil is the main access point for this module, and will expose two method, parse
and serialize
.
Code: Simple YAML Parsing
import { YamlUtil } from '@travetto/yaml';
export function main(): void {
const obj = YamlUtil.parse(`
name: Source
age: 20
fields:
sub:
- a
- b
- c
sub2: [1,2,3]
sub3: {"k":5, "v":20}
`);
console.log(JSON.stringify(obj, null, 2));
}
Terminal: Simple YAML Parsing
$ trv main doc/parse.ts
{
"name": "Source",
"age": 20,
"fields": {
"sub": [
"a",
"b",
"c"
],
"sub2": [
1,
2,
3
],
"sub3": {
"k": 5,
"v": 20
}
}
}
Code: Simple YAML Serialization
import { YamlUtil } from '@travetto/yaml';
export function main(): void {
const text = YamlUtil.serialize({
name: 'Source',
age: 20,
fields: {
sub: [
'a',
'b',
'c'
],
sub2: [1, 2, 3],
sub3: { k: 5, v: 20 }
}
});
console.log(text);
}
Terminal: Simple YAML Serialization
$ trv main doc/serialize.ts
name: Source
age: 20
fields:
sub:
- a
- b
- c
sub2:
- 1
- 2
- 3
sub3:
k: 5
v: 20