Add helper functions to make some code clearer
This commit is contained in:
@ -33,6 +33,13 @@ export const normalize = (url: string, keepTrailing = false): string => {
|
||||
return url.replace(/\/\/+/g, "/").replace(/\/+$/, keepTrailing ? "/" : "")
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove leading and trailing slashes.
|
||||
*/
|
||||
export const trimSlashes = (url: string): string => {
|
||||
return url.replace(/^\/+|\/+$/g, "")
|
||||
}
|
||||
|
||||
/**
|
||||
* Get options embedded in the HTML or query params.
|
||||
*/
|
||||
@ -75,3 +82,17 @@ export const getOptions = <T extends Options>(): T => {
|
||||
|
||||
return options
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrap the value in an array if it's not already an array. If the value is
|
||||
* undefined return an empty array.
|
||||
*/
|
||||
export const arrayify = <T>(value?: T | T[]): T[] => {
|
||||
if (Array.isArray(value)) {
|
||||
return value
|
||||
}
|
||||
if (typeof value === "undefined") {
|
||||
return []
|
||||
}
|
||||
return [value]
|
||||
}
|
||||
|
Reference in New Issue
Block a user