@barchart/chart-lib 2.330.1 → 2.331.0

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.
@@ -2434,16 +2434,35 @@ declare module "@barchart/chart-lib" {
2434
2434
  /** A helper method to return a simplified structure of the chart. */
2435
2435
  function getChartOutline(definitionOrTemplate: string): IChartOutline;
2436
2436
 
2437
- /** Please consider this as an opaque structure. */
2438
- interface ExpressionInternalModel {}
2437
+ /** An operator of an expression, including syntetic (internal) operators
2438
+ * like 'sym', 'num' and 'grp'.
2439
+ */
2440
+ export type ExprOperator = "+" | "-" | "*" | "/" | "^" | "sym" | "num" | "grp";
2441
+
2442
+ /** A root node of the expression AST.
2443
+ * You will rarely if ever need to look at or navigate this tree.
2444
+ */
2445
+ export interface ExprNode {
2446
+ op: ExprOperator;
2447
+ left: ExprNodeOrVal;
2448
+ right: ExprNodeOrVal;
2449
+ }
2450
+
2451
+ /** The only three possible values stored in an expression. */
2452
+ export type ExprValue = string | number | null;
2453
+
2454
+ /** The expression AST tree is heterogenous, each node can be a value (a leaf)
2455
+ * or a node pointing at other nodes.
2456
+ */
2457
+ export type ExprNodeOrVal = ExprNode | ExprValue;
2439
2458
 
2440
2459
  /** A structure representing a successful parse of an expression. */
2441
2460
  export interface ExpressionSuccess {
2442
2461
  /** The expression given to be parsed. */
2443
2462
  text: string;
2444
- /** An internal expression model, basically an AST of the expression. */
2445
- model: ExpressionInternalModel;
2446
- /** The same expression given on input but parsed and formatter - basically a cleaned up version of the expression with most whitespace thrown out. */
2463
+ /** The (root of the) AST of the expression. */
2464
+ model: ExprNode;
2465
+ /** The same expression given on input but parsed and formatted - basically a cleaned up version of the expression with most whitespace thrown out. */
2447
2466
  clean: string;
2448
2467
  /** The list of symbols found while parsing the expression. */
2449
2468
  symbols: string[];
@@ -2460,18 +2479,40 @@ declare module "@barchart/chart-lib" {
2460
2479
  /** If you would like to parse expressions yourself (chart will do this automatically for you).
2461
2480
  * @param text The text of the expression. Syntax is described in detail on our documentation site.
2462
2481
  * @param useCache (`false` by default) If the parsing code should cache the expressions parsed.
2463
- * @returns Either a successful parse result @see ExpressionSuccess or a failed result @see ExpressionError
2482
+ * @returns Either a successful parse result {@link ExpressionSuccess} or a failed result {@link ExpressionError}
2464
2483
  */
2465
2484
  function parseExpression(text: string, useCache?: boolean): ExpressionSuccess | ExpressionError;
2466
2485
 
2467
2486
  export type ValueProviderFunc = (symbol: string, field?: string) => number | null;
2468
2487
 
2469
2488
  /** If you would like to evaluate an expression yourself.
2470
- * @param model Same as @see model property of the @see ExpressionSuccess
2489
+ * @param model The expression AST (see {@link ExprNode})
2471
2490
  * @param valueProvider A function which, given a symbol name, returns a price.
2472
2491
  * @returns The computed value of the expression.
2473
2492
  */
2474
- function evaluateExpression(model: ExpressionInternalModel, valueProvider: ValueProviderFunc): number;
2493
+ function evaluateExpression(model: ExprNode, valueProvider: ValueProviderFunc): number;
2494
+
2495
+ /** Produces well-formatted string out of the expression AST
2496
+ * @param model The expression AST (see {@link ExprNode})
2497
+ * @returns Expression formatted as a string
2498
+ */
2499
+ function formatExpression(model: ExprNode): string;
2500
+
2501
+ /** Returns a list of symbols found in the expression AST
2502
+ * @param model The expression AST (see {@link ExprNode})
2503
+ * @returns Symbols as an array of strings
2504
+ */
2505
+ function getExpressionSymbols(model: ExprNode): string[];
2506
+
2507
+ export type StringReplacerFunc = (symbol: string) => string;
2508
+
2509
+ /** Replace symbols in an expression. The library will traverse the tree and call you back
2510
+ * for each symbol name found, you may return the same name or a different one.
2511
+ * @param model The expression AST (see {@link ExprNode})
2512
+ * @param replacer A callback which performs the replacement
2513
+ * @returns A new AST tree (see {@link ExprNode}) with the symbols replaced
2514
+ */
2515
+ function replaceExpressionSymbols(model: ExprNode, replacer: StringReplacerFunc): ExprNode;
2475
2516
 
2476
2517
  interface StudyMetaRange {
2477
2518
  min: number;
@@ -2845,6 +2886,9 @@ declare module "@barchart/chart-lib" {
2845
2886
  SnapshotDataFeed,
2846
2887
  parseExpression,
2847
2888
  evaluateExpression,
2889
+ formatExpression,
2890
+ getExpressionSymbols,
2891
+ replaceExpressionSymbols,
2848
2892
  Topics,
2849
2893
  PubSub,
2850
2894
  };