diff --git a/doc/TwigJS.md b/doc/TwigJS.md index f914b0eb..5ed56f2a 100644 --- a/doc/TwigJS.md +++ b/doc/TwigJS.md @@ -71,6 +71,7 @@ Extra filters: * filter `osmParseDate`: returns an array with the lower and upper boundary of the year of a `start_date` tag. See [https://github.com/plepe/openstreetmap-date-parser](openstreetmap-date-parser) for details. * filter `osmFormatDate`: returns the date as localized strings. Accept an object for options, e.g. `{{ tags.start_date|osmFormatDate({ format: 'short' }) }}`. See [https://github.com/plepe/openstreetmap-date-format](openstreetmap-date-format) for details. * filter `natsort`: Sort an array naturally, see [https://www.npmjs.com/package/natsort](natsort) for details. +* filter `ksort`: Sort an associative array by key (alphabetic) * filter `unique`: Remove duplicate elements from an array. * filter `md5`: calculate md5 hash of a string. * filter `enumerate`: enumerate the given list, e.g. "foo, bar, and bla". Input either an array (`[ "foo", "bar", "bla" ]|enumerate`) or a string with `;` as separator (`"foo;bar;bla"|enumerate`). diff --git a/src/twigFunctions.js b/src/twigFunctions.js index d774e3d1..3171e82e 100644 --- a/src/twigFunctions.js +++ b/src/twigFunctions.js @@ -122,6 +122,17 @@ function enumerate (list) { } OverpassLayer.twig.extendFunction('enumerate', (list) => enumerate(list)) OverpassLayer.twig.extendFilter('enumerate', (list) => enumerate(list)) +OverpassLayer.twig.extendFilter('ksort', (list) => { + if (Array.isArray(list)) { + return list + } + + let keys = list._keys || Object.keys(list) + keys.sort() + let result = Object.assign({}, list) + result._keys = keys + return result +}) OverpassLayer.twig.extendFunction('debug', function () { console.log.apply(null, arguments) })