From 1e5096a1ef8accd5a3eb6b3178fd3c9601fedb93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20B=C3=B6sch-Plepelits?= Date: Tue, 30 Aug 2022 20:47:25 +0200 Subject: [PATCH] CustomCategory: make testing extensible for more fields --- src/customCategory.js | 49 ++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 26 deletions(-) diff --git a/src/customCategory.js b/src/customCategory.js index b7d2d1f5..099125a5 100644 --- a/src/customCategory.js +++ b/src/customCategory.js @@ -427,38 +427,35 @@ function customCategoryTest (value) { return new Error('"query" can not be parsed!') } - let fields = ['feature', 'memberFeature'] - for (let i1 = 0; i1 < fields.length; i1++) { - const k1 = fields[i1] - if (data[k1]) { - for (const k2 in data[k1]) { - const err = customCategoryTestCompile(data[k1][k2]) - if (err) { - return new Error('Compiling /' + k1 + '/' + k2 + ': ' + err.message) - } + const testPaths = [ + [ /^(memberF|f)eature$/, /./ ], + [ /^(memberF|f)eature$/, /^style(:.*)$/, /./ ], + [ 'info' ] + ] - if (k2 === 'style' || k2.match(/^style:/)) { - for (const k3 in data[k1][k2]) { - const err = customCategoryTestCompile(data[k1][k2][k3]) - if (err) { - return new Error('Compiling /' + k1 + '/' + k2 + '/' + k3 + ': ' + err.message) - } - } - } - } - } + try { + testPaths.forEach(path => customCategoryTestCompilePath(data, path, '')) } + catch (err) { + return err + } +} - fields = ['info'] - for (let i1 = 0; i1 < fields.length; i1++) { - const k1 = fields[i1] - if (data[k1]) { - const err = customCategoryTestCompile(data[k1]) - if (err) { - return new Error('Compiling /' + k1 + ': ' + err.message) +function customCategoryTestCompilePath (data, subPaths, path) { + if (subPaths.length) { + const p = subPaths[0] + + for (const k in data) { + if (typeof p === 'string' ? k === p : k.match(p)) { + customCategoryTestCompilePath(data[k], subPaths.slice(1), path + '/' + k) } } } + + const err = customCategoryTestCompile(data) + if (err) { + throw new Error('Compiling ' + path + ': ' + err.message) + } } function customCategoryTestCompile (data) {