From 34235d4d443ee29690ecd42aef8f3d9fb9b31e3e Mon Sep 17 00:00:00 2001 From: Aya Morisawa Date: Sun, 9 Dec 2018 09:44:24 +0900 Subject: [PATCH] Refactor getTextCount (#3553) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Acid Chicken (硫酸鶏) --- src/client/app/common/views/components/mfm.ts | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/client/app/common/views/components/mfm.ts b/src/client/app/common/views/components/mfm.ts index ecb22b6bc..31d5d1660 100644 --- a/src/client/app/common/views/components/mfm.ts +++ b/src/client/app/common/views/components/mfm.ts @@ -10,15 +10,9 @@ import { toUnicode } from 'punycode'; import syntaxHighlight from '../../../../../mfm/syntax-highlight'; function getTextCount(tokens: Node[]): number { - let count = 0; - const extract = (tokens: Node[]) => { - count += sum(tokens.filter(x => x.name === 'text').map(x => length(x.props.text))); - tokens.filter(x => x.children).forEach(x => { - extract(x.children); - }); - }; - extract(tokens); - return count; + const rootCount = sum(tokens.filter(x => x.name === 'text').map(x => length(x.props.text))); + const childrenCount = sum(tokens.filter(x => x.children).map(x => getTextCount(x.children))); + return rootCount + childrenCount; } function getChildrenCount(tokens: Node[]): number {