From bb75b36b77a5663bf6ff837c4ae71707bb400446 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Mon, 19 Sep 2022 12:51:46 +0200 Subject: [PATCH] Replace some unnecessary `String.prototype.search` usage Most of the `String.prototype.search` call-sites found throughout the code-base is actually not necessary, since we usually only want a *boolean*, and those can be replaced with `RegExp.prototype.test` instead. --- src/core/evaluator.js | 3 +-- src/core/fonts.js | 9 ++++----- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/core/evaluator.js b/src/core/evaluator.js index dc58bfba8..874060c55 100644 --- a/src/core/evaluator.js +++ b/src/core/evaluator.js @@ -3866,8 +3866,7 @@ class PartialEvaluator { // Simulating descriptor flags attribute const fontNameWoStyle = baseFontName.split("-")[0]; return ( - fontNameWoStyle in getSerifFonts() || - fontNameWoStyle.search(/serif/gi) !== -1 + fontNameWoStyle in getSerifFonts() || /serif/gi.test(fontNameWoStyle) ); } diff --git a/src/core/fonts.js b/src/core/fonts.js index 372fc81ea..a2ae7f591 100644 --- a/src/core/fonts.js +++ b/src/core/fonts.js @@ -1119,17 +1119,16 @@ class Font { } } - this.bold = fontName.search(/bold/gi) !== -1; - this.italic = - fontName.search(/oblique/gi) !== -1 || fontName.search(/italic/gi) !== -1; + this.bold = /bold/gi.test(fontName); + this.italic = /oblique|italic/gi.test(fontName); // Use 'name' instead of 'fontName' here because the original // name ArialBlack for example will be replaced by Helvetica. - this.black = name.search(/Black/g) !== -1; + this.black = /Black/g.test(name); // Use 'name' instead of 'fontName' here because the original // name ArialNarrow for example will be replaced by Helvetica. - const isNarrow = name.search(/Narrow/g) !== -1; + const isNarrow = /Narrow/g.test(name); // if at least one width is present, remeasure all chars when exists this.remeasure =