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

Support filtering annotations by page number or range #5997

Merged
merged 2 commits into from
Dec 5, 2023

Conversation

robertknight
Copy link
Member

Support page:{number} or page:{start}-{end} filters in the sidebar. When the query is a number, it must match the page label exactly. When it is a range, it matches the page number if:

  • The start and end of the range, and page number, are all numeric
  • The page number is between the parsed start and end points, inclusive

In the context of VitalSource assignments with page numbers, the plan is that this filtering will be used as part of a "focus on pages in range X-Y", which will be implemented internally similar to the way that "focus on user X" works when grading.

Part of #5937.

fieldValues: ann => [pageLabel(ann)?.trim() ?? ''],
matches: (pageLabel: string, pageTerm: string) =>
pageLabelInRange(pageLabel, pageTerm),
normalize: (val: string) => val.trim(),
Copy link
Member Author

Choose a reason for hiding this comment

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

This code currently reparses the page numbers for every annotation that is matched. That's OK since the number of annotations is expected to be small in the grand scheme of things (hundreds at most). If this needs to be optimized then we can convert annotation page ranges to some pre-parsed representation.

Copy link

codecov bot commented Dec 4, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (07b6fcc) 99.44% compared to head (d6c3fc3) 99.44%.

Additional details and impacted files
@@                      Coverage Diff                       @@
##           display-page-numbers-on-cards    #5997   +/-   ##
==============================================================
  Coverage                          99.44%   99.44%           
==============================================================
  Files                                259      260    +1     
  Lines                               9918     9941   +23     
  Branches                            2375     2386   +11     
==============================================================
+ Hits                                9863     9886   +23     
  Misses                                55       55           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Support `page:{number}` or `page:{start}-{end}` filters in the sidebar.  When
the query is a number, it must match the page label exactly. When it is a range,
it matches the page number if:

 - The start and end of the range, and page number, are all numeric
 - The page number is between the parsed `start` and `end` points,
   inclusive

Part of #5937.
Copy link
Contributor

@acelaya acelaya left a comment

Choose a reason for hiding this comment

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

LGTM. I just added a suggestion to reduce conditional nesting in one function.

Comment on lines 11 to 35
if (range.includes('-')) {
let [start, end] = range.split('-');
if (!start) {
start = label;
}
if (!end) {
end = label;
}
const [startInt, endInt, labelInt] = [
parseInt(start),
parseInt(end),
parseInt(label),
];
if (
Number.isInteger(startInt) &&
Number.isInteger(endInt) &&
Number.isInteger(labelInt)
) {
return labelInt >= startInt && labelInt <= endInt;
} else {
return false;
}
} else {
return label === range;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps we can simplify the conditions that need to be mentally dragged by adding an early return for the simplest condition, and adding the rest afterwards:

Suggested change
if (range.includes('-')) {
let [start, end] = range.split('-');
if (!start) {
start = label;
}
if (!end) {
end = label;
}
const [startInt, endInt, labelInt] = [
parseInt(start),
parseInt(end),
parseInt(label),
];
if (
Number.isInteger(startInt) &&
Number.isInteger(endInt) &&
Number.isInteger(labelInt)
) {
return labelInt >= startInt && labelInt <= endInt;
} else {
return false;
}
} else {
return label === range;
}
if (!range.includes('-')) {
return label === range;
}
let [start, end] = range.split('-');
if (!start) {
start = label;
}
if (!end) {
end = label;
}
const [startInt, endInt, labelInt] = [
parseInt(start),
parseInt(end),
parseInt(label),
];
if (
Number.isInteger(startInt) &&
Number.isInteger(endInt) &&
Number.isInteger(labelInt)
) {
return labelInt >= startInt && labelInt <= endInt;
}
return false;

Copy link
Member Author

@robertknight robertknight Dec 5, 2023

Choose a reason for hiding this comment

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

I agree. I prefer the early-return version as long as that code path is very simple.

@robertknight robertknight merged commit a8c7cae into display-page-numbers-on-cards Dec 5, 2023
@robertknight robertknight deleted the page-range-filter branch December 5, 2023 10:02
@robertknight robertknight restored the page-range-filter branch December 5, 2023 10:41
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.

2 participants