Super simple key value with type safety and implement your own load and save function.
Because it's cool.
Cross-platform.
No ability to load
and save
.
type Data = {
foo: number;
bar: string;
lok: boolean;
};
const defaultData: Data = {
foo: 1,
bar: "test",
lok: true,
};
const dummy = new SSKV<Data>(defaultData);
dummy.get("foo");
dummy.get("bar");
dummy.set("bar", "change");
dummy.get("bar");
Persistence with JSON file.
type Data = {
foo: number;
bar: string;
lok: boolean;
};
const defaultData: Data = {
foo: 1,
bar: "test",
lok: true,
};
const dummy = new SSKV<Data>(defaultData, {
save: (currentData, defaultData) => {
const data = Object.assign({}, defaultData, currentData);
fs.writeFileSync("data.json", JSON.stringify(data));
},
load: () => {
return JSON.parse(fs.readFileSync("data.json", "utf8")) as Data;
},
});
// Both of these is equal operation for load and get
dummy.load().get("foo");
dummy.get("foo", true);
// Both of these is equal operation for set and save
dummy.set("bar", "change").save();
dummy.set("bar", "change", true);
Persistence with JSON file.
type Data = {
foo: number;
bar: string;
lok: boolean;
};
const defaultData: Data = {
foo: 1,
bar: "test",
lok: true,
};
const dummy = new SSKV<Data>(defaultData, {
save: (currentData, defaultData) => {
const data = Object.assign({}, defaultData, currentData);
Deno.writeTextFileSync("data.json", JSON.stringify(data));
},
load: () => {
const decoder = new TextDecoder("utf-8");
const data = Deno.readFileSync("data.json");
return JSON.parse(decoder.decode(data)) as Data;
},
});
// Both of these is equal operation for load and get
dummy.load().get("foo");
dummy.get("foo", true);
// Both of these is equal operation for set and save
dummy.set("bar", "change").save();
dummy.set("bar", "change", true);
Persistence with JSON in localStorage
.
type Data = {
foo: number;
bar: string;
lok: boolean;
};
const defaultData: Data = {
foo: 1,
bar: "test",
lok: true,
};
const dummy = new SSKV<Data>(defaultData, {
save: (currentData, defaultData) => {
const data = Object.assign({}, defaultData, currentData);
localStorage.setItem("data", JSON.stringify(data));
},
load: () => {
return JSON.parse(localStorage.getItem("data") ?? "{}") as Data;
},
});
// Both of these is equal operation for load and get
dummy.load().get("foo");
dummy.get("foo", true);
// Both of these is equal operation for set and save
dummy.set("bar", "change").save();
dummy.set("bar", "change", true);