Skip to content

Commit

Permalink
Try with a class
Browse files Browse the repository at this point in the history
  • Loading branch information
jdesrosiers committed May 3, 2024
1 parent f13a04d commit cad7eef
Showing 1 changed file with 19 additions and 21 deletions.
40 changes: 19 additions & 21 deletions lib/instance.js
Expand Up @@ -12,21 +12,21 @@ export const fromJs = (value, uri = "", pointer = "", parent = undefined) => {
case "number":
case "string":
case "boolean":
return cons(uri, pointer, value, jsType, [], parent);
return new JsonNode(uri, pointer, value, jsType, [], parent);
case "object":
if (value === null) {
return cons(uri, pointer, value, "null", [], parent);
return new JsonNode(uri, pointer, value, "null", [], parent);
} else if (Array.isArray(value)) {
const arrayNode = cons(uri, pointer, value, "array", [], parent);
const arrayNode = new JsonNode(uri, pointer, value, "array", [], parent);
arrayNode.children = value.map((item, index) => {
return fromJs(item, uri, JsonPointer.append(index, pointer), arrayNode);
});
return arrayNode;
} else if (Object.getPrototypeOf(value) === Object.prototype) {
const objectNode = cons(uri, pointer, value, "object", [], parent);
const objectNode = new JsonNode(uri, pointer, value, "object", [], parent);
objectNode.children = Object.entries(value).map((entry) => {
const propertyPointer = JsonPointer.append(entry[0], pointer);
const propertyNode = cons(uri, propertyPointer, undefined, "property", [], objectNode);
const propertyNode = new JsonNode(uri, propertyPointer, undefined, "property", [], objectNode);
propertyNode.children = entry.map((property) => fromJs(property, uri, propertyPointer, propertyNode));
return propertyNode;
});
Expand All @@ -40,22 +40,20 @@ export const fromJs = (value, uri = "", pointer = "", parent = undefined) => {
}
};

const cons = (baseUri, pointer, value, type, children, parent) => {
const node = {
baseUri: baseUri ? toAbsoluteIri(baseUri) : "",
pointer: pointer,
value: value,
type: type,
children: children,
parent: parent,
valid: true,
errors: {},
annotations: {}
};
node.root = parent?.root ?? node;

return node;
};
export class JsonNode {
constructor(baseUri, pointer, value, type, children, parent) {
this.baseUri = baseUri ? toAbsoluteIri(baseUri) : "";
this.pointer = pointer;
this.value = value;
this.type = type;
this.root = parent?.root ?? this;
this.children = children;
this.parent = parent;
this.valid = true;
this.errors = {};
this.annotations = {};
}
}

export const get = (uri, instance) => {
const schemaId = toAbsoluteUri(uri);
Expand Down

0 comments on commit cad7eef

Please sign in to comment.