Skip to content

Commit

Permalink
Dumb down code for negate
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Sep 26, 2024
1 parent e85fe27 commit 678756a
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -589,10 +589,14 @@ function sequenceToRegExp(tokens: Flattened[], delimiter: string, keys: Keys) {
}

function negate(delimiter: string, backtrack: string) {
const values = [delimiter, backtrack].filter(Boolean);
const isSimple = values.every((value) => value.length === 1);
if (isSimple) return `[^${escape(values.join(""))}]`;
return `(?:(?!${values.map(escape).join("|")}).)`;
if (backtrack.length < 2) {
if (delimiter.length < 2) return `[^${escape(delimiter + backtrack)}]`;
return `(?:(?!${escape(delimiter)})[^${escape(backtrack)}])`;
}
if (delimiter.length < 2) {
return `(?:(?!${escape(backtrack)})[^${escape(delimiter)}])`;
}
return `(?:(?!${escape(backtrack)}|${escape(delimiter)})[\\s\\S])`;
}

/**
Expand Down

0 comments on commit 678756a

Please sign in to comment.