lukeleppan / better-word-count

Counts the words of selected text in the editor.
MIT License
256 stars 40 forks source link

Update StatUtils.ts for number formatting #114

Open DrYoshiyahu opened 10 months ago

DrYoshiyahu commented 10 months ago

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.

StephenWeber commented 7 months ago

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

> better-word-count@0.10.0 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 commented 6 months ago

Just checking in here, I made a PR against this PR's branch https://github.com/DrYoshiyahu/better-word-count/pull/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.