add USE_FILTER_DESCRIPTIONS option

This commit is contained in:
Lachlan Kermode
2020-06-30 12:55:13 +02:00
parent 1501bbda78
commit e381d0d15a
4 changed files with 33 additions and 15 deletions

View File

@@ -29,10 +29,30 @@ const isLeaf = node => (Object.keys(node.children).length === 0)
const isDuplicate = (node, set) => { return (set.has(node.key)) }
/*
* Traverse a filter tree and check its duplicates
* Traverse a filter tree and check its duplicates. Also recompose as
* description if `features.USE_FILTER_DESCRIPTIONS` is true.
*/
function validateTree (node, parent, set, duplicates) {
if (!Array.isArray(node) || !node.length) {
function validateFilterTree (node, parent, set, duplicates, hasFilterDescriptions) {
if (hasFilterDescriptions) {
if (node.key === '_root') {
node.isDescription = true // setting first set of nodes to values
} else if (!parent.isDescription) {
node.isDescription = true
} else {
node.isDescription = false
}
if (node.isDescription && node.key !== 'root') {
parent.description = node.key
parent.children = node.children
delete parent.isDescription
}
if (isLeaf(node)) {
delete parent.isDescription
}
}
if (typeof (node) !== 'object' || typeof (node.children) !== 'object') {
return
}
// If it's a leaf, check that it's not duplicate
@@ -49,7 +69,7 @@ function validateTree (node, parent, set, duplicates) {
} else {
// If it's not a leaf, simply keep going
Object.values(node.children).forEach((childNode) => {
validateTree(childNode, node, set, duplicates)
validateFilterTree(childNode, node, set, duplicates, hasFilterDescriptions)
})
}
}
@@ -57,7 +77,7 @@ function validateTree (node, parent, set, duplicates) {
/*
* Validate domain schema
*/
export function validateDomain (domain) {
export function validateDomain (domain, features) {
const sanitizedDomain = {
events: [],
categories: [],
@@ -143,7 +163,7 @@ export function validateDomain (domain) {
// Validate uniqueness of filters
const filterSet = new Set([])
const duplicateFilters = []
validateTree(domain.filters, {}, filterSet, duplicateFilters)
validateFilterTree(domain.filters, {}, filterSet, duplicateFilters, features.USE_FILTER_DESCRIPTIONS)
// Duplicated filters
if (duplicateFilters.length > 0) {