Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added UUID/GUID formatting in UUID Generator extension #1343

Merged
merged 6 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions docs/Extensions/UuidGenerator/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Custom Web Search Extension
# UUID / GUID Generator

This extension allows to generate [Universally Unique Identifiers](https://en.wikipedia.org/wiki/Universally_unique_identifier), also known as UUID or GUID.
This can be done either instantly for a single UUID or through a dedicated UI, where multiple UUIDs can be generated at once.
Expand All @@ -7,20 +7,28 @@ This can be done either instantly for a single UUID or through a dedicated UI, w

![Example](example-generator.png)

This extension also allows to format the UUID to several different styles in a fast way.

![Example](example-formatting.png)

## Settings

- **UUID Version**: The UUID version, either [v4](https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_(random)), [v6](https://en.wikipedia.org/wiki/Universally_unique_identifier#Versions_1_and_6_(date-time_and_MAC_address)) or [v7](https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_7_(timestamp_and_random))
- **UUID Version**: The UUID version, either [v4](<https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_(random)>), [v6](<https://en.wikipedia.org/wiki/Universally_unique_identifier#Versions_1_and_6_(date-time_and_MAC_address)>) or [v7](<https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_7_(timestamp_and_random)>)
- **Number of UUIDs to generate**: The number of UUIDs that will be created, only applies when using the UI of the extension
- **Uppercase**: Whether the characters of the UUID should be uppercase
- **Hyphen**: Whether the UUID should contain hyphens (-)
- **Braces**: Whether the UUID should be wrapped in braces ({})
- **Quotes**: Whether the UUID should be wrapped in quotes
- **Search result formats**: Several different formats for fast formatting in search results

![Example](example-settings.png)

## About this extension

Author: [Christopher Steiner](https://github.com/ChristopherSteiner)

Co-Author: [Marco Senn-Haag](https://github.com/MarcoSennHaag)

Supported operating systems:

- Windows
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/Extensions/UuidGenerator/example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
271 changes: 158 additions & 113 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,6 @@
"react-router": "^7.1.3",
"react-router-dom": "^7.1.3",
"sharp": "^0.33.5",
"uuid": "^11.0.3"
"uuid": "^11.0.5"
}
}
6 changes: 6 additions & 0 deletions src/common/Extensions/UuidGenerator/UuidFormat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export type UuidFormat = {
uppercase: boolean;
hyphens: boolean;
braces: boolean;
quotes: boolean;
};
7 changes: 3 additions & 4 deletions src/common/Extensions/UuidGenerator/UuidGeneratorSetting.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import type { UuidFormat } from "./UuidFormat";
import type { UuidVersion } from "./UuidVersion";

export type UuidGeneratorSetting = {
uuidVersion: UuidVersion;
numberOfUuids: number;
uppercase: boolean;
hyphens: boolean;
braces: boolean;
quotes: boolean;
generatorFormat: UuidFormat;
searchResultFormats: UuidFormat[];
};
1 change: 1 addition & 0 deletions src/common/Extensions/UuidGenerator/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./UuidFormat";
export * from "./UuidGeneratorSetting";
export * from "./UuidVersion";
417 changes: 415 additions & 2 deletions src/main/Extensions/UuidGenerator/UuidGenerator.test.ts

Large diffs are not rendered by default.

51 changes: 46 additions & 5 deletions src/main/Extensions/UuidGenerator/UuidGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { UuidFormat } from "@common/Extensions/UuidGenerator";
import { v4 as uuidv4, v6 as uuidv6, v7 as uuidv7, validate as uuidValidate } from "uuid";

export class UuidGenerator {
Expand All @@ -13,24 +14,64 @@ export class UuidGenerator {
return uuidv7();
}

public static format(uuid: string, uppercase: boolean, hyphens: boolean, braces: boolean, quotes: boolean): string {
public static format(uuid: string, format: UuidFormat): string {
if (!uuidValidate(uuid)) {
throw new Error("Invalid UUID");
}

let formattedUuid = uuid;
if (uppercase) {
if (format.uppercase) {
formattedUuid = formattedUuid.toUpperCase();
}
if (!hyphens) {
if (!format.hyphens) {
formattedUuid = formattedUuid.replace(/-/g, "");
}
if (braces) {
if (format.braces) {
formattedUuid = `{${formattedUuid}}`;
}
if (quotes) {
if (format.quotes) {
formattedUuid = `"${formattedUuid}"`;
}
return formattedUuid;
}

public static reformat(uuid: string, format: UuidFormat): string {
let formattedUuid = uuid.replace(/["{}-]/g, "");
if (format.uppercase) {
formattedUuid = formattedUuid.toUpperCase();
} else {
formattedUuid = formattedUuid.toLowerCase();
}
if (format.hyphens) {
const tempUuid = formattedUuid.replace(/-/g, "");

formattedUuid =
tempUuid.substring(0, 8) +
"-" +
tempUuid.substring(8, 12) +
"-" +
tempUuid.substring(12, 16) +
"-" +
tempUuid.substring(16, 20) +
"-" +
tempUuid.substring(20);
} else {
formattedUuid = formattedUuid.replace(/-/g, "");
}
if (format.braces) {
formattedUuid = `{${formattedUuid}}`;
} else {
formattedUuid = formattedUuid.replace(/[{}]/g, "");
}
if (format.quotes) {
formattedUuid = `"${formattedUuid}"`;
} else {
formattedUuid = formattedUuid.replace(/"/g, "");
}
return formattedUuid;
}

public static validateUuid(uuid: string): boolean {
return uuidValidate(uuid);
}
}
96 changes: 72 additions & 24 deletions src/main/Extensions/UuidGenerator/UuidGeneratorExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import { getExtensionSettingKey } from "@common/Core/Extension";
import type { Image } from "@common/Core/Image";
import type { Resources, Translations } from "@common/Core/Translator";
import type { UuidGeneratorSetting as Settings, UuidVersion } from "@common/Extensions/UuidGenerator";
import type { UuidGeneratorSetting as Settings, UuidFormat, UuidVersion } from "@common/Extensions/UuidGenerator";
import { UuidGenerator } from "./UuidGenerator";

export class UuidGeneratorExtension implements Extension {
Expand All @@ -22,10 +22,8 @@ export class UuidGeneratorExtension implements Extension {
public readonly defaultSettings: Settings = {
uuidVersion: "v4",
numberOfUuids: 10,
uppercase: false,
hyphens: true,
braces: false,
quotes: false,
generatorFormat: { uppercase: false, hyphens: true, braces: false, quotes: false },
searchResultFormats: [],
};

public readonly author = {
Expand All @@ -40,40 +38,78 @@ export class UuidGeneratorExtension implements Extension {
) {}

public getInstantSearchResultItems(searchTerm: string): InstantSearchResultItems {
const uuidFormats: UuidFormat[] = this.getSettingValue("searchResultFormats");

let uuidSearchTerm = searchTerm;
if (uuidSearchTerm.toLowerCase().startsWith("uuid") || uuidSearchTerm.toLowerCase().startsWith("guid")) {
uuidSearchTerm = uuidSearchTerm.substring(4);
}

uuidSearchTerm = uuidSearchTerm.trim();
const possibleUuid = UuidGenerator.reformat(uuidSearchTerm, {
uppercase: false,
hyphens: true,
braces: false,
quotes: false,
});
if (this.validateUuid(possibleUuid)) {
return {
after: [],
before: uuidFormats.map((format, index) => {
const formattedUuid = UuidGenerator.format(possibleUuid, format);

return {
name: formattedUuid,
description: "UUID Generator",
descriptionTranslation: {
key: "generatorResult",
namespace: "extension[UuidGenerator]",
},
id: "uuidGenerator:instantResult-" + index,
image: this.getImage(),
defaultAction: createCopyToClipboardAction({
textToCopy: formattedUuid,
description: "Copy UUID to clipboard",
descriptionTranslation: {
key: "copyUuidToClipboard",
namespace: "extension[UuidGenerator]",
},
}),
};
}),
};
}

if (!["uuid", "guid"].includes(searchTerm.toLowerCase())) {
return createEmptyInstantSearchResult();
}

const uuid = this.generateUuid(
this.getSettingValue("uuidVersion"),
this.getSettingValue("uppercase"),
this.getSettingValue("hyphens"),
this.getSettingValue("braces"),
this.getSettingValue("quotes"),
);
const generatedUuid = this.generateUuid(this.getSettingValue("uuidVersion"), false, true, false, false);

return {
after: [],
before: [
{
name: uuid,
before: uuidFormats.map((format, index) => {
const formattedUuid = UuidGenerator.reformat(generatedUuid, format);

return {
name: formattedUuid,
description: "UUID Generator",
descriptionTranslation: {
key: "generatorResult",
namespace: "extension[UuidGenerator]",
},
id: "uuidGenerator:instantResult",
id: "uuidGenerator:instantResult-" + index,
image: this.getImage(),
defaultAction: createCopyToClipboardAction({
textToCopy: uuid,
textToCopy: formattedUuid,
description: "Copy UUID to clipboard",
descriptionTranslation: {
key: "copyUuidToClipboard",
namespace: "extension[UuidGenerator]",
},
}),
},
],
};
}),
};
}

Expand Down Expand Up @@ -110,6 +146,10 @@ export class UuidGeneratorExtension implements Extension {
hyphens: "Hyphens",
braces: "Braces",
quotes: "Quotes",
defaultGeneratorFormat: "Generator window format",
searchResultFormats: "Search result formats",
addSearchResultFormat: "Add format",
removeSearchResultFormat: "Remove format",
},
"de-CH": {
copyUuidToClipboard: "UUID in die Zwischenablage kopieren",
Expand All @@ -122,6 +162,10 @@ export class UuidGeneratorExtension implements Extension {
hyphens: "Bindestriche",
braces: "Geschweifte Klammern",
quotes: "Anführungszeichen",
defaultGeneratorFormat: "Format im Generator-Fenster",
searchResultFormats: "Formate der Suchresultate",
addSearchResultFormat: "Format hinzufügen",
removeSearchResultFormat: "Format entfernen",
},
};
}
Expand All @@ -146,10 +190,10 @@ export class UuidGeneratorExtension implements Extension {
result.push(
this.generateUuid(
settings.uuidVersion,
settings.uppercase,
settings.hyphens,
settings.braces,
settings.quotes,
settings.generatorFormat.uppercase,
settings.generatorFormat.hyphens,
settings.generatorFormat.braces,
settings.generatorFormat.quotes,
),
);
}
Expand Down Expand Up @@ -181,7 +225,7 @@ export class UuidGeneratorExtension implements Extension {
}
}

return UuidGenerator.format(uuid, uppercase, hyphens, braces, quotes);
return UuidGenerator.format(uuid, { uppercase, hyphens, braces, quotes });
}

private getSettingValue<T>(key: keyof Settings): T {
Expand All @@ -190,4 +234,8 @@ export class UuidGeneratorExtension implements Extension {
<T>this.getSettingDefaultValue(key),
);
}

private validateUuid(uuid: string): boolean {
return UuidGenerator.validateUuid(uuid);
}
}
15 changes: 9 additions & 6 deletions src/renderer/Extensions/UuidGenerator/Generator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,15 @@ export const Generator = ({
const generate = async () => {
try {
const uuids = await contextBridge.invokeExtension<UuidGeneratorSetting, InvocationResult>(extensionId, {
uuidVersion,
numberOfUuids,
uppercase,
hyphens,
braces,
quotes,
uuidVersion: uuidVersion,
numberOfUuids: numberOfUuids,
generatorFormat: {
uppercase: uppercase,
hyphens: hyphens,
braces: braces,
quotes: quotes,
},
searchResultFormats: [],
});

setGeneratedUuids(uuids.join("\n"));
Expand Down
Loading