UNPKG

@jackens/nnn

Version:

Jackens’ JavaScript helpers.

158 lines (157 loc) 8.31 kB
/** * The type of arguments of the `c` helper. */ export type C_Node = { [attribute_or_selector: string]: string | number | C_Node | undefined; }; /** * The type of arguments of the `c` helper. */ export type C_Root = Record<PropertyKey, C_Node>; /** * A simple JS-to-CSS (aka CSS-in-JS) helper. * * The `root` parameter provides a hierarchical description of CSS rules. * * - Keys of sub-objects whose values are NOT objects are treated as CSS attribute, and values are treated as values of those CSS attributes; the concatenation of keys of all parent objects is a CSS rule. * - All keys ignore the part starting with a splitter (default: `$$`) sign until the end of the key (e.g. `src$$1` → `src`, `@font-face$$1` → `@font-face`). * - In keys specifying CSS attribute, all uppercase letters are replaced by lowercase letters with an additional `-` character preceding them (e.g. `fontFamily` → `font-family`), while all `_` characters are replaced by `-` character (e.g. `font_family` → `font-family`). * - Commas in keys that makes a CSS rule cause it to “split” and create separate rules for each part (e.g. `{div:{margin:1,'.a,.b,.c':{margin:2}}}` → `div{margin:1}div.a,div.b,div.c{margin:2}`). * - Top-level keys that begin with `@` are not concatenated with sub-object keys. */ export declare const c: (root: C_Root, splitter?: string) => string; /** * A tiny helper for parsing CSV. */ export declare const csv_parse_raw: (csv: string, separator?: string) => string[][]; /** * A tiny helper for parsing CSV. */ export declare const csv_parse: (csv: string, separator?: string) => Record<PropertyKey, string>[]; /** * The type of arguments of the `escape_values` and `escape` helpers. */ export type Escape_Map = Map<unknown, (value?: unknown) => string>; /** * A generic helper for escaping `values` by given `escape_map`. */ export declare const escape_values: (escape_map: Escape_Map, values: unknown[]) => string[]; /** * A generic helper for escaping `values` by given `escape_map` (in *TemplateStrings* flavor). */ export declare const escape: (escape_map: Escape_Map, template: TemplateStringsArray, ...values: unknown[]) => string; /** * A helper that implements typographic corrections specific to Polish typography. */ export declare const fix_typography: (node: Node) => void; /** * The type of arguments of the `h` and `s` helpers. */ export type H_Args_1 = Record<PropertyKey, unknown> | null | undefined | Node | string | number | H_Args; /** * The type of arguments of the `h` and `s` helpers. */ export type H_Args = [string | Node, ...H_Args_1[]]; /** * A lightweight [HyperScript](https://github.com/hyperhype/hyperscript)-style helper for creating and modifying `HTMLElement`s (see also `s`). * * - The first argument of type `string` specifies the tag of the element to be created. * - The first argument of type `Node` specifies the element to be modified. * - All other arguments of type `Record<PropertyKey, unknown>` are mappings of attributes and properties. Keys starting with `$` specify *properties* (without the leading `$`) to be set on the element being created or modified. (Note that `$` is not a valid attribute name character.) All other keys specify *attributes* to be set by `setAttribute`. An attribute equal to `false` causes the attribute to be removed by `removeAttribute`. * - All other arguments of type `null` or `undefined` are simply ignored. * - All other arguments of type `Node` are appended to the element being created or modified. * - All other arguments of type `string`/`number` are converted to `Text` nodes and appended to the element being created or modified. * - All other arguments of type `HArgs` are passed to `h` and the results are appended to the element being created or modified. */ export declare const h: { <T extends keyof HTMLElementTagNameMap>(tag: T, ...args1: H_Args_1[]): HTMLElementTagNameMap[T]; <N extends Node>(node: N, ...args1: H_Args_1[]): N; (tag_or_node: string | Node, ...args1: H_Args_1[]): Node; }; /** * A lightweight [HyperScript](https://github.com/hyperhype/hyperscript)-style helper for creating and modifying `SVGElement`s (see also `h`). * * - The first argument of type `string` specifies the tag of the element to be created. * - The first argument of type `Node` specifies the element to be modified. * - All other arguments of type `Record<PropertyKey, unknown>` are mappings of attributes and properties. Keys starting with `$` specify *properties* (without the leading `$`) to be set on the element being created or modified. (Note that `$` is not a valid attribute name character.) All other keys specify *attributes* to be set by `setAttributeNS`. An attribute equal to `false` causes the attribute to be removed by `removeAttributeNS`. * - All other arguments of type `null` or `undefined` are simply ignored. * - All other arguments of type `Node` are appended to the element being created or modified. * - All other arguments of type `string`/`number` are converted to `Text` nodes and appended to the element being created or modified. * - All other arguments of type `HArgs` are passed to `s` and the results are appended to the element being created or modified. */ export declare const s: { <T extends keyof SVGElementTagNameMap>(tag: T, ...args1: H_Args_1[]): SVGElementTagNameMap[T]; <N extends Node>(node: N, ...args1: H_Args_1[]): N; (tag_or_node: string | Node, ...args1: H_Args_1[]): Node; }; /** * A convenient shortcut for `s('svg', ['use', { 'xlink:href': '#' + id }], ...args)`. */ export declare const svg_use: (id: string, ...args: H_Args_1[]) => SVGSVGElement; /** * A replacement for the `in` operator (not to be confused with the `for-in` loop) that works properly. */ export declare const has_own: (ref: unknown, key: unknown) => boolean; /** * A helper that checks if the given argument is of type `any[]`. */ export declare const is_array: (arg: any) => arg is any[]; /** * A helper that checks if the given argument is of type `number`. */ export declare const is_number: (arg: any) => arg is number; /** * A helper that checks if the given argument is of type `Record<PropertyKey, unknown>`. */ export declare const is_record: (arg: any) => arg is Record<PropertyKey, unknown>; /** * A helper that checks if the given argument is of type `string`. */ export declare const is_string: (arg: any) => arg is string; /** * `JSON.parse` with “JavaScript turned on”. * * Objects having *exactly* one property which is present in the `handlers` map, i.e. objects of the form: * * ```js * { "«handler_name»": [«params»] } * ``` * * are replaced by the result of call * * ```js * handlers['«handler_name»'](...«params») * ``` */ export declare const js_on_parse: (handlers: Record<PropertyKey, Function>, text: string) => any; import type { H_Args_1 } from './h.js'; /** * A generic helper for syntax highlighting (see also `nanolight_js`). */ export declare const nanolight: (pattern: RegExp, highlighters: ((chunk: string, index: number) => H_Args_1)[], code: string) => H_Args_1[]; /** * A helper for highlighting JavaScript (see also `nanolight`). */ export declare const nanolight_js: (code: string) => H_Args_1[]; /** * A helper that implements TypeScript’s `Pick` utility type (see also `omit`). */ export declare const pick: <T, K extends keyof T>(ref: T, keys: K[]) => Pick<T, K>; /** * A helper that implements TypeScript’s `Omit` utility type (see also `pick`). */ export declare const omit: <T, K extends keyof T>(ref: T, keys: unknown[]) => Omit<T, K>; /** * A helper for choosing the correct singular and plural. */ export declare const pl_ural: (singular: string, plural_2: string, plural_5: string, value: number) => string; /** * A helper that protects calls to nested properties by a `Proxy` that initializes non-existent values with an empty object. */ export declare const pro: (ref: unknown) => any; /** * A helper that generates a UUID v1 identifier (with a creation timestamp). * * - The optional `node` parameter should have the format `/^[0123456789abcdef]+$/`. Its value will be trimmed to last 12 characters and left padded with zeros. */ export declare const uuid_v1: (date?: Date, node?: string) => string;