Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prioritizes people autocomplete suggestions #762

Merged
merged 3 commits into from Jun 29, 2017

Conversation

jainkuniya
Copy link
Member

Prioritizes people autocomplete in order of :-

  • starts with name.
  • matches initial.
  • contains in name.
  • matches in email.

in a single iteration of users array.

@borisyankov
Copy link
Contributor

Great. I like it that you wrote tests for that.
Without looking at the implementation even, I know the code works.

I would say it is good enough to merge, but I think the code can be improved from stylistical point of view, so why not do it (I'll write my comments in a code review next)

Copy link
Contributor

@borisyankov borisyankov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems I found a good amount of things to change in the end 😄

I think these are quite interesting things to learn though.

Add few more tests... even add them before you implement the feature.
For example, add a test for duplicate items. Then once you have a failing test, make it pass by implementing this feature.

users.map((user) => {
if (user.email === ownEmail) return user;
else if (user.fullName.toLowerCase().startsWith(filter.toLowerCase())) {
startWith = [...startWith, user];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not performant. On every match it will create a new array.

export const filterUserList = (users: any[], filter: string = '', ownEmail: ?string): any[] =>
users.filter(user =>
user.email !== ownEmail &&
(filter === '' ||
user.fullName.toLowerCase().includes(filter.toLowerCase()) ||
user.email.toLowerCase().includes(filter.toLowerCase()))
);

export const alphabeticallySort = (users: any[]): UserType[] =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A better name would be 'sortAlphabetically' 🥇

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type for the parameters would be users: UserType[]

let initials = [];
let contains = [];
let matchesEmail = [];
users.map((user) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use users.filter

else if (user.fullName.toLowerCase().startsWith(filter.toLowerCase())) {
startWith = [...startWith, user];
return user;
} else if (user.fullName.replace(/(\s|[a-z])/g, '').toLowerCase().startsWith(filter.toLowerCase())) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extract each of these to 4 functions that are in the form of
users.filter(user => ....)

then this function will ony be

const usersByStartWith = getUsersByStartWith(users, filter);
const usersByinitials = getUsersByInitials(users, filter);
....
return [...startWith, ...initials, ...contains, ...matchesEmail];

I think a deduplication phase will be needed in the end though.
You can extract it to a getUniqueItems() function

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since there will be duplications between the 4 lists, you can use this:
https://stackoverflow.com/questions/31740155/lodash-remove-duplicates-from-array

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used lodash.uniqby 👍

- starts with fullName.
- matches initial.
- contains in name.
- matches in email.

in a single iteration of users array.

fix:zulip#756
@jainkuniya
Copy link
Member Author

@borisyankov updated 👍

@borisyankov
Copy link
Contributor

Awesome. The reworked version is exactly what I meant 🥇

@borisyankov borisyankov merged commit 5f94a62 into zulip:master Jun 29, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants