Skip to content

Commit 465e343

Browse files
committed
Factored function to get search terms
1 parent 02e8c77 commit 465e343

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

commands/lib/search.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ func searchLibrary(req *rpc.LibrarySearchRequest, lm *librariesmanager.Libraries
4141
status := rpc.LibrarySearchStatus_LIBRARY_SEARCH_STATUS_SUCCESS
4242

4343
// Split on anything but 0-9, a-z or :
44-
queryTerms := strings.FieldsFunc(strings.ToLower(req.GetQuery()), func(r rune) bool {
45-
return !((r >= '0' && r <= '9') || (r >= 'a' && r <= 'z') || r == ':')
46-
})
44+
queryTerms := rpc.SearchTermsFromQueryString(req.GetQuery())
4745

4846
for _, lib := range lm.Index.Libraries {
4947
matchTerm := func(x string) bool {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to license@arduino.cc.
15+
16+
package commands
17+
18+
import "strings"
19+
20+
// SearchTermsFromQueryString returns the terms inside the query string.
21+
// All non alphanumeric characters (expect ':') are considered separators.
22+
// All search terms are converted to lowercase.
23+
func SearchTermsFromQueryString(query string) []string {
24+
// Split on anything but 0-9, a-z or :
25+
return strings.FieldsFunc(strings.ToLower(query), func(r rune) bool {
26+
return !((r >= '0' && r <= '9') || (r >= 'a' && r <= 'z') || r == ':')
27+
})
28+
}

0 commit comments

Comments
 (0)