- 
                Notifications
    You must be signed in to change notification settings 
- Fork 178
refactor: use utility for cross-platform path regex construction #701
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
6a6eadb
              d1e63c5
              cef92ca
              3380a07
              b71a91d
              f125552
              3a5c6fd
              b6bc25d
              76554cc
              f46f4bd
              8d603ae
              File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@opennextjs/aws": patch | ||
| --- | ||
|  | ||
| refactor: use utility for cross-platform path regex construction | 
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| export function getCrossPlatformPathRegex( | ||
| regex: string, | ||
| opts: { escape: boolean } = { escape: true }, | ||
|          | ||
| ) { | ||
| const newExpr = ( | ||
| opts.escape ? regex.replace(/([[\]().*+?^$|])/g, "\\$1") : regex | ||
|         
                  james-elicx marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| ).replaceAll("/", "(?:\\/|\\\\)"); | ||
|         
                  james-elicx marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
|  | ||
| return new RegExp(newExpr, "g"); | ||
|         
                  james-elicx marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| } | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import { getCrossPlatformPathRegex } from "@opennextjs/aws/utils/regex.js"; | ||
|  | ||
| const specialChars = "^([123]+|[123]*)?$"; | ||
|  | ||
| describe("getCrossPlatformPathRegex", () => { | ||
|         
                  james-elicx marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| it("should return a regex without escaping characters", () => { | ||
| const regexp = getCrossPlatformPathRegex(specialChars, { escape: false }); | ||
| expect(regexp.source).toEqual(specialChars); | ||
| }); | ||
|  | ||
| it("should always create cross-platform separators", () => { | ||
| [true, false].forEach((v) => { | ||
| const regexp = getCrossPlatformPathRegex("test/path", { escape: v }); | ||
| expect(regexp.source).toEqual("test(?:\\/|\\\\)path"); | ||
| }); | ||
| }); | ||
|  | ||
| it("should return a regex with escaped characters", () => { | ||
| const regexp = getCrossPlatformPathRegex(specialChars, { escape: true }); | ||
| expect(regexp.source).toEqual("\\^\\(\\[123\\]\\+\\|\\[123\\]\\*\\)\\?\\$"); | ||
| }); | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.