@geospatial-sdk/core 0.0.5-dev.0 → 0.0.5-dev.11

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.
@@ -3,4 +3,39 @@ export declare function getLayerHash(layer: MapContextLayer, includeExtras?: boo
3
3
  export declare function isLayerSame(layerA: MapContextLayer, layerB: MapContextLayer): boolean;
4
4
  export declare function isLayerSameAndUnchanged(layerA: MapContextLayer, layerB: MapContextLayer): boolean;
5
5
  export declare function getLayerPosition(context: MapContext, layerModel: MapContextLayer): number;
6
+ /**
7
+ * Adds a layer to the context at a specific position or at the end if no position is specified.
8
+ *
9
+ * @param {MapContext} context - The current map context.
10
+ * @param {MapContextLayer} layerModel - The layer to be added.
11
+ * @param {number} [position] - The position at which to add the layer. If not specified, the layer is added at the end.
12
+ * @returns {MapContext} - The new map context with the added layer.
13
+ */
14
+ export declare function addLayerToContext(context: MapContext, layerModel: MapContextLayer, position?: number): MapContext;
15
+ /**
16
+ * Removes a layer from the context.
17
+ *
18
+ * @param {MapContext} context - The current map context.
19
+ * @param {MapContextLayer} layerModel - The layer to be removed.
20
+ * @returns {MapContext} - The new map context without the removed layer.
21
+ */
22
+ export declare function removeLayerFromContext(context: MapContext, layerModel: MapContextLayer): MapContext;
23
+ /**
24
+ * Replaces a layer in the context with a new layer.
25
+ *
26
+ * @param {MapContext} context - The current map context.
27
+ * @param {MapContextLayer} layerModel - The layer to be replaced.
28
+ * @param {MapContextLayer} replacement - The new layer that will replace the old one.
29
+ * @returns {MapContext} - The new map context with the replaced layer.
30
+ */
31
+ export declare function replaceLayerInContext(context: MapContext, layerModel: MapContextLayer, replacement: MapContextLayer): MapContext;
32
+ /**
33
+ * Changes the position of a layer in the context.
34
+ *
35
+ * @param {MapContext} context - The current map context.
36
+ * @param {MapContextLayer} layerModel - The layer whose position is to be changed.
37
+ * @param {number} position - The new position for the layer.
38
+ * @returns {MapContext} - The new map context with the layer moved to the new position.
39
+ */
40
+ export declare function changeLayerPositionInContext(context: MapContext, layerModel: MapContextLayer, position: number): MapContext;
6
41
  //# sourceMappingURL=map-context.d.ts.map
@@ -1 +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"}
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;AAED;;;;;;;GAOG;AAEH,wBAAgB,iBAAiB,CAC/B,OAAO,EAAE,UAAU,EACnB,UAAU,EAAE,eAAe,EAC3B,QAAQ,CAAC,EAAE,MAAM,GAChB,UAAU,CAQZ;AAED;;;;;;GAMG;AAEH,wBAAgB,sBAAsB,CAClC,OAAO,EAAE,UAAU,EACnB,UAAU,EAAE,eAAe,GACxB,UAAU,CAOhB;AAED;;;;;;;GAOG;AAEH,wBAAgB,qBAAqB,CACnC,OAAO,EAAE,UAAU,EACnB,UAAU,EAAE,eAAe,EAC3B,WAAW,EAAE,eAAe,GAC3B,UAAU,CAOZ;AAED;;;;;;;GAOG;AAEH,wBAAgB,4BAA4B,CAC1C,OAAO,EAAE,UAAU,EACnB,UAAU,EAAE,eAAe,EAC3B,QAAQ,EAAE,MAAM,GACf,UAAU,CAGZ"}
@@ -34,3 +34,64 @@ export function isLayerSameAndUnchanged(layerA, layerB) {
34
34
  export function getLayerPosition(context, layerModel) {
35
35
  return context.layers.findIndex((l) => isLayerSame(layerModel, l));
36
36
  }
37
+ /**
38
+ * Adds a layer to the context at a specific position or at the end if no position is specified.
39
+ *
40
+ * @param {MapContext} context - The current map context.
41
+ * @param {MapContextLayer} layerModel - The layer to be added.
42
+ * @param {number} [position] - The position at which to add the layer. If not specified, the layer is added at the end.
43
+ * @returns {MapContext} - The new map context with the added layer.
44
+ */
45
+ export function addLayerToContext(context, layerModel, position) {
46
+ const newContext = Object.assign(Object.assign({}, context), { layers: [...context.layers] });
47
+ if (position !== undefined) {
48
+ newContext.layers.splice(position, 0, layerModel);
49
+ }
50
+ else {
51
+ newContext.layers.push(layerModel);
52
+ }
53
+ return newContext;
54
+ }
55
+ /**
56
+ * Removes a layer from the context.
57
+ *
58
+ * @param {MapContext} context - The current map context.
59
+ * @param {MapContextLayer} layerModel - The layer to be removed.
60
+ * @returns {MapContext} - The new map context without the removed layer.
61
+ */
62
+ export function removeLayerFromContext(context, layerModel) {
63
+ const newContext = Object.assign(Object.assign({}, context), { layers: [...context.layers] });
64
+ const position = getLayerPosition(context, layerModel);
65
+ if (position >= 0) {
66
+ newContext.layers.splice(position, 1);
67
+ }
68
+ return newContext;
69
+ }
70
+ /**
71
+ * Replaces a layer in the context with a new layer.
72
+ *
73
+ * @param {MapContext} context - The current map context.
74
+ * @param {MapContextLayer} layerModel - The layer to be replaced.
75
+ * @param {MapContextLayer} replacement - The new layer that will replace the old one.
76
+ * @returns {MapContext} - The new map context with the replaced layer.
77
+ */
78
+ export function replaceLayerInContext(context, layerModel, replacement) {
79
+ const newContext = Object.assign(Object.assign({}, context), { layers: [...context.layers] });
80
+ const position = getLayerPosition(context, layerModel);
81
+ if (position >= 0) {
82
+ newContext.layers.splice(position, 1, replacement);
83
+ }
84
+ return newContext;
85
+ }
86
+ /**
87
+ * Changes the position of a layer in the context.
88
+ *
89
+ * @param {MapContext} context - The current map context.
90
+ * @param {MapContextLayer} layerModel - The layer whose position is to be changed.
91
+ * @param {number} position - The new position for the layer.
92
+ * @returns {MapContext} - The new map context with the layer moved to the new position.
93
+ */
94
+ export function changeLayerPositionInContext(context, layerModel, position) {
95
+ const newContext = removeLayerFromContext(context, layerModel);
96
+ return addLayerToContext(newContext, layerModel, position);
97
+ }
@@ -1,15 +1,16 @@
1
1
  import { describe } from "vitest";
2
2
  import {
3
- getLayerHash,
4
- getLayerPosition,
5
- isLayerSame,
6
- isLayerSameAndUnchanged,
3
+ addLayerToContext, changeLayerPositionInContext,
4
+ getLayerHash,
5
+ getLayerPosition,
6
+ isLayerSame,
7
+ isLayerSameAndUnchanged, removeLayerFromContext, replaceLayerInContext,
7
8
  } from "./map-context";
8
9
  import { MapContext } from "../model";
9
10
  import {
10
- SAMPLE_CONTEXT,
11
- SAMPLE_LAYER1,
12
- SAMPLE_LAYER2,
11
+ SAMPLE_CONTEXT,
12
+ SAMPLE_LAYER1,
13
+ SAMPLE_LAYER2, SAMPLE_LAYER3,
13
14
  } from "../../fixtures/map-context.fixtures";
14
15
 
15
16
  describe("Map context utils", () => {
@@ -300,4 +301,56 @@ describe("Map context utils", () => {
300
301
  expect(getLayerPosition(context, SAMPLE_LAYER2)).toBe(1);
301
302
  });
302
303
  });
304
+ describe("addLayerToContext", () => {
305
+ it("adds a layer to the context", () => {
306
+ const context: MapContext = {
307
+ ...SAMPLE_CONTEXT,
308
+ layers: [SAMPLE_LAYER1],
309
+ };
310
+ const newLayer = { ...SAMPLE_LAYER2, name: "newLayer" };
311
+ const newContext = addLayerToContext(context, newLayer);
312
+ expect(newContext.layers).toEqual([SAMPLE_LAYER1, newLayer]);
313
+ });
314
+ it("adds a layer at a specific position", () => {
315
+ const context: MapContext = {
316
+ ...SAMPLE_CONTEXT,
317
+ layers: [SAMPLE_LAYER1, SAMPLE_LAYER2],
318
+ };
319
+ const newLayer = { ...SAMPLE_LAYER2, name: "newLayer" };
320
+ const newContext = addLayerToContext(context, newLayer, 1);
321
+ expect(newContext.layers).toEqual([SAMPLE_LAYER1, newLayer, SAMPLE_LAYER2]);
322
+ });
323
+ });
324
+ describe("removeLayerFromContext", () =>{
325
+ it("removes a layer from the context", () => {
326
+ const context: MapContext = {
327
+ ...SAMPLE_CONTEXT,
328
+ layers: [SAMPLE_LAYER1, SAMPLE_LAYER2],
329
+ };
330
+ const newContext = removeLayerFromContext(context, SAMPLE_LAYER1);
331
+ expect(newContext.layers).toEqual([SAMPLE_LAYER2]);
332
+ });
333
+ });
334
+ describe("replaceLayerInContext", () => {
335
+ it("replaces a layer in the context", () => {
336
+ const context: MapContext = {
337
+ ...SAMPLE_CONTEXT,
338
+ layers: [SAMPLE_LAYER1, SAMPLE_LAYER2],
339
+ };
340
+ const replacementLayer = { ...SAMPLE_LAYER3};
341
+ const existingLayer = { ...SAMPLE_LAYER1};
342
+ const newContext = replaceLayerInContext(context, existingLayer, replacementLayer);
343
+ expect(newContext.layers).toEqual([replacementLayer, SAMPLE_LAYER2]);
344
+ });
345
+ });
346
+ describe("changeLayerPositionInContext", () => {
347
+ it("changes the position of a layer in the context", () => {
348
+ const context: MapContext = {
349
+ ...SAMPLE_CONTEXT,
350
+ layers: [SAMPLE_LAYER1, SAMPLE_LAYER2],
351
+ };
352
+ const newContext = changeLayerPositionInContext(context, SAMPLE_LAYER1, 1);
353
+ expect(newContext.layers).toEqual([SAMPLE_LAYER2, SAMPLE_LAYER1]);
354
+ });
355
+ });
303
356
  });
