diff --git a/src/api.ts b/src/api.ts index d6cb997..0ab23f5 100644 --- a/src/api.ts +++ b/src/api.ts @@ -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); } }); diff --git a/test/api.ts b/test/api.ts index ed5ccee..b56dec5 100644 --- a/test/api.ts +++ b/test/api.ts @@ -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); }); }); });