Skip to content

Commit c03d6c5

Browse files
committed
improvement(useLocalStorage): Improve useLocalStorage typescript types
1 parent f8a8dc8 commit c03d6c5

File tree

5 files changed

+96
-35
lines changed

5 files changed

+96
-35
lines changed

src/components/useCookie/stories/useCookie.md

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ Vue function that provides way to read, set and remove a cookie.
66

77
```typescript
88
interface UseCookieOptions {
9-
isParsing: boolean;
10-
serializer?: (value: any) => string;
11-
deserializer?: (value: string) => any;
9+
isParsing: boolean
10+
serializer?: SerializerFunction
11+
deserializer?: DeserializerFunction
1212
}
1313
```
1414

@@ -30,8 +30,8 @@ function useCookie(
3030
- `cookieName: string` the cookie name you wish to get/set/remove
3131
- `options: UseCookieOptions`
3232
- `isParsing: boolean` whether to enable parsing the cookie value or not, `false` by default
33-
- `serializer: Function` a custom serializer, `JSON.stringify` by default
34-
- `deserializer: Function` a custom deserializer, `JSON.parse` by default
33+
- `serializer: SerializerFunction` a custom serializer, `JSON.stringify` by default
34+
- `deserializer: DeserializerFunction` a custom deserializer, `JSON.parse` by default
3535
- `runOnMount: boolean` whether to get the cookie on mount or not, `true` by default
3636

3737
### Returns
@@ -49,32 +49,31 @@ function useCookie(
4949
```html
5050
<template>
5151
<div>
52-
Cookie: {{ cookie }}
53-
52+
<div>Cookie: {{ cookie }}</div>
5453
<button @click="getCookie">Get cookie</button>
5554
<button @click="setCookie('Value here')">Set cookie</button>
5655
<button @click="removeCookie">Remove cookie</button>
5756
</div>
5857
</template>
5958

6059
<script lang="ts">
61-
import Vue from 'vue'
62-
import { useCookie } from 'vue-use-kit'
60+
import Vue from 'vue'
61+
import { useCookie } from 'vue-use-kit'
6362
64-
export default Vue.extend({
65-
name: 'UseCookieDemo',
66-
setup() {
67-
const {
68-
cookie, getCookie, setCookie, removeCookie
69-
} = useCookie('i_love_cookies')
63+
export default Vue.extend({
64+
name: 'UseCookieDemo',
65+
setup() {
66+
const { cookie, getCookie, setCookie, removeCookie } = useCookie(
67+
'i_love_cookies'
68+
)
7069
71-
return {
72-
cookie,
73-
getCookie,
74-
setCookie,
75-
removeCookie,
70+
return {
71+
cookie,
72+
getCookie,
73+
setCookie,
74+
removeCookie
75+
}
7676
}
77-
}
78-
})
77+
})
7978
</script>
8079
```

src/components/useCookie/useCookie.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { CookieSerializeOptions } from 'cookie'
33
import {
44
createSerializer,
55
createDeserializer,
6+
SerializerFunction,
7+
DeserializerFunction,
68
trySerialize,
79
tryDeserialize,
810
isNullOrUndefined
@@ -11,8 +13,8 @@ import { ref, onMounted, Ref } from '@src/api'
1113

1214
export interface UseCookieOptions {
1315
isParsing: boolean
14-
serializer?: (value: any) => string
15-
deserializer?: (value: string) => any
16+
serializer?: SerializerFunction
17+
deserializer?: DeserializerFunction
1618
}
1719

1820
const defaultOptions = {
Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,77 @@
11
# useLocalStorage
22

3-
Vue function that...
3+
Vue function that provides way to read, update and delete a localStorage key
44

55
## Reference
66

77
```typescript
8-
// function useLocalStorage()
8+
interface UseLocalStorageOptions {
9+
isParsing: boolean
10+
serializer?: SerializerFunction
11+
deserializer?: DeserializerFunction
12+
}
13+
```
14+
15+
```typescript
16+
function useLocalStorage(
17+
key: string,
18+
options?: UseLocalStorageOptions,
19+
runOnMount?: boolean
20+
): {
21+
item: Ref<any>
22+
getItem: () => void
23+
setItem: (newVal: any) => void
24+
removeItem: () => void
25+
}
926
```
1027

1128
### Parameters
1229

13-
- `value: string` lorem ipsa
30+
- `key: string` the localstorage key you wish to get/set/remove
31+
- `options: UseLocalStorageOptions`
32+
- `isParsing: boolean` whether to enable parsing the localstorage key value or not, `false` by default
33+
- `serializer: SerializerFunction` a custom serializer, `JSON.stringify` by default
34+
- `deserializer: DeserializerFunction` a custom deserializer, `JSON.parse` by default
35+
- `runOnMount: boolean` whether to get the localstorage key on mount or not, `true` by default
1436

1537
### Returns
1638

17-
- `value: Ref<string>` lorem ipsa
39+
- `item: Ref<any>` the localstorage key value, it can be null, a string or a JSON object/array
40+
- `getItem: Function` get the localstorage key value
41+
- `setItem: Function` set the localstorage key value
42+
- `newVal: any`: the value to set, can be a string or an object/array
43+
- `removeItem: Function` delete the localstorage key
1844

1945
## Usage
2046

2147
```html
22-
<template></template>
48+
<template>
49+
<div>
50+
<div>Item: {{ item }}</div>
51+
<button @click="getItem">Get item</button>
52+
<button @click="setItem('Value here')">Set item</button>
53+
<button @click="removeItem">Remove item</button>
54+
</div>
55+
</template>
56+
57+
<script lang="ts">
58+
import Vue from 'vue'
59+
import { useLocalStorage } from 'vue-use-kit'
60+
61+
export default Vue.extend({
62+
name: 'UseLocalStorageDemo',
63+
setup() {
64+
const { item, getItem, setItem, removeItem } = useLocalStorage(
65+
'i_love_local_storage'
66+
)
67+
68+
return {
69+
item,
70+
getItem,
71+
setItem,
72+
removeItem
73+
}
74+
}
75+
})
76+
</script>
2377
```

src/components/useLocalStorage/useLocalStorage.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import {
22
createSerializer,
33
createDeserializer,
4+
SerializerFunction,
5+
DeserializerFunction,
46
trySerialize,
57
tryDeserialize,
68
isNullOrUndefined
@@ -9,8 +11,8 @@ import { ref, onMounted, Ref } from '@src/api'
911

1012
export interface UseLocalStorageOptions {
1113
isParsing: boolean
12-
serializer?: (value: any) => string
13-
deserializer?: (value: string) => any
14+
serializer?: SerializerFunction
15+
deserializer?: DeserializerFunction
1416
}
1517

1618
const defaultOptions = {

src/utils.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,20 @@ export const normalizeEntriesData = (data: [any, any][]) =>
4444
return acc
4545
}, {} as { [key: string]: any })
4646

47-
export const createSerializer = (serializer?: Function) =>
47+
export type SerializerFunction = (value: any) => string
48+
49+
export type DeserializerFunction = (value: string) => any
50+
51+
export const createSerializer = (serializer?: SerializerFunction) =>
4852
serializer || JSON.stringify
4953

50-
export const createDeserializer = (deserializer?: Function) =>
54+
export const createDeserializer = (deserializer?: DeserializerFunction) =>
5155
deserializer || JSON.parse
5256

5357
const fallbackToString = (val: any) => (isString(val) ? val : String(val))
5458
export const trySerialize = (
5559
val: any,
56-
serializer: Function,
60+
serializer: SerializerFunction,
5761
shouldSerialize?: boolean
5862
) => {
5963
if (!shouldSerialize) return fallbackToString(val)
@@ -66,7 +70,7 @@ export const trySerialize = (
6670

6771
export const tryDeserialize = (
6872
val: any,
69-
deserializer: Function,
73+
deserializer: DeserializerFunction,
7074
shouldDeserialize?: boolean
7175
) => {
7276
if (!shouldDeserialize) return val

0 commit comments

Comments
 (0)