@ledgerhq/lumen-utils-shared 0.1.4 → 0.1.5

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/dist/index.js CHANGED
@@ -1,24 +1,24 @@
1
1
  import { jsx as k } from "react/jsx-runtime";
2
- import { createContext as F, useRef as W, useContext as _, useMemo as I } from "react";
3
- import { clsx as R } from "clsx";
4
- import { twMerge as w } from "tailwind-merge";
5
- function Q(t) {
2
+ import { createContext as W, useRef as F, useContext as w, useMemo as I } from "react";
3
+ import { clsx as B } from "clsx";
4
+ import { twMerge as R } from "tailwind-merge";
5
+ function G(t) {
6
6
  return t ? t.split(/[-_ ]+/).map((e) => e.charAt(0).toUpperCase() + e.slice(1).toLowerCase()).join("") : "";
7
7
  }
8
- const B = 48, N = 17, q = 2;
9
- function Y(t) {
10
- const e = t.replace(/\D/g, "").length;
11
- return Math.max(
12
- N,
13
- B - e * q
14
- );
8
+ const _ = {
9
+ md: { max: 48, min: 17, scale: 2, startAt: 0 },
10
+ sm: { max: 28, min: 12, scale: 2, startAt: 6 }
11
+ };
12
+ function J(t, e = "md") {
13
+ const { max: r, min: o, scale: s, startAt: l } = _[e], i = t.replace(/\D/g, "").length;
14
+ return Math.max(o, r - Math.max(0, i - l) * s);
15
15
  }
16
16
  function A(t, e = !0) {
17
17
  if (!t) return "";
18
18
  const r = t.includes("."), [o, s] = t.split("."), l = o.replace(/\B(?=(\d{3})+(?!\d))/g, " ");
19
19
  return r && e ? s ? `${l}.${s}` : `${l}.` : l;
20
20
  }
21
- function tt(t, e = {}) {
21
+ function Q(t, e = {}) {
22
22
  const {
23
23
  allowDecimals: r = !0,
24
24
  thousandsSeparator: o = !0,
@@ -29,34 +29,34 @@ function tt(t, e = {}) {
29
29
  if (n = n.replace(/^0+(?=\d)/, ""), !r)
30
30
  return n = n.replace(/\D/g, ""), s > 0 && n.length > s && (n = n.slice(0, s)), o ? A(n) : n;
31
31
  n === "." && (n = "0.");
32
- const a = n.indexOf(".");
33
- if (a !== -1) {
34
- let u = n.slice(0, a), f = n.slice(a + 1).replace(/\./g, "");
35
- s > 0 && u.length > s && (u = u.slice(0, s)), f = f.slice(0, l), u === "" && (u = "0");
32
+ const u = n.indexOf(".");
33
+ if (u !== -1) {
34
+ let a = n.slice(0, u), f = n.slice(u + 1).replace(/\./g, "");
35
+ s > 0 && a.length > s && (a = a.slice(0, s)), f = f.slice(0, l), a === "" && (a = "0");
36
36
  const h = i.endsWith(".") || n.endsWith(".");
37
- n = f.length > 0 ? `${u}.${f}` : h ? `${u}.` : u;
37
+ n = f.length > 0 ? `${a}.${f}` : h ? `${a}.` : a;
38
38
  } else
39
39
  s > 0 && n.length > s && (n = n.slice(0, s));
40
40
  return o ? A(n) : n;
41
41
  }
42
- function z(t, e) {
42
+ function q(t, e) {
43
43
  if (Object.is(t, e)) return !0;
44
44
  const r = t, o = e, s = Object.keys(r), l = Object.keys(o);
45
45
  return s.length !== l.length ? !1 : s.every(
46
46
  (i) => Object.prototype.hasOwnProperty.call(o, i) && Object.is(r[i], o[i])
47
47
  );
48
48
  }
49
- function K(t, e) {
50
- const r = F(e), o = ({ children: l, value: i }) => {
51
- const n = W(i);
52
- return z(n.current, i) || (n.current = i), /* @__PURE__ */ k(r.Provider, { value: n.current, children: l });
49
+ function z(t, e) {
50
+ const r = W(e), o = ({ children: l, value: i }) => {
51
+ const n = F(i);
52
+ return q(n.current, i) || (n.current = i), /* @__PURE__ */ k(r.Provider, { value: n.current, children: l });
53
53
  };
54
54
  o.displayName = t + "Provider";
55
55
  function s({
56
56
  consumerName: l,
57
57
  contextRequired: i
58
58
  }) {
59
- const n = _(r);
59
+ const n = w(r);
60
60
  if (n)
61
61
  return n;
62
62
  if (i)
@@ -67,46 +67,46 @@ function K(t, e) {
67
67
  }
68
68
  return [o, s];
69
69
  }
70
- const [et, Z] = K("Disabled", { disabled: !1 }), nt = ({
70
+ const [X, K] = z("Disabled", { disabled: !1 }), tt = ({
71
71
  consumerName: t,
72
72
  contextRequired: e,
73
73
  mergeWith: r
74
74
  }) => {
75
- const o = Z({
75
+ const o = K({
76
76
  consumerName: t,
77
77
  contextRequired: e ?? !1
78
78
  });
79
79
  return !!(r?.disabled || o.disabled);
80
80
  };
81
- function rt(...t) {
82
- return w(R(t));
81
+ function et(...t) {
82
+ return R(B(t));
83
83
  }
84
- const O = (t) => typeof t == "string" || typeof t == "number", ot = (t) => Array.isArray(t) ? t.every(O) : O(t);
85
- function C(t, e) {
84
+ const O = (t) => typeof t == "string" || typeof t == "number", nt = (t) => Array.isArray(t) ? t.every(O) : O(t);
85
+ function S(t, e) {
86
86
  if (typeof t == "function")
87
87
  return t(e);
88
88
  typeof t == "object" && t !== null && "current" in t && (t.current = e);
89
89
  }
90
- function H(...t) {
90
+ function N(...t) {
91
91
  const e = /* @__PURE__ */ new Map();
92
92
  return (r) => {
93
93
  if (r === null) {
94
94
  t.forEach((o) => {
95
95
  const s = e.get(o);
96
- typeof s == "function" && s(), C(o, null);
96
+ typeof s == "function" && s(), S(o, null);
97
97
  }), e.clear();
98
98
  return;
99
99
  }
100
100
  t.forEach((o) => {
101
- const s = C(o, r);
101
+ const s = S(o, r);
102
102
  typeof s == "function" && e.set(o, s);
103
103
  });
104
104
  };
105
105
  }
106
- function it(...t) {
107
- return I(() => H(...t), t);
106
+ function rt(...t) {
107
+ return I(() => N(...t), t);
108
108
  }
109
- const st = (t, e) => {
109
+ const ot = (t, e) => {
110
110
  let r = t;
111
111
  for (const o of e) {
112
112
  if (r == null)
@@ -114,7 +114,7 @@ const st = (t, e) => {
114
114
  r = r[o];
115
115
  }
116
116
  return r;
117
- }, ct = ({
117
+ }, it = ({
118
118
  currentStep: t,
119
119
  totalSteps: e,
120
120
  size: r,
@@ -122,11 +122,11 @@ const st = (t, e) => {
122
122
  strokeWidth: s = 4,
123
123
  arcPercentage: l = 0.75
124
124
  }) => {
125
- const i = Math.min(Math.max(t, 0), e), n = o ?? `${i}/${e}`, a = e <= 0 ? 0 : i / e, u = (r - s) / 2, f = r / 2, h = r / 2, d = 2 * Math.PI * u, g = d * l, b = g * (1 - a), p = i <= 0, D = `${g} ${d}`, P = p ? g - 2 : b;
125
+ const i = Math.min(Math.max(t, 0), e), n = o ?? `${i}/${e}`, u = e <= 0 ? 0 : i / e, a = (r - s) / 2, f = r / 2, h = r / 2, d = 2 * Math.PI * a, g = d * l, b = g * (1 - u), p = i <= 0, D = `${g} ${d}`, P = p ? g - 2 : b;
126
126
  return {
127
127
  displayLabel: n,
128
- progress: a,
129
- r: u,
128
+ progress: u,
129
+ r: a,
130
130
  cx: f,
131
131
  cy: h,
132
132
  circumference: d,
@@ -136,7 +136,7 @@ const st = (t, e) => {
136
136
  progressDashOffset: P,
137
137
  showMinimalDot: p
138
138
  };
139
- }, lt = (t) => I(
139
+ }, st = (t) => I(
140
140
  () => ({
141
141
  integerPart: Array.from(t.integerPart, (e) => ({
142
142
  value: e,
@@ -148,57 +148,57 @@ const st = (t, e) => {
148
148
  })) : []
149
149
  }),
150
150
  [t.integerPart, t.decimalPart]
151
- ), ut = (t, e, r) => {
151
+ ), ct = (t, e, r) => {
152
152
  if (e) return r;
153
153
  const o = t.decimalPart ? `${t.decimalSeparator}${t.decimalPart}` : "", s = `${t.integerPart}${o}`;
154
154
  return t.currencyPosition === "end" ? `${s} ${t.currencyText}` : `${t.currencyText} ${s}`;
155
155
  };
156
- function U(t, e, r = {}) {
156
+ function Z(t, e, r = {}) {
157
157
  const { leading: o = !1, trailing: s = !0, maxWait: l } = r;
158
- let i = null, n = null, a = null, u = 0, f = null, h = null, d;
158
+ let i = null, n = null, u = null, a = 0, f = null, h = null, d;
159
159
  const g = l !== void 0, b = g ? Math.max(l, e) : 0, p = (c) => {
160
- u = c;
161
- const m = f, x = h;
162
- return f = null, h = null, d = t.apply(x, m), d;
160
+ a = c;
161
+ const m = f, y = h;
162
+ return f = null, h = null, d = t.apply(y, m), d;
163
163
  }, D = (c) => {
164
- const m = a ? c - a : 0;
164
+ const m = u ? c - u : 0;
165
165
  return e - m;
166
166
  }, P = (c) => {
167
- const m = a ? c - a : e, x = c - u;
168
- return a === null || m >= e || m < 0 || g && x >= b;
169
- }, $ = (c) => (i = null, s && f ? p(c) : (f = null, h = null, d)), y = () => {
167
+ const m = u ? c - u : e, y = c - a;
168
+ return u === null || m >= e || m < 0 || g && y >= b;
169
+ }, $ = (c) => (i = null, s && f ? p(c) : (f = null, h = null, d)), x = () => {
170
170
  const c = Date.now();
171
171
  if (P(c)) {
172
172
  $(c);
173
173
  return;
174
174
  }
175
- i = setTimeout(y, D(c));
175
+ i = setTimeout(x, D(c));
176
176
  }, M = () => {
177
177
  const c = Date.now();
178
- n = null, f && (p(c), i !== null && (clearTimeout(i), i = setTimeout(y, e)));
179
- }, E = (c) => (u = c, i = setTimeout(y, e), g && (n = setTimeout(M, b)), o ? p(c) : d), v = () => {
180
- i !== null && clearTimeout(i), n !== null && clearTimeout(n), u = 0, f = null, h = null, a = null, i = null, n = null;
178
+ n = null, f && (p(c), i !== null && (clearTimeout(i), i = setTimeout(x, e)));
179
+ }, v = (c) => (a = c, i = setTimeout(x, e), g && (n = setTimeout(M, b)), o ? p(c) : d), E = () => {
180
+ i !== null && clearTimeout(i), n !== null && clearTimeout(n), a = 0, f = null, h = null, u = null, i = null, n = null;
181
181
  }, L = () => i === null && n === null ? d : $(Date.now()), j = () => i !== null || n !== null, T = function(...c) {
182
- const m = Date.now(), x = P(m);
183
- if (f = c, h = this, a = m, x) {
182
+ const m = Date.now(), y = P(m);
183
+ if (f = c, h = this, u = m, y) {
184
184
  if (i === null)
185
- return E(m);
185
+ return v(m);
186
186
  if (g)
187
- return clearTimeout(i), i = setTimeout(y, e), p(m);
187
+ return clearTimeout(i), i = setTimeout(x, e), p(m);
188
188
  }
189
- return i === null && (i = setTimeout(y, e)), d;
189
+ return i === null && (i = setTimeout(x, e)), d;
190
190
  };
191
- return T.cancel = v, T.flush = L, T.pending = j, T;
191
+ return T.cancel = E, T.flush = L, T.pending = j, T;
192
192
  }
193
- function at(t, e, r = {}) {
193
+ function lt(t, e, r = {}) {
194
194
  const { leading: o = !0, trailing: s = !0 } = r;
195
- return U(t, e, {
195
+ return Z(t, e, {
196
196
  leading: o,
197
197
  trailing: s,
198
198
  maxWait: e
199
199
  });
200
200
  }
201
- const ft = ({
201
+ const at = ({
202
202
  onClick: t,
203
203
  disabled: e
204
204
  }) => t ? e ? {
@@ -212,39 +212,39 @@ const ft = ({
212
212
  (o.key === "Enter" || o.key === " ") && (o.preventDefault(), t?.(o));
213
213
  },
214
214
  onClick: t
215
- } : void 0, S = " ", dt = ({
215
+ } : void 0, C = " ", ut = ({
216
216
  label: t,
217
217
  placeholder: e
218
218
  }) => {
219
219
  const r = typeof e == "string" && e.trim().length > 0 ? e : void 0;
220
220
  return t ? {
221
- inputPlaceholder: r ?? S,
221
+ inputPlaceholder: r ?? C,
222
222
  labelStaysFloatedWithPlaceholder: r !== void 0
223
223
  } : {
224
- inputPlaceholder: e ?? S,
224
+ inputPlaceholder: e ?? C,
225
225
  labelStaysFloatedWithPlaceholder: !1
226
226
  };
227
227
  };
228
228
  export {
229
- et as DisabledProvider,
230
- C as assignRef,
231
- ut as buildAriaLabel,
232
- rt as cn,
233
- K as createSafeContext,
234
- U as debounce,
229
+ X as DisabledProvider,
230
+ S as assignRef,
231
+ ct as buildAriaLabel,
232
+ et as cn,
233
+ z as createSafeContext,
234
+ Z as debounce,
235
235
  A as formatThousands,
236
- ft as getButtonA11yProps,
237
- Y as getFontSize,
238
- st as getObjectPath,
239
- ct as getStepperCalculations,
240
- ot as isTextChildren,
241
- H as mergeRefs,
242
- dt as resolveBaseInputPlaceholder,
243
- z as shallowEqual,
244
- tt as textFormatter,
245
- at as throttle,
246
- Q as toPascalCase,
247
- nt as useDisabledContext,
248
- it as useMergedRef,
249
- lt as useSplitText
236
+ at as getButtonA11yProps,
237
+ J as getFontSize,
238
+ ot as getObjectPath,
239
+ it as getStepperCalculations,
240
+ nt as isTextChildren,
241
+ N as mergeRefs,
242
+ ut as resolveBaseInputPlaceholder,
243
+ q as shallowEqual,
244
+ Q as textFormatter,
245
+ lt as throttle,
246
+ G as toPascalCase,
247
+ tt as useDisabledContext,
248
+ rt as useMergedRef,
249
+ st as useSplitText
250
250
  };
@@ -1,6 +1,24 @@
1
+ declare const FONT_SIZE_BY_SIZE: {
2
+ readonly md: {
3
+ readonly max: 48;
4
+ readonly min: 17;
5
+ readonly scale: 2;
6
+ readonly startAt: 0;
7
+ };
8
+ readonly sm: {
9
+ readonly max: 28;
10
+ readonly min: 12;
11
+ readonly scale: 2;
12
+ readonly startAt: 6;
13
+ };
14
+ };
15
+ export type AmountInputFontSizeSize = keyof typeof FONT_SIZE_BY_SIZE;
1
16
  /**
2
17
  * Calculates the font size based on the number of digits in the input value.
3
- * Scales from 48px (max) to 17px (min) as digit count increases.
18
+ * Scales from max to min as digit count increases.
19
+ * - md: 48px (heading-0-semi-bold) down to 17px, shrinks from the first digit
20
+ * - sm: 28px (heading-2-semi-bold) down to 12px, shrinks after 6 digits
4
21
  */
5
- export declare function getFontSize(value: string): number;
22
+ export declare function getFontSize(value: string, size?: AmountInputFontSizeSize): number;
23
+ export {};
6
24
  //# sourceMappingURL=fontSizeFormatter.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"fontSizeFormatter.d.ts","sourceRoot":"","sources":["../../../src/lib/inputFormatter/fontSizeFormatter.ts"],"names":[],"mappings":"AAIA;;;GAGG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAOjD"}
1
+ {"version":3,"file":"fontSizeFormatter.d.ts","sourceRoot":"","sources":["../../../src/lib/inputFormatter/fontSizeFormatter.ts"],"names":[],"mappings":"AAAA,QAAA,MAAM,iBAAiB;;;;;;;;;;;;;CAGb,CAAC;AAEX,MAAM,MAAM,uBAAuB,GAAG,MAAM,OAAO,iBAAiB,CAAC;AAErE;;;;;GAKG;AACH,wBAAgB,WAAW,CACzB,KAAK,EAAE,MAAM,EACb,IAAI,GAAE,uBAA8B,GACnC,MAAM,CAKR"}
@@ -1,4 +1,4 @@
1
- export { getFontSize } from './fontSizeFormatter';
1
+ export { getFontSize, type AmountInputFontSizeSize } from './fontSizeFormatter';
2
2
  export { formatThousands } from './formatThousands';
3
3
  export { textFormatter } from './textFormatter';
4
4
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/lib/inputFormatter/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAClD,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/lib/inputFormatter/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,KAAK,uBAAuB,EAAE,MAAM,qBAAqB,CAAC;AAChF,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ledgerhq/lumen-utils-shared",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",
@@ -0,0 +1,46 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { getFontSize } from './fontSizeFormatter';
3
+
4
+ describe('getFontSize', () => {
5
+ describe('md (default)', () => {
6
+ it('returns 48px for empty value', () => {
7
+ expect(getFontSize('')).toBe(48);
8
+ });
9
+
10
+ it('returns 46px for single digit', () => {
11
+ expect(getFontSize('5')).toBe(46);
12
+ });
13
+
14
+ it('scales down as digit count increases', () => {
15
+ expect(getFontSize('12345')).toBe(38);
16
+ expect(getFontSize('123456789')).toBe(30);
17
+ });
18
+
19
+ it('does not go below 17px', () => {
20
+ expect(getFontSize('123456789012345678')).toBe(17);
21
+ });
22
+ });
23
+
24
+ describe('sm', () => {
25
+ it('returns 28px for empty value', () => {
26
+ expect(getFontSize('', 'sm')).toBe(28);
27
+ });
28
+
29
+ it('stays at 28px for up to 6 digits (scaling starts after startAt)', () => {
30
+ expect(getFontSize('5', 'sm')).toBe(28);
31
+ expect(getFontSize('123456', 'sm')).toBe(28);
32
+ });
33
+
34
+ it('scales down after 6 digits', () => {
35
+ // 7 digits: 28 - 1 * 2 = 26
36
+ expect(getFontSize('1234567', 'sm')).toBe(26);
37
+ // 10 digits: 28 - 4 * 2 = 20
38
+ expect(getFontSize('1234567890', 'sm')).toBe(20);
39
+ });
40
+
41
+ it('does not go below 12px', () => {
42
+ // 15 digits: 28 - 9 * 2 = 10 → clamped to 12
43
+ expect(getFontSize('123456789012345', 'sm')).toBe(12);
44
+ });
45
+ });
46
+ });
@@ -1,16 +1,22 @@
1
- const MAX_FONT_SIZE = 48;
2
- const MIN_FONT_SIZE = 17;
3
- const SCALE_FACTOR = 2;
1
+ const FONT_SIZE_BY_SIZE = {
2
+ md: { max: 48, min: 17, scale: 2, startAt: 0 },
3
+ sm: { max: 28, min: 12, scale: 2, startAt: 6 },
4
+ } as const;
5
+
6
+ export type AmountInputFontSizeSize = keyof typeof FONT_SIZE_BY_SIZE;
4
7
 
5
8
  /**
6
9
  * Calculates the font size based on the number of digits in the input value.
7
- * Scales from 48px (max) to 17px (min) as digit count increases.
10
+ * Scales from max to min as digit count increases.
11
+ * - md: 48px (heading-0-semi-bold) down to 17px, shrinks from the first digit
12
+ * - sm: 28px (heading-2-semi-bold) down to 12px, shrinks after 6 digits
8
13
  */
9
- export function getFontSize(value: string): number {
14
+ export function getFontSize(
15
+ value: string,
16
+ size: AmountInputFontSizeSize = 'md',
17
+ ): number {
18
+ const { max, min, scale, startAt } = FONT_SIZE_BY_SIZE[size];
10
19
  const digits = value.replace(/\D/g, '').length;
11
- const fontSize = Math.max(
12
- MIN_FONT_SIZE,
13
- MAX_FONT_SIZE - digits * SCALE_FACTOR,
14
- );
20
+ const fontSize = Math.max(min, max - Math.max(0, digits - startAt) * scale);
15
21
  return fontSize;
16
22
  }
@@ -1,3 +1,3 @@
1
- export { getFontSize } from './fontSizeFormatter';
1
+ export { getFontSize, type AmountInputFontSizeSize } from './fontSizeFormatter';
2
2
  export { formatThousands } from './formatThousands';
3
3
  export { textFormatter } from './textFormatter';