Skip to content

Commit

Permalink
FxToJSONOptions add dataFilter option
Browse files Browse the repository at this point in the history
  • Loading branch information
imingyu committed Dec 28, 2020
1 parent 29fc2f1 commit 206fbf9
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 14 deletions.
6 changes: 6 additions & 0 deletions playground/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,12 @@ <h3>名词术语&事件触发时机</h3>
var parseResult = ForgivingXmlParser.parseResultToJSON(json, {
locationInfo: $("#chkLocation").prop("checked"),
steps: $("#chkSteps").prop("checked"),
// dataFilter(s, r) {
// if (r.type === "attr") {
// r.content = "哈哈";
// }
// return r;
// },
});
if (parseResult.error && parseResult.error.stack) {
console.error(parseResult.error.stack);
Expand Down
2 changes: 1 addition & 1 deletion src/node/dtd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ export const DtdParser: FxNodeAdapter = {
},
serialize(
node: FxNodeJSON,
brotherNodes: FxNodeJSON[],
siblingNodes: FxNodeJSON[],
rootNodes: FxNodeJSON[],
rootSerializer: FxNodeSerializer,
options: FxSerializeOptions,
Expand Down
2 changes: 1 addition & 1 deletion src/node/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ export const ElementParser: FxNodeAdapter = {
},
serialize(
node: FxNodeJSON,
brotherNodes: FxNodeJSON[],
siblingNodes: FxNodeJSON[],
rootNodes: FxNodeJSON[],
rootSerializer: FxNodeSerializer,
options: FxSerializeOptions
Expand Down
2 changes: 1 addition & 1 deletion src/node/pi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export const ProcessingInstructionParser: FxNodeAdapter = {
},
serialize(
node: FxNodeJSON,
brotherNodes: FxNodeJSON[],
siblingNodes: FxNodeJSON[],
rootNodes: FxNodeJSON[],
rootSerializer: FxNodeSerializer,
options: FxSerializeOptions
Expand Down
19 changes: 16 additions & 3 deletions src/parse.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import {
FxNodeJSON,
FxParseContext,
FxParseOptions,
FxParseResult,
FxParseResultJSON,
FxToJSONOptions,
FxWrong,
} from "./types";
import { pick, nodeToJSON, lxWrongToJSON, moveCursor, findNodeParser } from "./util";
import { pick, nodeToJSON, lxWrongToJSON, moveCursor, findNodeParser, isFunc } from "./util";
import { TextParser } from "./node/text";
import { DEFAULT_PARSE_OPTIONS } from "./var";

Expand Down Expand Up @@ -63,13 +65,24 @@ export const parseResultToJSON = (
): FxParseResultJSON => {
const res: FxParseResultJSON = {};
if (parseResult.error) {
res.error = lxWrongToJSON(parseResult.error);
if (options && isFunc(options.dataFilter)) {
res.error = options.dataFilter(res.error, lxWrongToJSON(parseResult.error)) as FxWrong;
} else {
res.error = lxWrongToJSON(parseResult.error);
}
}
pick("maxLine", res, parseResult, options);
pick("maxCol", res, parseResult, options);
pick("xml", res, parseResult, options);
if (!parseResult.error) {
res.nodes = parseResult.nodes.map((node) => nodeToJSON(node, options));
let hasFilter = options && isFunc(options.dataFilter);
res.nodes = parseResult.nodes.map((node) => {
if (hasFilter) {
return options.dataFilter(node, nodeToJSON(node, options)) as FxNodeJSON;
} else {
return nodeToJSON(node, options) as FxNodeJSON;
}
});
}
return res;
};
13 changes: 10 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export interface FxMessage {
export interface FxNodeSerializeHandler {
(
currentNode: FxNodeJSON,
brotherNodes: FxNodeJSON[],
siblingNodes: FxNodeJSON[],
rootNodes: FxNodeJSON[],
rootSerializer: FxNodeSerializer,
parentNode: FxNodeJSON,
Expand Down Expand Up @@ -179,7 +179,7 @@ export interface FxAttrParseCallback {
export interface FxNodeSerializeMatcher {
(
currentNode: FxNodeJSON,
brotherNodes: FxNodeJSON[],
siblingNodes: FxNodeJSON[],
rootNodes: FxNodeJSON[],
options: FxSerializeOptions,
parentNode?: FxNodeJSON
Expand Down Expand Up @@ -215,7 +215,7 @@ export interface FxNodeAdapter {
serializeMatch: FxNodeSerializeMatcher;
serialize(
currentNode: FxNodeJSON,
brotherNodes: FxNodeJSON[],
siblingNodes: FxNodeJSON[],
rootNodes: FxNodeJSON[],
rootSerializer: FxNodeSerializer,
options: FxSerializeOptions,
Expand All @@ -226,12 +226,19 @@ export interface FxNodeParserMatcher {
(xml: string, cursor: FxCursorPosition, options: FxParseOptions): boolean;
}
export type FxParseOptionsKeys = keyof FxParseOptions;
export interface FxToJSONDataFilter {
(
source: FxWrong | FxNode | FxLocation | FxTryStep,
result: FxTryStep | FxWrong | FxNodeJSON | FxLocation
): FxWrong | FxNodeJSON | FxLocation | FxTryStep;
}
export interface FxToJSONOptions {
maxLine?: boolean;
maxCol?: boolean;
xml?: boolean;
locationInfo?: boolean;
steps?: boolean;
dataFilter?: FxToJSONDataFilter;
}
export interface FxParseResultJSON {
maxLine?: number;
Expand Down
25 changes: 20 additions & 5 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
FxParseContext,
FxStartTagCompare,
FxNodeLocationInfo,
FxLocation,
} from "./types";
import { REX_SPACE } from "./var";

Expand Down Expand Up @@ -108,6 +109,7 @@ export const pick = (
};
export const nodeToJSON = (node: FxNode, options?: FxToJSONOptions): FxNodeJSON => {
const res = {} as FxNodeJSON;
const hasFilter = options && isFunc(options.dataFilter);
for (let prop in node) {
if (prop !== "parent" && prop !== "parser") {
if (prop === "steps") {
Expand Down Expand Up @@ -144,7 +146,7 @@ export const nodeToJSON = (node: FxNode, options?: FxToJSONOptions): FxNodeJSON
res.data.splice(1, 1);
}
}
return res;
return hasFilter ? (options.dataFilter(step, res) as FxTryStep) : res;
});
}
} else if (prop === "closeType") {
Expand All @@ -153,12 +155,25 @@ export const nodeToJSON = (node: FxNode, options?: FxToJSONOptions): FxNodeJSON
}
} else if (prop !== "locationInfo") {
if (prop === "attrs" || prop === "children") {
res[prop] = node[prop].map((item) => nodeToJSON(item, options));
res[prop] = node[prop].map((item) => {
if (hasFilter) {
return options.dataFilter(
item,
nodeToJSON(item, options)
) as FxNodeJSON;
}
return nodeToJSON(item, options);
});
} else {
res[prop] = node[prop];
}
} else if (options && options.locationInfo) {
res[prop] = JSON.parse(JSON.stringify(node[prop]));
res[prop] = hasFilter
? (options.dataFilter(
node[prop],
JSON.parse(JSON.stringify(node[prop]))
) as FxLocation)
: JSON.parse(JSON.stringify(node[prop]));
}
}
}
Expand Down Expand Up @@ -349,13 +364,13 @@ export const findNodeParser = (

export const findNodeSerializer = (
currentNode: FxNodeJSON,
brotherNodes: FxNodeJSON[],
siblingNodes: FxNodeJSON[],
rootNodes: FxNodeJSON[],
options: FxSerializeOptions,
parentNode?: FxNodeJSON
): FxNodeAdapter => {
return options.nodeAdapters.find((parser) => {
return parser.serializeMatch(currentNode, brotherNodes, rootNodes, options, parentNode);
return parser.serializeMatch(currentNode, siblingNodes, rootNodes, options, parentNode);
});
};

Expand Down

0 comments on commit 206fbf9

Please sign in to comment.