@dra2020/baseclient 1.0.130 → 1.0.131

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.
@@ -2,21 +2,16 @@ interface DetailItem {
2
2
  expr?: string;
3
3
  text?: string;
4
4
  }
5
- interface DetailLine {
6
- label: string;
7
- items: DetailItem[];
8
- }
9
5
  export interface DetailResult {
10
- k: string;
11
6
  n: number;
12
7
  v: string;
13
- t?: string;
14
8
  }
15
9
  export declare class FormatDetail {
16
10
  valid: boolean;
17
11
  pattern: string;
18
- lines: DetailLine[];
12
+ items: DetailItem[];
19
13
  constructor(pattern: string);
20
- format(o: any): DetailResult[];
14
+ prepare(o: any): any;
15
+ format(o: any): DetailResult;
21
16
  }
22
17
  export {};
@@ -1,12 +1,11 @@
1
1
  //
2
- // FormatDetail will take a pattern that specifies a set of formatted detail lines.
3
- // Given an object with a set of integer properties, it will evaluate the pattern and produce
4
- // an array of { k: string, n: number, v: string } results for displaying the contents of the object.
2
+ // FormatDetail will take an expression that specifies a format detail lines.
3
+ // Given an object with a set of integer properties, it will evaluate the expression and produce
4
+ // a result { k: string, n: number, v: string } results for displaying the contents of the object.
5
5
  //
6
- // The formatting string contains a series of statements, separated by newline or semicolon.
7
- // A single statement is of the form:
8
- // label=expr
9
- // Expr can be an arithmetic expression using +-/*() as operators and variables are the field
6
+ // The formatting string is a statement of the form:
7
+ // =expr
8
+ // Expr can be an arithmetic expression using +-/*()?: as operators and variables are the field
10
9
  // names of properties on the object passed in. The special field name _tot represents the
11
10
  // total of all properties. The expression may also include double-quoted strings that are
12
11
  // passed through (e.g. for use as labels = area" sqm")
@@ -73,101 +72,95 @@ interface DetailItem
73
72
  text?: string,
74
73
  }
75
74
 
76
- interface DetailLine
77
- {
78
- label: string,
79
- items: DetailItem[],
80
- }
81
-
82
75
  export interface DetailResult
83
76
  {
84
- k: string, // label
85
77
  n: number, // value before formatting
86
78
  v: string, // value
87
- t?: string, // tooltip
88
79
  }
89
80
 
90
81
  const reInvalidChars = /[\.\[\]\\]/;
82
+ const reExpr = /^=(.*)$/
91
83
 
92
84
  export class FormatDetail
93
85
  {
94
86
  valid: boolean;
95
87
  pattern: string;
96
- lines: DetailLine[];
88
+ items: DetailItem[];
97
89
 
98
90
  constructor(pattern: string)
99
91
  {
100
92
  this.valid = true;
101
- this.pattern = pattern;
102
- this.lines = [];
103
- let lines = this.pattern.split(/[;\n]/).map(l => l.trim()).filter(l => !!l);
104
- lines.forEach(line => {
105
- let sides = line.split('=').map(l => l.trim()).filter(l => !!l);
106
- if (sides.length != 2)
107
- this.valid = false;
108
- else
109
- {
110
- const lhs = sides[0];
111
- const rhs = sides[1];
112
- let parse = rhs.split('"');
113
- let items: DetailItem[] = [];
114
- let state = 'expr';
115
- parse.forEach(subexpr => {
116
- if (state === 'expr')
117
- {
118
- if (subexpr.length)
119
- {
120
- // Don't allow unsafe actions
121
- if (reInvalidChars.test(subexpr))
122
- items.push({ text: subexpr });
123
- else
124
- items.push({ expr: subexpr });
125
- }
126
- state = 'text';
127
- }
128
- else // state === 'text'
129
- {
130
- if (subexpr.length)
131
- items.push({ text: subexpr });
132
- state = 'expr';
133
- }
134
- });
135
- this.lines.push( { label: lhs, items } );
136
- }
137
- });
93
+ this.pattern = pattern.trim();
94
+ let a = reExpr.exec(pattern);
95
+ if (a && a.length == 2)
96
+ {
97
+ this.items = [];
98
+ const expr = a[1];
99
+ const parse = expr.split('"');
100
+ let state = 'expr';
101
+ parse.forEach(subexpr => {
102
+ if (state === 'expr')
103
+ {
104
+ if (subexpr.length)
105
+ {
106
+ // Don't allow unsafe actions
107
+ if (reInvalidChars.test(subexpr))
108
+ this.items.push({ text: subexpr });
109
+ else
110
+ this.items.push({ expr: subexpr });
111
+ }
112
+ state = 'text';
113
+ }
114
+ else // state === 'text'
115
+ {
116
+ if (subexpr.length)
117
+ this.items.push({ text: subexpr });
118
+ state = 'expr';
119
+ }
120
+ });
121
+ }
122
+ else
123
+ {
124
+ this.valid = false;
125
+ this.items = [ { text: 'invalid' } ];
126
+ }
138
127
  }
139
128
 
140
- format(o: any): DetailResult[]
129
+ prepare(o: any): any
141
130
  {
142
- if (!o) return [];
143
- // Make sure there is a total field
144
- o = Util.deepCopy(o);
145
- if (o['Tot'] !== undefined)
146
- o['_tot'] = o['Tot'];
147
- else
131
+ if (o)
148
132
  {
149
- let t = 0;
150
- Object.values(o).forEach((v: any) => {
151
- if (!isNaN(v) && typeof v === 'number')
152
- t += v;
153
- });
154
- o['_tot'] = t;
155
- }
156
- let result: DetailResult[] = [];
157
- this.lines.forEach(line => {
158
- let n: number;
159
- let av = line.items.map(di => {
160
- if (di.text)
161
- return di.text;
162
- else
163
- {
164
- let e = new Evaluator(di.expr);
165
- n = e.eval(o);
166
- return Util.precisionRound(n, 0).toLocaleString();
167
- }
133
+ // Make sure there is a total field
134
+ o = Util.deepCopy(o);
135
+ if (o['Tot'] !== undefined)
136
+ o['_tot'] = o['Tot'];
137
+ else
138
+ {
139
+ let t = 0;
140
+ Object.values(o).forEach((v: any) => {
141
+ if (!isNaN(v) && typeof v === 'number')
142
+ t += v;
168
143
  });
169
- result.push({ k: line.label, n, v: av.join('') });
144
+ o['_tot'] = t;
145
+ }
146
+ }
147
+ return o;
148
+ }
149
+
150
+ format(o: any): DetailResult
151
+ {
152
+ if (!o) return { n: 0, v: '' };
153
+ let n: number;
154
+ let av = this.items.map(di => {
155
+ if (di.text)
156
+ return di.text;
157
+ else
158
+ {
159
+ let e = new Evaluator(di.expr);
160
+ n = e.eval(o);
161
+ return Util.precisionRound(n, 0).toLocaleString();
162
+ }
170
163
  });
171
- return result;
164
+ return { n, v: av.join('') }
172
165
  }
173
166
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dra2020/baseclient",
3
- "version": "1.0.130",
3
+ "version": "1.0.131",
4
4
  "description": "Utility functions for Javascript projects.",
5
5
  "main": "dist/baseclient.js",
6
6
  "types": "./dist/all/all.d.ts",