@@ -12,21 +12,21 @@ export const fromJs = (value, uri = "", pointer = "", parent = undefined) => {
12
12
case "number" :
13
13
case "string" :
14
14
case "boolean" :
15
- return cons ( uri , pointer , value , jsType , [ ] , parent ) ;
15
+ return new JsonNode ( uri , pointer , value , jsType , [ ] , parent ) ;
16
16
case "object" :
17
17
if ( value === null ) {
18
- return cons ( uri , pointer , value , "null" , [ ] , parent ) ;
18
+ return new JsonNode ( uri , pointer , value , "null" , [ ] , parent ) ;
19
19
} else if ( Array . isArray ( value ) ) {
20
- const arrayNode = cons ( uri , pointer , value , "array" , [ ] , parent ) ;
20
+ const arrayNode = new JsonNode ( uri , pointer , value , "array" , [ ] , parent ) ;
21
21
arrayNode . children = value . map ( ( item , index ) => {
22
22
return fromJs ( item , uri , JsonPointer . append ( index , pointer ) , arrayNode ) ;
23
23
} ) ;
24
24
return arrayNode ;
25
25
} else if ( Object . getPrototypeOf ( value ) === Object . prototype ) {
26
- const objectNode = cons ( uri , pointer , value , "object" , [ ] , parent ) ;
26
+ const objectNode = new JsonNode ( uri , pointer , value , "object" , [ ] , parent ) ;
27
27
objectNode . children = Object . entries ( value ) . map ( ( entry ) => {
28
28
const propertyPointer = JsonPointer . append ( entry [ 0 ] , pointer ) ;
29
- const propertyNode = cons ( uri , propertyPointer , undefined , "property" , [ ] , objectNode ) ;
29
+ const propertyNode = new JsonNode ( uri , propertyPointer , undefined , "property" , [ ] , objectNode ) ;
30
30
propertyNode . children = entry . map ( ( property ) => fromJs ( property , uri , propertyPointer , propertyNode ) ) ;
31
31
return propertyNode ;
32
32
} ) ;
@@ -40,22 +40,20 @@ export const fromJs = (value, uri = "", pointer = "", parent = undefined) => {
40
40
}
41
41
} ;
42
42
43
- const cons = ( baseUri , pointer , value , type , children , parent ) => {
44
- const node = {
45
- baseUri : baseUri ? toAbsoluteIri ( baseUri ) : "" ,
46
- pointer : pointer ,
47
- value : value ,
48
- type : type ,
49
- children : children ,
50
- parent : parent ,
51
- valid : true ,
52
- errors : { } ,
53
- annotations : { }
54
- } ;
55
- node . root = parent ?. root ?? node ;
56
-
57
- return node ;
58
- } ;
43
+ export class JsonNode {
44
+ constructor ( baseUri , pointer , value , type , children , parent ) {
45
+ this . baseUri = baseUri ? toAbsoluteIri ( baseUri ) : "" ;
46
+ this . pointer = pointer ;
47
+ this . value = value ;
48
+ this . type = type ;
49
+ this . root = parent ?. root ?? this ;
50
+ this . children = children ;
51
+ this . parent = parent ;
52
+ this . valid = true ;
53
+ this . errors = { } ;
54
+ this . annotations = { } ;
55
+ }
56
+ }
59
57
60
58
export const get = ( uri , instance ) => {
61
59
const schemaId = toAbsoluteUri ( uri ) ;
0 commit comments