Skip to content

Commit a718631

Browse files
committed
TwigJS: new filter 'parseDirection'
1 parent 77f0bee commit a718631

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

doc/TwigJS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ Extra filters:
8181
* filter `ksort`: Sort an associative array by key (alphabetic)
8282
* filter `unique`: Remove duplicate elements from an array.
8383
* filter `md5`: calculate md5 hash of a string.
84+
* filter `parseDirection`: parses the value of a direction (e.g. 'east' => 90, 'SE' => 135). If the value is a string, but not a cardinal direction return the value as number (e.g. '90' => 90). Other values are returned as is.
8485
* 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`).
8586
* filter `debug`: print the value (and further arguments) to the javascript console (via `console.log()`)
8687
* filter `wikipediaAbstract`: shows the abstract of a Wikipedia article in the selected data language (or, if not available, the language which was used in input, resp. 'en' for Wikidata input). Input is either 'language:article' (e.g. 'en:Douglas Adams') or a wikidata id (e.g. 'Q42').

src/twigFunctions.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,33 @@ const yaml = require('js-yaml')
99

1010
var md5cache = {}
1111

12+
const cardinalDirections = {
13+
'NORTH': 0,
14+
'N': 0,
15+
'NNE': 22.5,
16+
'NE': 45,
17+
'NORTHEAST': 45,
18+
'ENE': 67.5,
19+
'EAST': 90,
20+
'E': 90,
21+
'ESE': 112.5,
22+
'SE': 135,
23+
'SOUTHEAST': 45,
24+
'SSE': 157.5,
25+
'SOUTH': 180,
26+
'S': 180,
27+
'SSW': 202.5,
28+
'SW': 225,
29+
'SOUTHWEST': 225,
30+
'WSW': 247.5,
31+
'WEST': 270,
32+
'W': 270,
33+
'WNW': 292.5,
34+
'NW': 315,
35+
'NORTHWEST': 315,
36+
'NNW': 337.5
37+
}
38+
1239
OverpassLayer.twig.extendFunction('tagsPrefix', function (tags, prefix) {
1340
var ret = {}
1441
var count = 0
@@ -59,6 +86,18 @@ OverpassLayer.twig.extendFilter('matches', function (value, param) {
5986
OverpassLayer.twig.extendFilter('natsort', function (values, options) {
6087
return values.sort(natsort(options))
6188
})
89+
OverpassLayer.twig.extendFilter('parseDirection', function (value, options) {
90+
if (typeof value === 'string') {
91+
const valueUpper = value.trim().toUpperCase()
92+
if (valueUpper in cardinalDirections) {
93+
return cardinalDirections[valueUpper]
94+
}
95+
96+
return parseFloat(value)
97+
}
98+
99+
return value
100+
})
62101
OverpassLayer.twig.extendFilter('unique', function (values, options) {
63102
// source: https://stackoverflow.com/a/14438954
64103
function onlyUnique (value, index, self) {

0 commit comments

Comments
 (0)