mirror of
https://activitypub.software/TransFem-org/sfm-js
synced 2024-11-22 05:55:13 +00:00
modify(api): change the function signature of extract API
This commit is contained in:
parent
2d2bea6d3a
commit
10e50dd8fa
2 changed files with 5 additions and 12 deletions
13
src/api.ts
13
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);
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue