UNPKG

@nfps.dev/runtime

Version:

Runtime library for NFPs

49 lines (48 loc) 3.91 kB
import type { L, S } from 'ts-toolbelt'; import type { HtmlNodeCreator, SvgNodeCreator } from './web'; import { type JsonValue } from '@blake.regalia/belt'; type TrimLeft<s_text extends string> = s_text extends ` ${infer s_trimmed}` ? TrimLeft<s_trimmed> : s_text; type TrimRight<s_text extends string> = s_text extends `${infer s_trimmed} ` ? TrimRight<s_trimmed> : s_text; type Trim<s_text extends string> = TrimLeft<TrimRight<s_text>>; type RemoveAfter<s_delim extends string, s_text extends string> = s_text extends `${infer s_prefix}${s_delim}${string}` ? s_prefix : s_text; type RemoveModifiers<s_text extends string> = RemoveAfter<':', RemoveAfter<'[', RemoveAfter<'#', RemoveAfter<'.', s_text>>>>; type TakeLastAfterToken<s_token extends string, s_text extends string> = RemoveModifiers<L.Last<S.Split<Trim<s_text>, s_token>>>; type GetLastElementName<sq_selector extends string> = TakeLastAfterToken<'>', TakeLastAfterToken<' ', sq_selector>>; type GetEachElementName<z_parts, a_out extends string[] = []> = z_parts extends [] ? a_out : z_parts extends [string] ? [...a_out, GetLastElementName<z_parts[0]>] : z_parts extends [string, ...infer s_part] ? GetEachElementName<s_part, [...a_out, GetLastElementName<z_parts[0]>]> : []; type GetElementNames<sq_selector extends string> = GetEachElementName<S.Split<sq_selector, ','>>; type ElementByName<si_tag extends string> = si_tag extends keyof HTMLElementTagNameMap ? HTMLElementTagNameMap[si_tag] : si_tag extends keyof SVGElementTagNameMap ? SVGElementTagNameMap[si_tag] : HTMLElement | SVGElement; type MatchEachElement<a_tags, d_coerce extends Element = Element> = a_tags extends [] ? d_coerce : a_tags extends [string] ? ElementByName<a_tags[0]> : a_tags extends [string, ...infer a_rest] ? MatchEachElement<a_rest, d_coerce | ElementByName<a_tags[0]>> : d_coerce; type QueryResult<sq_selector extends string> = MatchEachElement<GetElementNames<sq_selector>>; /** * Typed querySelector * @param dm_node * @param sq_selector * @returns */ export declare const qs: <sq_coerce extends string = string, sq_selector extends string = string>(dm_node: { querySelector: Element['querySelector']; }, sq_selector: sq_selector) => MatchEachElement<GetEachElementName<S.Split<sq_coerce extends `${infer s_ignore}` ? sq_coerce : sq_selector, ",">, []>, Element> | null; /** * Typed querySelectorAll * @param dm_node * @param sq_selector * @returns */ export declare const qsa: <sq_coerce extends string = string, sq_selector extends string = string>(dm_node: { querySelectorAll: Element['querySelectorAll']; }, sq_selector: sq_selector) => MatchEachElement<GetEachElementName<S.Split<sq_coerce extends `${infer s_ignore}` ? sq_coerce : sq_selector, ",">, []>, Element>[]; export declare const create_svg: SvgNodeCreator; export declare const create_html: HtmlNodeCreator; export declare const ls_read: (si_key: string) => string | null; export declare const ls_write: <s_value extends string>(si_key: string, s_value: string) => s_value; export declare const ls_read_json: <w_out extends JsonValue>(si_key: string) => w_out | undefined; export declare const ls_write_json: <w_value extends JsonValue>(si_key: string, w_value: JsonValue) => w_value; export declare const ls_read_b64: (si_key: string) => Uint8Array | null; export declare const ls_write_b64: (si_key: string, atu8_data: Uint8Array) => Uint8Array; export type StructuredCloneable = null | string | number | boolean | Date | RegExp | Blob | FileList | ImageData | StructuredCloneable[] | { [key: string]: StructuredCloneable; } | Map<StructuredCloneable, StructuredCloneable> | Set<StructuredCloneable> | ArrayBuffer; export declare const idb_read: (w_key: IDBValidKey) => Promise<StructuredCloneable>; type IdbWriter = <w_value extends StructuredCloneable>(si_key: IDBValidKey, w_value: w_value) => Promise<w_value>; export declare const idb_write: IdbWriter; export {};