File tree Expand file tree Collapse file tree 3 files changed +17
-15
lines changed Expand file tree Collapse file tree 3 files changed +17
-15
lines changed Original file line number Diff line number Diff line change @@ -126,7 +126,7 @@ describe('useCookie', () => {
126126 )
127127 } )
128128
129- it ( 'should correctly set using the serializer' , async ( ) => {
129+ it ( 'should correctly set the object using the serializer' , async ( ) => {
130130 const cookieName = 'cookieName'
131131 const cookieValue = { value1 : 'testValue1' , value2 : 'testValue2' }
132132 const serializerVal = { value1 : 'testValue1+1' , value2 : 'testValue2+1' }
@@ -154,11 +154,10 @@ describe('useCookie', () => {
154154 const wrapper = mount (
155155 testComponent ( cookieName , cookieValue , {
156156 isParsing : false ,
157- serializer : ( obj : any ) =>
158- JSON . stringify ( {
159- value1 : `${ obj . value1 } +1` ,
160- value2 : `${ obj . value2 } +1`
161- } )
157+ serializer : ( obj : any ) => ( {
158+ value1 : `${ obj . value1 } +1` ,
159+ value2 : `${ obj . value2 } +1`
160+ } )
162161 } )
163162 )
164163 wrapper . find ( '#setCookie' ) . trigger ( 'click' )
Original file line number Diff line number Diff line change @@ -144,7 +144,7 @@ describe('useItem', () => {
144144 )
145145 } )
146146
147- it ( 'should correctly set using the serializer' , async ( ) => {
147+ it ( 'should correctly set the object using the serializer' , async ( ) => {
148148 const itemKey = 'itemKey'
149149 const itemValue = { value1 : 'testValue1' , value2 : 'testValue2' }
150150 const serializerVal = { value1 : 'testValue1+1' , value2 : 'testValue2+1' }
@@ -172,11 +172,10 @@ describe('useItem', () => {
172172 const wrapper = mount (
173173 testComponent ( itemKey , itemValue , {
174174 isParsing : false ,
175- serializer : ( obj : any ) =>
176- JSON . stringify ( {
177- value1 : `${ obj . value1 } +1` ,
178- value2 : `${ obj . value2 } +1`
179- } )
175+ serializer : ( obj : any ) => ( {
176+ value1 : `${ obj . value1 } +1` ,
177+ value2 : `${ obj . value2 } +1`
178+ } )
180179 } )
181180 )
182181 wrapper . find ( '#setItem' ) . trigger ( 'click' )
Original file line number Diff line number Diff line change 11const checkType = ( typeToCheck : any ) =>
22 Object . prototype . toString . call ( typeToCheck )
33
4+ export const isString = ( varToCheck : any ) =>
5+ checkType ( varToCheck ) === '[object String]'
6+
47export const isObj = ( varToCheck : any ) =>
58 checkType ( varToCheck ) === '[object Object]'
69
@@ -47,16 +50,17 @@ export const createSerializer = (serializer?: Function) =>
4750export const createDeserializer = ( deserializer ?: Function ) =>
4851 deserializer || JSON . parse
4952
53+ const fallbackToString = ( val : any ) => ( isString ( val ) ? val : String ( val ) )
5054export const trySerialize = (
5155 val : any ,
5256 serializer : Function ,
5357 shouldSerialize ?: boolean
5458) => {
55- if ( ! shouldSerialize ) return String ( val )
59+ if ( ! shouldSerialize ) return fallbackToString ( val )
5660 try {
57- return serializer ( val )
61+ return fallbackToString ( serializer ( val ) )
5862 } catch ( error ) {
59- return val
63+ return fallbackToString ( val )
6064 }
6165}
6266
You can’t perform that action at this time.
0 commit comments