-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dd0eab6
commit b4fecdb
Showing
8 changed files
with
144 additions
and
75 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,45 @@ | ||
import { Brand } from "ts-brand"; | ||
import assert from "assert"; | ||
|
||
/** | ||
* A string of a http-based registry-url | ||
*/ | ||
export type RegistryUrl = Brand<string, "RegistryUrl">; | ||
|
||
/** | ||
* Checks that a string is a valid registry | ||
* @param s The string | ||
*/ | ||
export function isRegistryUrl(s: string): s is RegistryUrl { | ||
return /http(s?):\/\//.test(s); | ||
} | ||
|
||
/** | ||
* Constructs a registry-url | ||
* @param s The string | ||
* @throws assert.AssertionError if string does not have valid format | ||
*/ | ||
export function registryUrl(s: string): RegistryUrl { | ||
assert(isRegistryUrl(s), `"${s}" is url`); | ||
return s; | ||
} | ||
|
||
/** | ||
* Removes trailing slash from a registry-url | ||
* @param registry The url | ||
*/ | ||
export function removeTrailingSlash(registry: RegistryUrl): RegistryUrl { | ||
if (registry.endsWith("/")) return registry.slice(0, -1) as RegistryUrl; | ||
return registry; | ||
} | ||
|
||
/** | ||
* Attempts to coerce a string into a registry-url, by | ||
* - Prepending http if it is missing | ||
* - Removing trailing slashes | ||
* @param s The string | ||
*/ | ||
export function coerceRegistryUrl(s: string): RegistryUrl { | ||
if (!s.toLowerCase().startsWith("http")) s = "http://" + s; | ||
return removeTrailingSlash(registryUrl(s)); | ||
} |
This file contains 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
Oops, something went wrong.