Skip to content

Commit

Permalink
fmt: add humanCount()
Browse files Browse the repository at this point in the history
  • Loading branch information
cztomsik committed Mar 30, 2024
1 parent 77443b8 commit 6286d2b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
16 changes: 15 additions & 1 deletion src/app/_util/fmt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,28 @@ export const dedent = (strings: TemplateStringsArray, ...values: any[]) => {
}

/**
* Returns a human readable string
* Returns a human readable identifier
*/
export const humanize = (str: string) =>
str
.replace(/_id$/, "")
.replace(/_/g, " ")
.replace(/\b\w/g, l => l.toUpperCase())

/**
* Returns a human readable count
*/
export const humanCount = (count: number) => {
switch (true) {
case count < 1_000:
return count.toString()
case count < 1_000_000:
return `${(count / 1_000).toFixed(1)}k`
default:
return `${(count / 1_000_000).toFixed(1)}m`
}
}

/**
* Returns a human readable size
*/
Expand Down
2 changes: 1 addition & 1 deletion src/app/_util/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export { indexOf, slice } from "./array"
export { err } from "./err"
export { dedent, humanSize, humanDuration, humanize } from "./fmt"
export { dedent, humanCount, humanSize, humanDuration, humanize } from "./fmt"
export { jsonLines } from "./jsonLines"
export { basename } from "./path"
export { parseHTML } from "./parseHTML"
Expand Down

0 comments on commit 6286d2b

Please sign in to comment.