@hanzo/ui 4.3.3 → 4.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/declare.d.ts ADDED
@@ -0,0 +1 @@
1
+ declare module 'number-abbreviate'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hanzo/ui",
3
- "version": "4.3.3",
3
+ "version": "4.3.4",
4
4
  "description": "Library that contains shared UI primitives, support for a common design system, and other boilerplate support.",
5
5
  "publishConfig": {
6
6
  "registry": "https://registry.npmjs.org/",
@@ -81,6 +81,7 @@
81
81
  "lodash.isplainobject": "^4.0.6",
82
82
  "lodash.merge": "^4.6.2",
83
83
  "markdown-to-jsx": "^7.4.7",
84
+ "number-abbreviate": "^2.0.0",
84
85
  "postcss-selector-parser": "^6.0.16",
85
86
  "react-day-picker": "^8.10.1",
86
87
  "react-intersection-observer": "^9.8.2",
@@ -0,0 +1,66 @@
1
+ import Abbr from 'number-abbreviate'
2
+
3
+ const abbr = new Abbr(['K', 'M', 'B', 'T'])
4
+
5
+ const formatToMaxChar = (
6
+ n: number | null,
7
+ maxChars: number,
8
+ /**
9
+ * Chars that will be added by ui if the number is rounded.
10
+ * For example, if the desired output for 10.15 is "~10.1",
11
+ * the tilda counts as 1 char.
12
+ */
13
+ roundingAdds: number = 1
14
+ ): {
15
+ result: string
16
+ change: 'rounded' | 'none' | 'empty'
17
+ } => {
18
+ if (n === null) {
19
+ return {
20
+ result: '',
21
+ change: 'empty'
22
+ }
23
+ }
24
+ const s = n.toString()
25
+ if (s.length > maxChars) {
26
+ // Highest number that can be rounded down to an
27
+ // acceptable string.
28
+ // Decimal point, plus one decimal place = 2 chars
29
+ const cuttoff = Math.pow(10, maxChars - 2 - roundingAdds) - 0.05
30
+ if (n < cuttoff) {
31
+ const intPortion = Math.floor(n)
32
+ const len = intPortion.toString().length
33
+ // 1 is for dec point itself
34
+ const availDecimals = maxChars - len - 1 - roundingAdds
35
+ // removes trailing zeros, if any
36
+ const roundedNumerical = parseFloat(n.toFixed(availDecimals))
37
+ return {
38
+ result: roundedNumerical.toString(),
39
+ change: 'rounded'
40
+ }
41
+ }
42
+ else {
43
+ const str = abbr.abbreviate(n, maxChars)
44
+ const numStr = str.slice(0, -1)
45
+ const abbreviation = str.slice(-1)
46
+ const numerical = parseFloat(numStr)
47
+ // minus abbr, dec point, and roundingAdds / tilda,
48
+ // (1 + 1 + roundingAdds)
49
+ // ("precision" does NOT include the decimal point itself,
50
+ // so we have to explicitly factor it in.)
51
+ const roundedString = numerical.toPrecision(maxChars - (2 + roundingAdds))
52
+ // remove trailing zeros, if any
53
+ const roundedNumerical = parseFloat(roundedString)
54
+ return {
55
+ result: roundedNumerical.toString() + abbreviation,
56
+ change: 'rounded'
57
+ }
58
+ }
59
+ }
60
+ return {
61
+ result: s,
62
+ change: 'none'
63
+ }
64
+ }
65
+
66
+ export default formatToMaxChar
package/util/index.ts CHANGED
@@ -66,6 +66,7 @@ export const capitalize = (str: string): string => (
66
66
  )
67
67
 
68
68
  export { default as spreadToTransform } from './spread-to-transform'
69
+ export { default as formatToMaxChar } from './format-to-max-char'
69
70
  // Must be imported from 'use client'
70
71
  // export * from './step-animation'
71
72