Skip to content
Snippets Groups Projects
Commit 10e50dd8 authored by marihachi's avatar marihachi
Browse files

modify(api): change the function signature of extract API

parent 2d2bea6d
No related branches found
No related tags found
No related merge requests found
......@@ -34,19 +34,12 @@ export function inspect(tree: MfmNode[], action: (node: MfmNode) => void): void
}
}
export function extract(nodes: MfmNode[], type: (MfmNode['type'] | MfmNode['type'][])): MfmNode[] {
export function extract(nodes: MfmNode[], predicate: (node: MfmNode) => boolean): MfmNode[] {
const dest = [] as MfmNode[];
inspect(nodes, (node) => {
if (Array.isArray(type)) {
if (type.some(i => i == node.type)) {
dest.push(node);
}
}
else {
if (type == node.type) {
dest.push(node);
}
if (predicate(node)) {
dest.push(node);
}
});
......
......@@ -42,7 +42,7 @@ after`;
MENTION('piyo', null, '@piyo'),
MENTION('bebeyo', null, '@bebeyo')
];
assert.deepStrictEqual(mfm.extract(nodes, 'mention'), expect);
assert.deepStrictEqual(mfm.extract(nodes, node => node.type == 'mention'), expect);
});
it('nested', () => {
......@@ -52,7 +52,7 @@ after`;
EMOJI_CODE('foo'),
EMOJI_CODE('piyo')
];
assert.deepStrictEqual(mfm.extract(nodes, 'emojiCode'), expect);
assert.deepStrictEqual(mfm.extract(nodes, node => node.type == 'emojiCode'), expect);
});
});
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment