@@ -30,41 +30,45 @@ type DefaultOptions = {
3030 | 'iso' ;
3131 delimiter ?: '.' | ',' ;
3232 empty ?: 'null' | 'undefined' ;
33+ emptyIfZero ?: boolean ;
3334} ;
3435
3536const defaultOptions : DefaultOptions = {
3637 trueStringValue : 'true' ,
37- dateFormat : 'iso'
38+ dateFormat : 'iso' ,
39+ emptyIfZero : true
3840} ;
3941
40- export function intProxy <
42+ ///// Proxy functions ///////////////////////////////////////////////
43+
44+ export function booleanProxy <
4145 T extends Record < string , unknown > ,
4246 Path extends FormPath < T >
4347> (
4448 form : Writable < T > ,
4549 path : Path ,
46- options : Pick < DefaultOptions , 'empty' > = { }
50+ options : Pick < DefaultOptions , 'trueStringValue' > = {
51+ trueStringValue : 'true'
52+ }
4753) {
48- return _stringProxy ( form , path , 'int ' , {
54+ return _stringProxy ( form , path , 'boolean ' , {
4955 ...defaultOptions ,
5056 ...options
51- } ) as FormPathType < T , Path > extends number ? Writable < string > : never ;
57+ } ) as FormPathType < T , Path > extends boolean ? Writable < string > : never ;
5258}
5359
54- export function booleanProxy <
60+ export function intProxy <
5561 T extends Record < string , unknown > ,
5662 Path extends FormPath < T >
5763> (
5864 form : Writable < T > ,
5965 path : Path ,
60- options : Pick < DefaultOptions , 'trueStringValue' > = {
61- trueStringValue : 'true'
62- }
66+ options : Pick < DefaultOptions , 'empty' | 'emptyIfZero' > = { }
6367) {
64- return _stringProxy ( form , path , 'boolean ' , {
68+ return _stringProxy ( form , path , 'int ' , {
6569 ...defaultOptions ,
6670 ...options
67- } ) as FormPathType < T , Path > extends boolean ? Writable < string > : never ;
71+ } ) as FormPathType < T , Path > extends number ? Writable < string > : never ;
6872}
6973
7074export function numberProxy <
@@ -73,7 +77,7 @@ export function numberProxy<
7377> (
7478 form : Writable < T > ,
7579 path : Path ,
76- options : Pick < DefaultOptions , 'empty' | 'delimiter' > = { }
80+ options : Pick < DefaultOptions , 'empty' | 'emptyIfZero' | ' delimiter'> = { }
7781) {
7882 return _stringProxy ( form , path , 'number' , {
7983 ...defaultOptions ,
@@ -117,6 +121,8 @@ export function stringProxy<
117121 } ) as FormPathType < T , Path > extends string ? Writable < string > : never ;
118122}
119123
124+ ///// Implementation ////////////////////////////////////////////////
125+
120126/**
121127 * Creates a string store that will pass its value to a field in the form.
122128 * @param form The form
@@ -134,8 +140,13 @@ function _stringProxy<
134140 options : DefaultOptions
135141) : Writable < string > {
136142 function toValue ( value : unknown ) {
137- if ( ! value && options . empty !== undefined )
143+ if (
144+ ! value &&
145+ options . empty !== undefined &&
146+ ( value !== 0 || options . emptyIfZero )
147+ ) {
138148 return options . empty === 'null' ? null : undefined ;
149+ }
139150
140151 if ( typeof value === 'number' ) {
141152 value = value . toString ( ) ;
@@ -157,8 +168,12 @@ function _stringProxy<
157168 if ( type == 'number' ) num = parseFloat ( numberToConvert ) ;
158169 else num = parseInt ( numberToConvert , 10 ) ;
159170
160- if ( options . empty !== undefined && ( isNaN ( num ) || num == 0 ) )
171+ if (
172+ options . empty !== undefined &&
173+ ( ( num === 0 && options . emptyIfZero ) || isNaN ( num ) )
174+ ) {
161175 return options . empty == 'null' ? null : undefined ;
176+ }
162177
163178 return num ;
164179 }
0 commit comments