@helpers4/url 2.0.0-alpha.1 → 2.0.0-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/lib/index.d.ts +8 -8
- package/lib/index.js +79 -9
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -12,13 +12,13 @@ A set of helpers for working with URLs.
|
|
|
12
12
|
[https://helpers4.js.org/url](https://helpers4.js.org/url)
|
|
13
13
|
|
|
14
14
|
<!-- AUTOMATIC-METHODS -->
|
|
15
|
-
- withoutLeadingSlash
|
|
16
15
|
- withTrailingSlash
|
|
17
|
-
-
|
|
16
|
+
- withoutLeadingSlash
|
|
18
17
|
- cleanPath
|
|
19
|
-
- withLeadingSlash
|
|
20
18
|
- relativeURLToAbsolute
|
|
19
|
+
- withLeadingSlash
|
|
21
20
|
- extractPureURI
|
|
21
|
+
- onlyPath
|
|
22
22
|
- withoutTrailingSlash
|
|
23
23
|
<!-- /AUTOMATIC-METHODS -->
|
|
24
24
|
|
package/lib/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
export *
|
|
2
|
-
export * from '
|
|
3
|
-
export
|
|
4
|
-
export
|
|
5
|
-
export *
|
|
6
|
-
export
|
|
7
|
-
export * from '
|
|
8
|
-
export * from '
|
|
1
|
+
export declare function withTrailingSlash(url: string): string /** * Adds a trailing slash `/` to the given URL if it is not already present. * * This function is useful for ensuring that URLs are properly formatted * with a trailing slash, which is often required in web development for * consistency and to avoid issues with relative paths. * * @param url - The URL string to be processed. Can be `string`, `undefined`, or `null`. * @returns The URL string with a trailing slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`. * * @see https://radashi.js.org/reference/url/withTrailingSlash * * @example * ```ts * withTrailingSlash('') // => '/' * withTrailingSlash('no/slash') // => 'no/slash/' * withTrailingSlash('already/has/slash/') // => 'already/has/slash/' * withTrailingSlash(undefined) // => undefined * withTrailingSlash(null) // => null * ``` */ export function withTrailingSlash(url: undefined): undefined /** * Adds a trailing slash `/` to the given URL if it is not already present. * * This function is useful for ensuring that URLs are properly formatted * with a trailing slash, which is often required in web development for * consistency and to avoid issues with relative paths. * * @param url - The URL string to be processed. Can be `string`, `undefined`, or `null`. * @returns The URL string with a trailing slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`. * * @see https://radashi.js.org/reference/url/withTrailingSlash * * @example * ```ts * withTrailingSlash('') // => '/' * withTrailingSlash('no/slash') // => 'no/slash/' * withTrailingSlash('already/has/slash/') // => 'already/has/slash/' * withTrailingSlash(undefined) // => undefined * withTrailingSlash(null) // => null * ``` */ export function withTrailingSlash(url: null): null /** * Adds a trailing slash `/` to the given URL if it is not already present. * * This function is useful for ensuring that URLs are properly formatted * with a trailing slash, which is often required in web development for * consistency and to avoid issues with relative paths. * * @param url - The URL string to be processed. Can be `string`, `undefined`, or `null`. * @returns The URL string with a trailing slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`. * * @see https://radashi.js.org/reference/url/withTrailingSlash * * @example * ```ts * withTrailingSlash('') // => '/' * withTrailingSlash('no/slash') // => 'no/slash/' * withTrailingSlash('already/has/slash/') // => 'already/has/slash/' * withTrailingSlash(undefined) // => undefined * withTrailingSlash(null) // => null * ``` */ export function withTrailingSlash( url: string | undefined | null, ): string | undefined | null;
|
|
2
|
+
export declare function withoutLeadingSlash(url: string): string /** * Removes the leading slash `/` from the given URL if it is present. * * This function is useful for ensuring that URLs are properly formatted * without a leading slash, which is often required in web development for * consistency and to avoid issues with relative paths. * * @param url - The URL string to be processed. Can be `string`, `undefined`, or `null`. * @returns The URL string without a leading slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`. * * @see https://radashi.js.org/reference/url/withoutLeadingSlash * * @example * ```ts * withoutLeadingSlash('') // => '' * withoutLeadingSlash('/') // => '' * withoutLeadingSlash('/no/slash') // => 'no/slash' * withoutLeadingSlash('already/has/slash') // => 'already/has/slash' * withoutLeadingSlash(undefined) // => undefined * withoutLeadingSlash(null) // => null * ``` */ export function withoutLeadingSlash(url: undefined): undefined /** * Removes the leading slash `/` from the given URL if it is present. * * This function is useful for ensuring that URLs are properly formatted * without a leading slash, which is often required in web development for * consistency and to avoid issues with relative paths. * * @param url - The URL string to be processed. Can be `string`, `undefined`, or `null`. * @returns The URL string without a leading slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`. * * @see https://radashi.js.org/reference/url/withoutLeadingSlash * * @example * ```ts * withoutLeadingSlash('') // => '' * withoutLeadingSlash('/') // => '' * withoutLeadingSlash('/no/slash') // => 'no/slash' * withoutLeadingSlash('already/has/slash') // => 'already/has/slash' * withoutLeadingSlash(undefined) // => undefined * withoutLeadingSlash(null) // => null * ``` */ export function withoutLeadingSlash(url: null): null /** * Removes the leading slash `/` from the given URL if it is present. * * This function is useful for ensuring that URLs are properly formatted * without a leading slash, which is often required in web development for * consistency and to avoid issues with relative paths. * * @param url - The URL string to be processed. Can be `string`, `undefined`, or `null`. * @returns The URL string without a leading slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`. * * @see https://radashi.js.org/reference/url/withoutLeadingSlash * * @example * ```ts * withoutLeadingSlash('') // => '' * withoutLeadingSlash('/') // => '' * withoutLeadingSlash('/no/slash') // => 'no/slash' * withoutLeadingSlash('already/has/slash') // => 'already/has/slash' * withoutLeadingSlash(undefined) // => undefined * withoutLeadingSlash(null) // => null * ``` */ export function withoutLeadingSlash( url: string | undefined | null, ): string | undefined | null;
|
|
3
|
+
export declare function cleanPath(url: string | undefined | null,): string | undefined | null;
|
|
4
|
+
export declare function relativeURLToAbsolute(relativeUrl: string): string;
|
|
5
|
+
export declare function withLeadingSlash(url: string): string /** * Adds a leading slash `/` to the given URL if it is not already present. * * This function is useful for ensuring that URLs are properly formatted * with a leading slash, which is often required in web development for * consistency and to avoid issues with relative paths. * * @param url - The URL string to be processed. Can be `string`, `undefined`, or `null`. * @returns The URL string with a leading slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`. * * @see https://radashi.js.org/reference/url/withLeadingSlash * * @example * ```ts * withLeadingSlash('') // => '/' * withLeadingSlash('no/slash') // => '/no/slash' * withLeadingSlash('/already/has/slash') // => '/already/has/slash' * withLeadingSlash(undefined) // => undefined * withLeadingSlash(null) // => null * ``` */ export function withLeadingSlash(url: undefined): undefined /** * Adds a leading slash `/` to the given URL if it is not already present. * * This function is useful for ensuring that URLs are properly formatted * with a leading slash, which is often required in web development for * consistency and to avoid issues with relative paths. * * @param url - The URL string to be processed. Can be `string`, `undefined`, or `null`. * @returns The URL string with a leading slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`. * * @see https://radashi.js.org/reference/url/withLeadingSlash * * @example * ```ts * withLeadingSlash('') // => '/' * withLeadingSlash('no/slash') // => '/no/slash' * withLeadingSlash('/already/has/slash') // => '/already/has/slash' * withLeadingSlash(undefined) // => undefined * withLeadingSlash(null) // => null * ``` */ export function withLeadingSlash(url: null): null /** * Adds a leading slash `/` to the given URL if it is not already present. * * This function is useful for ensuring that URLs are properly formatted * with a leading slash, which is often required in web development for * consistency and to avoid issues with relative paths. * * @param url - The URL string to be processed. Can be `string`, `undefined`, or `null`. * @returns The URL string with a leading slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`. * * @see https://radashi.js.org/reference/url/withLeadingSlash * * @example * ```ts * withLeadingSlash('') // => '/' * withLeadingSlash('no/slash') // => '/no/slash' * withLeadingSlash('/already/has/slash') // => '/already/has/slash' * withLeadingSlash(undefined) // => undefined * withLeadingSlash(null) // => null * ``` */ export function withLeadingSlash( url: string | undefined | null, ): string | undefined | null;
|
|
6
|
+
export declare function extractPureURI(url: string): string; export function extractPureURI(url: undefined): undefined; export function extractPureURI(url: null): null; export function extractPureURI(url: string | undefined | null): string | undefined | null;
|
|
7
|
+
export declare function onlyPath(url: string): string /** * Extract only the path from an URI with optional query and fragments. * * For example, all these parameters will return `/path`: * - `/path` * - `/path?query=thing` * - `/path#fragment` * - `/path?query=thing#fragment` * * @param url - The URL string to be processed. Can be `string`, `undefined`, or `null`. * @returns The URL string without query and fragment, or `undefined` if the input is `undefined`, or `null` if the input is `null`. * * @example * ```ts * onlyPath('/path') // => '/path' * onlyPath('/path?query=thing') // => '/path' * onlyPath('/path#fragment') // => '/path' * onlyPath('/path?query=thing#fragment') // => '/path' * onlyPath(undefined) // => undefined * onlyPath(null) // => null * ``` */ export function onlyPath(url: null): null /** * Extract only the path from an URI with optional query and fragments. * * For example, all these parameters will return `/path`: * - `/path` * - `/path?query=thing` * - `/path#fragment` * - `/path?query=thing#fragment` * * @param url - The URL string to be processed. Can be `string`, `undefined`, or `null`. * @returns The URL string without query and fragment, or `undefined` if the input is `undefined`, or `null` if the input is `null`. * * @example * ```ts * onlyPath('/path') // => '/path' * onlyPath('/path?query=thing') // => '/path' * onlyPath('/path#fragment') // => '/path' * onlyPath('/path?query=thing#fragment') // => '/path' * onlyPath(undefined) // => undefined * onlyPath(null) // => null * ``` */ export function onlyPath(url: undefined): undefined /** * Extract only the path from an URI with optional query and fragments. * * For example, all these parameters will return `/path`: * - `/path` * - `/path?query=thing` * - `/path#fragment` * - `/path?query=thing#fragment` * * @param url - The URL string to be processed. Can be `string`, `undefined`, or `null`. * @returns The URL string without query and fragment, or `undefined` if the input is `undefined`, or `null` if the input is `null`. * * @example * ```ts * onlyPath('/path') // => '/path' * onlyPath('/path?query=thing') // => '/path' * onlyPath('/path#fragment') // => '/path' * onlyPath('/path?query=thing#fragment') // => '/path' * onlyPath(undefined) // => undefined * onlyPath(null) // => null * ``` */ export function onlyPath( url: string | undefined | null, ): string | undefined | null;
|
|
8
|
+
export declare function withoutTrailingSlash(url: string): string /** * Removes the trailing slash `/` from the given URL if it is present. * * This function is useful for ensuring that URLs are properly formatted * without a trailing slash, which is often required in web development for * consistency and to avoid issues with relative paths. * * @param url - The URL string to be processed. Can be `string`, `undefined`, or `null`. * @returns The URL string without a trailing slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`. * * @see https://radashi.js.org/reference/url/withoutTrailingSlash * * @example * ```ts * withoutTrailingSlash('') // => '' * withoutTrailingSlash('/') // => '' * withoutTrailingSlash('no/slash/') // => 'no/slash' * withoutTrailingSlash('already/has/slash') // => 'already/has/slash' * withoutTrailingSlash(undefined) // => undefined * withoutTrailingSlash(null) // => null * ``` */ export function withoutTrailingSlash(url: undefined): undefined /** * Removes the trailing slash `/` from the given URL if it is present. * * This function is useful for ensuring that URLs are properly formatted * without a trailing slash, which is often required in web development for * consistency and to avoid issues with relative paths. * * @param url - The URL string to be processed. Can be `string`, `undefined`, or `null`. * @returns The URL string without a trailing slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`. * * @see https://radashi.js.org/reference/url/withoutTrailingSlash * * @example * ```ts * withoutTrailingSlash('') // => '' * withoutTrailingSlash('/') // => '' * withoutTrailingSlash('no/slash/') // => 'no/slash' * withoutTrailingSlash('already/has/slash') // => 'already/has/slash' * withoutTrailingSlash(undefined) // => undefined * withoutTrailingSlash(null) // => null * ``` */ export function withoutTrailingSlash(url: null): null /** * Removes the trailing slash `/` from the given URL if it is present. * * This function is useful for ensuring that URLs are properly formatted * without a trailing slash, which is often required in web development for * consistency and to avoid issues with relative paths. * * @param url - The URL string to be processed. Can be `string`, `undefined`, or `null`. * @returns The URL string without a trailing slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`. * * @see https://radashi.js.org/reference/url/withoutTrailingSlash * * @example * ```ts * withoutTrailingSlash('') // => '' * withoutTrailingSlash('/') // => '' * withoutTrailingSlash('no/slash/') // => 'no/slash' * withoutTrailingSlash('already/has/slash') // => 'already/has/slash' * withoutTrailingSlash(undefined) // => undefined * withoutTrailingSlash(null) // => null * ``` */ export function withoutTrailingSlash( url: string | undefined | null, ): string | undefined | null;
|
package/lib/index.js
CHANGED
|
@@ -1,9 +1,79 @@
|
|
|
1
|
-
// helpers/url/
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
// helpers/url/withTrailingSlash.ts
|
|
2
|
+
function withTrailingSlash(url) {
|
|
3
|
+
if (url === undefined || url === null) {
|
|
4
|
+
return url;
|
|
5
|
+
}
|
|
6
|
+
return url[url.length - 1] === "/" ? url : url + "/";
|
|
7
|
+
}
|
|
8
|
+
// helpers/url/withoutLeadingSlash.ts
|
|
9
|
+
function withoutLeadingSlash(url) {
|
|
10
|
+
if (url === undefined || url === null) {
|
|
11
|
+
return url;
|
|
12
|
+
}
|
|
13
|
+
return url[0] === "/" ? url.slice(1) : url;
|
|
14
|
+
}
|
|
15
|
+
// helpers/url/cleanPath.ts
|
|
16
|
+
function cleanPath(url) {
|
|
17
|
+
if (url === undefined || url === null) {
|
|
18
|
+
return url;
|
|
19
|
+
}
|
|
20
|
+
return url.replace(/([^:]\/)\/+/g, "$1");
|
|
21
|
+
}
|
|
22
|
+
// helpers/url/withoutTrailingSlash.ts
|
|
23
|
+
function withoutTrailingSlash(url) {
|
|
24
|
+
if (url === undefined || url === null) {
|
|
25
|
+
return url;
|
|
26
|
+
}
|
|
27
|
+
return url[url.length - 1] === "/" ? url.slice(0, -1) : url;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// helpers/url/withLeadingSlash.ts
|
|
31
|
+
function withLeadingSlash(url) {
|
|
32
|
+
if (url === undefined || url === null) {
|
|
33
|
+
return url;
|
|
34
|
+
}
|
|
35
|
+
return url[0] === "/" ? url : "/" + url;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// helpers/url/relativeURLToAbsolute.ts
|
|
39
|
+
function relativeURLToAbsolute(relativeUrl) {
|
|
40
|
+
return withoutTrailingSlash(document.baseURI ?? window.location.origin) + cleanPath(withLeadingSlash(relativeUrl));
|
|
41
|
+
}
|
|
42
|
+
// helpers/url/extractPureURI.ts
|
|
43
|
+
function extractPureURI(url) {
|
|
44
|
+
if (url === undefined || url === null) {
|
|
45
|
+
return url;
|
|
46
|
+
}
|
|
47
|
+
const queryIndex = url.indexOf("?");
|
|
48
|
+
const fragmentIndex = url.indexOf("#");
|
|
49
|
+
let cutIndex = -1;
|
|
50
|
+
if (queryIndex !== -1 && fragmentIndex !== -1) {
|
|
51
|
+
cutIndex = Math.min(queryIndex, fragmentIndex);
|
|
52
|
+
} else if (queryIndex !== -1) {
|
|
53
|
+
cutIndex = queryIndex;
|
|
54
|
+
} else if (fragmentIndex !== -1) {
|
|
55
|
+
cutIndex = fragmentIndex;
|
|
56
|
+
}
|
|
57
|
+
if (cutIndex === -1) {
|
|
58
|
+
return url;
|
|
59
|
+
}
|
|
60
|
+
return url.substring(0, cutIndex);
|
|
61
|
+
}
|
|
62
|
+
// helpers/url/onlyPath.ts
|
|
63
|
+
function onlyPath(url) {
|
|
64
|
+
if (url === undefined || url === null) {
|
|
65
|
+
return url;
|
|
66
|
+
}
|
|
67
|
+
const [path] = url.split(/[?#]/);
|
|
68
|
+
return path;
|
|
69
|
+
}
|
|
70
|
+
export {
|
|
71
|
+
withoutTrailingSlash,
|
|
72
|
+
withoutLeadingSlash,
|
|
73
|
+
withTrailingSlash,
|
|
74
|
+
withLeadingSlash,
|
|
75
|
+
relativeURLToAbsolute,
|
|
76
|
+
onlyPath,
|
|
77
|
+
extractPureURI,
|
|
78
|
+
cleanPath
|
|
79
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@helpers4/url",
|
|
3
|
-
"version": "2.0.0-alpha.
|
|
3
|
+
"version": "2.0.0-alpha.2",
|
|
4
4
|
"description": "A set of helpers in TS/JS, compatible with tree-shaking, for url.",
|
|
5
5
|
"author": "baxyz <baxy@etik.com>",
|
|
6
6
|
"license": "AGPL-3.0",
|
|
@@ -20,13 +20,13 @@
|
|
|
20
20
|
"keywords": [
|
|
21
21
|
"helpers",
|
|
22
22
|
"url",
|
|
23
|
-
"withoutLeadingSlash",
|
|
24
23
|
"withTrailingSlash",
|
|
25
|
-
"
|
|
24
|
+
"withoutLeadingSlash",
|
|
26
25
|
"cleanPath",
|
|
27
|
-
"withLeadingSlash",
|
|
28
26
|
"relativeURLToAbsolute",
|
|
27
|
+
"withLeadingSlash",
|
|
29
28
|
"extractPureURI",
|
|
29
|
+
"onlyPath",
|
|
30
30
|
"withoutTrailingSlash"
|
|
31
31
|
],
|
|
32
32
|
"files": [
|