Archived
1
0

Add helper functions to make some code clearer

This commit is contained in:
Asher
2020-07-30 12:14:31 -05:00
parent c581bca29d
commit e86c066438
3 changed files with 26 additions and 6 deletions

View File

@ -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]
}