Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
apply stripDiacritics to QueryMatcher instead of individually
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
  • Loading branch information
t3chguy committed Jun 23, 2018
1 parent 7cdc918 commit fd3ada7
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 28 deletions.
4 changes: 0 additions & 4 deletions src/autocomplete/Autocompleter.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ export type Completion = {
href: ?string,
};

export function stripDiacritics(str: string): string {
return str.normalize('NFD').replace(/[\u0300-\u036f]/g, '');
}

const PROVIDERS = [
UserProvider,
RoomProvider,
Expand Down
15 changes: 10 additions & 5 deletions src/autocomplete/QueryMatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,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
Expand All @@ -46,10 +50,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);
});
Expand Down Expand Up @@ -82,7 +87,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, '');
}
Expand All @@ -91,7 +96,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, '');
}
Expand Down
6 changes: 2 additions & 4 deletions src/autocomplete/RoomProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,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;

Expand All @@ -45,7 +44,7 @@ export default class RoomProvider extends AutocompleteProvider {
constructor() {
super(ROOM_REGEX);
this.matcher = new FuzzyMatcher([], {
keys: ['displayedAlias', '_name'],
keys: ['displayedAlias', 'name'],
});
}

Expand All @@ -69,12 +68,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,
Expand Down
19 changes: 4 additions & 15 deletions src/autocomplete/UserProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import MatrixClientPeg from '../MatrixClientPeg';
import type {MatrixEvent, Room, RoomMember, RoomState} from 'matrix-js-sdk';
import {makeUserPermalink} from "../matrix-to";
import type {Completion, SelectionRange} from "./Autocompleter";
import {stripDiacritics} from "./Autocompleter";

const USER_REGEX = /@\S*/g;

Expand All @@ -40,11 +39,11 @@ export default class UserProvider extends AutocompleteProvider {

constructor(room: Room) {
super(USER_REGEX, {
keys: ['_name'],
keys: ['name'],
});
this.room = room;
this.matcher = new FuzzyMatcher([], {
keys: ['_name', 'userId'],
keys: ['name', 'userId'],
shouldMatchPrefix: true,
shouldMatchWordsOnly: false,
});
Expand Down Expand Up @@ -109,7 +108,7 @@ export default class UserProvider extends AutocompleteProvider {
const fullMatch = command[0];
// Don't search if the query is a single "@"
if (fullMatch && fullMatch !== '@') {
completions = this.matcher.match(stripDiacritics(fullMatch)).map((user) => {
completions = this.matcher.match(fullMatch).map((user) => {
const displayName = (user.name || user.userId || '').replace(' (IRC)', ''); // FIXME when groups are done
return {
// Length of completion should equal length of text in decorator. draft-js
Expand Down Expand Up @@ -143,17 +142,7 @@ export default class UserProvider extends AutocompleteProvider {
}

const currentUserId = MatrixClientPeg.get().credentials.userId;

this.users = [];
this.room.getJoinedMembers().forEach(({userId, name, ...rest}) => {
if (userId === currentUserId) return; // skip self
this.users.push({
userId,
name,
_name: stripDiacritics(name),
...rest,
});
});
this.users = this.room.getJoinedMembers().filter(({userId}) => userId !== currentUserId);

this.users = _sortBy(this.users, (member) =>
1E20 - lastSpoken[member.userId] || 1E20,
Expand Down

0 comments on commit fd3ada7

Please sign in to comment.