-
Notifications
You must be signed in to change notification settings - Fork 146
refactor!: remove ufo
dependency
#440
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
Draft
johannschopplich
wants to merge
5
commits into
unjs:main
Choose a base branch
from
johannschopplich:refactor/native-url
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cc714ee
feat!: replace `ufo` with native `URL` utils
johannschopplich 997afbb
fix: reduce `URL` usage and fix tests
johannschopplich ad22663
refactor: share `http://localhost` string
johannschopplich d7075be
perf: drop `URL` usage in `withQuery`
johannschopplich 8478387
perf: return early if no query params in input URL
johannschopplich File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* eslint-disable unicorn/prefer-at */ | ||
export type QueryValue = | ||
| string | ||
| number | ||
| boolean | ||
| QueryValue[] | ||
| Record<string, any> | ||
| null | ||
| undefined; | ||
export type QueryObject = Record<string, QueryValue | QueryValue[]>; | ||
|
||
/** | ||
* Removes the leading slash from the given path if it has one. | ||
*/ | ||
export function withoutLeadingSlash(path?: string): string { | ||
if (!path || path === "/") { | ||
return "/"; | ||
} | ||
|
||
return path[0] === "/" ? path.slice(1) : path; | ||
} | ||
|
||
/** | ||
* Removes the trailing slash from the given path if it has one. | ||
*/ | ||
export function withoutTrailingSlash(path?: string): string { | ||
if (!path || path === "/") { | ||
return "/"; | ||
} | ||
|
||
return path[path.length - 1] === "/" ? path.slice(0, -1) : path; | ||
} | ||
|
||
/** | ||
* Joins the given base URL and path, ensuring that there is only one slash between them. | ||
*/ | ||
export function joinURL(base?: string, path?: string): string { | ||
if (!base || base === "/") { | ||
return path || "/"; | ||
} | ||
|
||
if (!path || path === "/") { | ||
return base || "/"; | ||
} | ||
|
||
const baseHasTrailing = base[base.length - 1] === "/"; | ||
const pathHasLeading = path[0] === "/"; | ||
if (baseHasTrailing && pathHasLeading) { | ||
return base + path.slice(1); | ||
} | ||
|
||
if (!baseHasTrailing && !pathHasLeading) { | ||
return `${base}/${path}`; | ||
} | ||
|
||
return base + path; | ||
} | ||
|
||
/** | ||
* Adds the base path to the input path, if it is not already present. | ||
*/ | ||
export function withBase(input = "", base = ""): string { | ||
if (!base || base === "/") { | ||
return input; | ||
} | ||
|
||
const _base = withoutTrailingSlash(base); | ||
if (input.startsWith(_base)) { | ||
return input; | ||
} | ||
|
||
return joinURL(_base, input); | ||
} | ||
|
||
/** | ||
* Returns the URL with the given query parameters. If a query parameter is undefined, it is omitted. | ||
*/ | ||
export function withQuery(input: string, query: QueryObject): string { | ||
const url = new URL(input, "http://localhost"); | ||
const searchParams = new URLSearchParams(url.search); | ||
|
||
for (const [key, value] of Object.entries(query)) { | ||
pi0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (value === undefined) { | ||
searchParams.delete(key); | ||
} else if (typeof value === "number" || typeof value === "boolean") { | ||
searchParams.set(key, String(value)); | ||
} else if (!value) { | ||
searchParams.set(key, ""); | ||
} else if (Array.isArray(value)) { | ||
for (const item of value) { | ||
searchParams.append(key, String(item)); | ||
} | ||
} else if (typeof value === "object") { | ||
searchParams.set(key, JSON.stringify(value)); | ||
} else { | ||
searchParams.set(key, String(value)); | ||
} | ||
} | ||
|
||
url.search = searchParams.toString(); | ||
let urlWithQuery = url.toString(); | ||
|
||
if (urlWithQuery.startsWith("http://localhost")) { | ||
urlWithQuery = urlWithQuery.slice(16); | ||
} | ||
|
||
return urlWithQuery; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.