-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support filtering annotations by page number or range
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.
- Loading branch information
1 parent
6c3eb16
commit 1123415
Showing
6 changed files
with
189 additions
and
2 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
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* Return true if the page number `label` is within `range`. | ||
* | ||
* @param label - A page number such as "10", "iv" | ||
* @param range - A page range expressed as a single page number, or a hyphen | ||
* separated range (eg. "10-12"). Page ranges are inclusive, so the page | ||
* range "10-12" matches "10", "11" and "12". This means there is no way to | ||
* specify an empty range. | ||
*/ | ||
export function pageLabelInRange(label: string, range: string): boolean { | ||
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; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { pageLabelInRange } from '../page-range'; | ||
|
||
describe('pageLabelInRange', () => { | ||
[ | ||
// Single item range | ||
{ | ||
label: '10', | ||
range: '10', | ||
match: true, | ||
}, | ||
{ | ||
label: '9', | ||
range: '10', | ||
match: false, | ||
}, | ||
|
||
// Number in middle of range | ||
{ | ||
label: '5', | ||
range: '4-8', | ||
match: true, | ||
}, | ||
|
||
// Number at start of range | ||
{ | ||
label: '4', | ||
range: '4-8', | ||
match: true, | ||
}, | ||
|
||
// Number at end of range | ||
{ | ||
label: '8', | ||
range: '4-8', | ||
match: true, | ||
}, | ||
|
||
// Number before range | ||
{ | ||
label: '3', | ||
range: '4-8', | ||
match: false, | ||
}, | ||
|
||
// Number after range | ||
{ | ||
label: '9', | ||
range: '4-8', | ||
match: false, | ||
}, | ||
|
||
// Range unbounded at start | ||
{ | ||
label: '5', | ||
range: '-8', | ||
match: true, | ||
}, | ||
|
||
// Range unbounded at end | ||
{ | ||
label: '5', | ||
range: '4-', | ||
match: true, | ||
}, | ||
|
||
// Open range | ||
{ | ||
label: '5', | ||
range: '-', | ||
match: true, | ||
}, | ||
|
||
// Non-numeric single item | ||
{ | ||
label: 'foo', | ||
range: 'foo', | ||
match: true, | ||
}, | ||
{ | ||
label: 'foo', | ||
range: 'bar', | ||
match: false, | ||
}, | ||
|
||
// Non-numeric range | ||
{ | ||
label: 'foo', | ||
range: 'foo-bar', | ||
match: false, | ||
}, | ||
].forEach(({ label, range, match }) => { | ||
it('returns true if the label is in the page range', () => { | ||
assert.equal(pageLabelInRange(label, range), match); | ||
}); | ||
}); | ||
}); |
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