Skip to content

Commit 8099709

Browse files
authored
Merge pull request #43 from adobe/issue-42
addClusterFacet: Add check for empty facetMatch
2 parents 478a983 + 37cda06 commit 8099709

File tree

3 files changed

+39
-12
lines changed

3 files changed

+39
-12
lines changed

distiller.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ export class DataChunks {
400400
addClusterFacet(facetName, baseFacet, {
401401
count: clustercount = Math.floor(Math.log10(this.facets[baseFacet].length)),
402402
producer = urlProducer,
403-
}) {
403+
}, facetCombiner = 'some', negativeCombiner = undefined) {
404404
const facetValues = this.facets[baseFacet];
405405

406406
const createClusterMap = () => {
@@ -434,9 +434,9 @@ export class DataChunks {
434434

435435
this.addFacet(facetName, (bundle) => {
436436
const facetMatch = facetValues.find((f) => f.entries.some((e) => e.id === bundle.id));
437-
const clusters = producer(facetMatch.value);
437+
const clusters = (facetMatch && producer(facetMatch.value)) || [];
438438
return [facetMatch.value, ...clusters.filter((cluster) => sortedClusters.includes(cluster))];
439-
});
439+
}, facetCombiner, negativeCombiner);
440440
}
441441

442442
/**

facets.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ export const facets = {
165165
return url.href;
166166
} catch (e) {
167167
// eslint-disable-next-line no-console
168-
console.error(`Invalid URL: ${source}`);
168+
// console.error(`Invalid URL: ${source}`);
169169
return null;
170170
}
171171
})

test/distiller.test.js

+35-8
Original file line numberDiff line numberDiff line change
@@ -935,12 +935,12 @@ describe('DataChunks.addClusterFacet()', () => {
935935
d.load(chunks);
936936

937937
// Define a facet function
938-
d.addFacet('url', (bundle) => [bundle.url]);
938+
d.addFacet('url', (bundle) => [bundle.url], 'some', 'never');
939939

940940
// Add a cluster facet based on the 'url' facet
941941
d.addClusterFacet('urlCluster', 'url', {
942942
count: Math.log10(d.facets.url.length),
943-
});
943+
}, 'some', 'never');
944944

945945
const { facets } = d;
946946

@@ -951,35 +951,62 @@ describe('DataChunks.addClusterFacet()', () => {
951951
assert.equal(facets.urlCluster[2].value, 'https://www.aem.live/developer/tutorial');
952952
});
953953

954+
it('should handle null facetMatch gracefully', () => {
955+
const d = new DataChunks();
956+
d.load(chunks);
957+
958+
// Define a facet function
959+
d.addFacet('url', (bundle) => [bundle.url], 'some', 'never');
960+
961+
// Add a cluster facet based on the 'url' facet
962+
d.addClusterFacet('urlCluster', 'url', {
963+
count: Math.log10(d.facets.url.length),
964+
}, 'some', 'never');
965+
966+
// Simulate a null facetMatch scenario
967+
const facetMatch = null;
968+
const producer = (value) => [value, value];
969+
const clusters = (facetMatch && producer(facetMatch.value)) || [];
970+
971+
assert.deepEqual(clusters, []);
972+
});
973+
954974
it('should handle empty facet values gracefully', () => {
955975
const d = new DataChunks();
956976
d.load([]);
957977

958978
// Define a facet function
959-
d.addFacet('url', (bundle) => [bundle.url]);
979+
d.addFacet('url', (bundle) => [bundle.url], 'some', 'never');
960980

961981
// Add a cluster facet based on the 'url' facet
962982
d.addClusterFacet('urlCluster', 'url', {
963983
count: Math.log10(d.facets.url.length),
964-
});
984+
}, 'some', 'never');
965985

966986
const { facets } = d;
967987

968988
assert.equal(facets.urlCluster.length, 0);
989+
990+
// Simulate an empty facetMatch scenario
991+
const facetMatch = {};
992+
const producer = (value) => [value, value];
993+
const clusters = (facetMatch && producer(facetMatch.value)) || [];
994+
995+
assert.deepEqual(clusters, [undefined, undefined]);
969996
});
970997

971998
it('should log the correct cluster count', () => {
972999
const d = new DataChunks();
9731000
d.load(chunks);
9741001

9751002
// Define a facet function
976-
d.addFacet('url', (bundle) => [bundle.url]);
1003+
d.addFacet('url', (bundle) => [bundle.url], 'some', 'never');
9771004

9781005
// Add a cluster facet based on the 'url' facet
9791006
const count = Math.floor(Math.log10(92));
9801007
d.addClusterFacet('urlCluster', 'url', {
9811008
count,
982-
});
1009+
}, 'some', 'never');
9831010

9841011
// Check if the count is correct
9851012
assert.strictEqual(count, 1);
@@ -990,12 +1017,12 @@ describe('DataChunks.addClusterFacet()', () => {
9901017
d.load(chunks);
9911018

9921019
// Define a facet function
993-
d.addFacet('url', (bundle) => [bundle.url]);
1020+
d.addFacet('url', (bundle) => [bundle.url], 'some', 'never');
9941021

9951022
// Add a cluster facet based on the 'url' facet
9961023
d.addClusterFacet('urlCluster', 'url', {
9971024
count: Math.log10(d.facets.url.length),
998-
});
1025+
}, 'some', 'never');
9991026

10001027
const { facets } = d;
10011028

0 commit comments

Comments
 (0)