From 94f7ec85e99d163cfa010821c8b99e4377172487 Mon Sep 17 00:00:00 2001 From: Timo Hocker Date: Mon, 29 Jun 2020 14:23:24 +0200 Subject: [PATCH] fix --- index.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index b74bdd0..d6795ab 100644 --- a/index.js +++ b/index.js @@ -78,6 +78,14 @@ function is_nil (obj) { || (typeof obj === 'number' && isNaN (obj)); } +function to_search_string (element, field) { + return Array.isArray (field) + ? field.map ((f) => element[f]) + .filter ((v) => typeof v !== 'undefined') + .join (' ') + : element[field]; +} + /** * filter nested objects * @@ -93,14 +101,7 @@ function recursive_filter (input, filters, children_key = 'children') { data[i] = e; let match = true; for (const filter of filters) { - const is_multi_filter - = Array.isArray (filter.fields); - - const search_str = is_multi_filter - ? filter.fields.map ((f) => e[f]) - .filter ((v) => typeof v !== 'undefined') - .join (' ') - : e[filter.field]; + const search_str = to_search_string(e, filter.field); if (!filter.filter.test (search_str)) { match = false;