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 21, 2020 · 5 revisions

Overview

patch(yamlString) is an immutable class function to patch yamlString according to its object methods. This class is used in Ryaml.patch()

Each object methods will return a new patch(yamlString) with updated YAML string.

API

Instantiation

patch(yamlString)

Each instantiation should be carried out using ordinary function call.

Parameter Type Description Default Required
yamlString string YAML in Javascript string Yes

Object Method

.wrapKeyPair({ tabSize, trailingScalar })

Keypairs will be wrapped into 2 lines, trailed with trailingScalar for the interconnection in-between.

It will wrap from

foo: bar

to

foo: _trailingScalar_
  bar
Parameter Type Description Default Required
tabSize number Size of tab, meant to be soft tab Yes
trailingScalar string String appending after key and colon "" No
Return Type Description
* Patch Immutable patcher object

Example in Ryaml

const { Ryaml } = require('reyaml-core');

console.log(new Ryaml('foo: bar')
    .patch(p => p.wrapKeyPair({
        tabSize: 2, 
        trailingScalar: '|-' 
    }))
    .raw);

// foo: |-\n  bar\n

.appendBlockScalar({ blockScalar })

Append block scalar to YAML line with key & colon only

foo: _blockScalar_
  bar
Parameter Type Description Default Required
blockScalar string Block scalar to be appended Yes
Return Type Description
* Patch Immutable patcher object

Example in Ryaml

const { Ryaml } = require('reyaml-core');

console.log(new Ryaml('foo:\n  bar')
    .patch(p => p.appendBlockScalar({ 
        blockScalar: '|-' 
    }))
    .raw);

// foo: |-\n  bar\n

.removeEmptyLine()

Remove all empty lines matching the following rules. Empty lines are defined as lines not affecting parsing in js-yaml.

  • Line with only whitespaces
  • Comment
Return Type Description
* Patch Immutable patcher object

Example in Ryaml

const { Ryaml } = require('reyaml-core');

console.log(new Ryaml('foo:\n\n\t\n\n  bar').patch(p => p.removeEmptyLine()).raw);

// foo:\n  bar\n

.unifyBlockScalar({ blockScalarMap })

Transform all matching block scalars in blockScalarMap into destinating block scalar universally. This transformation will be applied to all lines without exception.

blockScalarMap

{
    "|-": ">-", /* Convert from literal-strip to folded-strip */
    "|+": ">+", /* Convert from literal-keep to folded-keep */
}
Parameter Type Description Default Required
blockScalarMap Object Map between transformee and transformer Yes
Return Type Description
* Patch Immutable patcher object

Example in Ryaml

const { Ryaml } = require('reyaml-core');

console.log(new Ryaml('foo: |-\n  bar')
    .patch(p => p.unifyBlockScalar({ 
        blockScalarMap: { '|-': '>-' }
    }))
    .raw);

// foo: >-\n  bar\n

.appendKey({ postfix })

Patch each key with postfix.

foo_postfix_: bar
lorem_postfix_:
  ipsum
Parameter Type Description Default Required
postfix string Postfix for each key Yes
Return Type Description
* Patch Immutable patcher object

Example in Ryaml

const { Ryaml } = require('reyaml-core');

console.log(new Ryaml('foo: bar')
    .patch(p => p.appendKey({
        postfix: '_POSTFIX_'
    }))
    .raw);

// foo_POSTFIX_: bar

.wipeSingularArray()

Delete all array in YAML with size 1.

It will change from

foo:
  - lorem: ipsum
    reyaml: core

to

foo:
    lorem: ipsum
    reyaml: core
Return Type Description
* Patch Immutable patcher object

Example in Ryaml

const { Ryaml } = require('reyaml-core');

console.log(new Ryaml('foo:\n  - lorem: ipsum\n    reyaml: core')
    .patch(p => p.wipeSingularArray())
    .raw);

// foo:\n    lorem: ipsum\n    reyaml: core

.result()

Get underlying YAML string as result.

End of .patch(p => p) in Ryaml should return p but not p.result()

Return Type Description
* string Underlying YAML string