shelving
Version:
Toolkit for using data in JavaScript.
18 lines (17 loc) • 1.35 kB
TypeScript
import type { AnyCaller } from "./function.js";
import type { Optional } from "./optional.js";
/** Entity strings combine a type and ID, e.g. `challenge:a1b2c3` */
export type Entity<T extends string = string> = `${T}:${string}`;
/** Extract the type from an `Entity` string. */
export type EntityType<E extends Entity> = E extends Entity<infer T> ? T : never;
/** Empty entity has undefined type and ID. */
export type EmptyEntity = [type: undefined, id: undefined];
/** Empty entity. */
export declare const EMPTY_ENTITY: EmptyEntity;
/** Split an optional entity tag like `challenge:a1b2c3` into `["challenge", "a1b2c3"]`, or return `EmptyEntity` if the entity was invalid. */
export declare function getEntity<T extends string>(entity: Entity<T>): [type: T, id: string];
export declare function getEntity<T extends string>(entity: Optional<Entity<T>>): [type: T, id: string] | EmptyEntity;
export declare function getEntity(entity: Optional<string>): [type: string, id: string] | EmptyEntity;
/** Split an entity tag like `challenge:a1b2c3` into `type` and `id`, or throw `RequiredError` if the entity tag was invalid. */
export declare function requireEntity<T extends string>(entity: Entity<T>, caller?: AnyCaller): [type: T, id: string];
export declare function requireEntity(entity: string, caller?: AnyCaller): [type: string, id: string];