Proper char count implementation

This commit is contained in:
ZXY101
2023-10-03 11:43:23 +02:00
parent 50ea6c49f5
commit aff1566e28
2 changed files with 38 additions and 42 deletions

View File

@@ -0,0 +1,35 @@
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 cleaned.length;
}
export function getCharCount(pages: Page[], currentPage?: number) {
if (pages && pages.length > 0) {
const max = currentPage || pages.length
let charCount = 0;
for (let i = 0; i < max; i++) {
const blocks = pages[i].blocks;
blocks.forEach((block) => {
block.lines.forEach((line) => {
charCount += countChars(line);
});
});
}
return charCount;
}
}