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

This commit is contained in:
marihachi 2021-04-10 19:56:26 +09:00
parent 2d2bea6d3a
commit 10e50dd8fa
2 changed files with 5 additions and 12 deletions

View file

@ -34,20 +34,13 @@ 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)) {
if (predicate(node)) {
dest.push(node);
}
}
else {
if (type == node.type) {
dest.push(node);
}
}
});
return dest;

View file

@ -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);
});
});
});