Skip to content
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

docs: add jsdocs for parse utils #134

Merged
merged 4 commits into from
Mar 14, 2023
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ export interface ParsedHost {
port: string;
}

/**
* It takes a URL string and returns an object with the URL's protocol, auth, host, pathname, search,
* and hash
* @param [input] - The URL to parse.
* @param [defaultProto] - The default protocol to use if the input doesn't have one.
* @returns A parsed URL object.
*/
export function parseURL(input = "", defaultProto?: string): ParsedURL {
if (!hasProtocol(input, { acceptRelative: true })) {
return defaultProto ? parseURL(defaultProto + input) : parsePath(input);
Expand All @@ -44,6 +51,11 @@ export function parseURL(input = "", defaultProto?: string): ParsedURL {
};
}

/**
* It splits the input string into three parts, and returns an object with those three parts
* @param [input] - The URL to parse.
* @returns An object with three properties: `pathname`, `search`, and `hash`.
*/
export function parsePath(input = ""): ParsedURL {
const [pathname = "", search = "", hash = ""] = (
input.match(/([^#?]*)(\?[^#]*)?(#.*)?/) || []
Expand All @@ -56,6 +68,12 @@ export function parsePath(input = ""): ParsedURL {
};
}

/**
* It takes a string of the form `username:password` and returns an object with the username and
* password decoded
* @param [input] - The URL to parse.
* @returns An object with two properties: username and password.
*/
export function parseAuth(input = ""): ParsedAuth {
const [username, password] = input.split(":");
return {
Expand All @@ -64,6 +82,12 @@ export function parseAuth(input = ""): ParsedAuth {
};
}

/**
* It takes a string, and returns an object with two properties: `hostname` and `port`
* @param [input] - The URL to parse.
* @returns A function that takes a string and returns an object with two properties: hostname and
pi0 marked this conversation as resolved.
Show resolved Hide resolved
* port.
pi0 marked this conversation as resolved.
Show resolved Hide resolved
*/
export function parseHost(input = ""): ParsedHost {
const [hostname, port] = (input.match(/([^/:]*):?(\d+)?/) || []).splice(1);
return {
Expand All @@ -72,7 +96,12 @@ export function parseHost(input = ""): ParsedHost {
};
}

export function stringifyParsedURL(parsed: ParsedURL) {
/**
* It takes a `ParsedURL` object and returns the stringified URL
* @param [parsed] - The parsed URL
* @returns A stringified URL.
*/
export function stringifyParsedURL(parsed: ParsedURL): string {
const fullpath =
parsed.pathname +
(parsed.search
Expand Down