From 537eb324ed9db948e8311b432e90a4460ef32646 Mon Sep 17 00:00:00 2001 From: Birk Skyum Date: Sun, 17 Nov 2024 11:33:43 +0100 Subject: [PATCH] allow parsing of projection definition object --- src/expression/types/projection_definition.test.ts | 12 ++++++++++++ src/expression/types/projection_definition.ts | 5 +++++ 2 files changed, 17 insertions(+) diff --git a/src/expression/types/projection_definition.test.ts b/src/expression/types/projection_definition.test.ts index 532f21cd..906b3a0b 100644 --- a/src/expression/types/projection_definition.test.ts +++ b/src/expression/types/projection_definition.test.ts @@ -36,4 +36,16 @@ describe('Projection class', () => { expect(projection.to).toBe('vertical-perspective'); expect(projection.transition).toBe(0.5); }); + + test('should parse projection object', () => { + const projection = ProjectionDefinition.parse({from: 'mercator', to: 'vertical-perspective', transition: 0.5}); + expect(projection.from).toBe('mercator'); + expect(projection.to).toBe('vertical-perspective'); + expect(projection.transition).toBe(0.5); + }); + + test('should serialize projection', () => { + const projection = ProjectionDefinition.parse(['mercator', 'vertical-perspective', 0.5]); + expect(JSON.stringify(projection)).toBe('{\"from\":\"mercator\",\"to\":\"vertical-perspective\",\"transition\":0.5}'); + }); }); diff --git a/src/expression/types/projection_definition.ts b/src/expression/types/projection_definition.ts index dfb5c0bd..53832cde 100644 --- a/src/expression/types/projection_definition.ts +++ b/src/expression/types/projection_definition.ts @@ -20,6 +20,11 @@ export class ProjectionDefinition { if (Array.isArray(input) && input.length === 3 && typeof input[0] === 'string' && typeof input[1] === 'string' && typeof input[2] === 'number') { return new ProjectionDefinition(input[0], input[1], input[2]); } + + if (typeof input === 'object' && typeof input.from === 'string' && typeof input.to === 'string' && typeof input.transition === 'number') { + return new ProjectionDefinition(input.from, input.to, input.transition); + } + if (typeof input === 'string') { return new ProjectionDefinition(input, input, 1); }