@nice2dev/ui-math 1.2.0 → 1.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -43,7 +43,12 @@ export declare class CalculatorService {
43
43
  private variables;
44
44
  private config;
45
45
  constructor(config?: CalculatorConfig);
46
- /** Evaluate expression */
46
+ /**
47
+ * Evaluate an expression — parsed by {@link ExpressionParser}, never run as code (until 1.2.0 a
48
+ * chain of regex replacements fed `new Function`: `asin` came out as `aMath.sin`, `ceil` lost
49
+ * its `e` to Euler's number, a result in exponent notation summed wrongly, and any text ran).
50
+ * `log` is base 10 like the key, `ln` natural; trig functions follow `angleUnit`.
51
+ */
47
52
  evaluate(expression: string): number;
48
53
  /** Add to history */
49
54
  addHistory(expression: string, result: string, mode: CalculatorMode): void;
@@ -119,21 +124,40 @@ export declare interface EquationTemplate {
119
124
  }
120
125
 
121
126
  /**
122
- * Simple expression parser for math expressions.
123
- * Supports: +, -, *, /, ^, (), functions, variables
127
+ * Expression parser for math expressions — no `eval`: the input is parsed into a tree and the
128
+ * tree is evaluated, so only numbers, the listed functions and the given variables exist.
129
+ * Supports: + - * / ^ (right-associative, above unary minus: `-2^2` = -4), parentheses, function
130
+ * calls, variables, numbers with an exponent (`1.5e-7`) and implicit multiplication (`2x`, `2pi`,
131
+ * `2(x+1)`). `×`, `÷`, `−`, `π` and `√` are read as `*`, `/`, `-`, `pi` and `sqrt`.
124
132
  */
125
133
  export declare class ExpressionParser {
126
134
  private pos;
127
135
  private expr;
136
+ private readonly options;
137
+ constructor(options?: ExpressionParserOptions);
128
138
  parse(expression: string): MathFunction;
129
139
  private parseAddSub;
130
140
  private parseMulDiv;
131
- private parsePower;
132
141
  private parseUnary;
142
+ private parsePower;
133
143
  private parsePrimary;
144
+ /** A name from a table, honouring `ignoreCase`. */
145
+ private lookup;
134
146
  private evaluate;
135
147
  }
136
148
 
149
+ /** Options of {@link ExpressionParser}; every one is optional and off by default. */
150
+ export declare interface ExpressionParserOptions {
151
+ /** Functions by name, over the built-ins (e.g. `{ log: Math.log10 }` for a calculator). */
152
+ functions?: Record<string, (...args: number[]) => number>;
153
+ /** Constants by name, over `pi`, `e`, `phi`, `tau`. */
154
+ constants?: Record<string, number>;
155
+ /** Match function and constant names case-insensitively (`SIN`, `Pi`). */
156
+ ignoreCase?: boolean;
157
+ /** Throw on input left over after a whole expression, instead of ignoring it. */
158
+ strict?: boolean;
159
+ }
160
+
137
161
  export declare const FUNCTION_PRESETS: FunctionPreset[];
138
162
 
139
163
  export declare interface FunctionPreset {
@@ -564,7 +588,7 @@ export declare interface NiceGraphPlotterProps {
564
588
  style?: default_2.CSSProperties;
565
589
  }
566
590
 
567
- export declare function NiceI18nProvider({ locale, overrides, children }: NiceI18nProviderProps): default_2.FunctionComponentElement<default_2.ProviderProps<I18nContextValue>>;
591
+ export declare function NiceI18nProvider({ locale, overrides, children }: NiceI18nProviderProps): default_2.FunctionComponentElement<default_2.ProviderProps<I18nContextValue | null>>;
568
592
 
569
593
  export declare interface NiceI18nProviderProps {
570
594
  /** Language code, e.g. `'pl'`, `'cs'`. Falls back to English when unknown. */
@@ -679,6 +703,12 @@ export declare function useGraph(): GraphContextValue;
679
703
 
680
704
  export declare function useMathEditor(): MathEditorContextValue;
681
705
 
706
+ /**
707
+ * This package's translator: under its `NiceI18nProvider`, that provider's language and overrides;
708
+ * without one (since 1.2.2), the language of the app's global `NiceI18nProvider` — English when
709
+ * this dictionary does not have it. Before 1.2.2 the controls stayed English without the
710
+ * package's own provider, whatever language the app used.
711
+ */
682
712
  export declare function useNiceTranslation(): {
683
713
  t: NiceTranslateFn;
684
714
  locale: string;