mirror of
https://activitypub.software/TransFem-org/sfm-js
synced 2024-11-25 23:45:14 +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[];
|
const dest = [] as MfmNode[];
|
||||||
|
|
||||||
inspect(nodes, (node) => {
|
inspect(nodes, (node) => {
|
||||||
if (Array.isArray(type)) {
|
if (predicate(node)) {
|
||||||
if (type.some(i => i == node.type)) {
|
dest.push(node);
|
||||||
dest.push(node);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if (type == node.type) {
|
|
||||||
dest.push(node);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -42,7 +42,7 @@ after`;
|
||||||
MENTION('piyo', null, '@piyo'),
|
MENTION('piyo', null, '@piyo'),
|
||||||
MENTION('bebeyo', null, '@bebeyo')
|
MENTION('bebeyo', null, '@bebeyo')
|
||||||
];
|
];
|
||||||
assert.deepStrictEqual(mfm.extract(nodes, 'mention'), expect);
|
assert.deepStrictEqual(mfm.extract(nodes, node => node.type == 'mention'), expect);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('nested', () => {
|
it('nested', () => {
|
||||||
|
@ -52,7 +52,7 @@ after`;
|
||||||
EMOJI_CODE('foo'),
|
EMOJI_CODE('foo'),
|
||||||
EMOJI_CODE('piyo')
|
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