Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/drwpow/yaml-to-momoa
Convert a YAML file to Momoa JSON AST
https://github.com/drwpow/yaml-to-momoa
Last synced: 2 months ago
JSON representation
Convert a YAML file to Momoa JSON AST
- Host: GitHub
- URL: https://github.com/drwpow/yaml-to-momoa
- Owner: drwpow
- License: mit
- Created: 2024-08-10T16:05:53.000Z (5 months ago)
- Default Branch: main
- Last Pushed: 2024-10-24T22:36:58.000Z (2 months ago)
- Last Synced: 2024-10-26T03:42:21.648Z (2 months ago)
- Language: TypeScript
- Homepage:
- Size: 208 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# yaml-to-momoa
Convert YAML to a [Momoa JSON AST](https://www.npmjs.com/package/@humanwhocodes/momoa). Powered by [yaml](https://eemeli.org/yaml).
## Setup
```sh
npm i -D yaml-to-momoa
``````ts
import yamlToMomoa from "yaml-to-momoa";const yaml = `object:
property:
foo: bar
bar: 42`;const ast = yamlToMomoa(yaml);
console.log(ast); // DocumentNode
```You can then traverse the AST just like a Momoa node.
## FAQ
#### Why would I need this?
If you have some project using JSON and you’re using Momoa, and want to add YAML support. If you aren’t using Momoa, you don’t need this.
#### Can this convert Momoa AST to YAML AST?
No.
## Gotchas
- This preserves original locations (lines and columns) as best it can, but since YAML ↔ JSON syntax isn’t 1:1, this will result in “impossible” locations (the lines and columns won’t ever match up to any JSON source, since they came from YAML).
- Since `yaml` is doing the parsing, all YAML features are supported, but superset features have to make some concessions to map to a JSON structure. For example, references (`&[id]`) will all get expanded as if they were duplicated in the original file.
- Situations like this will lead to some interesting sourcemap locations, to the first point