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

Conversation

DrYoshiyahu
Copy link

@DrYoshiyahu DrYoshiyahu commented Nov 20, 2023

I wrote a new function (formatNumber) that removes floating point errors and adds thousands separators to a number, and applied this function to the results of most numeric variables sent to StatusBar.ts

At the very least, this hopefully resolves #103 and resolves #111

FYI: I haven't tested this change myself in Obsidian, so I may have missed something.

Wrote a new function (formatNumber) that removes floating point errors and adds thousands separators to a number, and applied this function to the results of most numeric variables sent to StatusBar.ts
@StephenWeber
Copy link

I like this approach, as it centralizes the formatting and does it consistently. I also 100% love that it uses the local number formats, sad I didn't think about that.

The types don't agree in StatUtils, though, all the StatUtils functions return a number and formatNumber returns a string. Other code uses these, such as calcTotalPages here https://github.com/lukeleppan/better-word-count/blob/master/src/stats/StatsManager.ts#L286

I'll try this function in StatsManager, that seems the last place where the calculations are consumed, stored, and provided to other things for display. I might make all those functions of the form public async getXXX(): Promise<number> return string instead, to see the result.

❯ npm run build

> [email protected] build /Users/sweber/workspace/better-word-count
> rollup --config rollup.config.js

[rollup-plugin-svelte] Forcing `"compilerOptions.css": false` because "emitCss" was truthy.

src/main.ts → dist/...
(!) Plugin typescript: @rollup/plugin-typescript TS2322: Type 'string' is not assignable to type 'number'.
src/utils/StatUtils.ts: (24:3)

24   return formatNumber(result);
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

src/utils/StatUtils.ts: (28:3)

28   return formatNumber(text.length);
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

src/utils/StatUtils.ts: (38:3)

38   return formatNumber(overallFn);
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

src/utils/StatUtils.ts: (45:3)

45   return formatNumber(uniqueCitations);
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

src/utils/StatUtils.ts: (54:3)

54   return formatNumber(sentences);
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

@StephenWeber
Copy link

Just checking in here, I made a PR against this PR's branch DrYoshiyahu#1 and I've been using that version of BWC for a few months now and am happy with the results for my use case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature Request] Ability to format numbers to show thousand separators Only display rounded statistics
2 participants