Skip to content

Commit 26dfcfb

Browse files
ngocle2497Admin
authored andcommitted
feat: add support set/get object
1 parent 40b71fe commit 26dfcfb

File tree

4 files changed

+34
-7
lines changed

4 files changed

+34
-7
lines changed

README.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,18 +155,17 @@ storage.clearAll()
155155

156156
### Objects
157157

158-
```js
158+
```tsx
159159
const user = {
160160
username: 'Marc',
161161
age: 21
162162
}
163163

164-
// Serialize the object into a JSON string
165-
storage.set('user', JSON.stringify(user))
164+
// Set
165+
storage.setObject('user', user)
166166

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}
170169
```
171170

172171
### Encryption

example/.eslintrc.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
};

src/MMKV.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,15 @@ export class MMKV implements MMKVInterface {
185185

186186
this.onValuesChanged([key]);
187187
}
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+
}
188197
getBoolean(key: string): boolean | undefined {
189198
const func = this.getFunctionFromCache('getBoolean');
190199
return func(key);
@@ -193,6 +202,14 @@ export class MMKV implements MMKVInterface {
193202
const func = this.getFunctionFromCache('getString');
194203
return func(key);
195204
}
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+
}
196213
getNumber(key: string): number | undefined {
197214
const func = this.getFunctionFromCache('getNumber');
198215
return func(key);

src/createMMKV.web.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export const createMMKV = (config: MMKVConfiguration): NativeMMKV => {
2626
if (config.path != null) {
2727
throw new Error("MMKV: 'path' is not supported on Web!");
2828
}
29-
29+
3030
// canUseDOM check prevents spam in Node server environments, such as Next.js server side props.
3131
if (!hasAccessToLocalStorage() && canUseDOM) {
3232
console.warn(

0 commit comments

Comments
 (0)