@npmstuff/argdown-core
Version:
A pluggable parser for the Argdown argumentation syntax
163 lines • 4.83 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.stringifyArgdownData = exports.prepareForJSON = exports.jsonReplacer = void 0;
const model_1 = require("./model");
const utils_1 = require("../utils");
const prepareEquivalenceClassForJSON = (s) => {
let copy = Object.assign({}, s);
if (copy.section) {
copy.section = copy.section.id;
}
return copy;
};
const prepareStatementForJSON = (s) => {
let copy = Object.assign({}, s);
if (copy.section) {
copy.section = copy.section.id;
}
return copy;
};
const prepareArgumentForJSON = (a) => {
let copy = Object.assign({}, a);
if (copy.section) {
copy.section = copy.section.id;
}
return copy;
};
const prepareMapEdgeForJSON = (e) => {
let edge = { id: e.id, type: e.type, relationType: e.relationType };
if (e.from) {
edge.from = e.from.id;
}
if (e.to) {
edge.to = e.to.id;
}
if (e.fromEquivalenceClass) {
edge.fromEquivalenceClass = e.fromEquivalenceClass.title;
}
if (e.toEquivalenceClass) {
edge.toEquivalenceClass = e.toEquivalenceClass.title;
}
return edge;
};
const prepareMapNodeForJSON = (n) => {
let node = {
id: n.id,
title: n.title,
type: n.type,
labelTitle: n.labelTitle,
labelText: n.labelText,
tags: n.tags,
color: n.color,
fontColor: n.fontColor
};
return node;
};
const prepareGroupMapNodeForJSON = (n) => {
let node = {
id: n.id,
title: n.title,
type: n.type,
color: n.color,
fontColor: n.fontColor,
isClosed: n.isClosed,
level: n.level,
labelTitle: n.labelTitle,
labelText: n.labelText,
children: n.children,
parent: n.parent
};
return node;
};
const prepareRelationForJSON = (r) => {
let rel = {
type: r.type,
relationType: r.relationType
};
if (r.from) {
rel.from = r.from.title;
rel.fromType = r.from.type;
}
if (r.to) {
rel.to = r.to.title;
rel.toType = r.to.type;
}
if (r.to.type === model_1.ArgdownTypes.INFERENCE) {
rel.to = r.to.argumentTitle;
rel.conclusionIndex = r.to.conclusionIndex;
}
return rel;
};
const prepareSectionForJSON = (s) => {
let copy = Object.assign({}, s);
if (copy.parent) {
copy.parent = copy.parent.id;
}
if (copy.heading) {
delete copy.heading;
}
return copy;
};
const jsonReplacer = (_key, value) => {
if (value && value.type) {
switch (value.type) {
case model_1.ArgdownTypes.ARGUMENT:
return prepareArgumentForJSON(value);
case model_1.ArgdownTypes.ARGUMENT_MAP_NODE:
return prepareMapNodeForJSON(value);
case model_1.ArgdownTypes.EQUIVALENCE_CLASS:
return prepareEquivalenceClassForJSON(value);
case model_1.ArgdownTypes.GROUP_MAP_NODE:
return prepareGroupMapNodeForJSON(value);
case model_1.ArgdownTypes.INFERENCE:
return value;
case model_1.ArgdownTypes.MAP_EDGE:
return prepareMapEdgeForJSON(value);
case model_1.ArgdownTypes.RELATION:
return prepareRelationForJSON(value);
case model_1.ArgdownTypes.RULE_NODE:
return value;
case model_1.ArgdownTypes.SECTION:
return prepareSectionForJSON(value);
case model_1.ArgdownTypes.STATEMENT:
return prepareStatementForJSON(value);
case model_1.ArgdownTypes.STATEMENT_MAP_NODE:
return prepareMapNodeForJSON(value);
default:
return value;
}
}
return value;
};
exports.jsonReplacer = jsonReplacer;
const prepareForJSON = (obj) => {
if ((0, utils_1.isObject)(obj)) {
const data = (0, exports.jsonReplacer)("", obj);
for (let key of Object.keys(data)) {
data[key] = (0, exports.prepareForJSON)(data[key]);
}
return data;
}
else if (Array.isArray(obj)) {
const arr = [];
for (let e of obj) {
arr.push((0, exports.prepareForJSON)(e));
}
return arr;
}
else {
return obj;
}
};
exports.prepareForJSON = prepareForJSON;
const stringifyArgdownData = (obj, replacer, space) => {
const wrapper = (key, value) => {
if (replacer) {
return (0, exports.jsonReplacer)(key, replacer(key, value));
}
return (0, exports.jsonReplacer)(key, value);
};
return JSON.stringify(obj, wrapper, space);
};
exports.stringifyArgdownData = stringifyArgdownData;
//# sourceMappingURL=toJSON.js.map
;