commit cbf60aa16ca36795e1517d49ded7a54c2d0244b4
parent 0f472c2b3b102bcc6debf5029186238ec172460e
Author: triesap <137732411+triesap@users.noreply.github.com>
Date: Fri, 27 Sep 2024 13:59:30 +0000
utils: add nostr utils, edit regex
Diffstat:
3 files changed, 50 insertions(+), 0 deletions(-)
diff --git a/utils/src/index.ts b/utils/src/index.ts
@@ -4,6 +4,7 @@ export * from "./currency"
export * from "./error"
export * from "./geolocation"
export * from "./jshashes"
+export * from "./nostr"
export * from "./regex"
export * from "./time"
export * from "./trade"
diff --git a/utils/src/nostr.ts b/utils/src/nostr.ts
@@ -0,0 +1,47 @@
+export type NostrRelayInformationDocument = {
+ id?: string;
+ name?: string;
+ description?: string;
+ contact?: string;
+ supported_nips?: number[];
+ software?: string;
+ version?: string;
+ limitation_payment_required?: string;
+ limitation_restricted_writes?: boolean;
+}
+
+export type NostrRelayInformationDocumentFormFields = { [K in keyof NostrRelayInformationDocument]: string; };
+
+export const parse_nostr_relay_information_document = (data: any): NostrRelayInformationDocument | undefined => {
+ const obj = JSON.parse(data);
+ return {
+ id: typeof obj.id === 'string' ? obj.id : undefined,
+ name: typeof obj.name === 'string' ? obj.name : undefined,
+ description: typeof obj.description === 'string' ? obj.description : undefined,
+ contact: typeof obj.contact === 'string' ? obj.contact : undefined,
+ supported_nips: Array.isArray(obj.supported_nips) && obj.supported_nips.every((nip: any) => typeof nip === 'number')
+ ? obj.supported_nips
+ : undefined,
+ software: typeof obj.software === 'string' ? obj.software : undefined,
+ version: typeof obj.version === 'string' ? obj.version : undefined,
+ limitation_payment_required: obj.limitation && typeof obj.limitation === 'object' && typeof obj.limitation.payment_required === 'string' ? obj.limitation.payment_required : undefined,
+ limitation_restricted_writes: obj.limitation && typeof obj.limitation === 'object' && typeof obj.limitation.restricted_writes === 'boolean' ? obj.limitation.restricted_writes : undefined,
+ };
+};
+
+export const parse_nostr_relay_information_document_fields = (data: any): NostrRelayInformationDocumentFormFields | undefined => {
+ const info_doc = parse_nostr_relay_information_document(data);
+ const result: Partial<NostrRelayInformationDocumentFormFields> = {};
+ Object.entries(info_doc).forEach(([key, value]: [keyof NostrRelayInformationDocument, string]) => {
+ if (typeof value === 'boolean') {
+ result[key] = value ? '1' : '0';
+ } else if (Array.isArray(value)) {
+ result[key] = value.join(', ');
+ } else if (value === null || value === undefined) {
+ result[key] = '';
+ } else {
+ result[key] = String(value);
+ }
+ });
+ return result;
+};
diff --git a/utils/src/regex.ts b/utils/src/regex.ts
@@ -5,4 +5,6 @@ export const regex: Record<string, RegExp> = {
alphanum: /[a-zA-Z0-9., ]$/,
price: /^\d+(\.\d+)?$/,
price_charset: /[0-9.]$/,
+ profile_name: /^[a-zA-Z0-9._]{1,30}$/,
+ profile_name_char: /[a-zA-Z0-9._]/,
};