File tree Expand file tree Collapse file tree 4 files changed +34
-7
lines changed Expand file tree Collapse file tree 4 files changed +34
-7
lines changed Original file line number Diff line number Diff line change @@ -155,18 +155,17 @@ storage.clearAll()
155
155
156
156
### Objects
157
157
158
- ``` js
158
+ ``` tsx
159
159
const user = {
160
160
username: ' Marc' ,
161
161
age: 21
162
162
}
163
163
164
- // Serialize the object into a JSON string
165
- storage .set (' user' , JSON . stringify ( user) )
164
+ // Set
165
+ storage .setObject (' user' , user )
166
166
167
- // Deserialize the JSON string into an object
168
- const jsonUser = storage .getString (' user' ) // { 'username': 'Marc', 'age': 21 }
169
- const userObject = JSON .parse (jsonUser)
167
+ // Get
168
+ const userObject = storage .getObject <typeof user >(' user' ) // {name: 'Marc', age: 21}
170
169
```
171
170
172
171
### Encryption
Original file line number Diff line number Diff line change
1
+ module . exports = {
2
+ parser : '@typescript-eslint/parser' ,
3
+ plugins : [ ] ,
4
+ parserOptions : {
5
+ sourceType : 'module' ,
6
+ } ,
7
+ env : {
8
+ node : true ,
9
+ es2020 : true ,
10
+ } ,
11
+ } ;
Original file line number Diff line number Diff line change @@ -185,6 +185,15 @@ export class MMKV implements MMKVInterface {
185
185
186
186
this . onValuesChanged ( [ key ] ) ;
187
187
}
188
+ setObject ( key : string , value : any ) : boolean {
189
+ try {
190
+ this . getFunctionFromCache ( 'set' ) ( key , JSON . stringify ( value ) ) ;
191
+ this . onValuesChanged ( [ key ] ) ;
192
+ return true ;
193
+ } catch {
194
+ return false ;
195
+ }
196
+ }
188
197
getBoolean ( key : string ) : boolean | undefined {
189
198
const func = this . getFunctionFromCache ( 'getBoolean' ) ;
190
199
return func ( key ) ;
@@ -193,6 +202,14 @@ export class MMKV implements MMKVInterface {
193
202
const func = this . getFunctionFromCache ( 'getString' ) ;
194
203
return func ( key ) ;
195
204
}
205
+ getObject < T = any > ( key : string ) : T | null {
206
+ try {
207
+ const valueString = this . getFunctionFromCache ( 'getString' ) ( key ) ;
208
+ return typeof valueString === 'string' ? JSON . parse ( valueString ) : null ;
209
+ } catch {
210
+ return null ;
211
+ }
212
+ }
196
213
getNumber ( key : string ) : number | undefined {
197
214
const func = this . getFunctionFromCache ( 'getNumber' ) ;
198
215
return func ( key ) ;
Original file line number Diff line number Diff line change @@ -26,7 +26,7 @@ export const createMMKV = (config: MMKVConfiguration): NativeMMKV => {
26
26
if ( config . path != null ) {
27
27
throw new Error ( "MMKV: 'path' is not supported on Web!" ) ;
28
28
}
29
-
29
+
30
30
// canUseDOM check prevents spam in Node server environments, such as Next.js server side props.
31
31
if ( ! hasAccessToLocalStorage ( ) && canUseDOM ) {
32
32
console . warn (
You can’t perform that action at this time.
0 commit comments