Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format numbers used in views #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 33 additions & 17 deletions src/stats/StatsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -368,8 +384,8 @@ export default class StatsManager {
return this.vaultStats.history[this.today].totalCitations;
}

public async getTotalPages(): Promise<number> {
if (!this.vaultStats) return await this.calcTotalPages();
return this.vaultStats.history[this.today].totalPages;
public async getTotalPages(): Promise<string> {
if (!this.vaultStats) return this.formatNumber(await this.calcTotalPages());
return this.formatNumber(this.vaultStats.history[this.today].totalPages);
}
}
7 changes: 4 additions & 3 deletions src/utils/StatUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -50,7 +51,6 @@ export function getSentenceCount(text: string): number {
/[^.!?\s][^.!?]*(?:[.!?](?!['"]?\s|$)[^.!?]*)*[.!?]?['"]?(?=\s|$)/gm
) || []
).length;

return sentences;
}

Expand All @@ -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 {
Expand Down