Implement storage/retrieval of strings
This commit is contained in:
parent
c8c99373a3
commit
201efeca8c
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "@fwdekker/template",
|
"name": "@fwdekker/template",
|
||||||
"version": "3.4.1",
|
"version": "3.5.0",
|
||||||
"description": "The base template for pages on fwdekker.com.",
|
"description": "The base template for pages on fwdekker.com.",
|
||||||
"author": "Florine W. Dekker",
|
"author": "Florine W. Dekker",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
|
|
@ -20,7 +20,6 @@ export interface Storage {
|
||||||
*
|
*
|
||||||
* @param name the name of the array to store
|
* @param name the name of the array to store
|
||||||
* @param value the array to store under the given name
|
* @param value the array to store under the given name
|
||||||
* @protected
|
|
||||||
*/
|
*/
|
||||||
setArray(name: string, value: any[]): void;
|
setArray(name: string, value: any[]): void;
|
||||||
|
|
||||||
|
@ -29,7 +28,6 @@ export interface Storage {
|
||||||
*
|
*
|
||||||
* @param name the name of the boolean to retrieve
|
* @param name the name of the boolean to retrieve
|
||||||
* @param def the value to return if no boolean is stored with the given name
|
* @param def the value to return if no boolean is stored with the given name
|
||||||
* @protected
|
|
||||||
*/
|
*/
|
||||||
getBoolean(name: string, def: boolean): boolean;
|
getBoolean(name: string, def: boolean): boolean;
|
||||||
|
|
||||||
|
@ -38,7 +36,6 @@ export interface Storage {
|
||||||
*
|
*
|
||||||
* @param name the name of the boolean to store
|
* @param name the name of the boolean to store
|
||||||
* @param value the boolean to store under the given name
|
* @param value the boolean to store under the given name
|
||||||
* @protected
|
|
||||||
*/
|
*/
|
||||||
setBoolean(name: string, value: boolean): void;
|
setBoolean(name: string, value: boolean): void;
|
||||||
|
|
||||||
|
@ -47,7 +44,6 @@ export interface Storage {
|
||||||
*
|
*
|
||||||
* @param name the name of the number to retrieve
|
* @param name the name of the number to retrieve
|
||||||
* @param def the value to return if no number is stored with the given name
|
* @param def the value to return if no number is stored with the given name
|
||||||
* @protected
|
|
||||||
*/
|
*/
|
||||||
getNumber(name: string, def: number): number;
|
getNumber(name: string, def: number): number;
|
||||||
|
|
||||||
|
@ -56,9 +52,24 @@ export interface Storage {
|
||||||
*
|
*
|
||||||
* @param name the name of the number to store
|
* @param name the name of the number to store
|
||||||
* @param value the number to store under the given name
|
* @param value the number to store under the given name
|
||||||
* @protected
|
|
||||||
*/
|
*/
|
||||||
setNumber(name: string, value: number): void;
|
setNumber(name: string, value: number): void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves a string from storage.
|
||||||
|
*
|
||||||
|
* @param name the name of the string to retrieve
|
||||||
|
* @param def the value to return if no string is stored with the given name
|
||||||
|
*/
|
||||||
|
getString(name: string, def: string): string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stores a string.
|
||||||
|
*
|
||||||
|
* @param name the name of the string to store
|
||||||
|
* @param value the number to store under the given name
|
||||||
|
*/
|
||||||
|
setString(name: string, value: string): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -115,28 +126,32 @@ export class LocalStorage implements Storage {
|
||||||
}
|
}
|
||||||
|
|
||||||
setArray(name: string, value: any[]): void {
|
setArray(name: string, value: any[]): void {
|
||||||
const item = this.read();
|
this.setString(name, JSON.stringify(value));
|
||||||
item[name] = JSON.stringify(value);
|
|
||||||
this.write(item);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getBoolean(name: string, def: boolean = false): boolean {
|
getBoolean(name: string, def: boolean = false): boolean {
|
||||||
return (this.read()[name] ?? `${def}`) === "true";
|
return this.getString(name, def ? "true" : "false") === "true";
|
||||||
}
|
}
|
||||||
|
|
||||||
setBoolean(name: string, value: boolean): void {
|
setBoolean(name: string, value: boolean): void {
|
||||||
const item = this.read();
|
this.setString(name, "" + value);
|
||||||
item[name] = "" + value;
|
|
||||||
this.write(item);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getNumber(name: string, def: number = 0): number {
|
getNumber(name: string, def: number = 0): number {
|
||||||
return +(this.read()[name] ?? def);
|
return +this.getString(name, "" + def);
|
||||||
}
|
}
|
||||||
|
|
||||||
setNumber(name: string, value: number): void {
|
setNumber(name: string, value: number): void {
|
||||||
|
this.setString(name, "" + value);
|
||||||
|
}
|
||||||
|
|
||||||
|
getString(name: string, def: string = ""): string {
|
||||||
|
return this.read()[name] ?? def;
|
||||||
|
}
|
||||||
|
|
||||||
|
setString(name: string, value: string): void {
|
||||||
const item = this.read();
|
const item = this.read();
|
||||||
item[name] = "" + value;
|
item[name] = value;
|
||||||
this.write(item);
|
this.write(item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -175,4 +190,12 @@ export class MemoryStorage implements Storage {
|
||||||
getNumber(name: string, def: number): number {
|
getNumber(name: string, def: number): number {
|
||||||
return this.storage[name] ?? def;
|
return this.storage[name] ?? def;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setString(name: string, value: string): void {
|
||||||
|
this.storage[name] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
getString(name: string, def: string): string {
|
||||||
|
return this.storage[name] ?? def;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue