yaml
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
YAML is a comprehensive JavaScript library that provides a definitive solution for YAML, a human-friendly data serialization standard. Amongst its abilities, YAML supports both YAML 1.1 and YAML 1.2, passes all yaml-test-suite tests, can take any string as input without crashing by parsing as much YAML out of it as it can, and allows for parsing, adjusting, and writing YAML comments and blank lines. The library comes with no external dependencies and can smoothly run on Node.js as well as modern browsers.
The use of YAML is straightforward and simply requires installation followed by importing the necessary methods or the whole library. Once installed using npm install yaml
, the library can be used in your JavaScript project as follows:
To import methods parse and stringify:
import { parse, stringify } from 'yaml'
Or to import the entire library:
import YAML from 'yaml'
// or
const YAML = require('yaml')
Here are some usage examples:
Parsing YAML:
import fs from 'fs'
import YAML from 'yaml'
YAML.parse('3.14159') // Output: 3.14159
YAML.parse('[ true, false, maybe, null ]\n') // Output: [ true, false, 'maybe', null ]
const file = fs.readFileSync('./file.yml', 'utf8')
YAML.parse(file)
// Output:
// { YAML:
// [ 'A human-readable data serialization language',
// 'https://en.wikipedia.org/wiki/YAML' ],
// yaml:
// [ 'A complete JavaScript implementation',
// 'https://www.npmjs.com/package/yaml' ] }
Stringifying YAML:
import YAML from 'yaml'
YAML.stringify(3.14159) // Output: '3.14159\n'
YAML.stringify([true, false, 'maybe', null])
// Output: `- true
// - false
// - maybe
// - null
// `
YAML.stringify({ number: 3, plain: 'string', block: 'two\nlines\n' })
// Output: `number: 3
// plain: string
// block: |
// two
// lines
// `
All of the comprehensive documentation for YAML including usage examples, API overviews, Document classes, Content Nodes, and Parsing YAML can be found on the project's documentation site, eemeli.org/yaml.