Skip to content

Commit 8c8eff8

Browse files
committed
Added delimiter option to numberProxy.
1 parent 97d3386 commit 8c8eff8

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
- Fixed client-side validation for `Date` schema fields. ([#232](https://github.com/ciscoheat/sveltekit-superforms/issues/232))
1414
- `numberProxy` and `intProxy` now works with the `empty` option. ([#232](https://github.com/ciscoheat/sveltekit-superforms/issues/232))
1515

16+
### Added
17+
18+
- Added `delimiter` option to `numberProxy`.
19+
1620
## [1.3.0] - 2023-07-14
1721

1822
### Fixed

src/lib/client/proxies.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type DefaultOptions = {
2828
| 'datetime-local'
2929
| 'time-local'
3030
| 'iso';
31+
delimiter?: '.' | ',';
3132
empty?: 'null' | 'undefined';
3233
};
3334

@@ -72,7 +73,7 @@ export function numberProxy<
7273
>(
7374
form: Writable<T>,
7475
path: Path,
75-
options: Pick<DefaultOptions, 'empty'> = {}
76+
options: Pick<DefaultOptions, 'empty' | 'delimiter'> = {}
7677
) {
7778
return _stringProxy(form, path, 'number', {
7879
...defaultOptions,
@@ -132,25 +133,29 @@ function _stringProxy<
132133
type: Type,
133134
options: DefaultOptions
134135
): Writable<string> {
135-
function toValue(val: unknown) {
136-
if (!val && options.empty !== undefined)
136+
function toValue(value: unknown) {
137+
if (!value && options.empty !== undefined)
137138
return options.empty === 'null' ? null : undefined;
138139

139-
if (typeof val === 'number') {
140-
val = val.toString();
140+
if (typeof value === 'number') {
141+
value = value.toString();
141142
}
142143

143-
if (typeof val !== 'string') {
144+
if (typeof value !== 'string') {
144145
throw new SuperFormError('stringProxy received a non-string value.');
145146
}
146147

147-
if (type == 'string') return val;
148-
else if (type == 'boolean') return !!val;
149-
else if (type == 'date') return new Date(val);
148+
if (type == 'string') return value;
149+
else if (type == 'boolean') return !!value;
150+
else if (type == 'date') return new Date(value);
151+
152+
const numberToConvert = options.delimiter
153+
? (value as string).replace(options.delimiter, '.')
154+
: value;
150155

151156
let num: number;
152-
if (type == 'number') num = parseFloat(val);
153-
else num = parseInt(val, 10);
157+
if (type == 'number') num = parseFloat(numberToConvert);
158+
else num = parseInt(numberToConvert, 10);
154159

155160
if (options.empty !== undefined && (isNaN(num) || num == 0))
156161
return options.empty == 'null' ? null : undefined;

0 commit comments

Comments
 (0)