What is yamljs?
The yamljs npm package is a library designed to parse YAML ('YAML Ain't Markup Language') strings and generate JavaScript objects, and vice versa. It provides an easy way to convert data between YAML, a human-friendly data serialization standard, and JavaScript objects. This can be particularly useful for configuration files, data exchange, and more, where YAML's readability and JavaScript's ubiquity are advantageous.
What are yamljs's main functionalities?
Parsing YAML to JavaScript Object
This feature allows you to convert a YAML string into a JavaScript object. It's particularly useful for reading configuration files or data represented in YAML format and using it within a JavaScript application.
const YAML = require('yamljs');
const yamlString = 'greeting: hello\nname: world';
const object = YAML.parse(yamlString);
console.log(object); // Output: { greeting: 'hello', name: 'world' }
Stringifying JavaScript Object to YAML
This feature enables the conversion of JavaScript objects into YAML strings. It's useful for creating configuration files or data exchange where YAML's human-readable format is preferred.
const YAML = require('yamljs');
const object = { greeting: 'hello', name: 'world' };
const yamlString = YAML.stringify(object, 4);
console.log(yamlString); // Output: 'greeting: hello\nname: world'
Other packages similar to yamljs
js-yaml
js-yaml is another popular YAML parsing and serialization library that offers similar functionalities to yamljs. It provides comprehensive YAML support, including the ability to customize schemas, which makes it more flexible in handling various YAML specifications compared to yamljs. js-yaml is known for its performance and extensive feature set.
yaml
The 'yaml' package is a modern YAML parser and stringifier, supporting YAML 1.2. It focuses on being highly compliant with the YAML specification and offers a simple API for parsing and stringifying. Compared to yamljs, it might offer better support for newer YAML features and specifications.
yaml.js
Standalone JavaScript YAML 1.2 Parser & Encoder. You don't need any javascript framework to use it.
Mainly inspired from Yaml Component (part of the php framework Symfony).
How to use
Import yaml.js in your html page:
<script type="text/javascript" src="yaml.js"></script>
Parse yaml string:
nativeObject = YAML.parse(yamlString);
Dump native object into yaml string:
yamlString = YAML.stringify(nativeObject[, inline /* @integer depth to start using inline notation at */ ]);
Load yaml file:
nativeObject = YAML.load('file.yml');
Load yaml file:
YAML.load('file.yml', function(result)
{
nativeObject = result;
});
Use with node.js
Install module:
npm install yamljs
Use it:
YAML = require('yamljs');
// parse YAML string
nativeObject = YAML.parse(yamlString);
// Generate YAML
yamlString = YAML.stringify(nativeObject, 4);
// Load yaml file using require
nativeObject = require('./myfile.yml');
Important
Symfony dropped support for YAML 1.1 spec. This means that yes
, no
and similar no longer convert to their boolean equivalents.
The internal Yaml().load()
and Yaml().loadFile()
methods renamed to Yaml().parse()
and Yaml().parseFile()
respectively. Exceptions replaced with YamlParseException
object.