github.com/haalcala/mattermost-server-change-repo@v0.0.0-20210713015153-16753fbeee5f/services/searchengine/utils.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package searchengine
     5  
     6  import (
     7  	"regexp"
     8  	"strings"
     9  
    10  	"github.com/mattermost/mattermost-server/v5/utils"
    11  )
    12  
    13  var EmailRegex = regexp.MustCompile(`^[^\s"]+@[^\s"]+$`)
    14  
    15  func GetSuggestionInputsSplitBy(term, splitStr string) []string {
    16  	splitTerm := strings.Split(strings.ToLower(term), splitStr)
    17  	var initialSuggestionList []string
    18  	for i := range splitTerm {
    19  		initialSuggestionList = append(initialSuggestionList, strings.Join(splitTerm[i:], splitStr))
    20  	}
    21  
    22  	suggestionList := []string{}
    23  	// If splitStr is not an empty space, we create a suggestion with it at the beginning
    24  	if splitStr == " " {
    25  		suggestionList = initialSuggestionList
    26  	} else {
    27  		for i, suggestion := range initialSuggestionList {
    28  			if i == 0 {
    29  				suggestionList = append(suggestionList, suggestion)
    30  			} else {
    31  				suggestionList = append(suggestionList, splitStr+suggestion, suggestion)
    32  			}
    33  		}
    34  	}
    35  	return suggestionList
    36  }
    37  
    38  func GetSuggestionInputsSplitByMultiple(term string, splitStrs []string) []string {
    39  	suggestionList := []string{}
    40  	for _, splitStr := range splitStrs {
    41  		suggestionList = append(suggestionList, GetSuggestionInputsSplitBy(term, splitStr)...)
    42  	}
    43  	return utils.RemoveDuplicatesFromStringArray(suggestionList)
    44  }