pimath 0.0.105 → 0.0.107

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/pi.js CHANGED
@@ -4990,14 +4990,25 @@ const fraction_1 = __webpack_require__(506);
4990
4990
  class NumExp {
4991
4991
  constructor(value, uniformize) {
4992
4992
  this._expression = value;
4993
- this._rpn = new shutingyard_1.Shutingyard(shutingyard_1.ShutingyardMode.NUMERIC).parse(value).rpn;
4993
+ try {
4994
+ this._rpn = new shutingyard_1.Shutingyard(shutingyard_1.ShutingyardMode.NUMERIC).parse(value, uniformize || uniformize === undefined).rpn;
4995
+ }
4996
+ catch (e) {
4997
+ this._rpn = null;
4998
+ this._isValid = false;
4999
+ }
4994
5000
  }
4995
5001
  get rpn() {
4996
5002
  return this._rpn;
4997
5003
  }
4998
5004
  get isValid() {
4999
5005
  if (this._isValid === undefined) {
5000
- this.evaluate({ x: 0 });
5006
+ try {
5007
+ const v = this.evaluate({ x: 0 });
5008
+ }
5009
+ catch {
5010
+ this._isValid = false;
5011
+ }
5001
5012
  }
5002
5013
  return this._isValid;
5003
5014
  }
@@ -5052,6 +5063,10 @@ class NumExp {
5052
5063
  }
5053
5064
  evaluate(values) {
5054
5065
  const stack = [];
5066
+ if (this._rpn === null) {
5067
+ this._isValid = false;
5068
+ return 0;
5069
+ }
5055
5070
  this.isValid = true;
5056
5071
  for (const element of this._rpn) {
5057
5072
  if (element.tokenType === shutingyard_1.ShutingyardType.COEFFICIENT) {
@@ -7608,78 +7623,92 @@ class Shutingyard {
7608
7623
  // add the last token
7609
7624
  return normalizedExpr + nextToken;
7610
7625
  }
7611
- /**
7612
- * Sanitize an expression by adding missing common operation (multiplication between parentheseses)
7613
- * @param expr
7614
- * @constructor
7615
- */
7616
- Uniformizer(expr) {
7617
- // TODO: Delete this old version
7618
- // Prefere "normalize", much more robust !
7619
- // Determiner if need to be uniformized
7620
- if (!this._uniformize) {
7621
- return expr;
7622
- }
7623
- // Generate the list of function token.
7624
- let fnToken = [];
7625
- for (let token in this._tokenConfig) {
7626
- if (this._tokenConfig[token].type === ShutingyardType.FUNCTION) {
7627
- fnToken.push(token);
7628
- }
7629
- }
7630
- // sort if from the lengthy to the smallest function
7631
- fnToken.sort((a, b) => b.length - a.length);
7632
- let tokenRegExp = new RegExp(`(${fnToken.join('|')})`, 'g');
7633
- let functionTokenOrder = Array.from(expr.matchAll(tokenRegExp));
7634
- let expr2;
7635
- // Replace all function by @
7636
- expr2 = expr.replace(tokenRegExp, '@');
7637
- // Add * before @ (functionn)
7638
- expr2 = expr2.replace(/([\da-zA-Z])(@)/g, "$1*$2");
7639
- // Replace missing multiplication between two parenthese
7640
- expr2 = expr2.replace(/\)\(/g, ')*(');
7641
- // Replace missing multiplication between number or setLetter and parenthese.
7642
- // 3x(x-4) => 3x*(x-4)
7643
- expr2 = expr2.replace(/([\da-zA-Z])(\()/g, "$1*$2");
7644
- // (x-4)3x => (x-4)*3x
7645
- expr2 = expr2.replace(/(\))([\da-zA-Z])/g, "$1*$2");
7646
- // Add multiplication between number and letters.
7647
- // 3x => 3*x
7648
- expr2 = expr2.replace(/([0-9])([a-zA-Z])/g, "$1*$2");
7649
- expr2 = expr2.replace(/([a-zA-Z])([0-9])/g, "$1*$2");
7650
- // Remove letter between function token and it's parenthese.
7651
- // for (let token of fnToken) {
7652
- // // Remove
7653
- // expr2 = expr2.replace(new RegExp(token + '\\*', 'g'), token);
7654
- // }
7655
- // Add multiplication between letters ?
7656
- expr2 = expr2.replace(/([a-zA-Z])([a-zA-Z])/g, "$1*$2");
7657
- expr2 = expr2.replace(/([a-zA-Z])([a-zA-Z])/g, "$1*$2");
7658
- // Restore operation auto formatting (prevent adding the multiplication star)
7659
- let exprAsArray = expr2.split('@');
7660
- if (exprAsArray.length > 0) {
7661
- expr2 = "";
7662
- for (let idx in exprAsArray) {
7663
- }
7664
- for (let token of fnToken) {
7665
- // Remove
7666
- // expr2 = expr2.replace(new RegExp(token + '\\*', 'g'), token);
7667
- }
7668
- }
7669
- return expr2;
7670
- }
7626
+ // /**
7627
+ // * Sanitize an expression by adding missing common operation (multiplication between parentheseses)
7628
+ // * @param expr
7629
+ // * @constructor
7630
+ // */
7631
+ // Uniformizer(expr: string): string {
7632
+ // // TODO: Delete this old version
7633
+ // // Prefere "normalize", much more robust !
7634
+ // // Determiner if need to be uniformized
7635
+ // if (!this._uniformize) {
7636
+ // return expr
7637
+ // }
7638
+ //
7639
+ // // Generate the list of function token.
7640
+ // let fnToken: string[] = []
7641
+ // for (let token in this._tokenConfig) {
7642
+ // if (this._tokenConfig[token].type === ShutingyardType.FUNCTION) {
7643
+ // fnToken.push(token)
7644
+ // }
7645
+ // }
7646
+ // // sort if from the lengthy to the smallest function
7647
+ // fnToken.sort((a, b) => b.length - a.length)
7648
+ // let tokenRegExp = new RegExp(`(${fnToken.join('|')})`, 'g')
7649
+ // let functionTokenOrder = Array.from(expr.matchAll(tokenRegExp))
7650
+ //
7651
+ //
7652
+ // let expr2;
7653
+ //
7654
+ // // Replace all function by @
7655
+ // expr2 = expr.replace(tokenRegExp, '@')
7656
+ // // Add * before @ (functionn)
7657
+ // expr2 = expr2.replace(/([\da-zA-Z])(@)/g, "$1*$2");
7658
+ //
7659
+ // // Replace missing multiplication between two parenthese
7660
+ // expr2 = expr2.replace(/\)\(/g, ')*(');
7661
+ //
7662
+ // // Replace missing multiplication between number or setLetter and parenthese.
7663
+ //
7664
+ // // 3x(x-4) => 3x*(x-4)
7665
+ // expr2 = expr2.replace(/([\da-zA-Z])(\()/g, "$1*$2");
7666
+ //
7667
+ // // (x-4)3x => (x-4)*3x
7668
+ // expr2 = expr2.replace(/(\))([\da-zA-Z])/g, "$1*$2");
7669
+ //
7670
+ // // Add multiplication between number and letters.
7671
+ // // 3x => 3*x
7672
+ // expr2 = expr2.replace(/([0-9])([a-zA-Z])/g, "$1*$2");
7673
+ // expr2 = expr2.replace(/([a-zA-Z])([0-9])/g, "$1*$2");
7674
+ //
7675
+ // // Remove letter between function token and it's parenthese.
7676
+ // // for (let token of fnToken) {
7677
+ // // // Remove
7678
+ // // expr2 = expr2.replace(new RegExp(token + '\\*', 'g'), token);
7679
+ // // }
7680
+ // // Add multiplication between letters ?
7681
+ // expr2 = expr2.replace(/([a-zA-Z])([a-zA-Z])/g, "$1*$2");
7682
+ // expr2 = expr2.replace(/([a-zA-Z])([a-zA-Z])/g, "$1*$2");
7683
+ //
7684
+ // // Restore operation auto formatting (prevent adding the multiplication star)
7685
+ // let exprAsArray = expr2.split('@')
7686
+ //
7687
+ // if (exprAsArray.length > 0) {
7688
+ // expr2 = ""
7689
+ // for (let idx in exprAsArray) {
7690
+ // }
7691
+ // for (let token of fnToken) {
7692
+ // // Remove
7693
+ //
7694
+ // // expr2 = expr2.replace(new RegExp(token + '\\*', 'g'), token);
7695
+ // }
7696
+ // }
7697
+ //
7698
+ // return expr2;
7699
+ // }
7671
7700
  /**
7672
7701
  * Parse an expression using the shutting yard tree algorithms
7673
7702
  * @param expr (string) Expression to analyse
7674
7703
  * Returns a RPN list of items.
7675
- * @param operators
7704
+ * @param uniformize
7676
7705
  */
7677
- parse(expr, operators) {
7706
+ parse(expr, uniformize) {
7678
7707
  let outQueue = [], // Output queue
7679
7708
  opStack = [], // Operation queue
7680
7709
  token = '', tokenPos = 0, tokenType = '', previousOpStatckLength = 0;
7681
7710
  // Normalize the input if required.
7682
- if (this._uniformize)
7711
+ if (uniformize || this._uniformize)
7683
7712
  expr = this.normalize(expr);
7684
7713
  let securityLoopLvl1 = 50, securityLoopLvl2_default = 50, securityLoopLvl2;
7685
7714
  while (tokenPos < expr.length) {