Files
mokuro-reader/src/lib/util/count-chars.ts
2024-03-10 03:32:24 +02:00

39 lines
1.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { Page } from "$lib/types";
/**
* @license BSD-3-Clause
* Copyright (c) 2023, ッツ Reader Authors
* All rights reserved.
*/
// isNotJapaneseRegex aquired from ttsu reader
// https://github.com/ttu-ttu/ebook-reader/blob/main/apps/web/src/lib/functions/get-character-count.ts
export function countChars(line: string) {
const isNotJapaneseRegex = /[^0-9A-Z-------\p{Radical}\p{Unified_Ideograph}]+/gimu
const cleaned = line.replace(isNotJapaneseRegex, '')
return Array.from(cleaned).length;
}
export function getCharCount(pages: Page[], currentPage?: number) {
let charCount = 0;
let lineCount = 0;
if (pages && pages.length > 0) {
const max = currentPage || pages.length
for (let i = 0; i < max; i++) {
const blocks = pages[i].blocks;
blocks.forEach((block) => {
lineCount += block.lines.length;
block.lines.forEach((line) => {
charCount += countChars(line);
});
});
}
}
return { charCount, lineCount };
}