Skip to content

Commit

Permalink
Filter out RegExp special characters in filter (keybase#16322)
Browse files Browse the repository at this point in the history
  • Loading branch information
akalin-keybase authored Mar 2, 2019
1 parent c8ca02b commit 322233c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
10 changes: 2 additions & 8 deletions shared/chat/inbox/container/filtered.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import * as Types from '../../../constants/types/chat2'
import * as Constants from '../../../constants/chat2'
import {memoize} from '../../../util/memoize'
import {makeInsertMatcher} from '../../../util/string'
import type {RowItem} from '../index.types'

const score = (lcFilter: string, lcYou: string, names: Array<string>, insertMatcher: RegExp): number => {
Expand Down Expand Up @@ -107,14 +108,7 @@ const getFilteredRowsAndMetadata = memoize<Types.MetaMap, string, string, void,
const metas = metaMap.valueSeq().toArray()
const lcFilter = filter.toLowerCase()
const lcYou = username.toLowerCase()
const insertMatcher = new RegExp(
`${filter
.replace(/ |#/g, '')
.split('')
.map(c => `${c}.*?`)
.join('')}`,
'i'
)
const insertMatcher = makeInsertMatcher(filter)
const rows: Array<RowItem> = metas
.map(meta => {
if (!Constants.isValidConversationIDKey(meta.conversationIDKey)) {
Expand Down
15 changes: 15 additions & 0 deletions shared/util/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@ export function pluralize(str: string, count?: number): string {
return count === 1 ? str : str.endsWith('s') ? str : `${str}s`
}

// Returns a RegExp that matches any string with the given filter
// string (with special characters removed) as a subsequence.
export function makeInsertMatcher(filter: string): RegExp {
// Clear RegExp special characters: see
// https://stackoverflow.com/a/9310752 .
return new RegExp(
`${filter
.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '')
.split('')
.map(c => `${c}.*?`)
.join('')}`,
'i'
)
}

export function toStringForLog(a: any): string {
switch (typeof a) {
case 'undefined':
Expand Down

0 comments on commit 322233c

Please sign in to comment.