Fixes poor typing on get

Adds ensureFile
This commit is contained in:
2024-10-18 19:30:05 -06:00
parent 0c34d521bc
commit fd7b218dc3
3 changed files with 12 additions and 4 deletions

View File

@@ -1,6 +1,6 @@
{ {
"name": "@bearmetal/store", "name": "@bearmetal/store",
"version": "0.0.1", "version": "0.0.2",
"description": "A simple store for storing data in a JSON file.", "description": "A simple store for storing data in a JSON file.",
"files": [ "files": [
"mod.ts", "mod.ts",
@@ -9,6 +9,7 @@
"exports": "./mod.ts", "exports": "./mod.ts",
"imports": { "imports": {
"@std/assert": "jsr:@std/assert@^1.0.6", "@std/assert": "jsr:@std/assert@^1.0.6",
"@std/fs": "jsr:@std/fs@^1.0.4",
"@std/testing": "jsr:@std/testing@^1.0.3" "@std/testing": "jsr:@std/testing@^1.0.3"
} }
} }

1
deno.lock generated
View File

@@ -44,6 +44,7 @@
"workspace": { "workspace": {
"dependencies": [ "dependencies": [
"jsr:@std/assert@^1.0.6", "jsr:@std/assert@^1.0.6",
"jsr:@std/fs@^1.0.4",
"jsr:@std/testing@^1.0.3" "jsr:@std/testing@^1.0.3"
] ]
} }

View File

@@ -1,5 +1,9 @@
import { ensureFileSync } from "@std/fs";
const REFRESH_EVENT = "bm:refresh-store"; const REFRESH_EVENT = "bm:refresh-store";
type StoreValue = string | number | boolean;
/** /**
* A no-dep, lightweight, simple store for storing data in a JSON file. * A no-dep, lightweight, simple store for storing data in a JSON file.
* usage: * usage:
@@ -37,6 +41,8 @@ export class BearMetalStore {
constructor(storePath?: string) { constructor(storePath?: string) {
this.storePath = storePath || Deno.env.get("BEAR_METAL_STORE_PATH") || this.storePath = storePath || Deno.env.get("BEAR_METAL_STORE_PATH") ||
"./BearMetal/store.json"; "./BearMetal/store.json";
ensureFileSync(this.storePath);
this.EVENT_NAME = REFRESH_EVENT + this.storePath; this.EVENT_NAME = REFRESH_EVENT + this.storePath;
this.readIn(); this.readIn();
@@ -47,8 +53,8 @@ export class BearMetalStore {
* @param key * @param key
* @returns The value stored at the key, or undefined if the key doesn't exist. * @returns The value stored at the key, or undefined if the key doesn't exist.
*/ */
public get(key: string): string | number | boolean { public get<T = StoreValue>(key: string): T {
return this.store[key]; return this.store[key] as T;
} }
/** /**
@@ -56,7 +62,7 @@ export class BearMetalStore {
* @param key * @param key
* @param value * @param value
*/ */
public set(key: string, value: string | number | boolean): void { public set(key: string, value: StoreValue): void {
this.store[key] = value; this.store[key] = value;
this.writeOut(); this.writeOut();
} }