fix: do not consider null as value in greedyTemplate

cf #820#issuecomment-2227821746
This commit is contained in:
Yohan Boniface 2024-07-22 11:56:10 +02:00
parent da8e206cd0
commit 6b4b6ce53b
2 changed files with 15 additions and 3 deletions

View file

@ -242,10 +242,12 @@ export function greedyTemplate(str, data, ignore) {
} }
for (let i = 0; i < vars.length; i++) { for (let i = 0; i < vars.length; i++) {
path = vars[i] path = vars[i]
if (path.startsWith('"') && path.endsWith('"')) if (path.startsWith('"') && path.endsWith('"')) {
value = path.substring(1, path.length - 1) // static default value. value = path.substring(1, path.length - 1) // static default value.
else value = getValue(data, path.split('.')) } else {
if (value !== undefined) break value = getValue(data, path.split('.'))
}
if (value !== undefined && value !== null) break
} }
if (value === undefined) { if (value === undefined) {
if (ignore) value = str if (ignore) value = str

View file

@ -433,6 +433,16 @@ describe('Utils', () => {
'A phrase with [[https://osm.org|link]] as fallback.' '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()', () => { describe('#flattenCoordinates()', () => {