@webgogol/core-share
v0.13.0
Published
Core utilities shared across @webgogol packages.
Readme
@webgogol/core-share
Core utilities shared across @webgogol packages.
This package provides a collection of utility functions and types designed to be used across multiple projects within the @webgogol ecosystem. It includes modules for handling Base64 conversions, managing contracts, data manipulation, event handling, file operations, and more.
Installation
pnpm add @webgogol/core-shareUsage
import { uuid, shortenPath, truncateString } from "@webgogol/core-share";
const id = uuid();
const short = shortenPath("c:/very/long/path/to/file.txt");
const preview = truncateString("Some very long text...", 80);API Reference
Base64 Utilities (base64.ts)
arrayBufferToBase64(buffer: ArrayBuffer): string: Converts anArrayBufferto a Base64 string.base64ToArrayBuffer(base64: string): ArrayBuffer: Converts a Base64 string to anArrayBuffer.uint8ArrayToBase64(a: Uint8Array): string: Converts aUint8Arrayto a Base64 string.
Contract Utilities (contract.ts)
- Types:
Contract,BriefContract,FlowContract,ProcessorContract,StatusFlowContract,StepStatusProcessorContract,VaultId. brief(contract: Contract): BriefContract: Creates a brief version of a contract.checkContract(contract: Contract): Contract: Validates a contract.emptyContract(): Returns an empty contract object.emptyBriefContract(): Returns an empty brief contract object.emptyVaultId(): Returns an empty vault ID object.
Data Handling (data.ts)
- Types:
ContentType,Data,Format. data(source: string, contentType: ContentType, format: Format = "base64"): Data: Creates a data URL.
Event Handling (event.ts)
AppErrorEvent: Custom error event class.emit(event: Event): Dispatches an event.handleEvent(event: Event, handler: HandlerEvent): Handles a specific event.rwl(message: string, level: "log" | "warn" | "error" = "log"): A simple event logger.- Type:
HandlerEvent.
File Utilities (file.ts)
replaceExt(path: string, ext: string): string: Replaces the extension of a file path.replaceName(path: string, name: string): string: Replaces the name of a file in a path.
Fingerprinting (fingerprint.ts)
fingerprint(): Promise<string>: Generates a browser fingerprint.
Key Utilities (key.ts)
akey(parts: string[]): string: Assembles a key from parts.removeFirstKey(key: string): string: Removes the first segment of a key.removePagination(key: string): string: Removes pagination from a key.rkey(key: string): string[]: Splits a key into its component parts.
Path Utilities (path.ts)
shortenPath(path: string, maxLength = 50): string: Shortens a file path to a maximum length.
Object Utilities (object.ts)
truncate(value: unknown, maxLength = 100): unknown: Truncates strings in primitives, arrays, and objects to a max length.truncateObject<T extends object>(obj: T, maxLength = 100): T: Safely truncates all string fields within an object.truncateString(text: string, maxLength = 100): string: Truncates a string to a max length with an ellipsis.
Prefix Utilities (prefix.ts)
- Type:
Prefix. getPrefix(text: string): Prefix | undefined: Extracts a prefix from a string.hasAnyPrefix(text: string): boolean: Checks if a string has any known prefix.hasPrefix(text: string, prefix: Prefix): boolean: Checks if a string has a specific prefix.noPrefix(text: string): string: Removes any known prefix from a string.prefix(text: string, prefix: Prefix): string: Adds a prefix to a string.
SQLite Utilities (sqlite.ts)
fromSqliteValue(value: any, type: string): any: Converts a value from its SQLite representation.getSqliteType(value: any): string: Determines the SQLite type of a value.toSqliteValue(value: any): any: Converts a value to its SQLite representation.SQLITE_TYPE_MAP: A map of JavaScript types to SQLite types.
General Tools (tool.ts)
contractId(id: string): string: Formats a contract ID.objectId(): string: Generates a new object ID.parsedJsonOrSame(json: string): any: Safely parses a JSON string.uuid(): string: Generates a UUID.
