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 · 6 revisions

Overview

Rjson is an immutable class which wraps a JSON Object as source reference.

The main usage of Rjson is to transform the underlying JSON Object into D3 Hierarchy Object using .toD3(), while features including children Object marking, traversing, modification and truncation are also available.

API

Instantiation

new Rjson(raw[, config])

Each instantiation should be carried out using new operator.

Parameter Type Description Default Required
raw Object JSON Object Yes
config Object Immutable Environment configuration require('./config.js') No

Object Attribute

.raw

Underlying mutable JSON Object (i.e. Update object hierarchy in .raw will alter Rjson).

Accessibility
Getter Yes
Setter No

.keyCount

Number of keys in raw object.

Accessibility
Getter Yes
Setter No

Object Method

.clone()

Clone underlying JSON Object via stringify-parsing (i.e. JSON.parse(string)).

Return Type Description
* Rjson Immutable Rjson

.insert({ key, insertee })

Insert "insertee" inside every "key" in object hierarchy.

Parameter Type Description Default Required
key string Key of keypair in JSON object Yes
insertee Rjson Immutable Rjson to be inserted Yes
Return Type Description
* Rjson Immutable Rjson

Example

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

console.log(
    new Rjson([
        { b: { a: 'a' } }, 
        { b: { d: 'd' } }
    ])
    .insert({ 
        key: 'b', 
        insertee: new Rjson({ c: 'c' }) 
    }).raw);

// [ { b: { a: 'a', c: 'c' } }, { b: { d: 'd', c: 'c' } } ]

.markLine({ lineNo })

This method uses the following variables from config -> marker, markerMap.highlight.

Transform raw object on active YAML line to target marked form.

Parameter Type Description Default Required
lineNo number Active YAML line number for indicating which object in raw object hierarchy to be transformed Yes
Return Type Description
* Rjson Immutable Rjson

Example

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

console.log(
    new Rjson({ a: { b: 'b' } })
    .markLine({ lineNo: 0 })
    .raw);

// { a: { '*': 'highlight', '**': { b: 'b' } } }

.truncate({ lineNo[, level][, siblingSize][, trimMark] })

This method uses the following variables from config -> marker, markerMap.truncatedUp, markerMap.truncatedDown, markerMap.truncatedLeft, markerMap.truncatedRight, symbol.sectionLeft, symbol.sectionRight.

Truncate raw object hierarchy pivoted to object with lineNo, vertically by level, horizontally by siblingSize.

Parameter Type Description Default Required
lineNo number Pivot to lineNo for trimming Yes
level number Retain N level upwards N level downwards No
siblingSize number Retain N left siblings N right siblings No
trimMark boolean Show marking of regarding trim true No
Return Type Description
* Rjson Immutable Rjson

Example on level

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

console.log(
    new Rjson({ a: { b: { c: { d: { e: 'e' } } } } })
    .truncate({ lineNo: 2, level: 1 })
    .raw);

// { b: { c: { d: { '*': 'truncatedDown' } } } }

Example on siblingSize

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

console.log(
    new Rjson([ 0, 1, 2, 3, 4 ])
    .truncate({ 
        lineNo: 5, 
        siblingSize: 1, 
        trimMark: false 
    })
    .raw);

// [ 1, 2, 3 ]

.traverse(traverser => traverser)

Traverse underlying JSON Object. Please view Traverse API for usage on traverser.

Parameter Type Description Default Required
traverser Traverse Traverse using traverse() with clone of raw object. traverser should return itself at the end of lambda function Yes
Return Type Description
* Rjson Immutable Rjson

Example

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

rj.traverse(t => t
    .toDeepestTerminal(t.constructor.to.MIDDLE)
    .then((sourceObj, name, self) => { /* ... */ })
);
// or
rj.traverse(t => { return t
    .toDeepestTerminal(t.constructor.to.MIDDLE)
    .then((sourceObj, name, self) => { /* ... */ }));
});

.modify(modifier => modifier)

Modify underlying JSON Object. Please view Modify API for usage on modifier.

Parameter Type Description Default Required
modifier Modify Modify using modify() with clone of raw object. modifier should return itself at the end of lambda function Yes
Return Type Description
* Rjson Immutable Rjson

Example

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

rj.modify(m => m.prepend({ foo: 'bar' }));
// or
rj.modify(m => { return m.prepend({ foo: 'bar' }) });

.toD3([{ [profile] }])

Convert to D3 hierarchical tree 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 JSON key patching
  • d3Tree - Replace trailing symbol.keyPostfix in JSON key with empty space length 1

d3Tree uses the variables from config -> marker, markerMap, nodeMap, symbol.section, symbol.keyPostfix, size.maxStringSize.

Example

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

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