-
Notifications
You must be signed in to change notification settings - Fork 0
Rjson
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.
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 |
Underlying mutable JSON Object (i.e. Update object hierarchy in .raw
will alter Rjson
).
Accessibility | |
---|---|
Getter | Yes |
Setter | No |
Number of keys in raw object.
Accessibility | |
---|---|
Getter | Yes |
Setter | No |
Clone underlying JSON Object via stringify-parsing (i.e. JSON.parse(string)
).
Return | Type | Description |
---|---|---|
* | Rjson |
Immutable Rjson
|
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
|
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' } } ]
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
|
const { Rjson } = require('reyaml-core');
console.log(
new Rjson({ a: { b: 'b' } })
.markLine({ lineNo: 0 })
.raw);
// { a: { '*': 'highlight', '**': { b: 'b' } } }
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
|
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' } } } }
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 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
|
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 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
|
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' }) });
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 |
-
default
- Passthrough without JSON key patching -
d3Tree
- Replace trailingsymbol.keyPostfix
in JSON key with empty space length 1
d3Tree uses the variables from config ->
marker
,markerMap
,nodeMap
,symbol.section
,symbol.keyPostfix
,size.maxStringSize
.
const { Ryaml } = require('reyaml-core');
const ry = new Ryaml('foo: bar');
console.log(ry.toRjson({ profile: 'd3Tree' }).toD3({ profile: 'd3Tree' }));
REyaml-Core Wiki page is released under the same license as repository itself.