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 keys, get, set, has and del aliases #402

Merged
merged 4 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ coverage
dist
drivers
/server*
docs/.*
30 changes: 30 additions & 0 deletions docs/1.guide/1.index.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ Checks if storage contains a key. Resolves to either `true` or `false`.
await storage.hasItem("foo:bar");
```

You can also use the `has` alias:

```js
await storage.has("foo:bar");
```

### `getItem(key, opts?)`

Gets the value of a key in storage. Resolves to either a javascript primitive value or `undefined`.
Expand All @@ -48,6 +54,12 @@ Gets the value of a key in storage. Resolves to either a javascript primitive va
await storage.getItem("foo:bar");
```

You can also use the `get` alias:

```js
await storage.get("foo:bar");
```

### `getItems(items, opts)`

(Experimental) Gets the value of a multiple keys in storage in parallel.
Expand Down Expand Up @@ -79,6 +91,12 @@ If value is `undefined`, it is same as calling `removeItem(key)`.
await storage.setItem("foo:bar", "baz");
```

You can also use the `set` alias:

```js
await storage.set("foo:bar", "baz");
```

### `setItems(items, opts)`

(Experimental) Add/Update items in parallel to the storage.
Expand Down Expand Up @@ -108,6 +126,12 @@ await storage.removeItem("foo:bar", { removeMeta: true });
// same as await storage.removeItem("foo:bar", true);
```

You can also use the `del` alias:

```js
await storage.del("foo:bar");
```

### `getMeta(key, opts = { nativeOnly? })`

Get metadata object for a specific key.
Expand Down Expand Up @@ -151,6 +175,12 @@ If a base is provided, only keys starting with the base will be returned also on
await storage.getKeys();
```

You can also use the `keys` alias:

```js
await storage.keys();
```

### `clear(base?, opts?)`

Removes all stored key/values. If a base is provided, only mounts matching base will be cleared.
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
"build": "unbuild",
"demo": "vite demo",
"dev": "vitest",
"lint": "eslint --ext .ts . && prettier -c src test demo docs/content",
"lint:fix": "eslint --ext .ts . --fix && prettier -w src test demo docs/content",
"lint": "eslint --ext .ts . && prettier -c src test demo docs/1.guide docs/2.drivers",
"lint:fix": "eslint --ext .ts . --fix && prettier -w src test demo docs/1.guide docs/2.drivers",
"prepack": "pnpm build",
"release": "pnpm test && changelogen --release && git push --follow-tags && pnpm publish",
"test": "pnpm lint && pnpm test:types && vitest run --coverage",
Expand Down
6 changes: 6 additions & 0 deletions src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,12 @@ export function createStorage<T extends StorageValue>(
base: m.mountpoint,
}));
},
// Aliases
keys: (base, opts = {}) => storage.getKeys(base, opts),
get: (key, opts = {}) => storage.getItem(key, opts),
set: (key, value, opts = {}) => storage.setItem(key, value, opts),
has: (key, opts = {}) => storage.hasItem(key, opts),
del: (key, opts = {}) => storage.removeItem(key, opts),
};

return storage;
Expand Down
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,10 @@ export interface Storage<T extends StorageValue = StorageValue> {
base?: string,
options?: { parents?: boolean }
) => { base: string; driver: Driver }[];
// Aliases
keys: Storage["getKeys"];
get: Storage["getItem"];
set: Storage["setItem"];
has: Storage["hasItem"];
del: Storage["removeItem"];
atinux marked this conversation as resolved.
Show resolved Hide resolved
}
11 changes: 11 additions & 0 deletions test/storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,15 @@ describe("utils", () => {
const storage = createStorage();
expect(async () => await storage.setItem("foo", [])).not.toThrow();
});

it("has aliases", async () => {
const storage = createStorage();

await storage.setItem("foo", "bar");
expect(await storage.has("foo")).toBe(true);
expect(await storage.get("foo")).toBe("bar");
expect(await storage.keys()).toEqual(["foo"]);
await storage.del("foo");
expect(await storage.has("foo")).toBe(false);
});
});
Loading