create centralized recurse function

This commit is contained in:
marlon-erler
2024-07-27 16:16:38 +02:00
parent ce69ac0ff3
commit ad057328df
2 changed files with 50 additions and 69 deletions
+22 -27
View File
@@ -500,18 +500,6 @@
this.initializeTree(); this.initializeTree();
} }
}; };
removeRecursively = (pathComponents) => {
loop_over_files: for (const key of Object.keys(localStorage)) {
const pathComponentsOfCurrentEntity = _StorageModel.stringToPathComponents(key);
loop_over_path_components: for (let i = 0; i < pathComponents.length; i++) {
if (!pathComponentsOfCurrentEntity[i]) continue loop_over_files;
if (pathComponentsOfCurrentEntity[i] != pathComponents[i])
continue loop_over_files;
}
this.remove(pathComponentsOfCurrentEntity, false);
}
this.initializeTree();
};
rename = (sourcePathComponents, destinationPathComponents, shouldInitialize = true) => { rename = (sourcePathComponents, destinationPathComponents, shouldInitialize = true) => {
const content = this.read(sourcePathComponents); const content = this.read(sourcePathComponents);
if (content == null) return false; if (content == null) return false;
@@ -522,27 +510,34 @@
} }
return true; return true;
}; };
renameRecursively = (sourcePathComponents, destinationPathComponents) => { // recursion
recurse = (pathComponents, fn) => {
loop_over_files: for (const key of Object.keys(localStorage)) { loop_over_files: for (const key of Object.keys(localStorage)) {
const originalPathComponentsOfCurrentEntity = _StorageModel.stringToPathComponents(key); const pathComponentsOfCurrentEntity = _StorageModel.stringToPathComponents(key);
loop_over_path_components: for (let i = 0; i < sourcePathComponents.length; i++) { loop_over_path_components: for (let i = 0; i < pathComponents.length; i++) {
if (!originalPathComponentsOfCurrentEntity[i]) continue loop_over_files; if (!pathComponentsOfCurrentEntity[i]) continue loop_over_files;
if (originalPathComponentsOfCurrentEntity[i] != sourcePathComponents[i]) if (pathComponentsOfCurrentEntity[i] != pathComponents[i])
continue loop_over_files; continue loop_over_files;
} }
const relativePathOfCurrentEntity = originalPathComponentsOfCurrentEntity.slice( fn(pathComponentsOfCurrentEntity);
}
this.initializeTree();
};
removeRecursively = (pathComponents) => {
this.recurse(pathComponents, (path) => this.remove(path, false));
this.initializeTree();
};
renameRecursively = (sourcePathComponents, destinationPathComponents) => {
this.recurse(sourcePathComponents, (path) => {
const relativePathOfCurrentEntity = path.slice(
sourcePathComponents.length sourcePathComponents.length
); );
const destinationPathComponentsOfCurrentEntity = [ const destinationPathComponentsOfCurrentEntity = [
...destinationPathComponents, ...destinationPathComponents,
...relativePathOfCurrentEntity ...relativePathOfCurrentEntity
]; ];
this.rename( this.rename(path, destinationPathComponentsOfCurrentEntity, false);
originalPathComponentsOfCurrentEntity, });
destinationPathComponentsOfCurrentEntity,
false
);
}
this.initializeTree(); this.initializeTree();
}; };
// stringifiable // stringifiable
@@ -3689,20 +3684,20 @@
function close() { function close() {
isOpen.value = false; isOpen.value = false;
} }
const id = v4_default(); const suggestionId = v4_default();
return /* @__PURE__ */ createElement("div", { class: "modal", "toggle:open": isOpen }, /* @__PURE__ */ createElement("div", { style: "max-width: 64rem" }, /* @__PURE__ */ createElement("main", null, /* @__PURE__ */ createElement("h2", null, headline), /* @__PURE__ */ createElement("div", { class: "flex-row width-input" }, /* @__PURE__ */ createElement( return /* @__PURE__ */ createElement("div", { class: "modal", "toggle:open": isOpen }, /* @__PURE__ */ createElement("div", { style: "max-width: 64rem" }, /* @__PURE__ */ createElement("main", null, /* @__PURE__ */ createElement("h2", null, headline), /* @__PURE__ */ createElement("div", { class: "flex-row width-input" }, /* @__PURE__ */ createElement(
"input", "input",
{ {
placeholder: translations.general.searchLabel, placeholder: translations.general.searchLabel,
"bind:value": searchViewModel.searchInput, "bind:value": searchViewModel.searchInput,
"on:enter": searchViewModel.applySearch, "on:enter": searchViewModel.applySearch,
list: id list: suggestionId
} }
), /* @__PURE__ */ createElement( ), /* @__PURE__ */ createElement(
"datalist", "datalist",
{ {
hidden: true, hidden: true,
id, id: suggestionId,
"children:append": [searchViewModel.suggestions, StringToOption] "children:append": [searchViewModel.suggestions, StringToOption]
} }
), /* @__PURE__ */ createElement( ), /* @__PURE__ */ createElement(
+27 -41
View File
@@ -50,29 +50,6 @@ export default class StorageModel {
} }
}; };
removeRecursively = (pathComponents: string[]): void => {
loop_over_files: for (const key of Object.keys(localStorage)) {
// get path of current entity
const pathComponentsOfCurrentEntity: string[] =
StorageModel.stringToPathComponents(key);
// exit if entity does not match
loop_over_path_components: for (
let i = 0;
i < pathComponents.length;
i++
) {
if (!pathComponentsOfCurrentEntity[i]) continue loop_over_files;
if (pathComponentsOfCurrentEntity[i] != pathComponents[i])
continue loop_over_files;
}
// remove
this.remove(pathComponentsOfCurrentEntity, false);
}
this.initializeTree();
};
rename = ( rename = (
sourcePathComponents: string[], sourcePathComponents: string[],
destinationPathComponents: string[], destinationPathComponents: string[],
@@ -90,43 +67,52 @@ export default class StorageModel {
return true; return true;
}; };
renameRecursively = ( // recursion
sourcePathComponents: string[], recurse = (pathComponents: string[], fn: (path: string[]) => void): void => {
destinationPathComponents: string[]
): void => {
loop_over_files: for (const key of Object.keys(localStorage)) { loop_over_files: for (const key of Object.keys(localStorage)) {
// get path of current entity // get path of current entity
const originalPathComponentsOfCurrentEntity: string[] = const pathComponentsOfCurrentEntity: string[] =
StorageModel.stringToPathComponents(key); StorageModel.stringToPathComponents(key);
// exit if entity does not match // exit if entity does not match
loop_over_path_components: for ( loop_over_path_components: for (
let i = 0; let i = 0;
i < sourcePathComponents.length; i < pathComponents.length;
i++ i++
) { ) {
if (!originalPathComponentsOfCurrentEntity[i]) continue loop_over_files; if (!pathComponentsOfCurrentEntity[i]) continue loop_over_files;
if (originalPathComponentsOfCurrentEntity[i] != sourcePathComponents[i]) if (pathComponentsOfCurrentEntity[i] != pathComponents[i])
continue loop_over_files; continue loop_over_files;
} }
// execute
fn(pathComponentsOfCurrentEntity);
}
this.initializeTree();
};
removeRecursively = (pathComponents: string[]): void => {
this.recurse(pathComponents, (path: string[]) => this.remove(path, false));
this.initializeTree();
};
renameRecursively = (
sourcePathComponents: string[],
destinationPathComponents: string[]
): void => {
this.recurse(sourcePathComponents, (path: string[]) => {
// assemble destinationPath // assemble destinationPath
const relativePathOfCurrentEntity = const relativePathOfCurrentEntity = path.slice(
originalPathComponentsOfCurrentEntity.slice( sourcePathComponents.length
sourcePathComponents.length );
);
const destinationPathComponentsOfCurrentEntity = [ const destinationPathComponentsOfCurrentEntity = [
...destinationPathComponents, ...destinationPathComponents,
...relativePathOfCurrentEntity, ...relativePathOfCurrentEntity,
]; ];
// rename // rename
this.rename( this.rename(path, destinationPathComponentsOfCurrentEntity, false);
originalPathComponentsOfCurrentEntity, });
destinationPathComponentsOfCurrentEntity,
false
);
}
this.initializeTree(); this.initializeTree();
}; };