diff --git a/src/stats/StatsManager.ts b/src/stats/StatsManager.ts index 3b78cc4..1886f60 100644 --- a/src/stats/StatsManager.ts +++ b/src/stats/StatsManager.ts @@ -212,7 +212,7 @@ export default class StatsManager { this.vaultStats.history[this.today].footnotes = footnotes; this.vaultStats.history[this.today].citations = citations; this.vaultStats.history[this.today].pages = pages; - this.vaultStats.history[this.today].files = this.getTotalFiles(); + this.vaultStats.history[this.today].files = this.getTotalFileCount(); await this.update(); } else { @@ -315,31 +315,47 @@ export default class StatsManager { return citations; } - public getDailyWords(): number { - return this.vaultStats.history[this.today].words; + // Removes floating point errors and adds thousands separators to a number. + private formatNumber(number: number): string { + if (typeof Intl !== 'undefined' && typeof Intl.NumberFormat === 'function') { + // Use the user's local settings if available + return Math.round(number).toLocaleString(); + } else { + // Default to 'en-US' otherwise + return Math.round(number).toLocaleString('en-US'); + } } - public getDailyCharacters(): number { - return this.vaultStats.history[this.today].characters; + + public getDailyWords(): string { + return this.formatNumber(this.vaultStats.history[this.today].words); } - public getDailySentences(): number { - return this.vaultStats.history[this.today].sentences; + public getDailyCharacters(): string { + return this.formatNumber(this.vaultStats.history[this.today].characters); } + public getDailySentences(): string { + return this.formatNumber(this.vaultStats.history[this.today].sentences); + } - public getDailyFootnotes(): number { - return this.vaultStats.history[this.today].footnotes; + + public getDailyFootnotes(): string { + return this.formatNumber(this.vaultStats.history[this.today].footnotes); } - public getDailyCitations(): number { - return this.vaultStats.history[this.today].citations; + public getDailyCitations(): string { + return this.formatNumber(this.vaultStats.history[this.today].citations); + } + public getDailyPages(): string { + return this.formatNumber(this.vaultStats.history[this.today].pages); } - public getDailyPages(): number { - return this.vaultStats.history[this.today].pages; + + public getTotalFiles(): string { + return this.formatNumber(this.vault.getMarkdownFiles().length); } - public getTotalFiles(): number { + public getTotalFileCount(): number { return this.vault.getMarkdownFiles().length; } @@ -368,8 +384,8 @@ export default class StatsManager { return this.vaultStats.history[this.today].totalCitations; } - public async getTotalPages(): Promise { - if (!this.vaultStats) return await this.calcTotalPages(); - return this.vaultStats.history[this.today].totalPages; + public async getTotalPages(): Promise { + if (!this.vaultStats) return this.formatNumber(await this.calcTotalPages()); + return this.formatNumber(this.vaultStats.history[this.today].totalPages); } } diff --git a/src/utils/StatUtils.ts b/src/utils/StatUtils.ts index 723f90a..36ea4aa 100644 --- a/src/utils/StatUtils.ts +++ b/src/utils/StatUtils.ts @@ -20,7 +20,8 @@ export function getWordCount(text: string): number { ].join("|"), "g" ); - return (text.match(pattern) || []).length; + const result = (text.match(pattern) || []).length; + return result; } export function getCharacterCount(text: string): number { @@ -50,7 +51,6 @@ export function getSentenceCount(text: string): number { /[^.!?\s][^.!?]*(?:[.!?](?!['"]?\s|$)[^.!?]*)*[.!?]?['"]?(?=\s|$)/gm ) || [] ).length; - return sentences; } @@ -59,7 +59,8 @@ export function getPageCount(text: string, pageWords: number): number { } export function getTotalFileCount(vault: Vault): number { - return vault.getMarkdownFiles().length; + const fileCount = vault.getMarkdownFiles().length; + return fileCount; } export function cleanComments(text: string): string {