Move the normalize
helper function out of PDFFindController
In the event that multiple instances of `PDFFindController` ever exists simultaneously, they will all be able to share just one `normalize` function in this way. Furthermore, the regular expression is now created lazily rather than at class construction time.
This commit is contained in:
parent
dc98bf76eb
commit
12d8b52c49
@ -40,6 +40,18 @@ const CHARACTERS_TO_NORMALIZE = {
|
||||
'\u00BE': '3/4', // Vulgar fraction three quarters
|
||||
};
|
||||
|
||||
let normalizationRegex = null;
|
||||
function normalize(text) {
|
||||
if (!normalizationRegex) {
|
||||
// Compile the regular expression for text normalization once.
|
||||
const replace = Object.keys(CHARACTERS_TO_NORMALIZE).join('');
|
||||
normalizationRegex = new RegExp(`[${replace}]`, 'g');
|
||||
}
|
||||
return text.replace(normalizationRegex, function(ch) {
|
||||
return CHARACTERS_TO_NORMALIZE[ch];
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @typedef {Object} PDFFindControllerOptions
|
||||
* @property {IPDFLinkService} linkService - The navigation/linking service.
|
||||
@ -59,10 +71,6 @@ class PDFFindController {
|
||||
|
||||
this._reset();
|
||||
eventBus.on('findbarclose', this._onFindBarClose.bind(this));
|
||||
|
||||
// Compile the regular expression for text normalization once.
|
||||
const replace = Object.keys(CHARACTERS_TO_NORMALIZE).join('');
|
||||
this._normalizationRegex = new RegExp(`[${replace}]`, 'g');
|
||||
}
|
||||
|
||||
get highlightMatches() {
|
||||
@ -164,12 +172,6 @@ class PDFFindController {
|
||||
this._firstPageCapability = createPromiseCapability();
|
||||
}
|
||||
|
||||
_normalize(text) {
|
||||
return text.replace(this._normalizationRegex, function(ch) {
|
||||
return CHARACTERS_TO_NORMALIZE[ch];
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper for multi-term search that fills the `matchesWithLength` array
|
||||
* and handles cases where one search term includes another search term (for
|
||||
@ -304,8 +306,8 @@ class PDFFindController {
|
||||
}
|
||||
|
||||
_calculateMatch(pageIndex) {
|
||||
let pageContent = this._normalize(this._pageContents[pageIndex]);
|
||||
let query = this._normalize(this._state.query);
|
||||
let pageContent = normalize(this._pageContents[pageIndex]);
|
||||
let query = normalize(this._state.query);
|
||||
const { caseSensitive, entireWord, phraseSearch, } = this._state;
|
||||
|
||||
if (query.length === 0) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user