@rango-dev/widget-embedded 0.58.1-next.5 → 0.58.1-next.6

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rango-dev/widget-embedded",
3
- "version": "0.58.1-next.5",
3
+ "version": "0.58.1-next.6",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "source": "./src/index.ts",
@@ -30,7 +30,7 @@
30
30
  "@rango-dev/queue-manager-rango-preset": "^0.60.1-next.2",
31
31
  "@rango-dev/queue-manager-react": "^0.33.0",
32
32
  "@rango-dev/signer-solana": "^0.46.2-next.0",
33
- "@rango-dev/ui": "^0.61.1-next.2",
33
+ "@rango-dev/ui": "^0.61.1-next.3",
34
34
  "@rango-dev/wallets-core": "^0.57.1-next.0",
35
35
  "@rango-dev/wallets-react": "^0.44.1-next.2",
36
36
  "@rango-dev/wallets-shared": "^0.58.1-next.2",
@@ -54,4 +54,4 @@
54
54
  "publishConfig": {
55
55
  "access": "public"
56
56
  }
57
- }
57
+ }
@@ -26,6 +26,7 @@ import {
26
26
  import { isPositiveNumber, sanitizeInputAmount } from '../utils/numbers';
27
27
  import {
28
28
  ensureLeadingZeroForDecimal,
29
+ parseNumericValue,
29
30
  removeLeadingZeros,
30
31
  } from '../utils/sanitizers';
31
32
  import { getUsdInputFrom, getUsdOutputFrom } from '../utils/swap';
@@ -249,7 +250,7 @@ const initializer: StateCreator<
249
250
  }));
250
251
  },
251
252
  setInputAmount: (amount) => {
252
- let sanitized = amount;
253
+ let sanitized = parseNumericValue(amount);
253
254
  if (!isZeroValue(amount)) {
254
255
  // sanitize once a meaningful digit is entered (e.g. "00001" → "1")
255
256
  sanitized = removeLeadingZeros(sanitized);
@@ -134,5 +134,9 @@ export function sanitizeInputAmount(amount: string): string {
134
134
  return '0';
135
135
  }
136
136
 
137
+ if (amount.endsWith('.')) {
138
+ return amount.slice(0, -1);
139
+ }
140
+
137
141
  return stripTrailingZeros(amount);
138
142
  }
@@ -4,6 +4,7 @@ import { describe, expect, test } from 'vitest';
4
4
  import {
5
5
  ensureLeadingZeroForDecimal,
6
6
  formatThousandsWithCommas,
7
+ parseNumericValue,
7
8
  removeLeadingZeros,
8
9
  replaceSpacesWithDash,
9
10
  stripTrailingZeros,
@@ -119,4 +120,46 @@ describe('check sanitization behaviors', () => {
119
120
  expect(stripTrailingZeros('1000')).toBe('1000');
120
121
  });
121
122
  });
123
+
124
+ describe('parseNumericValue', () => {
125
+ test('removes commas and keeps digits', () => {
126
+ expect(parseNumericValue('300,222')).toBe('300222');
127
+ });
128
+
129
+ test('removes letters and keeps only the first decimal', () => {
130
+ expect(parseNumericValue('12a3.4b5')).toBe('123.45');
131
+ });
132
+
133
+ test('removes extra dots and keeps the first one', () => {
134
+ expect(parseNumericValue('1.2.3.4')).toBe('1.234');
135
+ });
136
+
137
+ test('keeps a leading decimal', () => {
138
+ expect(parseNumericValue('.5')).toBe('.5');
139
+ });
140
+
141
+ test('removes symbols and letters', () => {
142
+ expect(parseNumericValue('$1,2a3.4!')).toBe('123.4');
143
+ });
144
+
145
+ test('returns empty string if no digits', () => {
146
+ expect(parseNumericValue('abc!@#')).toBe('');
147
+ });
148
+
149
+ test('handles empty string', () => {
150
+ expect(parseNumericValue('')).toBe('');
151
+ });
152
+
153
+ test('keeps only the first dot if string starts with multiple dots', () => {
154
+ expect(parseNumericValue('...123.45')).toBe('.12345');
155
+ });
156
+
157
+ test('handles string with only dots', () => {
158
+ expect(parseNumericValue('....')).toBe('.');
159
+ });
160
+
161
+ test('handles string with digits only', () => {
162
+ expect(parseNumericValue('123456')).toBe('123456');
163
+ });
164
+ });
122
165
  });
@@ -39,3 +39,29 @@ export function stripTrailingZeros(input: string): string {
39
39
  const s = input.replace(/(\.\d*?[1-9])0+$/, '$1');
40
40
  return s.replace(/\.0+$/, '');
41
41
  }
42
+
43
+ /**
44
+ * Normalize a numeric input string by removing all non-numeric characters
45
+ * except digits and a single decimal separator.
46
+ *
47
+ * This function:
48
+ * - strips letters, spaces, commas, and symbols
49
+ * - preserves digits (`0–9`)
50
+ * - keeps only the first `.` as the decimal point
51
+ *
52
+ * @example "300,222" → "300222"
53
+ * @example "12a3.4b5" → "123.45"
54
+ * @example "1.2.3.4" → "1.234"
55
+ * @example ".5" → ".5"
56
+ */
57
+ export function parseNumericValue(value: string): string {
58
+ value = value
59
+ // 1. Remove everything except digits and dots
60
+ .replace(/[^\d.]/g, '')
61
+ // 2. Keep only the first dot
62
+ .replace(/\./g, (_, offset, string) =>
63
+ string.indexOf('.') === offset ? '.' : ''
64
+ );
65
+
66
+ return value;
67
+ }