mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-10-17 02:56:16 +08:00
Fix safari issue (#2666)
This commit is contained in:
parent
95a8a32008
commit
746fcbe934
2 changed files with 30 additions and 3 deletions
|
@ -13,6 +13,7 @@ import {
|
|||
TextExtractor,
|
||||
} from "../utils/search-service";
|
||||
import { debounce } from "../utils/debounce";
|
||||
import { splitByAndKeep } from "../utils/strings";
|
||||
|
||||
export let selectedId = 1;
|
||||
|
||||
|
@ -41,9 +42,7 @@ function highlightMatches(text: string, matchedText: string[]): string {
|
|||
if (matchedText.length === 0) {
|
||||
return text;
|
||||
}
|
||||
const words = text.split(
|
||||
/(?=[.,"/#!$%^&*;:{}=\-_`~()\s])|(?<=[.,"/#!$%^&*;:{}=\-_`~()\s])/g
|
||||
);
|
||||
const words = splitByAndKeep(text, `.,"/#!$%^&*;:{}=-_\`~() `.split(""));
|
||||
|
||||
const normalizedWords = words.map((word) => {
|
||||
const shouldHighlight = matchedText.find((match) => {
|
||||
|
|
28
frontend/src/scripts/utils/strings.ts
Normal file
28
frontend/src/scripts/utils/strings.ts
Normal file
|
@ -0,0 +1,28 @@
|
|||
/**
|
||||
*
|
||||
* @param text String to split
|
||||
* @param delimiters Single character delimiters.
|
||||
*/
|
||||
export function splitByAndKeep(text: string, delimiters: string[]): string[] {
|
||||
const splitString: string[] = [];
|
||||
let currentToken: string[] = [];
|
||||
const delimiterSet = new Set<string>(delimiters);
|
||||
|
||||
for (const char of text) {
|
||||
if (delimiterSet.has(char)) {
|
||||
if (currentToken.length > 0) {
|
||||
splitString.push(currentToken.join(""));
|
||||
}
|
||||
splitString.push(char);
|
||||
currentToken = [];
|
||||
} else {
|
||||
currentToken.push(char);
|
||||
}
|
||||
}
|
||||
|
||||
if (currentToken.length > 0) {
|
||||
splitString.push(currentToken.join(""));
|
||||
}
|
||||
|
||||
return splitString;
|
||||
}
|
Loading…
Add table
Reference in a new issue