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 all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Expand Up @@ -3,3 +3,4 @@ coverage
dist
drivers
/server*
docs/.*
31 changes: 31 additions & 0 deletions docs/1.guide/1.index.md
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,13 @@ await storage.removeItem("foo:bar", { removeMeta: true });
// same as await storage.removeItem("foo:bar", true);
```

You can also use the `del` or `remove` aliases:

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

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

Get metadata object for a specific key.
Expand Down Expand Up @@ -151,6 +176,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
7 changes: 7 additions & 0 deletions src/storage.ts
Expand Up @@ -447,6 +447,13 @@ 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),
remove: (key, opts = {}) => storage.removeItem(key, opts),
};

return storage;
Expand Down
7 changes: 7 additions & 0 deletions src/types.ts
Expand Up @@ -125,4 +125,11 @@ 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
remove: Storage["removeItem"];
}
14 changes: 14 additions & 0 deletions test/storage.test.ts
Expand Up @@ -159,4 +159,18 @@ 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);
await storage.setItem("bar", "baz");
await storage.remove("bar");
expect(await storage.has("bar")).toBe(false);
});
});