What is @stoplight/yaml?
@stoplight/yaml is a versatile npm package that provides utilities for parsing, stringifying, and processing YAML data. It is particularly useful for developers working with YAML configurations, API specifications, and other structured data formats.
What are @stoplight/yaml's main functionalities?
Parsing YAML to JavaScript Object
This feature allows you to convert a YAML string into a JavaScript object. The `parse` function takes a YAML string as input and returns the corresponding JavaScript object.
const { parse } = require('@stoplight/yaml');
const yamlString = 'key: value\nlist:\n - item1\n - item2';
const jsObject = parse(yamlString);
console.log(jsObject);
Stringifying JavaScript Object to YAML
This feature allows you to convert a JavaScript object into a YAML string. The `stringify` function takes a JavaScript object as input and returns the corresponding YAML string.
const { stringify } = require('@stoplight/yaml');
const jsObject = { key: 'value', list: ['item1', 'item2'] };
const yamlString = stringify(jsObject);
console.log(yamlString);
Handling YAML Errors
This feature demonstrates how to handle errors that may occur during YAML parsing. The `parse` function will throw an error if the input YAML string is invalid, which can be caught and handled appropriately.
const { parse } = require('@stoplight/yaml');
try {
const jsObject = parse('invalid: yaml: string');
} catch (error) {
console.error('YAML parsing error:', error.message);
}
Other packages similar to @stoplight/yaml
js-yaml
js-yaml is a popular YAML parser and dumper for JavaScript. It provides similar functionalities to @stoplight/yaml, such as parsing YAML strings to JavaScript objects and stringifying JavaScript objects to YAML. It is widely used and well-documented, making it a strong alternative.
yaml
yaml is another robust YAML parser and stringifier for JavaScript. It offers a comprehensive set of features, including support for custom tags and schema definitions. It is known for its performance and flexibility, making it a good choice for complex YAML processing tasks.
yamljs
yamljs is a lightweight YAML parser and stringifier for JavaScript. It provides basic functionalities for converting between YAML strings and JavaScript objects. While it may not have as many features as some other packages, it is easy to use and suitable for simple YAML processing needs.
@stoplight/yaml
Useful functions when working with YAML.
Installation
Supported in modern browsers and node.
yarn add @stoplight/yaml
Usage
import { parseWithPointers } from "@stoplight/yaml";
const result = parseWithPointers("foo: bar");
console.log(result.data);
console.log(result.pointers);
- parseWithPointers: Like
JSON.parse(val)
but also returns a source map that includes a JSON path pointer for every property in the result (with line information).
Contributing
- Clone repo.
- Create / checkout
feature/{name}
, chore/{name}
, or fix/{name}
branch. - Install deps:
yarn
. - Make your changes.
- Run tests:
yarn test.prod
. - Stage relevant files to git.
- Commit:
yarn commit
. NOTE: Commits that don't follow the conventional format will be rejected. yarn commit
creates this format for you, or you can put it together manually and then do a regular git commit
. - Push:
git push
. - Open PR targeting the
develop
branch.