Skip to content

Commit

Permalink
Merge pull request #8 from camptocamp/utilities-modifying-context
Browse files Browse the repository at this point in the history
Add Utility Functions for Modifying Layers in Map Context
  • Loading branch information
ronitjadhav authored Jun 26, 2024
2 parents cc9736c + 1d82239 commit a94ee71
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 7 deletions.
67 changes: 60 additions & 7 deletions packages/core/lib/utils/map-context.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { describe } from "vitest";
import {
getLayerHash,
getLayerPosition,
isLayerSame,
isLayerSameAndUnchanged,
addLayerToContext, changeLayerPositionInContext,
getLayerHash,
getLayerPosition,
isLayerSame,
isLayerSameAndUnchanged, removeLayerFromContext, replaceLayerInContext,
} from "./map-context";
import { MapContext } from "../model";
import {
SAMPLE_CONTEXT,
SAMPLE_LAYER1,
SAMPLE_LAYER2,
SAMPLE_CONTEXT,
SAMPLE_LAYER1,
SAMPLE_LAYER2, SAMPLE_LAYER3,
} from "../../fixtures/map-context.fixtures";

describe("Map context utils", () => {
Expand Down Expand Up @@ -300,4 +301,56 @@ describe("Map context utils", () => {
expect(getLayerPosition(context, SAMPLE_LAYER2)).toBe(1);
});
});
describe("addLayerToContext", () => {
it("adds a layer to the context", () => {
const context: MapContext = {
...SAMPLE_CONTEXT,
layers: [SAMPLE_LAYER1],
};
const newLayer = { ...SAMPLE_LAYER2, name: "newLayer" };
const newContext = addLayerToContext(context, newLayer);
expect(newContext.layers).toEqual([SAMPLE_LAYER1, newLayer]);
});
it("adds a layer at a specific position", () => {
const context: MapContext = {
...SAMPLE_CONTEXT,
layers: [SAMPLE_LAYER1, SAMPLE_LAYER2],
};
const newLayer = { ...SAMPLE_LAYER2, name: "newLayer" };
const newContext = addLayerToContext(context, newLayer, 1);
expect(newContext.layers).toEqual([SAMPLE_LAYER1, newLayer, SAMPLE_LAYER2]);
});
});
describe("removeLayerFromContext", () =>{
it("removes a layer from the context", () => {
const context: MapContext = {
...SAMPLE_CONTEXT,
layers: [SAMPLE_LAYER1, SAMPLE_LAYER2],
};
const newContext = removeLayerFromContext(context, SAMPLE_LAYER1);
expect(newContext.layers).toEqual([SAMPLE_LAYER2]);
});
});
describe("replaceLayerInContext", () => {
it("replaces a layer in the context", () => {
const context: MapContext = {
...SAMPLE_CONTEXT,
layers: [SAMPLE_LAYER1, SAMPLE_LAYER2],
};
const replacementLayer = { ...SAMPLE_LAYER3};
const existingLayer = { ...SAMPLE_LAYER1};
const newContext = replaceLayerInContext(context, existingLayer, replacementLayer);
expect(newContext.layers).toEqual([replacementLayer, SAMPLE_LAYER2]);
});
});
describe("changeLayerPositionInContext", () => {
it("changes the position of a layer in the context", () => {
const context: MapContext = {
...SAMPLE_CONTEXT,
layers: [SAMPLE_LAYER1, SAMPLE_LAYER2],
};
const newContext = changeLayerPositionInContext(context, SAMPLE_LAYER1, 1);
expect(newContext.layers).toEqual([SAMPLE_LAYER2, SAMPLE_LAYER1]);
});
});
});
83 changes: 83 additions & 0 deletions packages/core/lib/utils/map-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,86 @@ export function getLayerPosition(
): number {
return context.layers.findIndex((l) => isLayerSame(layerModel, l));
}

/**
* Adds a layer to the context at a specific position or at the end if no position is specified.
*
* @param {MapContext} context - The current map context.
* @param {MapContextLayer} layerModel - The layer to be added.
* @param {number} [position] - The position at which to add the layer. If not specified, the layer is added at the end.
* @returns {MapContext} - The new map context with the added layer.
*/

export function addLayerToContext(
context: MapContext,
layerModel: MapContextLayer,
position?: number,
): MapContext {
const newContext = { ...context, layers: [...context.layers]};
if (position !== undefined) {
newContext.layers.splice(position, 0, layerModel);
} else {
newContext.layers.push(layerModel);
}
return newContext;
}

/**
* Removes a layer from the context.
*
* @param {MapContext} context - The current map context.
* @param {MapContextLayer} layerModel - The layer to be removed.
* @returns {MapContext} - The new map context without the removed layer.
*/

export function removeLayerFromContext(
context: MapContext,
layerModel: MapContextLayer,
): MapContext {
const newContext = { ...context, layers: [...context.layers] };
const position = getLayerPosition(context, layerModel);
if (position >= 0) {
newContext.layers.splice(position, 1);
}
return newContext;
}

/**
* Replaces a layer in the context with a new layer.
*
* @param {MapContext} context - The current map context.
* @param {MapContextLayer} layerModel - The layer to be replaced.
* @param {MapContextLayer} replacement - The new layer that will replace the old one.
* @returns {MapContext} - The new map context with the replaced layer.
*/

export function replaceLayerInContext(
context: MapContext,
layerModel: MapContextLayer,
replacement: MapContextLayer,
): MapContext {
const newContext = { ...context, layers: [...context.layers] };
const position = getLayerPosition(context, layerModel);
if (position >= 0) {
newContext.layers.splice(position, 1, replacement);
}
return newContext;
}

/**
* Changes the position of a layer in the context.
*
* @param {MapContext} context - The current map context.
* @param {MapContextLayer} layerModel - The layer whose position is to be changed.
* @param {number} position - The new position for the layer.
* @returns {MapContext} - The new map context with the layer moved to the new position.
*/

export function changeLayerPositionInContext(
context: MapContext,
layerModel: MapContextLayer,
position: number,
): MapContext {
const newContext = removeLayerFromContext(context, layerModel);
return addLayerToContext(newContext, layerModel, position);
}

0 comments on commit a94ee71

Please sign in to comment.