Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support set/get object #562

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,18 +155,17 @@ storage.clearAll()

### Objects

```js
```tsx
const user = {
username: 'Marc',
age: 21
}

// Serialize the object into a JSON string
storage.set('user', JSON.stringify(user))
// Set
storage.setObject('user', user)

// Deserialize the JSON string into an object
const jsonUser = storage.getString('user') // { 'username': 'Marc', 'age': 21 }
const userObject = JSON.parse(jsonUser)
// Get
const userObject = storage.getObject<typeof user>('user') // {name: 'Marc', age: 21}
```

### Encryption
Expand Down
11 changes: 11 additions & 0 deletions example/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
parser: '@typescript-eslint/parser',
plugins: [],
parserOptions: {
sourceType: 'module',
},
env: {
node: true,
es2020: true,
},
};
17 changes: 17 additions & 0 deletions src/MMKV.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,15 @@ export class MMKV implements MMKVInterface {

this.onValuesChanged([key]);
}
setObject(key: string, value: any): boolean {
try {
this.getFunctionFromCache('set')(key, JSON.stringify(value));
this.onValuesChanged([key]);
return true;
} catch {
return false;
}
}
getBoolean(key: string): boolean | undefined {
const func = this.getFunctionFromCache('getBoolean');
return func(key);
Expand All @@ -193,6 +202,14 @@ export class MMKV implements MMKVInterface {
const func = this.getFunctionFromCache('getString');
return func(key);
}
getObject<T = any>(key: string): T | null {
try {
const valueString = this.getFunctionFromCache('getString')(key);
return typeof valueString === 'string' ? JSON.parse(valueString) : null;
} catch {
return null;
}
}
getNumber(key: string): number | undefined {
const func = this.getFunctionFromCache('getNumber');
return func(key);
Expand Down
2 changes: 1 addition & 1 deletion src/createMMKV.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const createMMKV = (config: MMKVConfiguration): NativeMMKV => {
if (config.path != null) {
throw new Error("MMKV: 'path' is not supported on Web!");
}

// canUseDOM check prevents spam in Node server environments, such as Next.js server side props.
if (!hasAccessToLocalStorage() && canUseDOM) {
console.warn(
Expand Down
Loading