What is yaml-js?
The yaml-js npm package is a JavaScript library for parsing and stringifying YAML (YAML Ain't Markup Language) data. It allows developers to easily convert YAML data to JavaScript objects and vice versa, making it useful for configuration files, data serialization, and more.
What are yaml-js's main functionalities?
Parsing YAML to JavaScript Object
This feature allows you to parse a YAML string and convert it into a JavaScript object. The `load` method is used to achieve this.
const yaml = require('yaml-js');
const yamlString = 'name: John Doe\nage: 30';
const jsObject = yaml.load(yamlString);
console.log(jsObject);
Stringifying JavaScript Object to YAML
This feature allows you to convert a JavaScript object into a YAML string. The `dump` method is used to achieve this.
const yaml = require('yaml-js');
const jsObject = { name: 'John Doe', age: 30 };
const yamlString = yaml.dump(jsObject);
console.log(yamlString);
Other packages similar to yaml-js
js-yaml
js-yaml is a popular YAML parser and dumper for JavaScript. It is widely used and has a more active community compared to yaml-js. js-yaml provides similar functionalities for parsing and stringifying YAML data, but it also includes additional features like schema support and custom types.
yaml
yaml is another YAML parser and stringifier for JavaScript. It is known for its performance and compliance with the YAML 1.2 specification. The yaml package offers a more modern API and better error handling compared to yaml-js.
yaml-js
yaml-js is currently a YAML loader, and eventually a YAML dumper, ported pretty
much line-for-line from PyYAML. The goal is to create a
reliable and specification-complete YAML processor in pure Javascript. You can
try it out here.
Current Status
Currently loading works well, and passes the
yaml-spec test suite.
If you use the library and find any bugs, don't hesitate to create an
issue.
How Do I Get It?
npm install yaml-js
How Do I Use It?
In node (CoffeeScript):
yaml = require 'yaml-js'
console.log yaml.load '''
---
phrase1:
- hello
- &world world
phrase2:
- goodbye
- *world
phrase3: >
What is up
in this place.
'''
# { phrase1: [ 'hello', 'world' ],
# phrase2: [ 'goodbye', 'world' ],
# phrase3: 'What is up in this place.' }
In the browser:
<script src='yaml.min.js'></script>
<script>
console.log(yaml.load('hello: world'));
</script>
License
WTFPL