From 3908dbc38a565c7bbfcdf2416222d5945828da6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20B=C3=B6sch-Plepelits?= Date: Sat, 20 Aug 2022 17:06:24 +0100 Subject: [PATCH] twigFunctions yaml, json_pp: improve removing twig artefacts --- src/twigFunctions.js | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/twigFunctions.js b/src/twigFunctions.js index d184356c..1d49c321 100644 --- a/src/twigFunctions.js +++ b/src/twigFunctions.js @@ -144,16 +144,33 @@ OverpassLayer.twig.extendFilter('debug', function (value, param) { OverpassLayer.twig.extendFilter('json_pp', function (value, param) { const options = param[0] || {} - value = JSON.parse(JSON.stringify(value)) - delete value._keys // remove TwigJS artefact + if (value === 'undefined') { + return 'null' + } + + value = twigClear(value) return JSON.stringify(value, null, 'indent' in options ? ' '.repeat(options.indent) : ' ') }) OverpassLayer.twig.extendFilter('yaml', function (value, param) { const options = param[0] || {} - value = JSON.parse(JSON.stringify(value)) - delete value._keys // remove TwigJS artefact + value = twigClear(value) return yaml.dump(value, options) }) + +function twigClear (value) { + if (value === null || typeof value !== 'object') { + return value + } + + const v = {} + for (let k in value) { + if (k !== '_keys') { + v[k] = value[k] + } + } + + return v +}