@dra2020/baseclient 1.0.147 → 1.0.149

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.
package/dist/geo/geo.d.ts CHANGED
@@ -20,6 +20,8 @@ export interface NormalizeOptions {
20
20
  checkRewind?: boolean;
21
21
  ensureID?: boolean;
22
22
  }
23
+ export declare function applyCanonicalID(col: GeoFeatureCollection): void;
24
+ export declare function isValidId(col: GeoFeatureCollection): boolean;
23
25
  export declare function geoEnsureID(col: GeoFeatureCollection, options?: NormalizeOptions): void;
24
26
  export declare function geoNormalizeFeature(f: any, options?: NormalizeOptions): GeoFeature;
25
27
  export declare function geoNormalizeCollection(col: GeoFeatureCollection, options?: NormalizeOptions): GeoFeatureCollection;
@@ -29,6 +29,7 @@ export declare class Deadline {
29
29
  done(): boolean;
30
30
  }
31
31
  export declare function createGuid(): string;
32
+ export declare function isGuid(s: string): boolean;
32
33
  export declare function createKeyedGuid(key: string): string;
33
34
  export declare function guidKey(guid: string): string;
34
35
  export declare function sizeof(a: any): number;
package/lib/geo/geo.ts CHANGED
@@ -44,6 +44,27 @@ export interface NormalizeOptions
44
44
  }
45
45
  const NormalizeAll: NormalizeOptions = { joinPolygons: true, checkRewind: true, ensureID: true };
46
46
 
47
+ export function applyCanonicalID(col: GeoFeatureCollection): void
48
+ {
49
+ if (col && Array.isArray(col.features))
50
+ col.features.forEach((f: GeoFeature, n: number) => { f.properties.id = String(n+1) });
51
+ }
52
+
53
+ export function isValidId(col: GeoFeatureCollection): boolean
54
+ {
55
+ if (! col || ! Array.isArray(col.features)) return true;
56
+
57
+ let set = new Set<string>();
58
+ for (let i = 0; i < col.features.length; i++)
59
+ {
60
+ let f = col.features[i];
61
+ if (typeof f.properties.id !== 'string') return false; // id must be string
62
+ if (set.has(f.properties.id)) return false; // id must be unique
63
+ set.add(f.properties.id);
64
+ }
65
+ return true;
66
+ }
67
+
47
68
  // set the canonical 'id' property from the best property value.
48
69
  // if joinPolygons is true, we do not enforce uniqueness.
49
70
  //
@@ -58,7 +79,6 @@ export function geoEnsureID(col: GeoFeatureCollection, options?: NormalizeOption
58
79
  if (col && col.features && col.features.length > 0)
59
80
  {
60
81
  let f = col.features[0];
61
- if (f.properties.id !== undefined) return; // short-cut - assume if 'id' is set, we're all good.
62
82
  props.forEach(p => {
63
83
  if (prop === undefined)
64
84
  if (f.properties[p] !== undefined)
@@ -71,12 +91,13 @@ export function geoEnsureID(col: GeoFeatureCollection, options?: NormalizeOption
71
91
  }
72
92
  });
73
93
  if (prop)
74
- col.features.forEach(f => { f.properties.id = f.properties[prop] });
75
- else
76
94
  {
77
- let n = 1;
78
- col.features.forEach(f => { f.properties.id = String(n++) });
95
+ col.features.forEach(f => { f.properties.id = f.properties[prop] });
96
+ if (! isValidId(col))
97
+ applyCanonicalID(col);
79
98
  }
99
+ else
100
+ applyCanonicalID(col);
80
101
  }
81
102
  }
82
103
 
package/lib/util/util.ts CHANGED
@@ -132,6 +132,13 @@ export function createGuid(): string
132
132
  });
133
133
  }
134
134
 
135
+ const reGuid = /.*-.*-.*-.*-/;
136
+
137
+ export function isGuid(s: string): boolean
138
+ {
139
+ return reGuid.test(s) && s.length >= 36
140
+ }
141
+
135
142
  export function createKeyedGuid(key: string): string
136
143
  {
137
144
  return `xxxxxxxx-xxxx-${key}xxx-yxxx-xxxxxxxxxxxx`.replace(/[xy]/g, function(c) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dra2020/baseclient",
3
- "version": "1.0.147",
3
+ "version": "1.0.149",
4
4
  "description": "Utility functions for Javascript projects.",
5
5
  "main": "dist/baseclient.js",
6
6
  "types": "./dist/all/all.d.ts",