@dra2020/baseclient 1.0.134 → 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.
@@ -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
  //
@@ -160,21 +160,19 @@ export class FormatDetail
160
160
 
161
161
  static prepare(o: any): any
162
162
  {
163
- if (o)
163
+ if (o && o.Tot === undefined && o.Total === undefined)
164
164
  {
165
- // 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
+
166
173
  o = Util.deepCopy(o);
167
- if (o['Tot'] !== undefined)
168
- o['_tot'] = o['Tot'];
169
- else
170
- {
171
- let t = 0;
172
- Object.values(o).forEach((v: any) => {
173
- if (!isNaN(v) && typeof v === 'number')
174
- t += v;
175
- });
176
- o['_tot'] = t;
177
- }
174
+ o.Tot = t;
175
+ o.Total = t;
178
176
  }
179
177
  return o;
180
178
  }
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.134",
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",