@dra2020/baseclient 1.0.133 → 1.0.135

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;
@@ -10,7 +13,8 @@ export declare class FormatDetail {
10
13
  pattern: string;
11
14
  items: DetailItem[];
12
15
  _error: boolean;
13
- constructor(pattern: string);
16
+ options: DetailOptions;
17
+ constructor(pattern: string, options?: DetailOptions);
14
18
  get error(): boolean;
15
19
  static prepare(o: any): any;
16
20
  format(o: any): DetailResult;
@@ -69,3 +69,6 @@ export declare function distance(x0: number, y0: number, x1: number, y1: number)
69
69
  export declare function deg2rad(num: number): number;
70
70
  export declare function rad2deg(num: number): number;
71
71
  export declare function wrapLon(lon: number): number;
72
+ export declare function isNumber(s: string): boolean;
73
+ export declare function toNumber(a: any): number;
74
+ export declare function toSafeNumber(a: any): number;
@@ -6,7 +6,7 @@
6
6
  // The formatting string is a statement of the form:
7
7
  // =expr
8
8
  // Expr can be an arithmetic expression using +-/*()?: as operators and variables are the field
9
- // names of properties on the object passed in. The special field name _tot represents the
9
+ // names of properties on the object passed in. The special field name Tot represents the
10
10
  // total of all properties. The expression may also include double-quoted strings that are
11
11
  // passed through (e.g. for use as labels = area" sqm")
12
12
  //
@@ -16,6 +16,18 @@ 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;
@@ -102,9 +114,11 @@ export class FormatDetail
102
114
  pattern: string;
103
115
  items: DetailItem[];
104
116
  _error: boolean;
117
+ options: DetailOptions;
105
118
 
106
- constructor(pattern: string)
119
+ constructor(pattern: string, options?: DetailOptions)
107
120
  {
121
+ this.options = Util.shallowAssignImmutable(DefaultDetailOptions, options);
108
122
  this._error = false;
109
123
  this.pattern = pattern.trim();
110
124
  let a = reExpr.exec(pattern);
@@ -146,21 +160,19 @@ export class FormatDetail
146
160
 
147
161
  static prepare(o: any): any
148
162
  {
149
- if (o)
163
+ if (o && o.Tot === undefined && o.Total === undefined)
150
164
  {
151
- // Make sure there is a total field
165
+ // Add a total field
166
+ let t = 0;
167
+ Object.keys(o).forEach((k: string) => {
168
+ let v: any = o[k];
169
+ if (!isNaN(v) && typeof v === 'number')
170
+ t += v;
171
+ });
172
+
152
173
  o = Util.deepCopy(o);
153
- if (o['Tot'] !== undefined)
154
- o['_tot'] = o['Tot'];
155
- else
156
- {
157
- let t = 0;
158
- Object.values(o).forEach((v: any) => {
159
- if (!isNaN(v) && typeof v === 'number')
160
- t += v;
161
- });
162
- o['_tot'] = t;
163
- }
174
+ o.Tot = t;
175
+ o.Total = t;
164
176
  }
165
177
  return o;
166
178
  }
@@ -182,7 +194,7 @@ export class FormatDetail
182
194
  n = e.eval(o);
183
195
  if (! this._error)
184
196
  this._error = e.error;
185
- return Util.precisionRound(n, 0).toLocaleString();
197
+ return this.options.numberFormat(n);
186
198
  }
187
199
  });
188
200
  return { n, v: av.join('') }
package/lib/util/util.ts CHANGED
@@ -708,3 +708,17 @@ export function wrapLon(lon: number): number
708
708
  let worlds = Math.floor((lon + 180) / 360);
709
709
  return lon - (worlds * 360);
710
710
  }
711
+
712
+ let reNumber = /^\s*-?\d+(\.\d+)?([eE][+-]?\d+)?\s*$/;
713
+ export function isNumber(s: string): boolean { return !!s && reNumber.test(s) }
714
+ export function toNumber(a: any): number
715
+ {
716
+ if (typeof a === 'number') return a;
717
+ if (typeof a === 'string' && !isNumber(a as string)) return NaN;
718
+ return Number(a);
719
+ }
720
+ export function toSafeNumber(a: any): number
721
+ {
722
+ let n = toNumber(a);
723
+ if (isNaN(n) || typeof n !== 'number') return 0;
724
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dra2020/baseclient",
3
- "version": "1.0.133",
3
+ "version": "1.0.135",
4
4
  "description": "Utility functions for Javascript projects.",
5
5
  "main": "dist/baseclient.js",
6
6
  "types": "./dist/all/all.d.ts",