1+ import YAML from 'yaml'
2+
13import { types } from 'util'
24import { DeepPartial , Merge } from '@nnode/common'
35
@@ -35,8 +37,22 @@ export class ObjectMap {
3537 Object . keys ( instance ) . forEach ( ( name ) => this . map ( this . objmap , name ) )
3638 }
3739
40+ static fromJson < T extends any > ( source : T ) : ObjectMap {
41+ return new ObjectMap ( source , { include : { functions : false } } )
42+ }
43+
44+ static fromJsonString < T extends any > ( json : string ) : ObjectMap {
45+ const source : T = JSON . parse ( json )
46+ return ObjectMap . fromJson ( source )
47+ }
48+
49+ static fromYaml < T extends any > ( yaml : string ) : ObjectMap {
50+ const source : T = YAML . parse ( yaml )
51+ return new ObjectMap ( source , { include : { dates : false } } )
52+ }
53+
3854 get nodes ( ) : ObjectMapValue [ ] {
39- return this . select ( ( objmap ) => objmap . properties . length === 0 )
55+ return this . select ( )
4056 }
4157
4258 get root ( ) : ObjectMapValue {
@@ -55,33 +71,6 @@ export class ObjectMap {
5571 return this . get ( path ) . value
5672 }
5773
58- materialze ( ) : any {
59- const conjure = ( objmap : ObjectMapValue , target : any ) : any => {
60- if ( this . include ( objmap . type ) === false ) {
61- return target
62- }
63-
64- return objmap . properties
65- . filter ( ( property ) => this . include ( property . type ) )
66- . reduce < any > ( ( result , property ) => {
67- if ( objmap . type === 'array' ) {
68- const value = result [ property . name ] || [ ]
69- result [ property . name ] = value . push ( property . value )
70- } else if ( property . type === 'object' ) {
71- result [ property . name ] = conjure ( property , { } )
72- } else {
73- result [ property . name ] = property . value
74- }
75-
76- return result
77- } , target )
78- }
79-
80- const material = { }
81- conjure ( this . root , material )
82- return material
83- }
84-
8574 paths ( ) : string [ ] {
8675 return this . select ( ) . map ( ( objmap ) => this . pathstr ( objmap ) )
8776 }
@@ -108,6 +97,15 @@ export class ObjectMap {
10897 return objmap
10998 }
11099
100+ toObject ( ) : any {
101+ return this . materialze ( )
102+ }
103+
104+ toYaml ( ) : string {
105+ const obj = this . toObject ( )
106+ return YAML . stringify ( obj )
107+ }
108+
111109 private include ( type : string ) : boolean {
112110 switch ( type ) {
113111 case 'array' :
@@ -159,6 +157,33 @@ export class ObjectMap {
159157 return objvalue
160158 }
161159
160+ private materialze ( ) : any {
161+ const conjure = ( objmap : ObjectMapValue , target : any ) : any => {
162+ if ( this . include ( objmap . type ) === false ) {
163+ return target
164+ }
165+
166+ return objmap . properties
167+ . filter ( ( property ) => this . include ( property . type ) )
168+ . reduce < any > ( ( result , property ) => {
169+ if ( objmap . type === 'array' ) {
170+ const value = result [ property . name ] || [ ]
171+ result [ property . name ] = value . push ( property . value )
172+ } else if ( property . type === 'object' ) {
173+ result [ property . name ] = conjure ( property , { } )
174+ } else {
175+ result [ property . name ] = property . value
176+ }
177+
178+ return result
179+ } , target )
180+ }
181+
182+ const material = { }
183+ conjure ( this . root , material )
184+ return material
185+ }
186+
162187 private node ( objmap : ObjectMapValue , property : string ) : ObjectMapValue {
163188 return objmap . properties . reduce ( ( result , current ) => ( current . name === property ? current : result ) , objmap )
164189 }
0 commit comments