Skip to content
This repository has been archived by the owner on Aug 3, 2021. It is now read-only.
Cedric Poon edited this page Jan 15, 2020 · 16 revisions

Overview

Ryaml is an immutable Object which wraps a YAML string as source reference.

The main usage of Ryaml is to transform the underlying YAML string into Rjson using .toRjson(), while literal alteration on YAML is also supported via .patch().

API

Instantiation

new Ryaml(raw[, config])

Each instantiation should be carried out using new operator.

Parameter Type Description Default Value
raw string YAML in Javascript string
config Object Environment configuration require('./config.js')

Class Method

Ryaml.isJunkLine({ line })

Determine whenever it is a junk line according to the following rules.

  • Line with only whitespaces
  • Comment
  • Array separator (i.e. ---)
Parameter Type Description Default Value
line string YAML line in Javascript string
Return Type Description
* boolean Is junk line or not

Object Method

.countJunkLine({ lineNo })

Count number of junk lines (e.g. empty line) in YAML string. Same set of rules from Ryaml.isJunkLine({ line }) is adopted.

Parameter Type Description Default Value
lineNo number For gettng junk line before given line number
Return Type Description
* number Number of junk lines before lineNo

.patch(patcher => patcher)

Patch YAML string with given patcher function. The patching behaviour is performed through Regex and string processing, which means YAML string itself will not be parsed in the using of this method. Please view Patch API for usage on patcher.

Parameter Type Description Default Value
patcher Function For actual patching the original YAML literal string via string processing. patcher should return itself at the end of lambda function
Return Type Description
* Ryaml Immutable Ryaml with new YAML string

Example

const { Ryaml } = require('reyaml-core');
const ry = new Ryaml('foo: bar');

ry.patch(p => p.removeEmptyLine().wipeSingularArray());
// or
ry.patch(p => { return p.removeEmptyLine().wipeSingularArray() });

toRjson([{ [profile] }])

Convert to Rjson object. Additional behaviours can be applied to the conversion procedure by supplying profile.

Parameter Type Description Default Value
profile string Profile name for regarding additional behaviours default
Return Type Description
* Rjson Immutable Rjson with converted JSON Object

Profile

  • default - Passthrough without YAML patching
  • d3Tree - Patch YAML exclusively for D3 Hierarchy. It will ...
    • Remove empty lines; and
    • Remove singular array; and
    • Wrap key pair to 2-liner; and
    • Append key postfix

Example

const { Ryaml } = require('reyaml-core');
const ry = new Ryaml('foo: bar');

ry.toRjson({ profile: 'd3Tree' }).toD3({ profile: 'd3Tree' });
Clone this wiki locally