Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
yaml-ast-parser
Advanced tools
[![Build Status](https://travis-ci.org/mulesoft-labs/yaml-ast-parser.svg?branch=master)](https://travis-ci.org/mulesoft-labs/yaml-ast-parser)
The yaml-ast-parser npm package is a tool used for parsing YAML documents into an abstract syntax tree (AST). This allows developers to analyze and manipulate YAML data programmatically, making it useful for tasks such as configuration file processing, data validation, and transformation.
Parsing YAML to AST
This feature allows the conversion of a YAML string into an AST structure. The code sample demonstrates how to parse a simple YAML text into an AST using yaml-ast-parser, which can then be traversed or manipulated according to the needs of the application.
const yamlAstParser = require('yaml-ast-parser');
const yamlText = 'a: 1\nb: 2\nc:\n - 3\n - 4';
const ast = yamlAstParser.load(yamlText);
console.log(ast);
Error Reporting
This feature is useful for error handling when parsing YAML. The code sample shows how to catch and report errors that occur during the parsing process, which is essential for debugging and ensuring data integrity.
const yamlAstParser = require('yaml-ast-parser');
const invalidYaml = 'a: 1\nb: \nc: 2';
try {
yamlAstParser.load(invalidYaml);
} catch (error) {
console.error('Parsing error:', error);
}
js-yaml is another popular YAML parsing library that provides similar functionality to yaml-ast-parser. It can parse YAML into a JavaScript object and also serialize JavaScript objects into YAML. Compared to yaml-ast-parser, js-yaml is often preferred for its additional features like custom types and safe loading options, which enhance security by avoiding the execution of arbitrary code.
The 'yaml' package is a modern YAML parser and stringifier, supporting YAML 1.2 and 1.1. It offers a more intuitive API and additional features like schema customization, which are not as prominent in yaml-ast-parser. This makes it a strong alternative for applications requiring advanced YAML manipulation and customization capabilities.
This is a fork of JS-YAML which supports parsing of YAML into AST.
In additional to parsing YAML to AST, it has following features:
!include
tag used in RAMLThe type information below is relevant when using TypeScript, if using from JavaScript only the field/method information is relevant.
load
method can be used to load the tree and returns a YAMLNode
.
YAMLNode
class is an ancestor for all node kinds.
It's kind
field determine node kind, one of Kind
enum:
SCALAR
, MAPPING
, MAP
, SEQ
, ANCHOR_REF
or INCLUDE_REF
.After node kind is determined, it can be cast to one of the YAMLNode
descendants types:
YAMLScalar
, YAMLMapping
, YamlMap
, YAMLSequence
or YAMLAnchorReference
.class | important members |
---|---|
YAMLNode | startPosition and endPosition provide node range. |
YAMLScalar | string value field |
YAMLMapping | YAMLScalar key and YAMLNode value fields |
YAMLSequence | YAMLNode[] items field |
YamlMap | YAMLMapping[] mappings field |
YAMLAnchorReference | string referencesAnchor and YAMLNode value |
Scalars are one of the three main node types defined by YAML and are effectively leaf nodes.
There are many factors that can influence the type of datum represent in scalar node (context, schema, tag, etc.).
To help inspection of a YAMLScalar
to determine its datatype when a document uses the Core Schema, you can pass the YAMLScalar
to the determineScalarType
function. It will return an enum value indicating null
, bool
, int
, float
, or string
.
Once you know the type, there are also some helper functions to help read the value by passing them the string, value
: parseYamlBoolean
, parseYamlFloat
, and parseYamlInteger
.
FAQs
[![Build Status](https://travis-ci.org/mulesoft-labs/yaml-ast-parser.svg?branch=master)](https://travis-ci.org/mulesoft-labs/yaml-ast-parser)
The npm package yaml-ast-parser receives a total of 3,393,698 weekly downloads. As such, yaml-ast-parser popularity was classified as popular.
We found that yaml-ast-parser demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.