Skip to content

leeway-deng/js-yaml

Repository files navigation

JS-YAML - YAML 1.1 parser for JavaScript

Online Demo

This is a native port of PyYAML, the most advanced YAML parser. Now you can use all modern YAML feature right in JavaScript. Originally snapshoted version - PyYAML 3.10 (2011-05-30).

Installation

YAML module for node.js

npm install js-yaml

bundled YAML library for browser

<script src="js-yaml.min.js"></script>
<script type="text/javascript">
var yaml = require('/lib/js-yaml');
</script>

API

JS-YAML automatically registers handlers for .yml and .yaml files. You can load them just with require. That's mostly equivalent to calling loadAll() on file handler ang gathering all documents into array. Just with one string!

require('js-yaml');

// Get array of documents, or throw exception on error
var docs = require('/home/ixti/examples.yml');

console.log(docs);

If you are sure, that file has only one document, chained shift() will help to exclude array wrapper:

require('js-yaml');

// Get array of documents, or throw exception on error
var singleDoc = require('/home/ixti/examples.yml').shift();

console.log(singleDoc);

load (string|buffer|file_resource)

Parses source as single YAML document. Returns JS object or throws exception on error.

This function does NOT understands multi-doc sources, it throws exception on those.

var yaml = require('js-yaml');

// pass the string
fs.readFile('/home/ixti/example.yml', 'utf8', function (err, data) {
  if (err) {
    // handle error
    return;
  }
  try {
    console.log( yaml.load(data) );
  } catch(e) {
    console.log(e);
  }
});

loadAll (string|buffer|file_resource, iterator)

Same as Load, but understands multi-doc sources and apply iterator to each document.

var yaml = require('js-yaml');

// pass the string
fs.readFile('/home/ixti/example.yml', 'utf8', function (err, data) {
  if (err) {
    // handle error
    return;
  }

  try {
    yaml.loadAll(data, function (doc) {
      console.log(doc);
    });
  } catch(e) {
    console.log(e);
  }
});

JavaScript YAML tags scheme

The list of standard YAML tags and corresponding JavaScipt types. See also YAML Tag Discussion and Yaml Types.

!!null ''                   # null
!!bool 'yes'                # bool
!!int '3...'                # number
!!float '3.14...'           # number
!!binary '...base64...'     # buffer
!!timestamp 'YYYY-...'      # date
!!omap [ ... ]              # array of key-value pairs
!!pairs [ ... ]             # array or array pairs
!!set { ... }               # array of objects with given keys and null values
!!str '...'                 # string
!!seq [ ... ]               # array
!!map { ... }               # object

JavaScript-specific tags

!!js/regexp /pattern/gim    # RegExp
!!js/undefined ''           # Undefined

More JavaScript-specific YAML tags will be availble soon: (function).

Caveats

Note, that if you use arrays as key in JS, it's automatically converted to string. So, if you have maps as key in YAML, result will flat:

---
? - foo
  - bar
: - baz

=>

{ "foo,bar": ["baz"] }

License

View the LICENSE file (MIT).

About

JavaScript YAML parser and dumper. Very fast.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 94.2%
  • HTML 3.5%
  • Makefile 1.4%
  • Other 0.9%