chore: add facets.getParser method

This commit is contained in:
Yohan Boniface 2024-04-18 16:22:00 +02:00
parent b0c6b42c17
commit eb0d17154c
2 changed files with 23 additions and 9 deletions

View file

@ -25,11 +25,12 @@ export default class Facets {
names.forEach((name) => {
let value = feature.properties[name]
const type = defined[name]['type']
const parser = this.getParser(type)
value = parser(value)
switch (type) {
case 'date':
case 'datetime':
case 'number':
value = type === 'number' ? parseFloat(value) : new Date(value)
if (!isNaN(value)) {
if (isNaN(properties[name].min) || properties[name].min > value) {
properties[name].min = value
@ -40,7 +41,7 @@ export default class Facets {
}
break
default:
value = String(value || '') || L._('<empty value>')
value = value || L._('<empty value>')
if (!properties[name].choices.includes(value)) {
properties[name].choices.push(value)
}
@ -130,4 +131,20 @@ export default class Facets {
return acc
}, {})
}
getParser(type) {
switch(type) {
case 'number':
return parseFloat
case 'datetime':
return (v) => new Date(v)
case 'date':
return (v) => new Date(new Date(v).toDateString()) // Without time
default:
return (v) => String(v || '')
}
}
}

View file

@ -496,15 +496,12 @@ U.FeatureMixin = {
const selected = this.map.facets.selected
for (let [name, { type, min, max, choices }] of Object.entries(selected)) {
let value = this.properties[name]
let parser
let parser = this.map.facets.getParser(type)
value = parser(value)
switch (type) {
case 'date':
case 'datetime':
case 'number':
if (type === 'number') parser = parseFloat
else if (type == 'datetime') parser = (v) => new Date(v)
else parser = (v) => new Date(new Date(v).toDateString()) // Without time
value = value != null ? value : undefined
min = parser(min)
max = parser(max)
value = parser(value)
@ -512,8 +509,8 @@ U.FeatureMixin = {
if (!isNaN(max) && !isNaN(value) && max < value) return false
break
default:
value = String(value || '') || L._('<empty value>')
if (choices?.length && (!value || !choices.includes(value))) return false
value = value || L._('<empty value>')
if (choices?.length && !choices.includes(value)) return false
break
}
}