Skip to content

Commit

Permalink
don't use replaceAll as it breaks in some browsers
Browse files Browse the repository at this point in the history
  • Loading branch information
bezoerb committed Oct 16, 2023
1 parent ef16ab0 commit 3d2b871
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@
"import/no-unresolved": 0,
"import/no-unassigned-import": 0,
"unicorn/no-reduce": 0,
"unicorn/prefer-module": 0
"unicorn/prefer-module": 0,
"unicorn/prefer-string-replace-all": 0
}
},
"npmName": "text-metrics",
Expand Down
18 changes: 9 additions & 9 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export function addWordAndLetterSpacing(ws, ls) {
}

return (text) => {
const words = text.trim().replaceAll(/\s+/gi, ' ').split(' ').length - 1;
const words = text.trim().replace(/\s+/gi, ' ').split(' ').length - 1;
const chars = text.length;

return words * wordAddon + chars * letterAddon;
Expand Down Expand Up @@ -292,13 +292,13 @@ export function normalizeWhitespace(text, ws) {
}

case 'pre-line': {
return (text || '').replaceAll(/\s+/gm, ' ').trim();
return (text || '').replace(/\s+/gm, ' ').trim();
}

default: {
return (text || '')
.replaceAll(/[\r\n]/gm, ' ')
.replaceAll(/\s+/gm, ' ')
.replace(/[\r\n]/gm, ' ')
.replace(/\s+/gm, ' ')
.trim();
}
}
Expand Down Expand Up @@ -336,10 +336,10 @@ export function getStyledText(text, style) {
export function prepareText(text) {
// Convert to unicode
text = (text || '')
.replaceAll(/<wbr>/gi, '\u200B')
.replaceAll(/<br\s*\/?>/gi, '\u000A')
.replaceAll(/&shy;/gi, '\u00AD')
.replaceAll(/&mdash;/gi, '\u2014');
.replace(/<wbr>/gi, '\u200B')
.replace(/<br\s*\/?>/gi, '\u000A')
.replace(/&shy;/gi, '\u00AD')
.replace(/&mdash;/gi, '\u2014');

if (/&#(\d+)(;?)|&#[xX]([a-fA-F\d]+)(;?)|&([\da-zA-Z]+);/g.test(text) && console) {
console.error(
Expand Down Expand Up @@ -388,7 +388,7 @@ export function normalizeOptions(options) {

// Normalize keys (fontSize => font-size)
for (const key of Object.keys(options || {})) {
const dashedKey = key.replaceAll(/([A-Z])/g, ($1) => '-' + $1.toLowerCase());
const dashedKey = key.replace(/([A-Z])/g, ($1) => '-' + $1.toLowerCase());
options_[dashedKey] = options[key];
}

Expand Down

0 comments on commit 3d2b871

Please sign in to comment.