-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
117 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
function escape(searchTerm: string): string { | ||
return searchTerm.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&"); | ||
} | ||
|
||
function ignoreAccents(searchTerm: string): string { | ||
return searchTerm | ||
.replace(/[cčć]/gi, "[cčć]") | ||
.replace(/[sš]/gi, "[sš]") | ||
.replace(/[zž]/gi, "[zž]") | ||
.replace(/[aàáâäæãåā]/gi, "[aàáâäæãåā]") | ||
.replace(/[eèéêëēėę]/gi, "[eèéêëēėę]") | ||
.replace(/[iîïíīįì]/gi, "[iîïíīįì]") | ||
.replace(/[oôöòóœøōõ]/gi, "[oôöòóœøōõ]") | ||
.replace(/[uûüùúū]/gi, "[uûüùúū]") | ||
.replace(/[dđ]/gi, "[dđ]"); | ||
} | ||
|
||
type THasName = { | ||
name: string; | ||
}; | ||
|
||
function filterEntitiesBySearchTerm<E extends THasName>( | ||
entities: E[], | ||
searchTerm: string | ||
): E[] { | ||
searchTerm = searchTerm.toLowerCase(); | ||
searchTerm = escape(searchTerm); | ||
searchTerm = ignoreAccents(searchTerm); | ||
const regExp = new RegExp(searchTerm); | ||
|
||
return entities.filter((entity) => regExp.test(entity.name.toLowerCase())); | ||
} | ||
|
||
export { filterEntitiesBySearchTerm }; |