- 
                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 10 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,22 @@ | ||
| /** | ||
| * Constructs a regular expression for a path that supports separators for multiple platforms | ||
| * - Uses posix separators (`/`) as the input that should be made cross-platform. | ||
| * - Special characters are escaped by default but can be controlled through opts.escape. | ||
| * - Posix separators are always escaped. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * getCrossPlatformPathRegex("./middleware.mjs") | ||
| * getCrossPlatformPathRegex(String.raw`\./middleware\.(mjs|cjs)`, { escape: false }) | ||
| * ``` | ||
| */ | ||
| export function getCrossPlatformPathRegex( | ||
|         
                  james-elicx marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| regex: string, | ||
| opts: { escape: boolean } = { escape: true }, | ||
|          | ||
| ) { | ||
| const newExpr = ( | ||
| opts.escape ? regex.replace(/([[\]().*+?^$|{}\\])/g, "\\$1") : regex | ||
| ).replaceAll("/", String.raw`(?:\/|\\)`); | ||
|  | ||
| 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,41 @@ | ||
| import { getCrossPlatformPathRegex } from "@opennextjs/aws/utils/regex.js"; | ||
|  | ||
| const specialChars = "^([123]+|[123]{1,3})*\\?$"; | ||
|  | ||
| 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(String.raw`test(?:\/|\\)path`); | ||
| }); | ||
| }); | ||
|  | ||
| it("should return a regex with escaped characters", () => { | ||
| const regexp = getCrossPlatformPathRegex(specialChars, { escape: true }); | ||
| expect(regexp.source).toEqual( | ||
| String.raw`\^\(\[123\]\+\|\[123\]\{1,3\}\)\*\\\?\$`, | ||
| ); | ||
| }); | ||
|  | ||
| it("should return cross-platform paths with escaped special characters", () => { | ||
| [ | ||
| ["core/resolve.js", String.raw`core(?:\/|\\)resolve\.js`], | ||
| ["./middleware.mjs", String.raw`\.(?:\/|\\)middleware\.mjs`], | ||
| ].forEach(([input, output]) => | ||
| expect(getCrossPlatformPathRegex(input).source).toEqual(output), | ||
| ); | ||
| }); | ||
|  | ||
| it("should return cross-platform paths without escaping special characters", () => { | ||
| const regex = getCrossPlatformPathRegex( | ||
| String.raw`\./middleware\.(mjs|cjs)`, | ||
| { escape: false }, | ||
| ); | ||
| expect(regex.source).toEqual(String.raw`\.(?:\/|\\)middleware\.(mjs|cjs)`); | ||
| }); | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.