@robinpath/yaml
0.1.3Node.jsPublicParse, 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.parseto perform this operation - Convert a JavaScript value into a YAML string -- Use
yaml.stringifyto perform this operation - Read and parse a YAML file from disk -- Use
yaml.parseFileto perform this operation - Write a value as YAML to a file on disk -- Use
yaml.writeFileto perform this operation - Parse a multi-document YAML string into an array of documents -- Use
yaml.parseAllto perform this operation
Quick Reference
| Function | Description | Returns |
|---|---|---|
parse | Parse a YAML string into a JavaScript object, array, or value | Parsed JavaScript value (object, array, string, number, etc.) |
stringify | Convert a JavaScript value into a YAML string | YAML formatted string |
parseFile | Read and parse a YAML file from disk | Parsed JavaScript value from the YAML file |
writeFile | Write a value as YAML to a file on disk | true on successful write |
parseAll | Parse a multi-document YAML string into an array of documents | Array of parsed documents |
isValid | Check whether a string is valid YAML | true if the string is valid YAML, false otherwise |
get | Parse YAML and retrieve a nested value by dot-path | The value at the specified path, or undefined if not found |
toJSON | Convert a YAML string to a JSON string | JSON string representation of the YAML data |
fromJSON | Convert a JSON string to a YAML string | YAML 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"
| Parameter | Type | Required | Description |
|---|---|---|---|
yamlString | string | Yes | The YAML string to parse |
stringify
Convert a JavaScript value into a YAML string
Module: yaml | Returns: string -- YAML formatted string
yaml.stringify $data
| Parameter | Type | Required | Description |
|---|---|---|---|
value | any | Yes | The value to convert to YAML |
indent | number | No | Number 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"
| Parameter | Type | Required | Description |
|---|---|---|---|
filePath | string | Yes | Path 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
| Parameter | Type | Required | Description |
|---|---|---|---|
filePath | string | Yes | Path to the file to write |
value | any | Yes | The value to serialize as YAML |
indent | number | No | Number 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"
| Parameter | Type | Required | Description |
|---|---|---|---|
yamlString | string | Yes | Multi-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"
| Parameter | Type | Required | Description |
|---|---|---|---|
yamlString | string | Yes | The 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"
| Parameter | Type | Required | Description |
|---|---|---|---|
yamlString | string | Yes | The YAML string to parse |
dotPath | string | Yes | Dot-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"
| Parameter | Type | Required | Description |
|---|---|---|---|
yamlString | string | Yes | The 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}'
| Parameter | Type | Required | Description |
|---|---|---|---|
jsonString | string | Yes | The JSON string to convert |
Error Handling
All functions throw on failure. Common errors:
| Error | Cause |
|---|---|
| (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)
| Version | Tag | Published |
|---|---|---|
| 0.1.3 | latest | 1 months ago |
Related Modules
@robinpathv0.1.4
SMTP email sending and address parsing for RobinPath
hash
JS@robinpathv0.1.3
Cryptographic hashing utilities: MD5, SHA family, HMAC, CRC32, file hashing, UUID v5 generation, secure random bytes, and content fingerprinting
csv
JS@robinpathv0.1.2
Parse and stringify CSV data
apollo
JS@robinpathv0.1.2
Apollo module for RobinPath.
$ robinpath add @robinpath/yaml
