Skip to content

Commit

Permalink
perf: cache regular expresions for string formatters
Browse files Browse the repository at this point in the history
  • Loading branch information
juanjoDiaz committed Jan 17, 2024
1 parent ab22f53 commit 0356b9d
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 8 deletions.
3 changes: 2 additions & 1 deletion dist/cdn/formatters/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ function stringFormatter(opts = {}) {
if (!quote || quote === escapedQuote) {
return (value) => value;
}
const quoteRegExp = new RegExp(quote, "g");
return (value) => {
if (value.includes(quote)) {
value = value.replace(new RegExp(quote, "g"), escapedQuote);
value = value.replace(quoteRegExp, escapedQuote);
}
return `${quote}${value}${quote}`;
};
Expand Down
3 changes: 2 additions & 1 deletion dist/cdn/formatters/stringExcel.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// packages/formatters/src/stringExcel.ts
var quote = '"';
var escapedQuote = '""""';
var quoteRegExp = new RegExp(quote, "g");
function stringExcel(value) {
return `"=""${value.replace(new RegExp(quote, "g"), escapedQuote)}"""`;
return `"=""${value.replace(quoteRegExp, escapedQuote)}"""`;
}
export {
stringExcel as default
Expand Down
4 changes: 3 additions & 1 deletion dist/cdn/whatwg/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// packages/whatwg/src/index.ts
import { default as default2 } from "./AsyncParser.js";
import { default as default3 } from "./TransformStream.js";
import {
default as default3
} from "./TransformStream.js";
export {
default2 as AsyncParser,
default3 as TransformStream
Expand Down
3 changes: 2 additions & 1 deletion packages/formatters/src/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ export default function stringFormatter(
return (value) => value;
}

const quoteRegExp = new RegExp(quote, 'g');
return (value) => {
if (value.includes(quote)) {
value = value.replace(new RegExp(quote, 'g'), escapedQuote);
value = value.replace(quoteRegExp, escapedQuote);
}

return `${quote}${value}${quote}`;
Expand Down
3 changes: 2 additions & 1 deletion packages/formatters/src/stringExcel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const quote = '"';
const escapedQuote = '""""';

const quoteRegExp = new RegExp(quote, 'g');
export default function stringExcel(value: string) {
return `"=""${value.replace(new RegExp(quote, 'g'), escapedQuote)}"""`;
return `"=""${value.replace(quoteRegExp, escapedQuote)}"""`;
}
7 changes: 4 additions & 3 deletions packages/test-helpers/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const lfRegExp = new RegExp('(?<!\r)\n', 'g');
export function forceCrlfEol(content: string): string {
// eslint-disable-next-line no-control-regex
return content.replace(new RegExp('(?<!\r)\n', 'g'), '\r\n');
return content.replace(lfRegExp, '\r\n');
}

const crlfRegExp = new RegExp('\r\n', 'g');
export function forceLfEol(content: string): string {
// eslint-disable-next-line no-control-regex
return content.replace(new RegExp('\r\n', 'g'), '\n');
return content.replace(crlfRegExp, '\n');
}

0 comments on commit 0356b9d

Please sign in to comment.