Skip to content
/ sskv Public

Super simple key value with type safety and implement your own load and save function.

License

Notifications You must be signed in to change notification settings

anasrar/sskv

Repository files navigation

SSKV

Super simple key value with type safety and implement your own load and save function.

Why Type safety

Because it's cool.

Why Implement Load and Save Manually

Cross-platform.

Example

In-Memory

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");

Node.js

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);

Deno

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);

Browser localStorage

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);

About

Super simple key value with type safety and implement your own load and save function.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published