@geospatial-sdk/core 0.0.5-alpha.0

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.
Files changed (43) hide show
  1. package/LICENSE +28 -0
  2. package/README.md +11 -0
  3. package/dist/index.d.ts +3 -0
  4. package/dist/index.d.ts.map +1 -0
  5. package/dist/index.js +3 -0
  6. package/dist/model/index.d.ts +3 -0
  7. package/dist/model/index.d.ts.map +1 -0
  8. package/dist/model/index.js +2 -0
  9. package/dist/model/map-context-diff.d.ts +35 -0
  10. package/dist/model/map-context-diff.d.ts.map +1 -0
  11. package/dist/model/map-context-diff.js +1 -0
  12. package/dist/model/map-context.d.ts +82 -0
  13. package/dist/model/map-context.d.ts.map +1 -0
  14. package/dist/model/map-context.js +1 -0
  15. package/dist/utils/freeze.d.ts +2 -0
  16. package/dist/utils/freeze.d.ts.map +1 -0
  17. package/dist/utils/freeze.js +15 -0
  18. package/dist/utils/index.d.ts +5 -0
  19. package/dist/utils/index.d.ts.map +1 -0
  20. package/dist/utils/index.js +4 -0
  21. package/dist/utils/map-context-diff.d.ts +22 -0
  22. package/dist/utils/map-context-diff.d.ts.map +1 -0
  23. package/dist/utils/map-context-diff.js +79 -0
  24. package/dist/utils/map-context.d.ts +6 -0
  25. package/dist/utils/map-context.d.ts.map +1 -0
  26. package/dist/utils/map-context.js +36 -0
  27. package/dist/utils/url.d.ts +7 -0
  28. package/dist/utils/url.d.ts.map +1 -0
  29. package/dist/utils/url.js +17 -0
  30. package/lib/index.ts +3 -0
  31. package/lib/model/index.ts +2 -0
  32. package/lib/model/map-context-diff.ts +37 -0
  33. package/lib/model/map-context.ts +100 -0
  34. package/lib/utils/freeze.test.ts +52 -0
  35. package/lib/utils/freeze.ts +14 -0
  36. package/lib/utils/index.ts +4 -0
  37. package/lib/utils/map-context-diff.test.ts +275 -0
  38. package/lib/utils/map-context-diff.ts +102 -0
  39. package/lib/utils/map-context.test.ts +303 -0
  40. package/lib/utils/map-context.ts +51 -0
  41. package/lib/utils/url.test.ts +14 -0
  42. package/lib/utils/url.ts +20 -0
  43. package/package.json +26 -0
package/LICENSE ADDED
@@ -0,0 +1,28 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2023, Camptocamp
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are met:
7
+
8
+ 1. Redistributions of source code must retain the above copyright notice, this
9
+ list of conditions and the following disclaimer.
10
+
11
+ 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ this list of conditions and the following disclaimer in the documentation
13
+ and/or other materials provided with the distribution.
14
+
15
+ 3. Neither the name of the copyright holder nor the names of its
16
+ contributors may be used to endorse or promote products derived from
17
+ this software without specific prior written permission.
18
+
19
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md ADDED
@@ -0,0 +1,11 @@
1
+ # `core`
2
+
3
+ > TODO: description
4
+
5
+ ## Usage
6
+
7
+ ```
8
+ const core = require('core');
9
+
10
+ // TODO: DEMONSTRATE API
11
+ ```
@@ -0,0 +1,3 @@
1
+ export * from "./utils";
2
+ export * from "./model";
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AACA,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ // PUBLIC API
2
+ export * from "./utils";
3
+ export * from "./model";
@@ -0,0 +1,3 @@
1
+ export * from "./map-context";
2
+ export * from "./map-context-diff";
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../lib/model/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,oBAAoB,CAAC"}
@@ -0,0 +1,2 @@
1
+ export * from "./map-context";
2
+ export * from "./map-context-diff";
@@ -0,0 +1,35 @@
1
+ import { MapContextLayer, MapContextView } from "./map-context";
2
+ /**
3
+ * Associates a position to a layer; the position is the index of
4
+ * the layer in the layers array
5
+ */
6
+ export interface MapContextLayerPositioned {
7
+ layer: MapContextLayer;
8
+ position: number;
9
+ }
10
+ /**
11
+ * Describes a layer being moved to a different position
12
+ */
13
+ export interface MapContextLayerReordered {
14
+ layer: MapContextLayer;
15
+ newPosition: number;
16
+ previousPosition: number;
17
+ }
18
+ /**
19
+ * Describes a delta between two contexts, in order to be
20
+ * applied to an existing map.
21
+ *
22
+ * For positions to be correct the order of operations should be:
23
+ * 1. remove layers
24
+ * 2. add layers
25
+ * 3. move layers
26
+ * 4. change layers
27
+ */
28
+ export interface MapContextDiff {
29
+ layersChanged: MapContextLayerPositioned[];
30
+ layersReordered: MapContextLayerReordered[];
31
+ layersRemoved: MapContextLayerPositioned[];
32
+ layersAdded: MapContextLayerPositioned[];
33
+ viewChanges: MapContextView;
34
+ }
35
+ //# sourceMappingURL=map-context-diff.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"map-context-diff.d.ts","sourceRoot":"","sources":["../../lib/model/map-context-diff.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAEhE;;;GAGG;AACH,MAAM,WAAW,yBAAyB;IACxC,KAAK,EAAE,eAAe,CAAC;IACvB,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED;;GAEG;AACH,MAAM,WAAW,wBAAwB;IACvC,KAAK,EAAE,eAAe,CAAC;IACvB,WAAW,EAAE,MAAM,CAAC;IACpB,gBAAgB,EAAE,MAAM,CAAC;CAC1B;AAED;;;;;;;;;GASG;AACH,MAAM,WAAW,cAAc;IAC7B,aAAa,EAAE,yBAAyB,EAAE,CAAC;IAC3C,eAAe,EAAE,wBAAwB,EAAE,CAAC;IAC5C,aAAa,EAAE,yBAAyB,EAAE,CAAC;IAC3C,WAAW,EAAE,yBAAyB,EAAE,CAAC;IACzC,WAAW,EAAE,cAAc,CAAC;CAC7B"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,82 @@
1
+ import { FeatureCollection, Geometry } from "geojson";
2
+ export type LayerDimensions = Record<string, string>;
3
+ export type LayerExtras = Record<string, unknown>;
4
+ export interface MapContextBaseLayer {
5
+ id?: string | number;
6
+ version?: number;
7
+ /**
8
+ * This property can be used to store anything application-specific on layers; as its content may occasionally
9
+ * be serialized to JSON for change detection purposes, it is not recommended to store Functions or other
10
+ * non-serializable entities
11
+ */
12
+ extras?: LayerExtras;
13
+ visibility?: boolean;
14
+ opacity?: number;
15
+ label?: string;
16
+ attributions?: string;
17
+ }
18
+ export interface MapContextLayerWms extends MapContextBaseLayer {
19
+ type: "wms";
20
+ url: string;
21
+ name: string;
22
+ dimensions?: LayerDimensions;
23
+ style?: string;
24
+ }
25
+ export interface MapContextLayerWmts extends MapContextBaseLayer {
26
+ type: "wmts";
27
+ url: string;
28
+ name: string;
29
+ dimensions?: LayerDimensions;
30
+ style?: string;
31
+ }
32
+ export interface MapContextLayerWfs extends MapContextBaseLayer {
33
+ type: "wfs";
34
+ url: string;
35
+ featureType: string;
36
+ }
37
+ export interface MapContextLayerXyz extends MapContextBaseLayer {
38
+ type: "xyz";
39
+ url: string;
40
+ }
41
+ interface LayerGeojson extends MapContextBaseLayer {
42
+ type: "geojson";
43
+ }
44
+ interface LayerGeojsonWithUrl extends LayerGeojson {
45
+ url: string;
46
+ data?: never;
47
+ }
48
+ interface LayerGeojsonWithData extends LayerGeojson {
49
+ data: FeatureCollection<Geometry | null> | string;
50
+ url?: never;
51
+ }
52
+ /**
53
+ * @interface
54
+ */
55
+ export type MapContextLayerGeojson = LayerGeojsonWithUrl | LayerGeojsonWithData;
56
+ /**
57
+ * @interface
58
+ */
59
+ export type MapContextLayer = MapContextLayerWms | MapContextLayerWmts | MapContextLayerWfs | MapContextLayerXyz | MapContextLayerGeojson;
60
+ export type Coordinate = [number, number];
61
+ /**
62
+ * Min X, min Y, max X, max Y
63
+ */
64
+ export type Extent = [number, number, number, number];
65
+ /**
66
+ * @property center Expressed in longitude/latitude
67
+ * @property extent Expressed in longitude/latitude
68
+ * @property maxExtent Expressed in longitude/latitude
69
+ */
70
+ export interface MapContextView {
71
+ center?: Coordinate;
72
+ zoom?: number;
73
+ extent?: Extent;
74
+ maxZoom?: number;
75
+ maxExtent?: Extent;
76
+ }
77
+ export interface MapContext {
78
+ layers: MapContextLayer[];
79
+ view: MapContextView;
80
+ }
81
+ export {};
82
+ //# sourceMappingURL=map-context.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"map-context.d.ts","sourceRoot":"","sources":["../../lib/model/map-context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAEtD,MAAM,MAAM,eAAe,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AAErD,MAAM,MAAM,WAAW,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAElD,MAAM,WAAW,mBAAmB;IAClC,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IACrB,OAAO,CAAC,EAAE,MAAM,CAAC;IAEjB;;;;OAIG;IACH,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED,MAAM,WAAW,kBAAmB,SAAQ,mBAAmB;IAC7D,IAAI,EAAE,KAAK,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,CAAC,EAAE,eAAe,CAAC;IAC7B,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,mBAAoB,SAAQ,mBAAmB;IAC9D,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,CAAC,EAAE,eAAe,CAAC;IAC7B,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,kBAAmB,SAAQ,mBAAmB;IAC7D,IAAI,EAAE,KAAK,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,WAAW,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,kBAAmB,SAAQ,mBAAmB;IAC7D,IAAI,EAAE,KAAK,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;CACb;AAED,UAAU,YAAa,SAAQ,mBAAmB;IAChD,IAAI,EAAE,SAAS,CAAC;CACjB;AACD,UAAU,mBAAoB,SAAQ,YAAY;IAChD,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,CAAC,EAAE,KAAK,CAAC;CACd;AACD,UAAU,oBAAqB,SAAQ,YAAY;IACjD,IAAI,EAAE,iBAAiB,CAAC,QAAQ,GAAG,IAAI,CAAC,GAAG,MAAM,CAAC;IAClD,GAAG,CAAC,EAAE,KAAK,CAAC;CACb;AAED;;GAEG;AACH,MAAM,MAAM,sBAAsB,GAAG,mBAAmB,GAAG,oBAAoB,CAAC;AAEhF;;GAEG;AACH,MAAM,MAAM,eAAe,GACvB,kBAAkB,GAClB,mBAAmB,GACnB,kBAAkB,GAClB,kBAAkB,GAClB,sBAAsB,CAAC;AAE3B,MAAM,MAAM,UAAU,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AAE1C;;GAEG;AACH,MAAM,MAAM,MAAM,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;AAEtD;;;;GAIG;AACH,MAAM,WAAW,cAAc;IAC7B,MAAM,CAAC,EAAE,UAAU,CAAC;IACpB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,UAAU;IACzB,MAAM,EAAE,eAAe,EAAE,CAAC;IAC1B,IAAI,EAAE,cAAc,CAAC;CACtB"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ export declare function deepFreeze<U>(obj: U): U;
2
+ //# sourceMappingURL=freeze.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"freeze.d.ts","sourceRoot":"","sources":["../../lib/utils/freeze.ts"],"names":[],"mappings":"AAAA,wBAAgB,UAAU,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,CAavC"}
@@ -0,0 +1,15 @@
1
+ export function deepFreeze(obj) {
2
+ if (Array.isArray(obj)) {
3
+ for (const elt of obj) {
4
+ deepFreeze(elt);
5
+ }
6
+ return obj;
7
+ }
8
+ else if (obj instanceof Object) {
9
+ for (const prop in obj) {
10
+ deepFreeze(obj[prop]);
11
+ }
12
+ return Object.freeze(obj);
13
+ }
14
+ return obj;
15
+ }
@@ -0,0 +1,5 @@
1
+ export * from "./url";
2
+ export * from "./freeze";
3
+ export { computeMapContextDiff } from "./map-context-diff";
4
+ export { getLayerPosition } from "./map-context";
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../lib/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,OAAO,CAAC;AACtB,cAAc,UAAU,CAAC;AACzB,OAAO,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;AAC3D,OAAO,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC"}
@@ -0,0 +1,4 @@
1
+ export * from "./url";
2
+ export * from "./freeze";
3
+ export { computeMapContextDiff } from "./map-context-diff";
4
+ export { getLayerPosition } from "./map-context";
@@ -0,0 +1,22 @@
1
+ import { MapContext, MapContextDiff } from "../model";
2
+ /**
3
+ * The following logic is produced by identifying layers in both context
4
+ * and determining whether they have been added, removed, changed or reordered.
5
+ *
6
+ * Identifying layers to determine if they have been added/removed/reordered is done like so:
7
+ * 1. For layers with an `id` property, use non-strict equality on it (e.g. '2' and 2 are equivalent);
8
+ * 2. For layers without `id`, compute a hash of their base properties _excluding the `extras` property_
9
+ *
10
+ * Determining whether layers have changed is done like so:
11
+ * 1. For layers with an `id` property, the value of the `version` field is compared;
12
+ * if values are different (using non-strict equality), then the layer is considered to have changed; otherwise
13
+ * it is considered to have remained the same
14
+ * 2. For layers without `id`, a full hash is computed _including the `extras` property_;
15
+ * this means that a layer which only had changes in its `extras` object will not be considered added/removed,
16
+ * but only changed
17
+ *
18
+ * @param nextContext
19
+ * @param previousContext
20
+ */
21
+ export declare function computeMapContextDiff(nextContext: MapContext, previousContext: MapContext): MapContextDiff;
22
+ //# sourceMappingURL=map-context-diff.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"map-context-diff.d.ts","sourceRoot":"","sources":["../../lib/utils/map-context-diff.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EACV,cAAc,EAKf,MAAM,UAAU,CAAC;AAGlB;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,qBAAqB,CACnC,WAAW,EAAE,UAAU,EACvB,eAAe,EAAE,UAAU,GAC1B,cAAc,CAqEhB"}
@@ -0,0 +1,79 @@
1
+ import { isLayerSame, isLayerSameAndUnchanged } from "./map-context";
2
+ /**
3
+ * The following logic is produced by identifying layers in both context
4
+ * and determining whether they have been added, removed, changed or reordered.
5
+ *
6
+ * Identifying layers to determine if they have been added/removed/reordered is done like so:
7
+ * 1. For layers with an `id` property, use non-strict equality on it (e.g. '2' and 2 are equivalent);
8
+ * 2. For layers without `id`, compute a hash of their base properties _excluding the `extras` property_
9
+ *
10
+ * Determining whether layers have changed is done like so:
11
+ * 1. For layers with an `id` property, the value of the `version` field is compared;
12
+ * if values are different (using non-strict equality), then the layer is considered to have changed; otherwise
13
+ * it is considered to have remained the same
14
+ * 2. For layers without `id`, a full hash is computed _including the `extras` property_;
15
+ * this means that a layer which only had changes in its `extras` object will not be considered added/removed,
16
+ * but only changed
17
+ *
18
+ * @param nextContext
19
+ * @param previousContext
20
+ */
21
+ export function computeMapContextDiff(nextContext, previousContext) {
22
+ function getLayerPosition(layer, layers) {
23
+ for (let i = 0; i < layers.length; i++) {
24
+ if (isLayerSame(layers[i], layer)) {
25
+ return i;
26
+ }
27
+ }
28
+ return -1;
29
+ }
30
+ const layersChanged = [];
31
+ const layersReordered = [];
32
+ const layersRemoved = [];
33
+ const layersAdded = [];
34
+ const viewChanges = {};
35
+ // loop on prev context layers (for removed layers)
36
+ for (let i = 0; i < previousContext.layers.length; i++) {
37
+ const layer = previousContext.layers[i];
38
+ const nextPosition = getLayerPosition(layer, nextContext.layers);
39
+ const prevPosition = getLayerPosition(layer, previousContext.layers);
40
+ if (nextPosition === -1) {
41
+ layersRemoved.push({ layer, position: prevPosition });
42
+ }
43
+ }
44
+ // loop on next context layers (for added & updated)
45
+ for (let i = 0; i < nextContext.layers.length; i++) {
46
+ const layer = nextContext.layers[i];
47
+ const prevPosition = getLayerPosition(layer, previousContext.layers);
48
+ if (prevPosition === -1) {
49
+ layersAdded.push({ layer, position: i });
50
+ }
51
+ else {
52
+ const prevLayer = previousContext.layers[prevPosition];
53
+ if (!isLayerSameAndUnchanged(layer, prevLayer)) {
54
+ layersChanged.push({ layer, position: i });
55
+ }
56
+ }
57
+ }
58
+ // look for moved layers
59
+ const prevLayersFiltered = previousContext.layers.filter((l) => !layersRemoved.find(({ layer }) => l === layer));
60
+ const nextLayersFiltered = nextContext.layers.filter((l) => !layersAdded.find(({ layer }) => l === layer));
61
+ for (let i = 0; i < nextLayersFiltered.length; i++) {
62
+ const layer = nextLayersFiltered[i];
63
+ const prevPosition = getLayerPosition(layer, prevLayersFiltered);
64
+ if (i !== prevPosition) {
65
+ layersReordered.push({
66
+ layer,
67
+ newPosition: getLayerPosition(layer, nextContext.layers),
68
+ previousPosition: getLayerPosition(layer, previousContext.layers),
69
+ });
70
+ }
71
+ }
72
+ return {
73
+ layersAdded,
74
+ layersChanged,
75
+ layersRemoved,
76
+ layersReordered,
77
+ viewChanges,
78
+ };
79
+ }
@@ -0,0 +1,6 @@
1
+ import { MapContext, MapContextLayer } from "../model";
2
+ export declare function getLayerHash(layer: MapContextLayer, includeExtras?: boolean): string;
3
+ export declare function isLayerSame(layerA: MapContextLayer, layerB: MapContextLayer): boolean;
4
+ export declare function isLayerSameAndUnchanged(layerA: MapContextLayer, layerB: MapContextLayer): boolean;
5
+ export declare function getLayerPosition(context: MapContext, layerModel: MapContextLayer): number;
6
+ //# sourceMappingURL=map-context.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"map-context.d.ts","sourceRoot":"","sources":["../../lib/utils/map-context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAEvD,wBAAgB,YAAY,CAC1B,KAAK,EAAE,eAAe,EACtB,aAAa,UAAQ,GACpB,MAAM,CAkBR;AAED,wBAAgB,WAAW,CACzB,MAAM,EAAE,eAAe,EACvB,MAAM,EAAE,eAAe,GACtB,OAAO,CAKT;AAED,wBAAgB,uBAAuB,CACrC,MAAM,EAAE,eAAe,EACvB,MAAM,EAAE,eAAe,GACtB,OAAO,CAKT;AAED,wBAAgB,gBAAgB,CAC9B,OAAO,EAAE,UAAU,EACnB,UAAU,EAAE,eAAe,GAC1B,MAAM,CAER"}
@@ -0,0 +1,36 @@
1
+ export function getLayerHash(layer, includeExtras = false) {
2
+ function getHash(input) {
3
+ if (input instanceof Object) {
4
+ const obj = {};
5
+ const keys = Object.keys(input).sort();
6
+ for (const key of keys) {
7
+ if (!includeExtras && key === "extras")
8
+ continue;
9
+ obj[key] = getHash(input[key]);
10
+ }
11
+ const hash = JSON.stringify(obj)
12
+ .split("")
13
+ .reduce((prev, curr) => (prev << 5) - prev + curr.charCodeAt(0), 0);
14
+ return (hash >>> 0).toString();
15
+ }
16
+ else {
17
+ return JSON.stringify(input);
18
+ }
19
+ }
20
+ return getHash(layer);
21
+ }
22
+ export function isLayerSame(layerA, layerB) {
23
+ if ("id" in layerA && "id" in layerB) {
24
+ return layerA.id == layerB.id;
25
+ }
26
+ return getLayerHash(layerA) === getLayerHash(layerB);
27
+ }
28
+ export function isLayerSameAndUnchanged(layerA, layerB) {
29
+ if ("id" in layerA && "id" in layerB) {
30
+ return layerA.id == layerB.id && layerA.version == layerB.version;
31
+ }
32
+ return getLayerHash(layerA, true) === getLayerHash(layerB, true);
33
+ }
34
+ export function getLayerPosition(context, layerModel) {
35
+ return context.layers.findIndex((l) => isLayerSame(layerModel, l));
36
+ }
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Removes the given search params from the URL completely; this is case-insensitive
3
+ * @param url
4
+ * @param searchParams
5
+ */
6
+ export declare function removeSearchParams(url: string, searchParams: string[]): string;
7
+ //# sourceMappingURL=url.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"url.d.ts","sourceRoot":"","sources":["../../lib/utils/url.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,wBAAgB,kBAAkB,CAChC,GAAG,EAAE,MAAM,EACX,YAAY,EAAE,MAAM,EAAE,GACrB,MAAM,CAWR"}
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Removes the given search params from the URL completely; this is case-insensitive
3
+ * @param url
4
+ * @param searchParams
5
+ */
6
+ export function removeSearchParams(url, searchParams) {
7
+ const toDelete = [];
8
+ const urlObj = new URL(url, window.location.toString());
9
+ const keysLower = searchParams.map((p) => p.toLowerCase());
10
+ for (const param of urlObj.searchParams.keys()) {
11
+ if (keysLower.indexOf(param.toLowerCase()) > -1) {
12
+ toDelete.push(param);
13
+ }
14
+ }
15
+ toDelete.map((param) => urlObj.searchParams.delete(param));
16
+ return urlObj.toString();
17
+ }
package/lib/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ // PUBLIC API
2
+ export * from "./utils";
3
+ export * from "./model";
@@ -0,0 +1,2 @@
1
+ export * from "./map-context";
2
+ export * from "./map-context-diff";
@@ -0,0 +1,37 @@
1
+ import { MapContextLayer, MapContextView } from "./map-context";
2
+
3
+ /**
4
+ * Associates a position to a layer; the position is the index of
5
+ * the layer in the layers array
6
+ */
7
+ export interface MapContextLayerPositioned {
8
+ layer: MapContextLayer;
9
+ position: number;
10
+ }
11
+
12
+ /**
13
+ * Describes a layer being moved to a different position
14
+ */
15
+ export interface MapContextLayerReordered {
16
+ layer: MapContextLayer;
17
+ newPosition: number;
18
+ previousPosition: number;
19
+ }
20
+
21
+ /**
22
+ * Describes a delta between two contexts, in order to be
23
+ * applied to an existing map.
24
+ *
25
+ * For positions to be correct the order of operations should be:
26
+ * 1. remove layers
27
+ * 2. add layers
28
+ * 3. move layers
29
+ * 4. change layers
30
+ */
31
+ export interface MapContextDiff {
32
+ layersChanged: MapContextLayerPositioned[];
33
+ layersReordered: MapContextLayerReordered[];
34
+ layersRemoved: MapContextLayerPositioned[];
35
+ layersAdded: MapContextLayerPositioned[];
36
+ viewChanges: MapContextView;
37
+ }
@@ -0,0 +1,100 @@
1
+ import { FeatureCollection, Geometry } from "geojson";
2
+
3
+ export type LayerDimensions = Record<string, string>;
4
+
5
+ export type LayerExtras = Record<string, unknown>;
6
+
7
+ export interface MapContextBaseLayer {
8
+ id?: string | number;
9
+ version?: number;
10
+
11
+ /**
12
+ * This property can be used to store anything application-specific on layers; as its content may occasionally
13
+ * be serialized to JSON for change detection purposes, it is not recommended to store Functions or other
14
+ * non-serializable entities
15
+ */
16
+ extras?: LayerExtras;
17
+ visibility?: boolean;
18
+ opacity?: number;
19
+ label?: string;
20
+ attributions?: string;
21
+ }
22
+
23
+ export interface MapContextLayerWms extends MapContextBaseLayer {
24
+ type: "wms";
25
+ url: string;
26
+ name: string;
27
+ dimensions?: LayerDimensions;
28
+ style?: string;
29
+ }
30
+
31
+ export interface MapContextLayerWmts extends MapContextBaseLayer {
32
+ type: "wmts";
33
+ url: string;
34
+ name: string;
35
+ dimensions?: LayerDimensions;
36
+ style?: string;
37
+ }
38
+
39
+ export interface MapContextLayerWfs extends MapContextBaseLayer {
40
+ type: "wfs";
41
+ url: string;
42
+ featureType: string;
43
+ }
44
+
45
+ export interface MapContextLayerXyz extends MapContextBaseLayer {
46
+ type: "xyz";
47
+ url: string;
48
+ }
49
+
50
+ interface LayerGeojson extends MapContextBaseLayer {
51
+ type: "geojson";
52
+ }
53
+ interface LayerGeojsonWithUrl extends LayerGeojson {
54
+ url: string;
55
+ data?: never;
56
+ }
57
+ interface LayerGeojsonWithData extends LayerGeojson {
58
+ data: FeatureCollection<Geometry | null> | string;
59
+ url?: never;
60
+ }
61
+
62
+ /**
63
+ * @interface
64
+ */
65
+ export type MapContextLayerGeojson = LayerGeojsonWithUrl | LayerGeojsonWithData;
66
+
67
+ /**
68
+ * @interface
69
+ */
70
+ export type MapContextLayer =
71
+ | MapContextLayerWms
72
+ | MapContextLayerWmts
73
+ | MapContextLayerWfs
74
+ | MapContextLayerXyz
75
+ | MapContextLayerGeojson;
76
+
77
+ export type Coordinate = [number, number];
78
+
79
+ /**
80
+ * Min X, min Y, max X, max Y
81
+ */
82
+ export type Extent = [number, number, number, number];
83
+
84
+ /**
85
+ * @property center Expressed in longitude/latitude
86
+ * @property extent Expressed in longitude/latitude
87
+ * @property maxExtent Expressed in longitude/latitude
88
+ */
89
+ export interface MapContextView {
90
+ center?: Coordinate;
91
+ zoom?: number;
92
+ extent?: Extent;
93
+ maxZoom?: number;
94
+ maxExtent?: Extent;
95
+ }
96
+
97
+ export interface MapContext {
98
+ layers: MapContextLayer[];
99
+ view: MapContextView;
100
+ }
@@ -0,0 +1,52 @@
1
+ import { deepFreeze } from "./freeze";
2
+
3
+ describe("freeze util", () => {
4
+ describe("deepFreeze", () => {
5
+ let obj: any;
6
+ beforeEach(() => {
7
+ obj = deepFreeze({
8
+ ab: "cde",
9
+ fg: {
10
+ h: true,
11
+ ij: 12,
12
+ },
13
+ k: [
14
+ {
15
+ l: 456,
16
+ mn: ["opqr"],
17
+ },
18
+ ],
19
+ });
20
+ });
21
+ it("prevents mutation of root properties", () => {
22
+ expect(() => {
23
+ obj.ab = "AA";
24
+ }).toThrow();
25
+ });
26
+ it("prevents mutation of nested properties", () => {
27
+ expect(() => {
28
+ obj.fg.h = "AA";
29
+ }).toThrow();
30
+ });
31
+ it("prevents mutation of objects inside nested arrays", () => {
32
+ expect(() => {
33
+ obj.k[0].l = "AA";
34
+ }).toThrow();
35
+ });
36
+ describe("starting with array", () => {
37
+ beforeEach(() => {
38
+ obj = deepFreeze([
39
+ {
40
+ l: 456,
41
+ mn: ["opqr"],
42
+ },
43
+ ]);
44
+ });
45
+ it("prevents mutation of nested objects", () => {
46
+ expect(() => {
47
+ obj[0].l = "AA";
48
+ }).toThrow();
49
+ });
50
+ });
51
+ });
52
+ });
@@ -0,0 +1,14 @@
1
+ export function deepFreeze<U>(obj: U): U {
2
+ if (Array.isArray(obj)) {
3
+ for (const elt of obj) {
4
+ deepFreeze(elt);
5
+ }
6
+ return obj;
7
+ } else if (obj instanceof Object) {
8
+ for (const prop in obj) {
9
+ deepFreeze((obj as Record<string, unknown>)[prop]);
10
+ }
11
+ return Object.freeze(obj);
12
+ }
13
+ return obj;
14
+ }
@@ -0,0 +1,4 @@
1
+ export * from "./url";
2
+ export * from "./freeze";
3
+ export { computeMapContextDiff } from "./map-context-diff";
4
+ export { getLayerPosition } from "./map-context";