fix: detect case in search key

This commit is contained in:
EnixCoda 2021-02-06 00:43:54 +08:00
parent ab96ebbe3c
commit f38d093147
No known key found for this signature in database
GPG key ID: 0C1A07377913A1DD
4 changed files with 14 additions and 7 deletions

View file

@ -29,7 +29,7 @@ export function SearchBar({ onSearch, onFocus, value }: Props) {
error: searchMode === 'regex' && !isValidRegexpSource(value),
})}
aria-label="search files"
placeholder={`Search files (${searchMode === 'regex' ? 'use RegExp' : 'match path'})`}
placeholder={`Search files`}
onChange={({ target: { value } }) => onSearch(value, searchMode)}
value={value}
/>

View file

@ -1,5 +1,6 @@
import * as React from 'react'
import { cx } from 'utils/cx'
import { hasUpperCase } from 'utils/general'
import { ModeShape } from '.'
import { Highlight } from '../Highlight'
@ -7,8 +8,8 @@ export const fuzzyMode: ModeShape = {
getSearchParams(searchKey) {
if (!searchKey) return null
const searchKeyInLowerCase = searchKey.toLowerCase()
const matchNode = (node: TreeNode) => fuzzyMatch(searchKeyInLowerCase, node.path.toLowerCase())
const matchNode = (node: TreeNode) =>
fuzzyMatch(searchKey, hasUpperCase(searchKey) ? node.path : node.path.toLowerCase())
return {
matchNode,
}
@ -26,7 +27,7 @@ export const fuzzyMode: ModeShape = {
return chunks.map((chunk, index, chunks) => {
const chunkIndexes = indexes.filter(i => i >= progress && i < chunk.length + progress)
const regexp = chunkIndexes.length
? new RegExp(chunkIndexes.map(i => `(?<=^.{${i - progress}}).`).join('|'), 'i')
? new RegExp(chunkIndexes.map(i => `(?<=^.{${i - progress}}).`).join('|'))
: undefined
progress += chunk.length + 1
return (

View file

@ -1,6 +1,6 @@
import * as React from 'react'
import { cx } from 'utils/cx'
import { isValidRegexpSource, searchKeyToRegexp } from 'utils/general'
import { hasUpperCase, isValidRegexpSource, searchKeyToRegexp } from 'utils/general'
import { ModeShape } from '.'
import { Highlight } from '../Highlight'
@ -21,7 +21,9 @@ export const regexMode: ModeShape = {
},
renderNodeLabelText(node, searchKey) {
const regex =
searchKey && isValidRegexpSource(searchKey) ? new RegExp(searchKey, 'gi') : undefined
searchKey && isValidRegexpSource(searchKey)
? new RegExp(searchKey, 'g' + (hasUpperCase(searchKey) ? '' : 'i'))
: undefined
const { name } = node
return name.includes('/') ? (
name.split('/').map((chunk, index, chunks) => (

View file

@ -147,7 +147,7 @@ export function searchKeyToRegexp(searchKey: string) {
if (!searchKey) return null
try {
const flags = /[A-Z]/.test(searchKey) ? '' : 'i'
const flags = hasUpperCase(searchKey) ? '' : 'i'
// case-sensitive when searchKey contains uppercase char
return new RegExp(searchKey, flags)
} catch (err) {
@ -155,6 +155,10 @@ export function searchKeyToRegexp(searchKey: string) {
}
}
export function hasUpperCase(input: string) {
return /[A-Z]/.test(input)
}
export async function renderReact(element: ReactElement) {
return new Promise<Node>(resolve => {
const mount = document.createElement('div')