Browse Source

CategoryOverpassFilter: specify values via path into data

master
parent
commit
3eb9925231
  1. 3
      package.json
  2. 5
      src/CategoryOverpassFilter.js
  3. 11
      src/getPathFromJSON.js
  4. 25
      test/getPathFromJSON.js

3
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"
},

5
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 => {

11
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]])
}

25
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
)
})
})
Loading…
Cancel
Save