mapped-yaml
v0.2.2
Published
A simple yaml parser providing source map
Downloads
478
Readme
mapped-yaml
A simple yaml parser providing source map as well.
npm i mapped-yaml
Usage
import { readFile } from 'fs/promises'
import { parse } from 'mapped-yaml'
async main() {
const content = await readFile('./stuff.yml', 'utf8')
const parsed = parse(content, './stuff.yml')
// 👉 access parsed contents of each node using `.object` property
assert(parsed.object['hellow'].object === 'world')
assert(parsed.object['stuff'].object[0].object === 42)
assert(parsed.object['stuff'].object[1].object['foo'].object === 'bar')
// 👉 access location of each node using `.location` property
console.log(parsed.object['stuff'].object[1].location)
// {
// file: File { ... },
// range: {
// start: { line: 4, character: 4 },
// end: { line: 4, character: 11 }
// }
// }
// 👉 get the contents of the file maybe with some surrounding lines even
console.log(
parsed.location.file.range( // --> returns the contents of the file for given range
parsed.object['stuff'].object[0].location.range, // --> the range of '- 42'
{ surrounding: 1 } // --> also return 1 surrounding line.
)
)
// {
// 2: { content: 'stuff:', surround: true },
// 3: { content: ' - 42', surround: false },
// 4: { content: ' - foo: bar', surround: true }
// }
}
# stuff.yml
hellow: world
stuff:
- 42
- foo: bar
⚠️ Line and character indexes are ZERO-BASED. ⚠️
Acknowledgement
This is a simple wrapper over yaml. If you don't need source mapping, most probably you should just use that package.