@dra2020/baseclient 1.0.131 → 1.0.133

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.
@@ -7,11 +7,12 @@ export interface DetailResult {
7
7
  v: string;
8
8
  }
9
9
  export declare class FormatDetail {
10
- valid: boolean;
11
10
  pattern: string;
12
11
  items: DetailItem[];
12
+ _error: boolean;
13
13
  constructor(pattern: string);
14
- prepare(o: any): any;
14
+ get error(): boolean;
15
+ static prepare(o: any): any;
15
16
  format(o: any): DetailResult;
16
17
  }
17
18
  export {};
@@ -19,14 +19,19 @@ const reParam = /^__\d+$/
19
19
  class Evaluator
20
20
  {
21
21
  expr: string;
22
+ _error: boolean;
22
23
 
23
24
  constructor(expr: string)
24
25
  {
25
26
  this.expr = expr;
27
+ this._error = false;
26
28
  }
27
29
 
30
+ get error(): boolean { return this._error }
31
+
28
32
  eval(o: any): number
29
33
  {
34
+ this._error = false;
30
35
  try
31
36
  {
32
37
  // Convert property names to valid Javascript to ensure expression safety
@@ -49,7 +54,17 @@ class Evaluator
49
54
  });
50
55
 
51
56
  // Remove any identifiers that aren't the simple parameters to prevent out-of-sandbox execution
52
- safeexpr = safeexpr.replace(reIdentifier, (match) => { return reParam.test(match) ? match : "invalid" });
57
+ safeexpr = safeexpr.replace(reIdentifier,
58
+ (match) => {
59
+ let valid = reParam.test(match);
60
+ if (valid)
61
+ return match;
62
+ else
63
+ {
64
+ this._error = true;
65
+ return 'invalid';
66
+ }
67
+ });
53
68
 
54
69
  // Create a new function that accepts the variables as parameters
55
70
  // and evaluates the expression
@@ -61,6 +76,7 @@ class Evaluator
61
76
  }
62
77
  catch (err)
63
78
  {
79
+ this._error = true;
64
80
  return 0;
65
81
  }
66
82
  }
@@ -83,13 +99,13 @@ const reExpr = /^=(.*)$/
83
99
 
84
100
  export class FormatDetail
85
101
  {
86
- valid: boolean;
87
102
  pattern: string;
88
103
  items: DetailItem[];
104
+ _error: boolean;
89
105
 
90
106
  constructor(pattern: string)
91
107
  {
92
- this.valid = true;
108
+ this._error = false;
93
109
  this.pattern = pattern.trim();
94
110
  let a = reExpr.exec(pattern);
95
111
  if (a && a.length == 2)
@@ -121,12 +137,14 @@ export class FormatDetail
121
137
  }
122
138
  else
123
139
  {
124
- this.valid = false;
140
+ this._error = true;
125
141
  this.items = [ { text: 'invalid' } ];
126
142
  }
127
143
  }
128
144
 
129
- prepare(o: any): any
145
+ get error(): boolean { return this._error }
146
+
147
+ static prepare(o: any): any
130
148
  {
131
149
  if (o)
132
150
  {
@@ -149,7 +167,11 @@ export class FormatDetail
149
167
 
150
168
  format(o: any): DetailResult
151
169
  {
152
- if (!o) return { n: 0, v: '' };
170
+ if (!o)
171
+ {
172
+ this._error = true;
173
+ return { n: 0, v: '' };
174
+ }
153
175
  let n: number;
154
176
  let av = this.items.map(di => {
155
177
  if (di.text)
@@ -158,6 +180,8 @@ export class FormatDetail
158
180
  {
159
181
  let e = new Evaluator(di.expr);
160
182
  n = e.eval(o);
183
+ if (! this._error)
184
+ this._error = e.error;
161
185
  return Util.precisionRound(n, 0).toLocaleString();
162
186
  }
163
187
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dra2020/baseclient",
3
- "version": "1.0.131",
3
+ "version": "1.0.133",
4
4
  "description": "Utility functions for Javascript projects.",
5
5
  "main": "dist/baseclient.js",
6
6
  "types": "./dist/all/all.d.ts",