Skip to content

Commit

Permalink
Remove specific variations handling from search algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Sep 9, 2020
1 parent 595547d commit 3d20c9f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 116 deletions.
50 changes: 5 additions & 45 deletions packages/block-editor/src/components/inserter/search-items.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
/**
* External dependencies
*/
import {
deburr,
differenceWith,
find,
intersectionWith,
isEmpty,
words,
} from 'lodash';
import { deburr, differenceWith, find, words } from 'lodash';

// Default search helpers
const defaultGetName = ( item ) => item.name || '';
const defaultGetTitle = ( item ) => item.title;
const defaultGetKeywords = ( item ) => item.keywords || [];
const defaultGetCategory = ( item ) => item.category;
const defaultGetCollection = () => null;
const defaultGetVariations = () => [];

/**
* Sanitizes the search input string.
Expand Down Expand Up @@ -92,33 +84,8 @@ export const searchBlockItems = (
)
),
};
return searchItems( items, searchInput, config ).map( ( item ) => {
if ( isEmpty( item.variations ) ) {
return item;
}

const matchedVariations = item.variations.filter(
( { title, keywords = [] } ) => {
return (
intersectionWith(
normalizedSearchTerms,
getNormalizedSearchTerms( title ).concat( keywords ),
( termToMatch, labelTerm ) =>
labelTerm.includes( termToMatch )
).length > 0
);
}
);
// When no variations matched, fallback to all variations.
if ( isEmpty( matchedVariations ) ) {
return item;
}

return {
...item,
variations: matchedVariations,
};
} );
return searchItems( items, searchInput, config );
};

/**
Expand Down Expand Up @@ -162,15 +129,13 @@ export function getItemSearchRank( item, searchTerm, config = {} ) {
getKeywords = defaultGetKeywords,
getCategory = defaultGetCategory,
getCollection = defaultGetCollection,
getVariations = defaultGetVariations,
} = config;

const name = getName( item );
const title = getTitle( item );
const keywords = getKeywords( item );
const category = getCategory( item );
const collection = getCollection( item );
const variations = getVariations( item );

const normalizedSearchInput = normalizeSearchInput( searchTerm );
const normalizedTitle = normalizeSearchInput( title );
Expand All @@ -185,14 +150,9 @@ export function getItemSearchRank( item, searchTerm, config = {} ) {
} else if ( normalizedTitle.startsWith( normalizedSearchInput ) ) {
rank += 20;
} else {
const terms = [
name,
title,
...keywords,
category,
collection,
...variations,
].join( ' ' );
const terms = [ name, title, ...keywords, category, collection ].join(
' '
);
const normalizedSearchTerms = words( normalizedSearchInput );
const unmatchedTerms = removeMatchingTerms(
normalizedSearchTerms,
Expand Down
71 changes: 0 additions & 71 deletions packages/block-editor/src/components/inserter/test/search-items.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,75 +131,4 @@ describe( 'searchBlockItems', () => {
)
).toEqual( [ youtubeItem ] );
} );

it( 'should match words using also variations and return all matched variations', () => {
const filteredItems = searchBlockItems(
items,
categories,
collections,
'variation'
);

expect( filteredItems ).toHaveLength( 1 );
expect( filteredItems[ 0 ].variations ).toHaveLength( 3 );
} );

it( 'should match words using also variations and filter out unmatched variations', () => {
const filteredItems = searchBlockItems(
items,
categories,
collections,
'variations two three'
);

expect( filteredItems ).toHaveLength( 1 );
expect( filteredItems[ 0 ].variations ).toHaveLength( 2 );
expect( filteredItems[ 0 ].variations[ 0 ].title ).toBe(
'Variation Two'
);
expect( filteredItems[ 0 ].variations[ 1 ].title ).toBe(
'Variation Three'
);
} );

it( 'should search in variation keywords if exist', () => {
const filteredItems = searchBlockItems(
items,
categories,
collections,
'music'
);
expect( filteredItems ).toHaveLength( 1 );
const [ { title, variations } ] = filteredItems;
expect( title ).toBe( 'With Variations' );
expect( variations[ 0 ].title ).toBe( 'Variation Three' );
} );

it( 'should search in both blocks/variation keywords if exist', () => {
const filteredItems = searchBlockItems(
items,
categories,
collections,
'random'
);
expect( filteredItems ).toHaveLength( 2 );
expect( filteredItems ).toEqual(
expect.arrayContaining( [
expect.objectContaining( { title: 'Paragraph' } ),
expect.objectContaining( {
title: 'With Variations',
variations: [
{
name: 'variation-three',
title: 'Variation Three',
keywords: expect.arrayContaining( [
'music',
'random',
] ),
},
],
} ),
] )
);
} );
} );

0 comments on commit 3d20c9f

Please sign in to comment.