@jackens/nnn
Version:
Jackens’ JavaScript helpers.
174 lines (173 loc) • 9.28 kB
TypeScript
/**
* 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 = Partial<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 CSV parsing.
*
* Options:
* - `header`: flag indicating that the parsed CSV has a header row (default: `true`)
* - `separator`: field separator (default: `','`)
*/
export declare const csvParse: {
(text: string): Partial<Array<Partial<Record<PropertyKey, string>>>>;
<HeaderTrue extends {
header: true;
}>(text: string, config: HeaderTrue): Partial<Array<Partial<Record<PropertyKey, string>>>>;
<HeaderFalse extends {
header: false;
}>(text: string, config: HeaderFalse): Partial<Array<Partial<Array<string>>>>;
(text: string, config: Partial<{
header: boolean;
separator: string;
}>): Partial<Array<Partial<Record<PropertyKey, string>>>> | Partial<Array<Partial<Array<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: Partial<Array<unknown>>) => Partial<Array<string>>;
/**
* A generic helper for escaping `values` by given `escapeMap` (in *TemplateStrings* flavor).
*/
export declare const escape: (escapeMap: EscapeMap, template: TemplateStringsArray, ...values: Partial<Array<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 = Partial<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 `Partial<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: Partial<Array<HArgs1>>): HTMLElementTagNameMap[T];
<N extends Node>(node: N, ...args1: Partial<Array<HArgs1>>): N;
(tagOrNode: string | Node, ...args1: Partial<Array<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 `Partial<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: Partial<Array<HArgs1>>): SVGElementTagNameMap[T];
<N extends Node>(node: N, ...args1: Partial<Array<HArgs1>>): N;
(tagOrNode: string | Node, ...args1: Partial<Array<HArgs1>>): Node;
};
/**
* A convenient shortcut for `s('svg', ['use', { 'xlink:href': '#' + id }], ...args)`.
*/
export declare const svgUse: (id: string, ...args: Partial<Array<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 `Partial<Record<PropertyKey, unknown>>`.
*/
export declare const isRecord: (arg: any) => arg is Partial<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: Partial<Record<PropertyKey, Function>>, text: string) => any;
/**
* Language translations helper.
*/
export declare const locale: (map: Partial<Record<PropertyKey, Partial<Record<PropertyKey, string>>>>, defaultVersion: string) => (text: string, version?: string) => string;
/**
* A generic helper for syntax highlighting (see also `nanolightJs`).
*/
export declare const nanolight: (pattern: RegExp, highlighters: Partial<Array<(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 extends Partial<Record<PropertyKey, unknown>>, K extends Array<keyof T>>(obj: Partial<Record<PropertyKey, unknown>>, keys: Partial<Array<unknown>>) => Pick<T, K[number]>;
/**
* A helper that implements TypeScript’s `Omit` utility type (see also `pick`).
*/
export declare const omit: <T extends Partial<Record<PropertyKey, unknown>>, K extends Array<keyof T>>(obj: Partial<Record<PropertyKey, unknown>>, keys: Partial<Array<unknown>>) => Omit<T, K[number]>;
/**
* 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;