UNPKG

@travetto/yaml

Version:

Simple YAML support, provides only clean subset of yaml

109 lines (92 loc) 1.91 kB
<!-- This file was generated by @travetto/doc and should not be modified directly --> <!-- Please modify https://github.com/travetto/travetto/tree/main/module/yaml/DOC.tsx and execute "npx trv doc" to rebuild --> # YAML ## Simple YAML support, provides only clean subset of yaml **Install: @travetto/yaml** ```bash npm install @travetto/yaml # or yarn add @travetto/yaml ``` In the desire to provide a minimal footprint, the framework provides a minimal [YAML](https://en.wikipedia.org/wiki/YAML) parser/serializer to handle standard configuration structure. [YamlUtil](https://github.com/travetto/travetto/tree/main/module/yaml/src/util.ts#L7) is the main access point for this module, and will expose two method, `parse` and `serialize`. **Code: Simple YAML Parsing** ```typescript 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** ```bash $ 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** ```typescript 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** ```bash $ trv main doc/serialize.ts name: Source age: 20 fields: sub: - a - b - c sub2: - 1 - 2 - 3 sub3: k: 5 v: 20 ```