Skip to content

Commit cad7eef

Browse files
committed
Try with a class
1 parent f13a04d commit cad7eef

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

lib/instance.js

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,21 @@ export const fromJs = (value, uri = "", pointer = "", parent = undefined) => {
1212
case "number":
1313
case "string":
1414
case "boolean":
15-
return cons(uri, pointer, value, jsType, [], parent);
15+
return new JsonNode(uri, pointer, value, jsType, [], parent);
1616
case "object":
1717
if (value === null) {
18-
return cons(uri, pointer, value, "null", [], parent);
18+
return new JsonNode(uri, pointer, value, "null", [], parent);
1919
} else if (Array.isArray(value)) {
20-
const arrayNode = cons(uri, pointer, value, "array", [], parent);
20+
const arrayNode = new JsonNode(uri, pointer, value, "array", [], parent);
2121
arrayNode.children = value.map((item, index) => {
2222
return fromJs(item, uri, JsonPointer.append(index, pointer), arrayNode);
2323
});
2424
return arrayNode;
2525
} 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);
2727
objectNode.children = Object.entries(value).map((entry) => {
2828
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);
3030
propertyNode.children = entry.map((property) => fromJs(property, uri, propertyPointer, propertyNode));
3131
return propertyNode;
3232
});
@@ -40,22 +40,20 @@ export const fromJs = (value, uri = "", pointer = "", parent = undefined) => {
4040
}
4141
};
4242

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+
}
5957

6058
export const get = (uri, instance) => {
6159
const schemaId = toAbsoluteUri(uri);

0 commit comments

Comments
 (0)