diff --git a/.eslintignore b/.eslintignore index 0eb7daf0..eb27621d 100644 --- a/.eslintignore +++ b/.eslintignore @@ -3,3 +3,4 @@ coverage dist drivers /server* +docs/.* diff --git a/docs/1.guide/1.index.md b/docs/1.guide/1.index.md index 37b2409b..65da879d 100644 --- a/docs/1.guide/1.index.md +++ b/docs/1.guide/1.index.md @@ -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`. @@ -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. @@ -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. @@ -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. @@ -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. diff --git a/src/storage.ts b/src/storage.ts index d53c755d..8765645e 100644 --- a/src/storage.ts +++ b/src/storage.ts @@ -447,6 +447,13 @@ export function createStorage( 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; diff --git a/src/types.ts b/src/types.ts index e33a8da5..4df65e25 100644 --- a/src/types.ts +++ b/src/types.ts @@ -125,4 +125,11 @@ export interface Storage { 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"]; + remove: Storage["removeItem"]; } diff --git a/test/storage.test.ts b/test/storage.test.ts index aac85e67..13542f72 100644 --- a/test/storage.test.ts +++ b/test/storage.test.ts @@ -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); + }); });