Modules@robinpath/yaml
yaml

@robinpath/yaml

0.1.3Node.jsPublic

Parse, stringify, and manipulate YAML data

YAML

Parse, stringify, and manipulate YAML data

Package: @robinpath/yaml | Category: Utility | Type: Utility

Authentication

No authentication required. All functions are available immediately.

Use Cases

Use the yaml module when you need to:

  • Parse a YAML string into a JavaScript object, array, or value -- Use yaml.parse to perform this operation
  • Convert a JavaScript value into a YAML string -- Use yaml.stringify to perform this operation
  • Read and parse a YAML file from disk -- Use yaml.parseFile to perform this operation
  • Write a value as YAML to a file on disk -- Use yaml.writeFile to perform this operation
  • Parse a multi-document YAML string into an array of documents -- Use yaml.parseAll to perform this operation

Quick Reference

FunctionDescriptionReturns
parseParse a YAML string into a JavaScript object, array, or valueParsed JavaScript value (object, array, string, number, etc.)
stringifyConvert a JavaScript value into a YAML stringYAML formatted string
parseFileRead and parse a YAML file from diskParsed JavaScript value from the YAML file
writeFileWrite a value as YAML to a file on disktrue on successful write
parseAllParse a multi-document YAML string into an array of documentsArray of parsed documents
isValidCheck whether a string is valid YAMLtrue if the string is valid YAML, false otherwise
getParse YAML and retrieve a nested value by dot-pathThe value at the specified path, or undefined if not found
toJSONConvert a YAML string to a JSON stringJSON string representation of the YAML data
fromJSONConvert a JSON string to a YAML stringYAML string representation of the JSON data

Functions

parse

Parse a YAML string into a JavaScript object, array, or value

Module: yaml | Returns: any -- Parsed JavaScript value (object, array, string, number, etc.)

yaml.parse "name: Alice\nage: 30"
ParameterTypeRequiredDescription
yamlStringstringYesThe YAML string to parse

stringify

Convert a JavaScript value into a YAML string

Module: yaml | Returns: string -- YAML formatted string

yaml.stringify $data
ParameterTypeRequiredDescription
valueanyYesThe value to convert to YAML
indentnumberNoNumber of spaces for indentation (default: 2)

parseFile

Read and parse a YAML file from disk

Module: yaml | Returns: any -- Parsed JavaScript value from the YAML file

yaml.parseFile "config.yaml"
ParameterTypeRequiredDescription
filePathstringYesPath to the YAML file to read

writeFile

Write a value as YAML to a file on disk

Module: yaml | Returns: boolean -- true on successful write

yaml.writeFile "output.yaml" $data
ParameterTypeRequiredDescription
filePathstringYesPath to the file to write
valueanyYesThe value to serialize as YAML
indentnumberNoNumber of spaces for indentation (default: 2)

parseAll

Parse a multi-document YAML string into an array of documents

Module: yaml | Returns: array -- Array of parsed documents

yaml.parseAll "---\nname: Alice\n---\nname: Bob"
ParameterTypeRequiredDescription
yamlStringstringYesMulti-document YAML string (documents separated by ---)

isValid

Check whether a string is valid YAML

Module: yaml | Returns: boolean -- true if the string is valid YAML, false otherwise

yaml.isValid "key: value"
ParameterTypeRequiredDescription
yamlStringstringYesThe YAML string to validate

get

Parse YAML and retrieve a nested value by dot-path

Module: yaml | Returns: any -- The value at the specified path, or undefined if not found

yaml.get "database:\n  host: localhost" "database.host"
ParameterTypeRequiredDescription
yamlStringstringYesThe YAML string to parse
dotPathstringYesDot-separated path to the desired value (e.g. "database.host")

toJSON

Convert a YAML string to a JSON string

Module: yaml | Returns: string -- JSON string representation of the YAML data

yaml.toJSON "name: Alice\nage: 30"
ParameterTypeRequiredDescription
yamlStringstringYesThe YAML string to convert

fromJSON

Convert a JSON string to a YAML string

Module: yaml | Returns: string -- YAML string representation of the JSON data

yaml.fromJSON '{"name":"Alice","age":30}'
ParameterTypeRequiredDescription
jsonStringstringYesThe JSON string to convert

Error Handling

All functions throw on failure. Common errors:

ErrorCause
(standard errors)Check function parameters and authentication
@desc "Parse and validate result"
do
  set $result as yaml.parse "name: Alice\nage: 30"
  if $result != null
    print "Success"
  else
    print "No result"
  end
enddo

Recipes

1. List and iterate

Retrieve all items and loop through them.

@desc "Get and iterate results"
do
  set $result as yaml.get "database:\n  host: localhost" "database.host"
  each $item in $result
    print $item
  end
enddo

2. Multi-step YAML workflow

Chain multiple yaml operations together.

@desc "Parse, stringify, and more"
do
  set $r_parse as yaml.parse "name: Alice\nage: 30"
  set $r_stringify as yaml.stringify $data
  set $r_parseFile as yaml.parseFile "config.yaml"
  print "All operations complete"
enddo

3. Safe parse with validation

Check results before proceeding.

@desc "Parse and validate result"
do
  set $result as yaml.parse "name: Alice\nage: 30"
  if $result != null
    print "Success: " + $result
  else
    print "Operation returned no data"
  end
enddo

Related Modules

  • json -- JSON module for complementary functionality

Versions (1)

VersionTagPublished
0.1.3latest1 months ago
Install
$ robinpath add @robinpath/yaml

Collaborators

Dumitru Balaban
Dumitru Balaban
@dumitru
View all @robinpath modules
Version0.1.3
LicenseMIT
Unpacked Size4.4 KB
Versions1
Weekly Downloads21
Total Downloads21
Stars0
Last Publish1 months ago
Created1 months ago

Keywords

Category

utilities