Skip to content

Commit c6d683e

Browse files
committed
Reuse replacer function
1 parent 425266f commit c6d683e

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

src/index.ts

+6-10
Original file line numberDiff line numberDiff line change
@@ -561,24 +561,20 @@ function escape(str: string) {
561561
}
562562

563563
/**
564-
* Escape and repeat a string for regular expressions.
564+
* Escape and repeat loose characters for regular expressions.
565565
*/
566-
function repeat(str: string) {
567-
return `${escape(str)}+`;
566+
function looseReplacer(value: string, loose: string) {
567+
return loose ? `${escape(value)}+` : escape(value);
568568
}
569569

570570
/**
571571
* Encode all non-delimiter characters using the encode function.
572572
*/
573573
function toStringify(loose: string) {
574-
if (loose) {
575-
const re = new RegExp(`[^${escape(loose)}]+|(.)`, "g");
576-
const replacer = (value: string, loose: string) =>
577-
loose ? repeat(value) : escape(value);
578-
return (value: string) => value.replace(re, replacer);
579-
}
574+
if (!loose) return escape;
580575

581-
return escape;
576+
const re = new RegExp(`[^${escape(loose)}]+|(.)`, "g");
577+
return (value: string) => value.replace(re, looseReplacer);
582578
}
583579

584580
/**

0 commit comments

Comments
 (0)