Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

addClusterFacet: Add check for empty facetMatch #43

Merged
merged 3 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions distiller.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ export class DataChunks {
addClusterFacet(facetName, baseFacet, {
count: clustercount = Math.floor(Math.log10(this.facets[baseFacet].length)),
producer = urlProducer,
}) {
}, facetCombiner = 'some', negativeCombiner = undefined) {
const facetValues = this.facets[baseFacet];

const createClusterMap = () => {
Expand Down Expand Up @@ -434,9 +434,9 @@ export class DataChunks {

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

/**
Expand Down
2 changes: 1 addition & 1 deletion facets.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export const facets = {
return url.href;
} catch (e) {
// eslint-disable-next-line no-console
console.error(`Invalid URL: ${source}`);
// console.error(`Invalid URL: ${source}`);
return null;
}
})
Expand Down
43 changes: 35 additions & 8 deletions test/distiller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -935,12 +935,12 @@ describe('DataChunks.addClusterFacet()', () => {
d.load(chunks);

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

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

const { facets } = d;

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

it('should handle null facetMatch gracefully', () => {
const d = new DataChunks();
d.load(chunks);

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

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

// Simulate a null facetMatch scenario
const facetMatch = null;
const producer = (value) => [value, value];
const clusters = (facetMatch && producer(facetMatch.value)) || [];

assert.deepEqual(clusters, []);
});

it('should handle empty facet values gracefully', () => {
const d = new DataChunks();
d.load([]);

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

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

const { facets } = d;

assert.equal(facets.urlCluster.length, 0);

// Simulate an empty facetMatch scenario
const facetMatch = {};
const producer = (value) => [value, value];
const clusters = (facetMatch && producer(facetMatch.value)) || [];

assert.deepEqual(clusters, [undefined, undefined]);
});

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

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

// Add a cluster facet based on the 'url' facet
const count = Math.floor(Math.log10(92));
d.addClusterFacet('urlCluster', 'url', {
count,
});
}, 'some', 'never');

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

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

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

const { facets } = d;

Expand Down