From b3c664095aa074cf7cfed2795e2d2522a9e90245 Mon Sep 17 00:00:00 2001 From: Andrei Kalfas Date: Mon, 3 Jun 2024 12:36:16 +0300 Subject: [PATCH] fix(rum-explorer): improve readability for filtering --- tools/rum/cruncher.js | 52 +++++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/tools/rum/cruncher.js b/tools/rum/cruncher.js index 1383d26f8..91a4e6149 100644 --- a/tools/rum/cruncher.js +++ b/tools/rum/cruncher.js @@ -385,36 +385,40 @@ export class DataChunks { * @param {string[]} skipped facets to skip */ filterBundles(bundles, filterSpec, skipped = []) { - const existenceFilter = ([facetName]) => this.facetFns[facetName]; - const skipFilter = ([facetName]) => !skipped.includes(facetName); - const valuesExtractor = (attributeName, bundle, parent) => { + const existenceFilterFn = ([facetName]) => this.facetFns[facetName]; + const skipFilterFn = ([facetName]) => !skipped.includes(facetName); + const valuesExtractorFn = (attributeName, bundle, parent) => { const facetValue = parent.facetFns[attributeName](bundle); return Array.isArray(facetValue) ? facetValue : [facetValue]; }; - const combinerExtractor = (attributeName, parent) => parent.facetFns[attributeName].combiner || 'some'; + const combinerExtractorFn = (attributeName, parent) => parent.facetFns[attributeName].combiner || 'some'; // eslint-disable-next-line max-len - return this.filtering(bundles, filterSpec, skipFilter, existenceFilter, valuesExtractor, combinerExtractor); + return this.applyFilter(bundles, filterSpec, skipFilterFn, existenceFilterFn, valuesExtractorFn, combinerExtractorFn); } /** * @private - * @param {Bundle[]} bundles - * @param {Object} filterSpec - * @param skipFilter function to skip filters - * @param existenceFilter function to filter out non-existing attributes - * @param valuesExtractor function to extract the probed values - * @param combinerExtractor function to extract the combiner - * @returns {*} + * @param {Bundle[]} bundles that will be filtered based on a filter specification. + * @param {Object} filterSpec the filter specification. + * @param skipFilterFn function to skip filters. Useful for skipping unwanted facets, + * in general skipping attributes. + * @param existenceFilterFn function to filter out non-existing attributes. + * This is used to skip facets that have not been added. In general, + * this can be used to whitelist attributes names. + * @param valuesExtractorFn function to extract the probed values. + * @param combinerExtractorFn function to extract the combiner. + * @returns {Bundle[]} the filtered bundles. */ - filtering(bundles, filterSpec, skipFilter, existenceFilter, valuesExtractor, combinerExtractor) { - const filterBy = Object.entries(filterSpec) // use the full filter spec - .filter(skipFilter) // except for skipped facets - .filter(([, desiredValues]) => desiredValues.length) // and filters that accept no values - .filter(existenceFilter); // and facets that don't exist + // eslint-disable-next-line max-len + applyFilter(bundles, filterSpec, skipFilterFn, existenceFilterFn, valuesExtractorFn, combinerExtractorFn) { + const filterBy = Object.entries(filterSpec) + .filter(skipFilterFn) + .filter(([, desiredValues]) => desiredValues.length) + .filter(existenceFilterFn); return bundles.filter((bundle) => { const matches = filterBy.map(([attributeName, desiredValues]) => { - const actualValues = valuesExtractor(attributeName, bundle, this); - const combiner = combinerExtractor(attributeName, this); + const actualValues = valuesExtractorFn(attributeName, bundle, this); + const combiner = combinerExtractorFn(attributeName, this); return desiredValues[combiner]((value) => actualValues.includes(value)); }); return matches.every((match) => match); @@ -429,13 +433,13 @@ export class DataChunks { * @returns {boolean} */ hasConversion(aBundle, filterSpec, combiner) { - const existenceFilter = () => true; - const skipFilter = () => true; - const valuesExtractor = (attributeName, bundle) => bundle.events.map((e) => e[attributeName]); - const combinerExtractor = () => combiner || 'every'; + const existenceFilterFn = () => true; + const skipFilterFn = () => true; + const valuesExtractorFn = (attributeName, bundle) => bundle.events.map((e) => e[attributeName]); + const combinerExtractorFn = () => combiner || 'every'; // eslint-disable-next-line max-len - return this.filtering([aBundle], filterSpec, skipFilter, existenceFilter, valuesExtractor, combinerExtractor).length > 0; + return this.applyFilter([aBundle], filterSpec, skipFilterFn, existenceFilterFn, valuesExtractorFn, combinerExtractorFn).length > 0; } filterBy(filterSpec) {