From 5d11f435c2289db2d5765c11f2c5f688badf23b9 Mon Sep 17 00:00:00 2001 From: Benjamin Gerber Date: Fri, 4 Oct 2024 10:16:14 +0200 Subject: [PATCH] fix: test if opacity is realy undefined --- src/SldStyleParser.ts | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/SldStyleParser.ts b/src/SldStyleParser.ts index da35352e..005c2957 100644 --- a/src/SldStyleParser.ts +++ b/src/SldStyleParser.ts @@ -162,7 +162,11 @@ export const defaultTranslations: SldStyleParserTranslations = { }, } as const; -const isNil = (val: unknown) => val === undefined || val === null; + +/** + * @returns true if the provided value is null or undefined. Returns false otherwise. + */ +const isNil = (val: unknown): boolean => val === undefined || val === null; /** * This parser can be used with the GeoStyler. @@ -808,7 +812,7 @@ export class SldStyleParser implements StyleParser { } textSymbolizer.color = color ? color : '#000000'; - textSymbolizer.opacity = opacity ? numberExpression(opacity) : 1; + textSymbolizer.opacity = isNil(opacity) ? 1 : numberExpression(opacity); const haloRadius = get(sldSymbolizer, 'Halo.Radius.#text'); if (!isNil(haloRadius)) { @@ -1181,7 +1185,7 @@ export class SldStyleParser implements StyleParser { } const label = getAttribute(cm, 'label'); let opacity = getAttribute(cm, 'opacity'); - if (opacity) { + if (!isNil(opacity)) { opacity = numberExpression(opacity); } return { @@ -1687,7 +1691,7 @@ export class SldStyleParser implements StyleParser { }] }]; - if (markSymbolizer.color || markSymbolizer.fillOpacity) { + if (markSymbolizer.color || !isNil(markSymbolizer.fillOpacity)) { const fillCssParamaters = []; if (markSymbolizer.color) { if (isGeoStylerFunction(markSymbolizer.color)) { @@ -1709,7 +1713,7 @@ export class SldStyleParser implements StyleParser { }); } } - if (markSymbolizer.fillOpacity) { + if (!isNil(markSymbolizer.fillOpacity)) { if (isGeoStylerFunction(markSymbolizer.fillOpacity)) { const children = geoStylerFunctionToSldFunction(markSymbolizer.fillOpacity); fillCssParamaters.push({ @@ -1779,7 +1783,7 @@ export class SldStyleParser implements StyleParser { }); } } - if (markSymbolizer.strokeOpacity) { + if (!isNil(markSymbolizer.strokeOpacity)) { if (isGeoStylerFunction(markSymbolizer.strokeOpacity)) { const children = geoStylerFunctionToSldFunction(markSymbolizer.strokeOpacity); strokeCssParameters.push({ @@ -1808,10 +1812,10 @@ export class SldStyleParser implements StyleParser { [Mark]: mark }]; - if (markSymbolizer.opacity) { + if (!isNil(markSymbolizer.opacity)) { graphic.push({ [Opacity]: [{ - '#text': markSymbolizer.opacity.toString() + '#text': markSymbolizer.opacity!.toString() }] }); } @@ -1907,7 +1911,7 @@ export class SldStyleParser implements StyleParser { } } - if (iconSymbolizer.opacity) { + if (!isNil(iconSymbolizer.opacity)) { graphic.push({ [Opacity]: [{ '#text': iconSymbolizer.opacity