Skip to content

Commit

Permalink
flexible names
Browse files Browse the repository at this point in the history
  • Loading branch information
Mazuh committed Feb 5, 2024
1 parent a0c0335 commit 08cc6a9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ describe("OPFS project listing service", () => {
expect(removeMock).toHaveBeenCalled();
});

it("throws error if retrieved filenames are corrupted with invalid patterns", async () => {
it("ignore retrieved filenames corrupted with invalid patterns", async () => {
(opfsAdapters.makeOpfsMainDirAdapter as jest.Mock).mockResolvedValueOnce({
retrieveFilenames: jest
.fn()
Expand All @@ -140,10 +140,10 @@ describe("OPFS project listing service", () => {
]),
});

await expect(() => retrieveProjectsListing()).rejects.toEqual(
new Error(
"Invalid project filename (corrupted data?): 91599476-833d-invaliduuid-826b-8fe768fad0bf_My cool API.json"
)
);
const listing = await retrieveProjectsListing();

expect(listing).toEqual({
items: [{ name: "MyAPI", uuid: "82184240-6b29-4ae8-82f5-fbe7d1bb814a" }],
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ export async function retrieveProjectsListing(): Promise<ProjectListing> {
});
const filenames = await opfsDir.retrieveFilenames();
return {
items: filenames.map((filename) => getListingItemFromFilename(filename)),
items: filenames
.reduce(
(acc, filename) => [...acc, getListingItemFromFilename(filename)],
[] as (ProjectListingItem | null)[]
)
.filter((it) => it !== null) as ProjectListingItem[],
};
}

Expand Down
21 changes: 16 additions & 5 deletions src/services/opfs-projects-shared-internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,13 @@ export function getProjectFilename(
*/
export function getListingItemFromFilename(
filename: string
): Pick<Project, "uuid" | "name"> {
): Pick<Project, "uuid" | "name"> | null {
const rFilename =
/(?<uuid>[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})_(?<name>[A-Za-z ]+)\.json/;
/^(?<uuid>[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})_(?<name>[A-Za-z 0-9]+)\.json$/;
const match = filename.match(rFilename);
if (!match || !match.groups) {
throw new Error(`Invalid project filename (corrupted data?): ${filename}`);
console.error(`Invalid project filename (corrupted data?): ${filename}`);
return null;
}

return {
Expand All @@ -110,13 +111,23 @@ export function getListingItemFromFilename(
export const PROJECTS_OPFS_SUBDIRECTORY = "projects";

function hygienizeProjectName(name: string): string {
return name.trim().replaceAll(".json", "").replaceAll('"', "");
return (
name
// just avoiding confusing/ugly trailing spaces:
.trim()
// possibly offends our regex of listing retrieval:
.replaceAll(/.json$/i, "")
// possibly offends our regex of listing retrieval:
.replaceAll('"', "")
// throws error on OPFS:
.replaceAll("/", " ")
);
}

async function retrieveProjectFilenameByUuid(uuid: string): Promise<string> {
const allFilenames = await retrieveProjectsFilenames();
const filename = allFilenames.find(
(filename) => getListingItemFromFilename(filename).uuid === uuid
(filename) => getListingItemFromFilename(filename)?.uuid === uuid
);

if (!filename) {
Expand Down

0 comments on commit 08cc6a9

Please sign in to comment.