From 473f7770a960d902930e88c9674d17b712cbd6af Mon Sep 17 00:00:00 2001 From: marihachi <marihachi0620@gmail.com> Date: Sat, 10 Apr 2021 15:43:21 +0900 Subject: [PATCH] refactor extract() --- src/api.ts | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/api.ts b/src/api.ts index 88e17e8..d6cb997 100644 --- a/src/api.ts +++ b/src/api.ts @@ -35,24 +35,20 @@ export function inspect(tree: MfmNode[], action: (node: MfmNode) => void): void } export function extract(nodes: MfmNode[], type: (MfmNode['type'] | MfmNode['type'][])): MfmNode[] { - function predicate(node: MfmNode, type: (MfmNode['type'] | MfmNode['type'][])): boolean { + const dest = [] as MfmNode[]; + + inspect(nodes, (node) => { if (Array.isArray(type)) { - return (type.some(i => i == node.type)); + if (type.some(i => i == node.type)) { + dest.push(node); + } } else { - return (type == node.type); + if (type == node.type) { + dest.push(node); + } } - } - - const dest = [] as MfmNode[]; - for (const node of nodes) { - if (predicate(node, type)) { - dest.push(node); - } - if (node.children != null) { - dest.push(...extract(node.children, type)); - } - } + }); return dest; } -- GitLab