From 6b4b6ce53bc02fb7c45545bbbceb6e101359bb17 Mon Sep 17 00:00:00 2001 From: Yohan Boniface Date: Mon, 22 Jul 2024 11:56:10 +0200 Subject: [PATCH] fix: do not consider null as value in greedyTemplate cf #820#issuecomment-2227821746 --- umap/static/umap/js/modules/utils.js | 8 +++++--- umap/static/umap/unittests/utils.js | 10 ++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/umap/static/umap/js/modules/utils.js b/umap/static/umap/js/modules/utils.js index 9b7b11f1..4a6cf680 100644 --- a/umap/static/umap/js/modules/utils.js +++ b/umap/static/umap/js/modules/utils.js @@ -242,10 +242,12 @@ export function greedyTemplate(str, data, ignore) { } for (let i = 0; i < vars.length; i++) { path = vars[i] - if (path.startsWith('"') && path.endsWith('"')) + if (path.startsWith('"') && path.endsWith('"')) { value = path.substring(1, path.length - 1) // static default value. - else value = getValue(data, path.split('.')) - if (value !== undefined) break + } else { + value = getValue(data, path.split('.')) + } + if (value !== undefined && value !== null) break } if (value === undefined) { if (ignore) value = str diff --git a/umap/static/umap/unittests/utils.js b/umap/static/umap/unittests/utils.js index 2726ecf7..b48c56e7 100644 --- a/umap/static/umap/unittests/utils.js +++ b/umap/static/umap/unittests/utils.js @@ -433,6 +433,16 @@ describe('Utils', () => { 'A phrase with [[https://osm.org|link]] as fallback.' ) }) + + it('should not consider null values', () => { + assert.equal( + Utils.greedyTemplate('A phrase with a {foo|fallback}.', { + foo: null, + fallback: 'default', + }), + 'A phrase with a default.' + ) + }) }) describe('#flattenCoordinates()', () => {