-
-
Notifications
You must be signed in to change notification settings - Fork 82
Added types for url-utils #685
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
dd62bac
7b4e571
5795a7a
fcc7436
6361779
18d88a9
615872c
7549bac
9d3d43c
5fbb27f
683e18c
201280b
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 |
|---|---|---|
| @@ -1,8 +1,13 @@ | ||
| // @ts-nocheck | ||
| function escapeRegExp(string) { | ||
| import type {BaseUrlOptionsInput} from './types'; | ||
|
|
||
| function escapeRegExp(string: string): string { | ||
| return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); | ||
| } | ||
|
|
||
| type BuildEarlyExitMatchOptions = BaseUrlOptionsInput & { | ||
| ignoreProtocol?: boolean; | ||
| }; | ||
|
|
||
| /** | ||
| * Build a regex pattern that matches any of the configured base URLs (site URL + CDN URLs). | ||
| * This is used for early exit optimizations - if content doesn't contain any of these URLs, | ||
|
|
@@ -16,14 +21,14 @@ function escapeRegExp(string) { | |
| * @param {boolean} [options.ignoreProtocol=true] - Whether to strip protocol from URLs | ||
| * @returns {string|null} Regex pattern matching any configured base URL, or null if none configured | ||
| */ | ||
| function buildEarlyExitMatch(siteUrl, options = {}) { | ||
| function buildEarlyExitMatch(siteUrl: string, options: BuildEarlyExitMatchOptions = {}): string | null { | ||
| const candidates = [siteUrl, options.imageBaseUrl, options.filesBaseUrl, options.mediaBaseUrl] | ||
| .filter(Boolean) | ||
| .map((value) => { | ||
| .filter((value): value is string => Boolean(value)) | ||
|
||
| .map((value: string) => { | ||
| let normalized = options.ignoreProtocol ? value.replace(/http:|https:/, '') : value; | ||
| return normalized.replace(/\/$/, ''); | ||
| }) | ||
| .filter(Boolean) | ||
| .filter((value): value is string => Boolean(value)) | ||
|
||
| .map(escapeRegExp); | ||
|
|
||
| if (!candidates.length) { | ||
|
|
@@ -37,7 +42,7 @@ function buildEarlyExitMatch(siteUrl, options = {}) { | |
| return `(?:${candidates.join('|')})`; | ||
| } | ||
|
|
||
| module.exports = { | ||
| export default { | ||
| buildEarlyExitMatch, | ||
| escapeRegExp | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need to type cast here? Can we instead make sure that
optionsis of typeAbsoluteToTransformReadyOptionsand then it overlaps?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch! I've updated
AbsoluteToTransformReadyOptionsto extendAbsoluteToRelativeOptions, which makes the types properly overlap. Removed type casts.