Skip to content

Commit 1736d2b

Browse files
committed
test for uppercase once in advance
1 parent 46c2631 commit 1736d2b

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

src/api/const.ts

+1
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ export const ERROR_PORT_CLOSED =
2121
export const ERROR_QUOTA_EXCEEDED = 'QUOTA_BYTES quota exceeded';
2222
export const BACKGROUND_SCRIPT_CONNECTION_INTERVAL = 30e3;
2323
export const BACKGROUND_SCRIPT_CONNECTION_NAME = 'jsdiff-devtools-page-connect';
24+
export const UPPERCASE_PATTERN = /\p{Lu}/u; // 'u' flag enables Unicode matching

src/stores/search.store.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { UPPERCASE_PATTERN } from '@/api/const.ts';
12
import { defineStore } from 'pinia';
23
import { markRaw } from 'vue';
34

@@ -53,6 +54,7 @@ export const useSearchStore = defineStore('searchStore', {
5354
highlightAll(
5455
this.deltaEl,
5556
this.searchQuery,
57+
UPPERCASE_PATTERN.test(this.searchQuery),
5658
/*IN/OUT*/ this.foundEls
5759
);
5860
this.currentIndex = -1;
@@ -80,7 +82,6 @@ export const useSearchStore = defineStore('searchStore', {
8082

8183
const CLASS_FOUND = 'jsdiff-found';
8284
const CLASS_FOUND_THIS = 'jsdiff-found-this';
83-
const UPPERCASE_PATTERN = /\p{Lu}/u; // 'u' flag enables Unicode matching
8485

8586
function indexInBound(index: number, arrayLength: number): number {
8687
if (index < 0) {
@@ -119,6 +120,7 @@ function highlightCurrent(el: HTMLElement) {
119120
function highlightAll(
120121
container: HTMLElement | null,
121122
query: string,
123+
isCaseSensitive: boolean,
122124
/*IN/OUT*/ els: HTMLElement[]
123125
) {
124126
if (!container) {
@@ -137,9 +139,9 @@ function highlightAll(
137139

138140
if (isLeafNode) {
139141
const text = firstChild.textContent || firstChild.innerText;
140-
const hasMatch = UPPERCASE_PATTERN.test(query)
141-
? text.includes(query) // case-sensitive
142-
: text.toLocaleLowerCase().includes(query); // case-insensitive
142+
const hasMatch = isCaseSensitive
143+
? text.includes(query)
144+
: text.toLocaleLowerCase().includes(query);
143145

144146
if (!hasMatch) {
145147
return;
@@ -160,7 +162,7 @@ function highlightAll(
160162
const child = containerNodes[n];
161163

162164
if (child.nodeType === Node.ELEMENT_NODE) {
163-
highlightAll(child, query, els); // recursion
165+
highlightAll(child, query, isCaseSensitive, els); // recursion
164166
}
165167
}
166168
}

0 commit comments

Comments
 (0)