From 3eb99252316b3d5980f8ea873e795234d3c9677f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Stephan=20B=C3=B6sch-Plepelits?= <skunk@xover.mud.at>
Date: Sun, 20 Jan 2019 21:56:31 +0100
Subject: [PATCH] CategoryOverpassFilter: specify values via path into data

---
 package.json                  |  3 ++-
 src/CategoryOverpassFilter.js |  5 +++++
 src/getPathFromJSON.js        | 11 +++++++++++
 test/getPathFromJSON.js       | 25 +++++++++++++++++++++++++
 4 files changed, 43 insertions(+), 1 deletion(-)
 create mode 100644 src/getPathFromJSON.js
 create mode 100644 test/getPathFromJSON.js

diff --git a/package.json b/package.json
index 257dd35f..3146a62c 100644
--- a/package.json
+++ b/package.json
@@ -68,7 +68,7 @@
     ]
   },
   "scripts": {
-    "test": "echo \"Error: no test specified\" && exit 1",
+    "test": "mocha --bail",
     "build": "npm run build-locales && browserify -g browserify-css src/index.js -o dist/tmp1.js && babel --presets env dist/tmp1.js > dist/tmp2.js && mv dist/tmp2.js dist/openstreetbrowser.js && rm dist/tmp1.js",
     "build-locales": "for i in `ls locales/` ; do browserify locales/$i -o dist/locale-$i ; done",
     "watch": "npm run build-locales && watchify --debug -g browserify-css src/index.js -o dist/openstreetbrowser.js -v",
@@ -79,6 +79,7 @@
     "browserify": "^14.4.0",
     "browserify-css": "^0.14.0",
     "leaflet-polylinedecorator": "https://github.com/plepe/Leaflet.PolylineDecorator.git",
+    "mocha": "^5.2.0",
     "standard": "^10.0.2",
     "watchify": "^3.9.0"
   },
diff --git a/src/CategoryOverpassFilter.js b/src/CategoryOverpassFilter.js
index ae7f29c5..24abd4f8 100644
--- a/src/CategoryOverpassFilter.js
+++ b/src/CategoryOverpassFilter.js
@@ -3,6 +3,7 @@ const tabs = require('modulekit-tabs')
 
 const state = require('./state')
 const Filter = require('overpass-frontend').Filter
+const getPathFromJSON = require('./getPathFromJSON')
 
 class CategoryOverpassFilter {
   constructor (master) {
@@ -35,6 +36,10 @@ class CategoryOverpassFilter {
       if ('values' in f) {
         let template = OverpassLayer.twig.twig({ data: f.valueName || '{{ value }}', autoescape: true })
 
+        if (typeof f.values === 'string') {
+          f.values = getPathFromJSON(f.values, this.master.data)
+        }
+
         if (Array.isArray(f.values) && f.valueName) {
           let newValues = {}
           f.values.forEach(value => {
diff --git a/src/getPathFromJSON.js b/src/getPathFromJSON.js
new file mode 100644
index 00000000..15a6e149
--- /dev/null
+++ b/src/getPathFromJSON.js
@@ -0,0 +1,11 @@
+module.exports = function getPathFromJSON (path, json) {
+  if (typeof path === 'string') {
+    path = path.split(/\./)
+  }
+
+  if (path.length === 0) {
+    return json
+  }
+
+  return getPathFromJSON(path.slice(1), json[path[0]])
+}
diff --git a/test/getPathFromJSON.js b/test/getPathFromJSON.js
new file mode 100644
index 00000000..f4eadf6b
--- /dev/null
+++ b/test/getPathFromJSON.js
@@ -0,0 +1,25 @@
+const getPathFromJSON = require('../src/getPathFromJSON')
+const assert = require('assert')
+
+describe('getPathFromJSON', function () {
+  it('const', function () {
+    assert.deepEqual(
+      getPathFromJSON('const', { const: { 'foo': 'foo', 'bar': 'bar' } }),
+      { 'foo': 'foo', 'bar': 'bar' }
+    )
+  })
+
+  it('const.x', function () {
+    assert.deepEqual(
+      getPathFromJSON('const.x', { const: { x: { 'foo': 'foo', 'bar': 'bar' } } }),
+      { 'foo': 'foo', 'bar': 'bar' }
+    )
+  })
+
+  it('const.y (not exist)', function () {
+    assert.deepEqual(
+      getPathFromJSON('const.y', { const: { x: { 'foo': 'foo', 'bar': 'bar' } } }),
+      undefined
+    )
+  })
+})