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 23, 2020 · 16 revisions

Overview

Ryaml is an immutable class 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 Required
raw string YAML in Javascript string Yes
config Object Environment configuration require('./config.js') No

Class Method

Ryaml.isJunkLine({ line })

Determine whenever it is a junk line according to the following rules. Junk line is defined as non-informative line.

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

Object Attribute

.raw

Underlying immutable YAML string (i.e. Update .raw will not alter Ryaml).

Accessibility
Getter Yes
Setter No

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 Required
lineNo number For getting junk line before given line number, unset to be counting all lines No
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 Required
patcher Patch For actual patching the original YAML literal string via string processing. patcher should return itself at the end of lambda function Yes
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 Required
profile string Profile name for regarding additional behaviours "default" No
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

d3Tree uses the variables from config -> blockScalarTranslation, size.tabSize, symbol.keyPostfix, wrapKeyPairScalar, appendBlockScalar.

Example

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

console.log(ry.toRjson({ profile: 'd3Tree' }).toD3({ profile: 'd3Tree' }));