@daiso-tech/core
Version:
The library offers flexible, framework-agnostic solutions for modern web applications, built on adaptable components that integrate seamlessly with popular frameworks like Next Js.
77 lines (76 loc) • 2.24 kB
TypeScript
/**
* @module Namespace
*/
import { type IKey, type INamespace } from "../../namespace/contracts/_module.js";
import { type ISerializable } from "../../serde/contracts/flexible-serde.contract.js";
import { type OneOrMore } from "../../utilities/_module.js";
/**
* Configuration for the `Namespace` class.
*
* IMPORT_PATH: `"@daiso-tech/core/namespace"`
*/
export type NamespaceSettings = {
/**
* The separator character inserted between each namespace segment.
* @default ":"
*/
delimiter?: string;
/**
* The string appended to the root namespace segment to distinguish it.
* @default "_rt"
*/
rootIdentifier?: string;
};
export type SerializedNamespace = {
version: "1";
root: string | Array<string>;
delimiter: string;
rootIdentifier: string;
};
/**
* The `Namespace` class adds prefixes/suffixes to keys to avoid conflicts and group related items.
*
* IMPORT_PATH: `"@daiso-tech/core/namespace"`
* @group Implementations
*
* @example
* ```ts
* import { Namespace } from "@daiso-tech/core/namspace";
*
* const namespace = new Namespace("@my-namespace");
*
* // Logs "@my-namespace:_rt"
* console.log(namespace.toString());
*
* const key = namespace.create("my-key");
*
* // Logs "my-key"
* console.log(key.get())
*
* // Logs "@my-namespace:_rt:my-key"
* console.log(key.toString())
*
* // You can extend the root
* const newNamespace = namespace.appendRoot("sub");
*
* // Logs "@my-namespace:sub:_rt"
* console.log(newNamespace.toString());
*
* ```
*/
export declare class Namespace implements INamespace, ISerializable<SerializedNamespace> {
private readonly root;
static deserialize(serialized: SerializedNamespace): Namespace;
private readonly delimiter;
private readonly rootIdentifier;
constructor(root: OneOrMore<string>, settings?: NamespaceSettings);
serialize(): SerializedNamespace;
setdelimiter(delimiter: string): INamespace;
setRootIdentifier(identifier: string): INamespace;
appendRoot(str: OneOrMore<string>): INamespace;
prependRoot(str: OneOrMore<string>): INamespace;
private validate;
private getKeyPrefixArray;
toString(): string;
create(key: string): IKey;
}