@@ -49,3 +49,86 @@ export function getLayerPosition(
49
49
  ): number {
50
50
  return context.layers.findIndex((l) => isLayerSame(layerModel, l));
51
51
  }
52
+
53
+ /**
54
+ * Adds a layer to the context at a specific position or at the end if no position is specified.
55
+ *
56
+ * @param {MapContext} context - The current map context.
57
+ * @param {MapContextLayer} layerModel - The layer to be added.
58
+ * @param {number} [position] - The position at which to add the layer. If not specified, the layer is added at the end.
59
+ * @returns {MapContext} - The new map context with the added layer.
60
+ */
61
+
62
+ export function addLayerToContext(
63
+ context: MapContext,
64
+ layerModel: MapContextLayer,
65
+ position?: number,
66
+ ): MapContext {
67
+ const newContext = { ...context, layers: [...context.layers]};
68
+ if (position !== undefined) {
69
+ newContext.layers.splice(position, 0, layerModel);
70
+ } else {
71
+ newContext.layers.push(layerModel);
72
+ }
73
+ return newContext;
74
+ }
75
+
76
+ /**
77
+ * Removes a layer from the context.
78
+ *
79
+ * @param {MapContext} context - The current map context.
80
+ * @param {MapContextLayer} layerModel - The layer to be removed.
81
+ * @returns {MapContext} - The new map context without the removed layer.
82
+ */
83
+
84
+ export function removeLayerFromContext(
85
+ context: MapContext,
86
+ layerModel: MapContextLayer,
87
+ ): MapContext {
88
+ const newContext = { ...context, layers: [...context.layers] };
89
+ const position = getLayerPosition(context, layerModel);
90
+ if (position >= 0) {
91
+ newContext.layers.splice(position, 1);
92
+ }
93
+ return newContext;
94
+ }
95
+
96
+ /**
97
+ * Replaces a layer in the context with a new layer.
98
+ *
99
+ * @param {MapContext} context - The current map context.
100
+ * @param {MapContextLayer} layerModel - The layer to be replaced.
101
+ * @param {MapContextLayer} replacement - The new layer that will replace the old one.
102
+ * @returns {MapContext} - The new map context with the replaced layer.
103
+ */
104
+
105
+ export function replaceLayerInContext(
106
+ context: MapContext,
107
+ layerModel: MapContextLayer,
108
+ replacement: MapContextLayer,
109
+ ): MapContext {
110
+ const newContext = { ...context, layers: [...context.layers] };
111
+ const position = getLayerPosition(context, layerModel);
112
+ if (position >= 0) {
113
+ newContext.layers.splice(position, 1, replacement);
114
+ }
115
+ return newContext;
116
+ }
117
+
118
+ /**
119
+ * Changes the position of a layer in the context.
120
+ *
121
+ * @param {MapContext} context - The current map context.
122
+ * @param {MapContextLayer} layerModel - The layer whose position is to be changed.
123
+ * @param {number} position - The new position for the layer.
124
+ * @returns {MapContext} - The new map context with the layer moved to the new position.
125
+ */
126
+
127
+ export function changeLayerPositionInContext(
128
+ context: MapContext,
129
+ layerModel: MapContextLayer,
130
+ position: number,
131
+ ): MapContext {
132
+ const newContext = removeLayerFromContext(context, layerModel);
133
+ return addLayerToContext(newContext, layerModel, position);
134
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geospatial-sdk/core",
3
- "version": "0.0.5-dev.0+97d2685",
3
+ "version": "0.0.5-dev.11+a94ee71",
4
4
  "description": "Core functions and models for the SDK",
5
5
  "author": "Olivia <olivia.guyot@camptocamp.com>",
6
6
  "homepage": "",
@@ -22,5 +22,5 @@
22
22
  "test": "vitest",
23
23
  "build": "tsc"
24
24
  },
25
- "gitHead": "97d2685498a9b6208d833a15480a5a9ca804e12f"
25
+ "gitHead": "a94ee7178decefed3ba074bbfd8a1a7c1813706d"
26
26
  }