rename variables for clarity
This commit is contained in:
+24
-24
@@ -4,39 +4,39 @@ import { parse, stringify } from "../utility";
|
|||||||
|
|
||||||
export default class StorageModel {
|
export default class StorageModel {
|
||||||
storageEntryTree: StorageEntry = {}; // basic
|
storageEntryTree: StorageEntry = {}; // basic
|
||||||
store = (directoryNames: string[], key: string, value: string): void => {
|
store = (pathComponents: string[], value: string): void => {
|
||||||
const unifiedPath = this.directoryNamesToKey(...directoryNames, key);
|
const key = this.pathComponentsToKey(...pathComponents);
|
||||||
localStorage.setItem(unifiedPath, value);
|
localStorage.setItem(key, value);
|
||||||
this.updateTree(...directoryNames, key);
|
this.updateTree(...pathComponents);
|
||||||
};
|
};
|
||||||
|
|
||||||
restore = (directoryNames: string[], key: string): string | null => {
|
restore = (pathComponents: string[]): string | null => {
|
||||||
const unifiedPath = this.directoryNamesToKey(...directoryNames, key);
|
const key = this.pathComponentsToKey(...pathComponents);
|
||||||
return localStorage.getItem(unifiedPath);
|
return localStorage.getItem(key);
|
||||||
};
|
};
|
||||||
|
|
||||||
remove = (directoryNames: string[], key: string): void => {
|
remove = (pathComponents: string[]): void => {
|
||||||
const unifiedPath = this.directoryNamesToKey(...directoryNames, key);
|
const key = this.pathComponentsToKey(...pathComponents);
|
||||||
localStorage.removeItem(unifiedPath);
|
localStorage.removeItem(key);
|
||||||
};
|
};
|
||||||
|
|
||||||
list = (...directoryNames: string[]): string[] => {
|
list = (...pathComponents: string[]): string[] => {
|
||||||
let currentParent = this.storageEntryTree;
|
let currentParent = this.storageEntryTree;
|
||||||
for (const directoryName of directoryNames) {
|
for (const component of pathComponents) {
|
||||||
currentParent = currentParent[directoryName];
|
currentParent = currentParent[component];
|
||||||
}
|
}
|
||||||
|
|
||||||
return [...Object.keys(currentParent)];
|
return [...Object.keys(currentParent)];
|
||||||
};
|
};
|
||||||
|
|
||||||
// array
|
// array
|
||||||
storeArray = (directoryNames: string[], key: string, value: any[]): void => {
|
storeArray = (pathComponents: string[], value: any[]): void => {
|
||||||
const valueString = stringify(value);
|
const valueString = stringify(value);
|
||||||
this.store(directoryNames, key, valueString);
|
this.store(pathComponents, valueString);
|
||||||
};
|
};
|
||||||
|
|
||||||
restoreArray = (directoryNames: string[], key: string): any[] | null => {
|
restoreArray = (pathComponents: string[]): any[] | null => {
|
||||||
const valueString = this.restore(directoryNames, key);
|
const valueString = this.restore(pathComponents);
|
||||||
if (!valueString) return null;
|
if (!valueString) return null;
|
||||||
|
|
||||||
const parsed = parse(valueString);
|
const parsed = parse(valueString);
|
||||||
@@ -48,15 +48,15 @@ export default class StorageModel {
|
|||||||
// init
|
// init
|
||||||
constructor() {
|
constructor() {
|
||||||
for (const key of Object.keys(localStorage)) {
|
for (const key of Object.keys(localStorage)) {
|
||||||
const directoryNames = this.keyToDirectoryNames(key);
|
const components = this.keyToPathComponents(key);
|
||||||
this.updateTree(...directoryNames);
|
this.updateTree(...components);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// utility
|
// utility
|
||||||
updateTree = (...pathParts: string[]) => {
|
updateTree = (...pathComponents: string[]) => {
|
||||||
let currentParent = this.storageEntryTree;
|
let currentParent = this.storageEntryTree;
|
||||||
for (const pathPart of pathParts) {
|
for (const pathPart of pathComponents) {
|
||||||
if (!currentParent[pathPart]) {
|
if (!currentParent[pathPart]) {
|
||||||
currentParent[pathPart] = {};
|
currentParent[pathPart] = {};
|
||||||
}
|
}
|
||||||
@@ -65,11 +65,11 @@ export default class StorageModel {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
directoryNamesToKey = (...directoryNames: string[]): string => {
|
pathComponentsToKey = (...pathComponents: string[]): string => {
|
||||||
return directoryNames.join("/");
|
return pathComponents.join("/");
|
||||||
};
|
};
|
||||||
|
|
||||||
keyToDirectoryNames = (key: string): string[] => {
|
keyToPathComponents = (key: string): string[] => {
|
||||||
return key.split("/");
|
return key.split("/");
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user