You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
849 B
33 lines
849 B
class Node {
|
|
constructor(nodeDef, children) {
|
|
Object.assign(this, nodeDef);
|
|
this.children = children;
|
|
}
|
|
}
|
|
|
|
class DiffNode extends Node {
|
|
constructor(nodeDef, diffType, children) {
|
|
super(nodeDef, children);
|
|
this.diff = { type: diffType };
|
|
}
|
|
}
|
|
|
|
function isPrimitive(test) {
|
|
return test !== Object(test);
|
|
};
|
|
|
|
function toPlainObject(theClass) {
|
|
if (isPrimitive(theClass)) {
|
|
return theClass;
|
|
} else if (Array.isArray(theClass)) {
|
|
return theClass.map(item => toPlainObject(item));
|
|
} else {
|
|
const keys = Object.getOwnPropertyNames(Object.assign({}, theClass));
|
|
return keys.reduce((classAsObj, key) => {
|
|
classAsObj[key] = toPlainObject(theClass[key]);
|
|
return classAsObj;
|
|
}, {});
|
|
}
|
|
}
|
|
|
|
export { Node, DiffNode, toPlainObject }; |