@dra2020/baseclient 1.0.132 → 1.0.134

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.
@@ -1,3 +1,6 @@
1
+ export interface DetailOptions {
2
+ numberFormat?: (n: number) => string;
3
+ }
1
4
  interface DetailItem {
2
5
  expr?: string;
3
6
  text?: string;
@@ -7,10 +10,12 @@ export interface DetailResult {
7
10
  v: string;
8
11
  }
9
12
  export declare class FormatDetail {
10
- valid: boolean;
11
13
  pattern: string;
12
14
  items: DetailItem[];
13
- constructor(pattern: string);
15
+ _error: boolean;
16
+ options: DetailOptions;
17
+ constructor(pattern: string, options?: DetailOptions);
18
+ get error(): boolean;
14
19
  static prepare(o: any): any;
15
20
  format(o: any): DetailResult;
16
21
  }
@@ -16,17 +16,34 @@ import * as Util from '../util/all';
16
16
  const reIdentifier = /\b[a-zA-Z_$][a-zA-Z0-9_$]*\b/g;
17
17
  const reParam = /^__\d+$/
18
18
 
19
+ function defaultNumberFormat(n: number): string
20
+ {
21
+ return Util.precisionRound(n, 0).toLocaleString();
22
+ }
23
+
24
+ export interface DetailOptions
25
+ {
26
+ numberFormat?: (n: number) => string,
27
+ }
28
+
29
+ const DefaultDetailOptions: DetailOptions = { numberFormat: defaultNumberFormat };
30
+
19
31
  class Evaluator
20
32
  {
21
33
  expr: string;
34
+ _error: boolean;
22
35
 
23
36
  constructor(expr: string)
24
37
  {
25
38
  this.expr = expr;
39
+ this._error = false;
26
40
  }
27
41
 
42
+ get error(): boolean { return this._error }
43
+
28
44
  eval(o: any): number
29
45
  {
46
+ this._error = false;
30
47
  try
31
48
  {
32
49
  // Convert property names to valid Javascript to ensure expression safety
@@ -49,7 +66,17 @@ class Evaluator
49
66
  });
50
67
 
51
68
  // 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" });
69
+ safeexpr = safeexpr.replace(reIdentifier,
70
+ (match) => {
71
+ let valid = reParam.test(match);
72
+ if (valid)
73
+ return match;
74
+ else
75
+ {
76
+ this._error = true;
77
+ return 'invalid';
78
+ }
79
+ });
53
80
 
54
81
  // Create a new function that accepts the variables as parameters
55
82
  // and evaluates the expression
@@ -61,6 +88,7 @@ class Evaluator
61
88
  }
62
89
  catch (err)
63
90
  {
91
+ this._error = true;
64
92
  return 0;
65
93
  }
66
94
  }
@@ -83,13 +111,15 @@ const reExpr = /^=(.*)$/
83
111
 
84
112
  export class FormatDetail
85
113
  {
86
- valid: boolean;
87
114
  pattern: string;
88
115
  items: DetailItem[];
116
+ _error: boolean;
117
+ options: DetailOptions;
89
118
 
90
- constructor(pattern: string)
119
+ constructor(pattern: string, options?: DetailOptions)
91
120
  {
92
- this.valid = true;
121
+ this.options = Util.shallowAssignImmutable(DefaultDetailOptions, options);
122
+ this._error = false;
93
123
  this.pattern = pattern.trim();
94
124
  let a = reExpr.exec(pattern);
95
125
  if (a && a.length == 2)
@@ -121,11 +151,13 @@ export class FormatDetail
121
151
  }
122
152
  else
123
153
  {
124
- this.valid = false;
154
+ this._error = true;
125
155
  this.items = [ { text: 'invalid' } ];
126
156
  }
127
157
  }
128
158
 
159
+ get error(): boolean { return this._error }
160
+
129
161
  static prepare(o: any): any
130
162
  {
131
163
  if (o)
@@ -149,7 +181,11 @@ export class FormatDetail
149
181
 
150
182
  format(o: any): DetailResult
151
183
  {
152
- if (!o) return { n: 0, v: '' };
184
+ if (!o)
185
+ {
186
+ this._error = true;
187
+ return { n: 0, v: '' };
188
+ }
153
189
  let n: number;
154
190
  let av = this.items.map(di => {
155
191
  if (di.text)
@@ -158,7 +194,9 @@ export class FormatDetail
158
194
  {
159
195
  let e = new Evaluator(di.expr);
160
196
  n = e.eval(o);
161
- return Util.precisionRound(n, 0).toLocaleString();
197
+ if (! this._error)
198
+ this._error = e.error;
199
+ return this.options.numberFormat(n);
162
200
  }
163
201
  });
164
202
  return { n, v: av.join('') }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dra2020/baseclient",
3
- "version": "1.0.132",
3
+ "version": "1.0.134",
4
4
  "description": "Utility functions for Javascript projects.",
5
5
  "main": "dist/baseclient.js",
6
6
  "types": "./dist/all/all.d.ts",