bananas-commerce-admin
Version:
What's this, an admin for apes?
84 lines (73 loc) • 2.07 kB
text/typescript
export * from "./contribute";
export * from "./country_code_formatter";
export * from "./form_validation";
export * from "./format_date";
export * from "./format_purchase_number";
export * from "./format_string";
export * from "./get_cookie";
export * from "./has_access";
export * from "./has_group";
export * from "./has_permission";
export * from "./is_email";
export * from "./is_positive_integer";
export * from "./is_url";
export * from "./pluralize";
export * from "./select_styles";
export * from "./timeline";
export * from "./to_title_case";
export function ensureTrailingSlash(path: string): string {
if (path != null && !path.endsWith("/")) {
return `${path}/`;
}
return path;
}
export function ensureLeadingHash(hash: string): string {
if (hash != null && !hash.startsWith("#")) {
return `#${hash}`;
}
return hash;
}
export function absolutePath(path: string, basename = "/"): string {
if (!path) {
return path;
}
let pathname = path;
// Make relative path absolute to basename
if (!pathname.startsWith("/")) {
pathname = ensureTrailingSlash(basename) + pathname;
}
// Expand path
if (pathname.indexOf(".") >= 0) {
const stack = [];
for (const part of pathname.split("/")) {
if (part === ".") {
continue;
} else if (part === ".." && stack.length > 0) {
stack.pop();
} else {
stack.push(part);
}
}
pathname = stack.join("/");
}
return ensureTrailingSlash(pathname);
}
export function nthIndexOf(str: string, pattern: string, n: number, start?: number): number {
const index = str.indexOf(pattern, start ?? 0);
if (index >= 0 && n > 1) {
return nthIndexOf(str, pattern, n - 1, index + 1);
}
return index;
}
/**
* Like Python’s `.capitalize()`.
*/
export function capitalize(string: string): string {
return string.slice(0, 1).toUpperCase() + string.slice(1).toLowerCase();
}
/**
* Like Python’s `.title()`.
*/
export function title(string: string): string {
return string.split(" ").map(capitalize).join(" ");
}