UNPKG

@jackens/nnn

Version:

Jackens’ JavaScript helpers.

157 lines (156 loc) 8.11 kB
/** * The type of arguments of the `c` helper. */ export type CNode = { [attributeOrSelector: string]: string | number | CNode | undefined; }; /** * The type of arguments of the `c` helper. */ export type CRoot = Record<PropertyKey, CNode>; /** * 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`). * - 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: CRoot, splitter?: string) => string; /** * A tiny helper for parsing CSV. */ export declare const csvParseRaw: (csv: string, separator?: string) => string[][]; /** * A tiny helper for parsing CSV. */ export declare const csvParse: (csv: string, separator?: string) => Record<PropertyKey, string>[]; /** * The type of arguments of the `escapeValues` and `escape` helpers. */ export type EscapeMap = Map<unknown, (value?: unknown) => string>; /** * A generic helper for escaping `values` by given `escapeMap`. */ export declare const escapeValues: (escapeMap: EscapeMap, values: unknown[]) => string[]; /** * A generic helper for escaping `values` by given `escapeMap` (in *TemplateStrings* flavor). */ export declare const escape: (escapeMap: EscapeMap, template: TemplateStringsArray, ...values: unknown[]) => string; /** * A helper that implements typographic corrections specific to Polish typography. */ export declare const fixTypography: (node: Node) => void; /** * The type of arguments of the `h` and `s` helpers. */ export type HArgs1 = Record<PropertyKey, unknown> | null | undefined | Node | string | number | HArgs; /** * The type of arguments of the `h` and `s` helpers. */ export type HArgs = [string | Node, ...HArgs1[]]; /** * 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: HArgs1[]): HTMLElementTagNameMap[T]; <N extends Node>(node: N, ...args1: HArgs1[]): N; (tagOrNode: string | Node, ...args1: HArgs1[]): 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: HArgs1[]): SVGElementTagNameMap[T]; <N extends Node>(node: N, ...args1: HArgs1[]): N; (tagOrNode: string | Node, ...args1: HArgs1[]): Node; }; /** * A convenient shortcut for `s('svg', ['use', { 'xlink:href': '#' + id }], ...args)`. */ export declare const svgUse: (id: string, ...args: HArgs1[]) => SVGSVGElement; /** * A replacement for the `in` operator (not to be confused with the `for-in` loop) that works properly. */ export declare const hasOwn: (ref: unknown, key: unknown) => boolean; /** * A helper that checks if the given argument is of type `any[]`. */ export declare const isArray: (arg: any) => arg is any[]; /** * A helper that checks if the given argument is of type `number`. */ export declare const isNumber: (arg: any) => arg is number; /** * A helper that checks if the given argument is of type `Record<PropertyKey, unknown>`. */ export declare const isRecord: (arg: any) => arg is Record<PropertyKey, unknown>; /** * A helper that checks if the given argument is of type `string`. */ export declare const isString: (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 * { "«handlerName»": [«params»] } * ``` * * are replaced by the result of call * * ```js * handlers['«handlerName»'](...«params») * ``` */ export declare const jsOnParse: (handlers: Record<PropertyKey, Function>, text: string) => any; /** * A generic helper for syntax highlighting (see also `nanolightJs`). */ export declare const nanolight: (pattern: RegExp, highlighters: ((chunk: string, index: number) => HArgs1)[], code: string) => HArgs1[]; /** * A helper for highlighting JavaScript (see also `nanolight`). */ export declare const nanolightJs: (code: string) => HArgs1[]; /** * 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 plUral: (singular: string, plural2: string, plural5: 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 uuid1: (date?: Date, node?: string) => string;