From dcf17615f9ef37e4c7f19ada9d211626cd52134b Mon Sep 17 00:00:00 2001 From: Cat Chen Date: Sun, 24 Nov 2024 23:21:19 -0800 Subject: [PATCH 1/2] Added jsdoc for JSR --- src/Storage.ts | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/Storage.ts b/src/Storage.ts index 7f37bef..6b1114f 100644 --- a/src/Storage.ts +++ b/src/Storage.ts @@ -30,10 +30,28 @@ function clear(): void { } } +/** + * A class that can be instantiated into a storage with a specific name and version. + * Type T stands for the type of the value that is stored in the storage. It has to be serializable to JSON. + */ class Storage { + /** + * Name of the storage. + */ name: string; + + /** + * Version of the storage with this particular name. + */ version: number; + /** + * Create or retrieve the storage with the given name and version. + * If the version is omitted, it will use the existing version of the storage with the given name. + * If there is no existing storage with the given name, it will throw an error.b + * @param name Name of the storage. + * @param version Version of the storage. It has to be a positive integer. + */ constructor(name: string, version?: number) { if (version !== undefined) { const parsedVersion = parseInt(`${version}`, 10); @@ -58,6 +76,10 @@ class Storage { } } + /** + * Read the value from the storage. + * @returns The value that is stored in the storage. If there is no value, it returns null. + */ read(): T | null { const key = `${this.name}:${this.version}`; const jsonString = get(key); @@ -75,6 +97,10 @@ class Storage { return value; } + /** + * Write the value to the storage. + * @param value The value to be stored in the storage. + */ write(value: T): void { const key = `${this.name}:${this.version}`; const jsonString = JSON.stringify(value); @@ -85,6 +111,9 @@ class Storage { } } + /** + * Reset all storages. (It even wipes out other localStorage content that are not written by this class.) + */ static reset(): void { clear(); } From 19c5c31516ba8cc7ff8f19e89a0f5bcf86de9d1d Mon Sep 17 00:00:00 2001 From: Cat Chen Date: Sun, 24 Nov 2024 23:24:26 -0800 Subject: [PATCH 2/2] Update src/Storage.ts Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/Storage.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Storage.ts b/src/Storage.ts index 6b1114f..ddb5797 100644 --- a/src/Storage.ts +++ b/src/Storage.ts @@ -48,7 +48,7 @@ class Storage { /** * Create or retrieve the storage with the given name and version. * If the version is omitted, it will use the existing version of the storage with the given name. - * If there is no existing storage with the given name, it will throw an error.b + * If there is no existing storage with the given name, it will throw an error. * @param name Name of the storage. * @param version Version of the storage. It has to be a positive integer. */