-
Notifications
You must be signed in to change notification settings - Fork 90
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
base: master
Are you sure you want to change the base?
Conversation
…unctionality with GP Advanced Select.
WalkthroughA new JavaScript snippet was added to modify the search behavior of the Gravity Forms Advanced Select field. The code overrides the default scoring function to only return items whose labels exactly match the search query, using a case-insensitive comparison. Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant AdvancedSelect
participant CustomScoreFunction
User->>AdvancedSelect: Enters search query
AdvancedSelect->>CustomScoreFunction: Passes query and item label
CustomScoreFunction-->>AdvancedSelect: Returns 1 if exact match, else 0
AdvancedSelect-->>User: Displays only exact match results
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
gp-advanced-select/gpadvs-search-exact-match.js
(1 hunks)
🧰 Additional context used
🧠 Learnings (1)
📓 Common learnings
Learnt from: veryspry
PR: gravitywiz/snippet-library#1077
File: gravity-forms/gw-require-alt-text-description-post-image.php:23-32
Timestamp: 2025-05-30T13:53:21.893Z
Learning: In Gravity Forms snippets, use 'gravityforms' as the translation domain for internationalization functions instead of creating custom domains, to maintain consistency with the related plugin.
⏰ Context from checks skipped due to timeout of 90000ms (1)
- GitHub Check: Danger JS
🔇 Additional comments (3)
gp-advanced-select/gpadvs-search-exact-match.js (3)
1-15
: Excellent documentation and setup instructions.The header documentation is comprehensive and provides clear context for the snippet's purpose, including helpful links to documentation and instruction videos. The installation instructions are clear and user-friendly.
16-23
: Good implementation of the empty search case.The logic correctly handles the case where no search query is provided by returning all items with a score of 1, which maintains the expected behavior when the search field is empty.
16-35
: No action required – thegpadvs_settings
hook and callback signature are correct.
Verified thatgpadvs_settings
is consistently used across multiple snippets (gp-advanced-select/*.js
) with the same signature (function(settings, gpadvs)
) and optional extra parameters (e.g.selectNamespace
) when needed.
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; | ||
}; |
There was a problem hiding this comment.
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.
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.
Context
⛑️ Ticket(s): https://secure.helpscout.net/conversation/2986058780/85959
💬 Slack: https://gravitywiz.slack.com/archives/GMP0ZMNSE/p1751379142647819
Summary
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.
TomSelect score docs here.
Instruction Video:
https://www.loom.com/share/4266734e5ab14870ba6b8bba28d01f68