This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 827
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
apply stripDiacritics to QueryMatcher instead of individually
Signed-off-by: Michael Telatynski <[email protected]>
- Loading branch information
Showing
4 changed files
with
18 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
//@flow | ||
/* | ||
Copyright 2017 Aviral Dasgupta | ||
Copyright 2018 Michael Telatynski <[email protected]> | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
|
@@ -27,6 +28,10 @@ class KeyMap { | |
priorityMap = new Map(); | ||
} | ||
|
||
function stripDiacritics(str: string): string { | ||
return str.normalize('NFD').replace(/[\u0300-\u036f]/g, ''); | ||
} | ||
|
||
export default class QueryMatcher { | ||
/** | ||
* @param {object[]} objects the objects to perform a match on | ||
|
@@ -46,10 +51,11 @@ export default class QueryMatcher { | |
objects.forEach((object, i) => { | ||
const keyValues = _at(object, keys); | ||
for (const keyValue of keyValues) { | ||
if (!map.hasOwnProperty(keyValue)) { | ||
map[keyValue] = []; | ||
const key = stripDiacritics(keyValue).toLowerCase(); | ||
if (!map.hasOwnProperty(key)) { | ||
map[key] = []; | ||
} | ||
map[keyValue].push(object); | ||
map[key].push(object); | ||
} | ||
keyMap.priorityMap.set(object, i); | ||
}); | ||
|
@@ -82,7 +88,7 @@ export default class QueryMatcher { | |
} | ||
|
||
match(query: String): Array<Object> { | ||
query = query.toLowerCase(); | ||
query = stripDiacritics(query).toLowerCase(); | ||
if (this.options.shouldMatchWordsOnly) { | ||
query = query.replace(/[^\w]/g, ''); | ||
} | ||
|
@@ -91,7 +97,7 @@ export default class QueryMatcher { | |
} | ||
const results = []; | ||
this.keyMap.keys.forEach((key) => { | ||
let resultKey = key.toLowerCase(); | ||
let resultKey = key; | ||
if (this.options.shouldMatchWordsOnly) { | ||
resultKey = resultKey.replace(/[^\w]/g, ''); | ||
} | ||
|
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 |
---|---|---|
|
@@ -2,7 +2,6 @@ | |
Copyright 2016 Aviral Dasgupta | ||
Copyright 2017 Vector Creations Ltd | ||
Copyright 2017, 2018 New Vector Ltd | ||
Copyright 2018 Michael Telatynski <[email protected]> | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
|
@@ -28,7 +27,6 @@ import sdk from '../index'; | |
import _sortBy from 'lodash/sortBy'; | ||
import {makeRoomPermalink} from "../matrix-to"; | ||
import type {Completion, SelectionRange} from "./Autocompleter"; | ||
import {stripDiacritics} from "./Autocompleter"; | ||
|
||
const ROOM_REGEX = /(?=#)(\S*)/g; | ||
|
||
|
@@ -45,7 +43,7 @@ export default class RoomProvider extends AutocompleteProvider { | |
constructor() { | ||
super(ROOM_REGEX); | ||
this.matcher = new FuzzyMatcher([], { | ||
keys: ['displayedAlias', '_name'], | ||
keys: ['displayedAlias', 'name'], | ||
}); | ||
} | ||
|
||
|
@@ -69,12 +67,11 @@ export default class RoomProvider extends AutocompleteProvider { | |
return { | ||
room: room, | ||
name: room.name, | ||
_name: stripDiacritics(room.name), | ||
displayedAlias: getDisplayAliasForRoom(room), | ||
}; | ||
})); | ||
const matchedString = command[0]; | ||
completions = this.matcher.match(stripDiacritics(matchedString)); | ||
completions = this.matcher.match(matchedString); | ||
completions = _sortBy(completions, [ | ||
(c) => score(matchedString, c.displayedAlias), | ||
(c) => c.displayedAlias.length, | ||
|
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