Skip to content

Commit f8a8dc8

Browse files
committed
improvement(useLocalStorage): Always return string from useLocalStorage serializer
1 parent 4f66f19 commit f8a8dc8

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

src/components/useCookie/useCookie.spec.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff 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')

src/components/useLocalStorage/useLocalStorage.spec.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff 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')

src/utils.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
const checkType = (typeToCheck: any) =>
22
Object.prototype.toString.call(typeToCheck)
33

4+
export const isString = (varToCheck: any) =>
5+
checkType(varToCheck) === '[object String]'
6+
47
export const isObj = (varToCheck: any) =>
58
checkType(varToCheck) === '[object Object]'
69

@@ -47,16 +50,17 @@ export const createSerializer = (serializer?: Function) =>
4750
export const createDeserializer = (deserializer?: Function) =>
4851
deserializer || JSON.parse
4952

53+
const fallbackToString = (val: any) => (isString(val) ? val : String(val))
5054
export 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

0 commit comments

Comments
 (0)