1
0
mirror of https://github.com/hotomoe/hotomoe synced 2024-12-05 02:08:14 +09:00

Refactor getTextCount (#3553)

Co-authored-by: Acid Chicken (硫酸鶏) <root@acid-chicken.com>
This commit is contained in:
Aya Morisawa 2018-12-09 09:44:24 +09:00 committed by GitHub
parent 17baf8770a
commit 34235d4d44
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -10,15 +10,9 @@ import { toUnicode } from 'punycode';
import syntaxHighlight from '../../../../../mfm/syntax-highlight'; import syntaxHighlight from '../../../../../mfm/syntax-highlight';
function getTextCount(tokens: Node[]): number { function getTextCount(tokens: Node[]): number {
let count = 0; const rootCount = sum(tokens.filter(x => x.name === 'text').map(x => length(x.props.text)));
const extract = (tokens: Node[]) => { const childrenCount = sum(tokens.filter(x => x.children).map(x => getTextCount(x.children)));
count += sum(tokens.filter(x => x.name === 'text').map(x => length(x.props.text))); return rootCount + childrenCount;
tokens.filter(x => x.children).forEach(x => {
extract(x.children);
});
};
extract(tokens);
return count;
} }
function getChildrenCount(tokens: Node[]): number { function getChildrenCount(tokens: Node[]): number {