@rwk/physics-math
Version:
Math for physics homework problems
164 lines • 6.72 kB
TypeScript
import { IUnitBase } from "./primitive-units";
import { IPFunction, Variable } from "./base";
/**
* A styler takes a LaTeX fragment and transforms it in some way.
*
* A [[Styler]] is required to be synchronous; no use of await or promises
* is allowed.
*
*/
export declare type Styler<I = string | number, R extends any[] = any[]> = (continuation: StylerFn<I>, thisStyle: Style) => StylerFn<I, R>;
declare type StylerFn<I = string | number, R extends any[] = any[]> = (ctx: StyleContext, arg: I, ...rest: R) => string;
declare type StyleKeyStyler = 'applyUnit' | 'applyUnitFunction' | 'function' | 'variable' | 'unit' | 'unitSymbol' | 'unitFraction' | 'unitProduct' | 'number' | 'call';
declare type StyleKeyData = 'exponentStyle' | 'numberSpecials' | 'numberPrecision' | 'numberFormat' | 'numberTrimTrailingZero';
/**
* The valid style keys to be used with [[setStyle]].
*/
export declare type StyleKey = StyleKeyStyler | StyleKeyData;
export declare enum NumberFormat {
normal = "normal",
scientific = "scientific",
engineering = "engineering"
}
/**
* A type map of [[StyleKey]] to [[Styler]] (plus the supporting data items).
*/
export declare type StyleMap = {
unit: Styler<IUnitBase>;
unitSymbol: Styler<IUnitBase>;
unitFraction: Styler<string, [string]>;
unitProduct: Styler<string[]>;
applyUnit: Styler<string | number, [IUnitBase]>;
applyUnitFunction: Styler<string | number, [IUnitBase]>;
number: Styler<number>;
variable: Styler<Variable>;
function: Styler<IPFunction>;
call: Styler<IPFunction, [Variable[]]>;
exponentStyle: Style | 'self' | 'exponent';
numberSpecials: Map<number, string>;
numberPrecision: number;
numberFormat: NumberFormat;
numberTrimTrailingZero: boolean;
};
declare type StyleFnFor<K extends keyof StyleMap> = K extends StyleKey ? StyleMap[K] extends Styler<infer S, infer A> ? StylerFn<S, A> : StyleMap[K] : never;
declare type StyleFnMap = {
[k in keyof StyleMap]: StyleFnFor<k>;
};
/**
* A source of [[StyleContext]] instances. Initially, you obtain a [[StyleContext]] from a [[Style]].
*/
export interface StyleContextHolder<T extends StyleContextHolder<T>> {
readonly context: StyleContext;
set(params: StyleMap): T;
wrap(params: StyleMap): T;
}
export declare class StyleContext implements StyleContextHolder<StyleContext> {
readonly style: Style;
readonly parent?: StyleContext;
constructor(style: Style, parent?: StyleContext, options?: any);
function(f: IPFunction): string;
call(f: IPFunction, vars?: Variable[]): string;
variable(v: string): string;
number(n: number): string;
applyUnit(s: string, unit: IUnitBase): string;
applyUnitFunction(s: string, unit: IUnitBase): string;
unit(unit: IUnitBase): string;
unitSymbol(unit: IUnitBase): string;
unitFraction(numer: string, denom: string): string;
unitProduct(terms: string[]): string;
get numberSpecials(): Map<number, string>;
get numberPrecision(): number;
get numberFormat(): NumberFormat;
get numberTrimTrailingZero(): boolean;
get exponentStyle(): StyleContext;
get context(): this;
set(params: StyleMap): StyleContext;
wrap(params: StyleMap): StyleContext;
}
/**
* This handles adding style formatting to the syntactic expressions components
* during LaTeX generation. It consists of a set of [[Styler]] functions, each
* of which can modify the generated LaTeX, typically by wrapping it with additional
* directives.
*
* The [[Style.set]], [[Style.wrap]], and [[Style.wrapWith]] methods can be chained
* to provide any combination.
*/
export declare class Style implements Readonly<StyleFnMap>, StyleContextHolder<Style> {
readonly call: StylerFn<IPFunction, [Variable[]]>;
readonly function: StylerFn<IPFunction>;
readonly variable: StylerFn<string>;
readonly number: StylerFn<number>;
readonly unit: StylerFn<IUnitBase>;
readonly unitSymbol: StylerFn<IUnitBase>;
readonly unitFraction: StylerFn<string, [string]>;
readonly unitProduct: StylerFn<string[]>;
readonly applyUnit: StylerFn<string | number, [IUnitBase]>;
readonly applyUnitFunction: StylerFn<string | number, [IUnitBase]>;
readonly numberFormat: NumberFormat;
readonly numberPrecision: number;
readonly numberSpecials: Map<number, string>;
readonly numberTrimTrailingZero: boolean;
readonly exponentStyle: Style;
constructor(init: StyleMap);
/**
* Returns a new [[Style]] with each [[Styler][] in `stylers` replacing
* the one in this one.
*/
set(stylers: Partial<StyleMap>): Style;
/**
* Returns a new [[Style]] with each [[Styler]] in `stylers` wrapping
* the one in this one. Each of the supplied [[Styler]] will receive
* the result of calling the current styler on the original string
* and this styler.
*/
wrap(stylers: Partial<StyleMap>): Style;
/**
* Returns a new [[Style]] with each [[Styler]] in `stylers` wrapping
* the one in this one. Each of the supplied [[Styler]] will receive
* the result of calling the current styler on the original string
* and the new styler.
*/
wrapWithNewStyle(stylers: Partial<StyleMap>): Style;
get context(): StyleContext;
}
export declare function fractions(max?: number, maxDenom?: number): Generator<number[], void, unknown>;
export declare let SPECIAL_NUMBERS: Map<number, string>;
export declare const specialNumber: Styler<number>;
export declare const chain: <I>(...stylers: Styler<I, any[]>[]) => Styler<I, any[]>;
/**
* Create a [[Styler]] that changes the color.
* @param color an [[RGB6Color]]
*/
export declare const colorStyler: (color: RGB6Color) => Styler<any>;
/**
* The stylers. These handle adding style formatting to the syntactic expressions
* components.
*/
export declare const DEFAULTS: StyleMap;
/**
* The initial [[DEFAULT_STYLE]].
*/
export declare const INITIAL_STYLE: Style;
/**
* The [[Style]] that items are formatted with by default. May be changed; the
* initial value is in [[INITIAL_STYLE]].
*
* @internal
*/
export declare let DEFAULT_STYLE: Style;
/**
* Set the default style to be used.
* @param style
*/
export declare const setStyle: (style: Style) => Style;
/**
* List of the valid style keys for [[setStyle]].
*/
export declare const LATEX_STYLE_KEYS: StyleKey[];
/**
* We take our colors in 6 RGB hex digits, e.g. RRGGBB, {RR, GG, BB} ∈ 00..FF.
*/
export declare type RGB6Color = string;
export {};
//# sourceMappingURL=latex.d.ts.map