Skip to content

Commit

Permalink
Add the location of emoji within the string to the SearchResult struct
Browse files Browse the repository at this point in the history
  • Loading branch information
tmdvs committed Jan 18, 2019
1 parent 26f10cf commit e91ad96
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
12 changes: 6 additions & 6 deletions README.md
Expand Up @@ -8,28 +8,28 @@ A collection of useful functions for working with emoji. For example: look up th
- [x] Search a string for the presence specific emoji
- [x] Look up the definition of a single emoji
- [x] Look up the definitions for a list of emojis
- [X] Import tool to update Emoji data with [Emojipedia](http://emojipedia.org/) specs
- [ ] Find the location of and occurrences of a specific emoji in a string
- [x] Import tool to update Emoji data with [Emojipedia](http://emojipedia.org/) specs
- [x] Find the location of and occurrences of a specific emoji in a string

## Examples
### Find all occurrences of emoji in a string
You can search a string for all occurrences of emoji. You will be returned an array of results specifying which emojis were found, and how many times each occurred.
You can search a string for all occurrences of emoji. You will be returned an array of results specifying which emojis were found, and how many times each occurred. The `Locations` property comprises of an array containing the start and end locations of each occurance of the emoji within the string you're searching.

```go
input := "This is a string 😄 🐷 with some 👍🏻🙈 emoji! 🐷 🏃🏿‍♂️"
result := emoji.FindAll(input)

// result: SearchResults{ SearchResult{ Match: Emoji{…}, Occurrences: 1 }, …}
// result: SearchResults{ SearchResult{ Match: Emoji{…}, Occurrences: 1, Locations: […] }, …}
```

### Search a string for the presence specific emoji
You can search a string for the presence of a specific emoji. You will be returned a `SearchResult` struct with the definition of the matching emoji and how many times it occurred in the string.
You can search a string for the presence of a specific emoji. You will be returned a `SearchResult` struct with the definition of the matching emoji, how many times it occurred in the string, and its location within the string.

```go
input := "This is a string 😄 🐷 with some 👍🏻🙈 emoji! 🐷 🏃🏿‍♂️"
result := emoji.Find("🐷", input)

// result: SearchResult{ Match: Emoji{ Key:"1F437", Value:"🐷", Descriptor: "pig" }, Occurrences: 2 } }
// result: SearchResult{ Match: Emoji{ Key:"1F437", Value:"🐷", Descriptor: "pig" }, Occurrences: 2, Locations: [[19 19] [42 42] } }
```

### Checking search results for the occurrence of a specific emoji
Expand Down
6 changes: 5 additions & 1 deletion search.go
Expand Up @@ -11,7 +11,7 @@ import (
type SearchResult struct {
Match interface{}
Occurrences int
// TODO: Add locations of emojis
Locations [][]int
}

// SearchResults - The result of a search
Expand Down Expand Up @@ -129,10 +129,14 @@ func FindAll(input string) (detectedEmojis SearchResults) {
// Have we already accounted for this match?
if i := detectedEmojis.IndexOf(e); i != -1 {
detectedEmojis[i].Occurrences++
detectedEmojis[i].Locations = append(detectedEmojis[i].Locations, []int{index, (nextIndex - 1)})
} else {
detectedEmojis = append(detectedEmojis, SearchResult{
Match: e,
Occurrences: 1,
Locations: [][]int{
[]int{index, (nextIndex - 1)},
},
})
}
}
Expand Down

0 comments on commit e91ad96

Please sign in to comment.