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

Update StatUtils.ts for number formatting #114

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
26 changes: 19 additions & 7 deletions src/utils/StatUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ export function getWordCount(text: string): number {
].join("|"),
"g"
);
return (text.match(pattern) || []).length;
const result = (text.match(pattern) || []).length;
return formatNumber(result);
}

export function getCharacterCount(text: string): number {
return text.length;
return formatNumber(text.length);
}

export function getFootnoteCount(text: string): number {
Expand All @@ -34,14 +35,14 @@ export function getFootnoteCount(text: string): number {
let overallFn = 0;
if (regularFn) overallFn += regularFn.length;
if (inlineFn) overallFn += inlineFn.length;
return overallFn;
return formatNumber(overallFn);
}

export function getCitationCount(text: string): number {
const pandocCitations = text.match(/@[A-Za-z0-9-]+[,;\]](?!\()/gi);
if (!pandocCitations) return 0;
const uniqueCitations = [...new Set(pandocCitations)].length;
return uniqueCitations;
return formatNumber(uniqueCitations);
}

export function getSentenceCount(text: string): number {
Expand All @@ -50,18 +51,29 @@ export function getSentenceCount(text: string): number {
/[^.!?\s][^.!?]*(?:[.!?](?!['"]?\s|$)[^.!?]*)*[.!?]?['"]?(?=\s|$)/gm
) || []
).length;

return sentences;
return formatNumber(sentences);
}

export function getPageCount(text: string, pageWords: number): number {
return parseFloat((getWordCount(text) / pageWords).toFixed(1));
}

export function getTotalFileCount(vault: Vault): number {
return vault.getMarkdownFiles().length;
const fileCount = vault.getMarkdownFiles().length;
return formatNumber(fileCount);
}

export function cleanComments(text: string): string {
return text.replace(MATCH_COMMENT, "").replace(MATCH_HTML_COMMENT, "");
}

// Removes floating point errors and adds thousands separators to a number.
function 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');
}
}