Skip to content

gpadvs-search-exact-match.js: Added snippet to enable exact match functionality with GP Advanced Select. #1128

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
35 changes: 35 additions & 0 deletions gp-advanced-select/gpadvs-search-exact-match.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Gravity Perks // Advanced Select // Search For Exact Match
* https://gravitywiz.com/documentation/gravity-forms-advanced-select/
*
* By default, Advanced Select will return any item whose label contains the search query. This
* snippet will change the search algorithm to only return items whose label matches the
* search query exactly.
*
* Instruction Video: https://www.loom.com/share/4266734e5ab14870ba6b8bba28d01f68
*
* Instructions:
*
* 1. Install this snippet with our free Custom JavaScript plugin.
* https://gravitywiz.com/gravity-forms-code-chest/
*/
gform.addFilter( 'gpadvs_settings', function( settings, gpadvs ) {
settings.score = function(search) {
if ( ! search ) {
return function() {
// Item has no search query, return all items
return 1;
};
}
search = search.toLowerCase();
return function(item) {
if ( item.text.toLowerCase() === search ) {
// High score for items matching search query exactly
return 1;
}
// Zero score for items not matching search query
return 0;
};
Comment on lines +24 to +32
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add defensive programming for safer property access.

The exact match logic is sound, but there's a potential runtime error if item.text is undefined, null, or not a string. Consider adding a safety check before calling toLowerCase().

Apply this diff to add defensive programming:

 	search = search.toLowerCase();
 	return function(item) {
-		if ( item.text.toLowerCase() === search ) {
+		if ( item.text && typeof item.text === 'string' && item.text.toLowerCase() === search ) {
 			// High score for items matching search query exactly
 			return 1;
 		}
 		// Zero score for items not matching search query
 		return 0;
 	};

This ensures the code gracefully handles cases where item.text might be undefined, null, or not a string, preventing potential JavaScript errors.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
search = search.toLowerCase();
return function(item) {
if ( item.text.toLowerCase() === search ) {
// High score for items matching search query exactly
return 1;
}
// Zero score for items not matching search query
return 0;
};
search = search.toLowerCase();
return function(item) {
if ( item.text && typeof item.text === 'string' && item.text.toLowerCase() === search ) {
// High score for items matching search query exactly
return 1;
}
// Zero score for items not matching search query
return 0;
};
🤖 Prompt for AI Agents
In gp-advanced-select/gpadvs-search-exact-match.js around lines 24 to 32, the
code calls toLowerCase() on item.text without checking if item.text is defined
and a string, which can cause runtime errors. Add a defensive check to ensure
item.text exists and is a string before calling toLowerCase(), and handle cases
where it is not to avoid errors.

};
return settings;
} );
Loading