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

fix duplicate name error warning logic #821

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

IsaacJrTypes
Copy link

The following code fixes the duplicate warning logic. Previously, the duplicate error logic would sort the names of the entries in the names input field, turn them into variable objects, and group them. Each group would be processed by the handle_group function, which would trigger updating the duplicateNameError knock-out observable if there more than 1 object in the group. The issue with this method is if the user inputs a,b,a , the a group would trigger the duplicateNameError function, but once the b group is processed, the duplicateNameError observable updates to null.

I used an object to track if there were any duplicates. Each time the user updates input names, a new object is created, and if there are duplicates, it returns the name error message.
CleanShot 2024-05-14 at 16 28 07

Copy link

@dqthai dqthai left a comment

Choose a reason for hiding this comment

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

Looks good to me

@christianp
Copy link
Member

Thanks for this.
I'd rather keep the duplicateNameError observable on the Variable object.
You could avoid resetting the duplicateNameError by running through in two passes:

  • for each Variable object, set duplicateNameError to null.
  • then run through the grouped names, and set duplicateNameError if the group has more than one element and duplicateNameError is not already non-null.

@IsaacJrTypes
Copy link
Author

@christianp
Thank you for the feedback! I updated the code to your suggestions.

Around line 2005, I subscribed to the Variable object's duplicateNameError observable to console.log the state of the observable.

CleanShot 2024-05-16 at 21 50 18

Any additional feedback is welcomed. I can also remove the comments if you think they aren't necessary.

@christianp
Copy link
Member

@IsaacJrTypes Thanks. This isn't quite correct: it only sets the error message on the first Variable object for each repeated name.

I'll add some aesthetic comments to the code as well.

group.forEach(function(n) {
n.v.duplicateNameError(group.length > 1 ? n.name : null);
group.forEach(function (n) {
n.v.duplicateNameError(null);
Copy link
Member

Choose a reason for hiding this comment

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

While it won't noticeably affect performance, you can avoid having two nested forEach calls by setting n.v.duplicateNameError(null) when you loop over each name, instead of here where you're looping over names inside groups.

@@ -771,20 +771,29 @@ $(document).ready(function() {

// Finally, mark duplicate names
names.sort(Numbas.util.sortBy('name'));
var groupArray = [];
Copy link
Member

Choose a reason for hiding this comment

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

I don't think it's a good idea to include the type of a variable in its name. This could just be called groups.

groupArray.forEach(function (group) {
var groupVariable = group[0];
if (group.length > 1 && groupVariable.name != "") {
groupVariable.v.duplicateNameError(groupVariable.name);
Copy link
Member

Choose a reason for hiding this comment

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

You're only setting duplicateNameError on the first Variable in the group. You should set it for each variable in the group.

Again, groupVariable unnecessarily includes the type of the variable in its name. Just group will do.

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.

3 participants