@geospatial-sdk/core 0.0.5-dev.46 → 0.0.5-dev.48

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,14 +2,12 @@ import { describe } from "vitest";
2
2
  import {
3
3
  addLayerToContext,
4
4
  changeLayerPositionInContext,
5
- getLayerHash,
6
5
  getLayerPosition,
7
- isLayerSame,
8
- isLayerSameAndUnchanged,
9
6
  removeLayerFromContext,
10
7
  replaceLayerInContext,
8
+ updateLayerInContext,
11
9
  } from "./map-context.js";
12
- import { MapContext } from "../model/index.js";
10
+ import { MapContext, MapContextLayer } from "../model/index.js";
13
11
  import {
14
12
  SAMPLE_CONTEXT,
15
13
  SAMPLE_LAYER1,
@@ -18,307 +16,6 @@ import {
18
16
  } from "../../fixtures/map-context.fixtures.js";
19
17
 
20
18
  describe("Map context utils", () => {
21
- describe("isLayerSame", () => {
22
- describe("layers with id", () => {
23
- it("compares non-strictly by id", () => {
24
- expect(
25
- isLayerSame(
26
- { ...SAMPLE_LAYER1, id: "a" },
27
- { ...SAMPLE_LAYER1, id: "b" },
28
- ),
29
- ).toBe(false);
30
- expect(
31
- isLayerSame(
32
- { ...SAMPLE_LAYER1, id: "ab" },
33
- { ...SAMPLE_LAYER2, id: "ab" },
34
- ),
35
- ).toBe(true);
36
- expect(
37
- isLayerSame(
38
- { ...SAMPLE_LAYER1, id: 1 },
39
- { ...SAMPLE_LAYER2, id: "01" },
40
- ),
41
- ).toBe(true);
42
- expect(
43
- isLayerSame({ ...SAMPLE_LAYER1, id: 1 }, { ...SAMPLE_LAYER2, id: 1 }),
44
- ).toBe(true);
45
- expect(
46
- isLayerSame({ ...SAMPLE_LAYER1, id: 1 }, { ...SAMPLE_LAYER1, id: 2 }),
47
- ).toBe(false);
48
- });
49
- });
50
- describe("layers without id", () => {
51
- it("compares by properties", () => {
52
- expect(isLayerSame(SAMPLE_LAYER1, SAMPLE_LAYER1)).toBe(true);
53
- expect(
54
- isLayerSame(SAMPLE_LAYER1, { ...SAMPLE_LAYER1, name: "abc" }),
55
- ).toBe(false);
56
- expect(
57
- isLayerSame(
58
- {
59
- ...SAMPLE_LAYER1,
60
- url: "http://abc.org",
61
- name: SAMPLE_LAYER1.name,
62
- },
63
- { ...SAMPLE_LAYER1, url: "http://abc.org" },
64
- ),
65
- ).toBe(true);
66
- expect(isLayerSame(SAMPLE_LAYER1, SAMPLE_LAYER2)).toBe(false);
67
- });
68
- it("ignores extras prop", () => {
69
- expect(
70
- isLayerSame(SAMPLE_LAYER1, {
71
- ...SAMPLE_LAYER1,
72
- extras: { otherProp: "abc" },
73
- }),
74
- ).toBe(true);
75
- expect(
76
- isLayerSame(SAMPLE_LAYER1, {
77
- ...SAMPLE_LAYER1,
78
- extras: undefined,
79
- }),
80
- ).toBe(true);
81
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
82
- const { extras, ...layer } = SAMPLE_LAYER1;
83
- expect(isLayerSame(SAMPLE_LAYER1, layer)).toBe(true);
84
- });
85
- });
86
- describe("layers with and without id", () => {
87
- it("compares by properties", () => {
88
- expect(
89
- isLayerSame(SAMPLE_LAYER1, { ...SAMPLE_LAYER1, id: "123" }),
90
- ).toBe(false);
91
- expect(
92
- isLayerSame(
93
- { ...SAMPLE_LAYER1, id: "123" },
94
- { id: "123", ...SAMPLE_LAYER1, name: SAMPLE_LAYER1.name },
95
- ),
96
- ).toBe(true);
97
- });
98
- });
99
- });
100
-
101
- describe("isLayerSameAndUnchanged", () => {
102
- describe("layers with id and version", () => {
103
- it("compares non-strictly by id and version field", () => {
104
- expect(
105
- isLayerSameAndUnchanged(
106
- { ...SAMPLE_LAYER1, id: "ab", version: 2 },
107
- { ...SAMPLE_LAYER2, id: "ab", version: 2 },
108
- ),
109
- ).toBe(true);
110
- expect(
111
- isLayerSameAndUnchanged(
112
- { ...SAMPLE_LAYER1, id: "ab", version: 2 },
113
- { ...SAMPLE_LAYER1, id: "ab", version: 1 },
114
- ),
115
- ).toBe(false);
116
- expect(
117
- isLayerSameAndUnchanged(
118
- { ...SAMPLE_LAYER1, id: 1 },
119
- { ...SAMPLE_LAYER1, id: "01", version: 3 },
120
- ),
121
- ).toBe(false);
122
- });
123
- });
124
- describe("layers with id and both without version", () => {
125
- it("compares by properties, including extras", () => {
126
- expect(
127
- isLayerSameAndUnchanged(
128
- { ...SAMPLE_LAYER1, id: "a" },
129
- { ...SAMPLE_LAYER1, id: "b" },
130
- ),
131
- ).toBe(false);
132
- expect(
133
- isLayerSameAndUnchanged(
134
- { ...SAMPLE_LAYER1, id: "a" },
135
- { ...SAMPLE_LAYER2, id: "a" },
136
- ),
137
- ).toBe(false);
138
- expect(
139
- isLayerSameAndUnchanged(
140
- { ...SAMPLE_LAYER1, id: "a" },
141
- { ...SAMPLE_LAYER1, id: "a" },
142
- ),
143
- ).toBe(true);
144
- expect(
145
- isLayerSameAndUnchanged(
146
- { ...SAMPLE_LAYER2, id: "b", extras: { hello: "world" } },
147
- { ...SAMPLE_LAYER2, id: "b", extras: { hello: "world" } },
148
- ),
149
- ).toBe(true);
150
- expect(
151
- isLayerSameAndUnchanged(
152
- { ...SAMPLE_LAYER2, id: "b", extras: { hello: "world" } },
153
- { ...SAMPLE_LAYER2, id: "b", extras: { foo: "bar" } },
154
- ),
155
- ).toBe(false);
156
- });
157
- });
158
- describe("layers without id", () => {
159
- it("compares by properties, including extras", () => {
160
- expect(isLayerSameAndUnchanged(SAMPLE_LAYER1, SAMPLE_LAYER1)).toBe(
161
- true,
162
- );
163
- expect(
164
- isLayerSameAndUnchanged(SAMPLE_LAYER1, {
165
- ...SAMPLE_LAYER1,
166
- name: "abc",
167
- }),
168
- ).toBe(false);
169
- expect(
170
- isLayerSameAndUnchanged(
171
- {
172
- ...SAMPLE_LAYER1,
173
- url: "http://abc.org",
174
- name: SAMPLE_LAYER1.name,
175
- },
176
- { ...SAMPLE_LAYER1, url: "http://abc.org" },
177
- ),
178
- ).toBe(true);
179
- expect(
180
- isLayerSameAndUnchanged(
181
- {
182
- ...SAMPLE_LAYER1,
183
- opacity: 1,
184
- },
185
- SAMPLE_LAYER1,
186
- ),
187
- ).toBe(false);
188
- expect(isLayerSameAndUnchanged(SAMPLE_LAYER1, SAMPLE_LAYER2)).toBe(
189
- false,
190
- );
191
- });
192
- it("takes into account extras prop", () => {
193
- expect(
194
- isLayerSameAndUnchanged(SAMPLE_LAYER1, {
195
- ...SAMPLE_LAYER1,
196
- extras: { otherProp: "abc" },
197
- }),
198
- ).toBe(false);
199
- expect(
200
- isLayerSameAndUnchanged(SAMPLE_LAYER1, {
201
- ...SAMPLE_LAYER1,
202
- extras: undefined,
203
- }),
204
- ).toBe(false);
205
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
206
- const { extras, ...layer } = SAMPLE_LAYER1;
207
- expect(isLayerSameAndUnchanged(SAMPLE_LAYER1, layer)).toBe(false);
208
- });
209
- });
210
- describe("layers with and without id", () => {
211
- it("compares by properties", () => {
212
- expect(
213
- isLayerSameAndUnchanged(SAMPLE_LAYER1, {
214
- ...SAMPLE_LAYER1,
215
- id: "123",
216
- }),
217
- ).toBe(false);
218
- expect(
219
- isLayerSameAndUnchanged(
220
- { ...SAMPLE_LAYER1, id: "123" },
221
- { id: "123", ...SAMPLE_LAYER1, name: SAMPLE_LAYER1.name },
222
- ),
223
- ).toBe(true);
224
- });
225
- });
226
- });
227
-
228
- describe("getLayerHash", () => {
229
- it("works with serializable entities", () => {
230
- expect(
231
- getLayerHash({
232
- ...SAMPLE_LAYER1,
233
- extras: {
234
- array: [11, 22, null, undefined],
235
- object: {},
236
- undef: undefined,
237
- },
238
- }),
239
- ).toBeTypeOf("string");
240
- });
241
- it("ignores extra property by default", () => {
242
- expect(
243
- getLayerHash({
244
- ...SAMPLE_LAYER1,
245
- extras: {
246
- array: [11, 22, null, undefined],
247
- object: {},
248
- },
249
- }),
250
- ).toEqual(
251
- getLayerHash({
252
- ...SAMPLE_LAYER1,
253
- extras: { hello: "world" },
254
- }),
255
- );
256
- });
257
- it("works with non-serializable entities (but they are ignored)", () => {
258
- const hash = getLayerHash(
259
- {
260
- ...SAMPLE_LAYER1,
261
- extras: {
262
- func: () => 123,
263
- canvas: document.createElement("canvas"),
264
- },
265
- },
266
- true,
267
- );
268
- expect(hash).toBeTypeOf("string");
269
- expect(hash).toEqual(
270
- getLayerHash(
271
- {
272
- ...SAMPLE_LAYER1,
273
- extras: {
274
- func: () => 456,
275
- canvas: document.createElement("div"),
276
- },
277
- },
278
- true,
279
- ),
280
- );
281
- expect(hash).not.toEqual(
282
- getLayerHash(
283
- {
284
- ...SAMPLE_LAYER1,
285
- extras: {},
286
- },
287
- true,
288
- ),
289
- );
290
- });
291
- it("does not take into account properties order", () => {
292
- expect(
293
- getLayerHash(
294
- {
295
- type: "wms",
296
- url: "http://abc.org/wms",
297
- name: "myLayer",
298
- extras: {
299
- array: [1, 2, 3],
300
- object: {},
301
- },
302
- },
303
- true,
304
- ),
305
- ).toEqual(
306
- getLayerHash(
307
- {
308
- extras: {
309
- array: [1, 2, 3],
310
- object: {},
311
- },
312
- url: "http://abc.org/wms",
313
- type: "wms",
314
- name: "myLayer",
315
- },
316
- true,
317
- ),
318
- );
319
- });
320
- });
321
-
322
19
  describe("getLayerPosition", () => {
323
20
  it("returns the index of the layer in the context", () => {
324
21
  const context: MapContext = {
@@ -393,4 +90,78 @@ describe("Map context utils", () => {
393
90
  expect(newContext.layers).toEqual([SAMPLE_LAYER2, SAMPLE_LAYER1]);
394
91
  });
395
92
  });
93
+
94
+ describe("updateLayerInContext", () => {
95
+ it("updates properties of a layer in the context (without id)", () => {
96
+ const context: MapContext = {
97
+ ...SAMPLE_CONTEXT,
98
+ layers: [SAMPLE_LAYER1, SAMPLE_LAYER2],
99
+ };
100
+ const newContext = updateLayerInContext(context, SAMPLE_LAYER1, {
101
+ opacity: 0.8,
102
+ visibility: false,
103
+ });
104
+ expect(newContext.layers).toHaveLength(2);
105
+ expect(newContext.layers[0]).toEqual({
106
+ ...SAMPLE_LAYER1,
107
+ opacity: 0.8,
108
+ visibility: false,
109
+ });
110
+ expect(newContext.layers[1]).toBe(SAMPLE_LAYER2);
111
+ });
112
+ it("does not modify the original context", () => {
113
+ const context: MapContext = {
114
+ ...SAMPLE_CONTEXT,
115
+ layers: [SAMPLE_LAYER1, SAMPLE_LAYER2],
116
+ };
117
+ const newContext = updateLayerInContext(context, SAMPLE_LAYER1, {
118
+ opacity: 0.8,
119
+ visibility: false,
120
+ });
121
+ expect(newContext).not.toBe(context);
122
+ expect(newContext.layers).not.toBe(context.layers);
123
+ });
124
+ it("updates properties of a layer with id in the context", () => {
125
+ const context: MapContext = {
126
+ ...SAMPLE_CONTEXT,
127
+ layers: [
128
+ SAMPLE_LAYER1,
129
+ SAMPLE_LAYER2,
130
+ { ...SAMPLE_LAYER3, id: "myLayer", version: 3 },
131
+ ],
132
+ };
133
+ const newContext = updateLayerInContext(
134
+ context,
135
+ // this is an empty layer that should not be used because we should track the layer by id in the context
136
+ {
137
+ id: "myLayer",
138
+ } as MapContextLayer,
139
+ {
140
+ opacity: 0.8,
141
+ visibility: false,
142
+ },
143
+ );
144
+ expect(newContext.layers).toHaveLength(3);
145
+ expect(newContext.layers[2]).toEqual({
146
+ ...SAMPLE_LAYER3,
147
+ id: "myLayer",
148
+ version: 4,
149
+ opacity: 0.8,
150
+ visibility: false,
151
+ });
152
+ expect(newContext.layers[0]).toBe(SAMPLE_LAYER1);
153
+ expect(newContext.layers[1]).toBe(SAMPLE_LAYER2);
154
+ });
155
+ it("changes the context ref even if the layer is not found (the layers array is untouched)", () => {
156
+ const context: MapContext = {
157
+ ...SAMPLE_CONTEXT,
158
+ layers: [SAMPLE_LAYER1, SAMPLE_LAYER2],
159
+ };
160
+ const newContext = updateLayerInContext(context, SAMPLE_LAYER3, {
161
+ opacity: 0.8,
162
+ });
163
+ expect(newContext).not.toBe(context);
164
+ expect(newContext.layers).toBe(context.layers);
165
+ });
166
+ });
396
167
  });
@@ -1,36 +1,5 @@
1
1
  import { MapContext, MapContextLayer } from "../model/index.js";
2
- import { getHash } from "./hash.js";
3
-
4
- export function getLayerHash(
5
- layer: MapContextLayer,
6
- includeExtras = false,
7
- ): string {
8
- return getHash(layer, includeExtras ? [] : ["extras"]);
9
- }
10
-
11
- export function isLayerSame(
12
- layerA: MapContextLayer,
13
- layerB: MapContextLayer,
14
- ): boolean {
15
- if ("id" in layerA && "id" in layerB) {
16
- return layerA.id == layerB.id;
17
- }
18
- return getLayerHash(layerA) === getLayerHash(layerB);
19
- }
20
-
21
- export function isLayerSameAndUnchanged(
22
- layerA: MapContextLayer,
23
- layerB: MapContextLayer,
24
- ): boolean {
25
- if (
26
- "id" in layerA &&
27
- "id" in layerB &&
28
- ("version" in layerA || "version" in layerB)
29
- ) {
30
- return layerA.id == layerB.id && layerA.version == layerB.version;
31
- }
32
- return getLayerHash(layerA, true) === getLayerHash(layerB, true);
33
- }
2
+ import { isLayerSame, updateLayer } from "./map-context-layer.js";
34
3
 
35
4
  export function getLayerPosition(
36
5
  context: MapContext,
@@ -42,10 +11,10 @@ export function getLayerPosition(
42
11
  /**
43
12
  * Adds a layer to the context at a specific position or at the end if no position is specified.
44
13
  *
45
- * @param {MapContext} context - The current map context.
46
- * @param {MapContextLayer} layerModel - The layer to be added.
47
- * @param {number} [position] - The position at which to add the layer. If not specified, the layer is added at the end.
48
- * @returns {MapContext} - The new map context with the added layer.
14
+ * @param context The current map context.
15
+ * @param layerModel The layer to be added.
16
+ * @param [position] The position at which to add the layer. If not specified, the layer is added at the end.
17
+ * @returns The new map context with the added layer.
49
18
  */
50
19
 
51
20
  export function addLayerToContext(
@@ -65,9 +34,9 @@ export function addLayerToContext(
65
34
  /**
66
35
  * Removes a layer from the context.
67
36
  *
68
- * @param {MapContext} context - The current map context.
69
- * @param {MapContextLayer} layerModel - The layer to be removed.
70
- * @returns {MapContext} - The new map context without the removed layer.
37
+ * @param context The current map context.
38
+ * @param layerModel The layer to be removed.
39
+ * @returns The new map context without the removed layer.
71
40
  */
72
41
 
73
42
  export function removeLayerFromContext(
@@ -85,10 +54,10 @@ export function removeLayerFromContext(
85
54
  /**
86
55
  * Replaces a layer in the context with a new layer.
87
56
  *
88
- * @param {MapContext} context - The current map context.
89
- * @param {MapContextLayer} layerModel - The layer to be replaced.
90
- * @param {MapContextLayer} replacement - The new layer that will replace the old one.
91
- * @returns {MapContext} - The new map context with the replaced layer.
57
+ * @param context The current map context.
58
+ * @param layerModel The layer to be replaced.
59
+ * @param replacement The new layer that will replace the old one.
60
+ * @returns The new map context with the replaced layer.
92
61
  */
93
62
 
94
63
  export function replaceLayerInContext(
@@ -104,13 +73,41 @@ export function replaceLayerInContext(
104
73
  return newContext;
105
74
  }
106
75
 
76
+ /**
77
+ * Updates an existing layer in the context by applying partial changes.
78
+ *
79
+ * Note: setting a property to `undefined` in the partial layer updates will remove that
80
+ * property from the existing layer.
81
+ *
82
+ * @param context The current map context.
83
+ * @param layerModel The layer to be updated.
84
+ * @param layerUpdates A partial layer object containing the properties to update on the existing layer.
85
+ * @returns The new map context with the updated layer.
86
+ */
87
+
88
+ export function updateLayerInContext(
89
+ context: MapContext,
90
+ layerModel: MapContextLayer,
91
+ layerUpdates: Partial<MapContextLayer>,
92
+ ): MapContext {
93
+ const position = getLayerPosition(context, layerModel);
94
+ if (position >= 0) {
95
+ const existing = context.layers[position];
96
+ const updated = updateLayer(existing, layerUpdates);
97
+ return replaceLayerInContext(context, layerModel, updated);
98
+ }
99
+ // we're building a new context so that the reference is changed anyway; this is done to be
100
+ // consistent with other utilities that always change the context reference even if the layer wasn't found
101
+ return { ...context };
102
+ }
103
+
107
104
  /**
108
105
  * Changes the position of a layer in the context.
109
106
  *
110
- * @param {MapContext} context - The current map context.
111
- * @param {MapContextLayer} layerModel - The layer whose position is to be changed.
112
- * @param {number} position - The new position for the layer.
113
- * @returns {MapContext} - The new map context with the layer moved to the new position.
107
+ * @param context The current map context.
108
+ * @param layerModel The layer whose position is to be changed.
109
+ * @param position The new position for the layer.
110
+ * @returns The new map context with the layer moved to the new position.
114
111
  */
115
112
 
116
113
  export function changeLayerPositionInContext(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geospatial-sdk/core",
3
- "version": "0.0.5-dev.46+b8d315e",
3
+ "version": "0.0.5-dev.48+a6a661d",
4
4
  "description": "Core functions and models for the SDK",
5
5
  "author": "Olivia <olivia.guyot@camptocamp.com>",
6
6
  "homepage": "",
@@ -22,7 +22,7 @@
22
22
  "test": "vitest",
23
23
  "build": "tsc"
24
24
  },
25
- "gitHead": "b8d315eaf71bccb50823f6e11c18c045ab1b6c5d",
25
+ "gitHead": "a6a661d277224d3e0acef7e2df817a3c5b7360e1",
26
26
  "dependencies": {
27
27
  "proj4": "^2.9.2"
28
28
  },