2021-04-03 17:26:43 +00:00
|
|
|
import { t } from "@lingui/macro";
|
2021-09-21 09:31:37 +00:00
|
|
|
|
2021-09-21 09:19:26 +00:00
|
|
|
import { html, TemplateResult } from "lit";
|
2021-09-21 09:31:37 +00:00
|
|
|
|
2021-03-17 16:11:39 +00:00
|
|
|
import "./elements/EmptyState";
|
2020-12-01 16:27:19 +00:00
|
|
|
|
2021-01-16 18:09:03 +00:00
|
|
|
export function getCookie(name: string): string {
|
|
|
|
let cookieValue = "";
|
2020-11-21 19:48:49 +00:00
|
|
|
if (document.cookie && document.cookie !== "") {
|
|
|
|
const cookies = document.cookie.split(";");
|
2020-10-16 12:10:27 +00:00
|
|
|
for (let i = 0; i < cookies.length; i++) {
|
|
|
|
const cookie = cookies[i].trim();
|
|
|
|
// Does this cookie string begin with the name we want?
|
2020-11-21 19:48:49 +00:00
|
|
|
if (cookie.substring(0, name.length + 1) === name + "=") {
|
2020-11-26 22:35:59 +00:00
|
|
|
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
|
2020-10-16 12:10:27 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cookieValue;
|
|
|
|
}
|
2020-11-21 18:22:53 +00:00
|
|
|
|
|
|
|
export function convertToSlug(text: string): string {
|
|
|
|
return text
|
|
|
|
.toLowerCase()
|
2020-11-21 19:48:49 +00:00
|
|
|
.replace(/ /g, "-")
|
|
|
|
.replace(/[^\w-]+/g, "");
|
2020-11-21 18:22:53 +00:00
|
|
|
}
|
2020-11-30 11:33:09 +00:00
|
|
|
|
2021-02-06 17:35:55 +00:00
|
|
|
export function convertToTitle(text: string): string {
|
2021-08-03 15:52:21 +00:00
|
|
|
return text.replace(/\w\S*/g, function (txt) {
|
|
|
|
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
|
|
|
|
});
|
2021-02-06 17:35:55 +00:00
|
|
|
}
|
|
|
|
|
2020-11-30 11:33:09 +00:00
|
|
|
export function truncate(input?: string, max = 10): string {
|
|
|
|
input = input || "";
|
|
|
|
const array = input.trim().split(" ");
|
|
|
|
const ellipsis = array.length > max ? "..." : "";
|
|
|
|
|
|
|
|
return array.slice(0, max).join(" ") + ellipsis;
|
|
|
|
}
|
2020-12-01 16:27:19 +00:00
|
|
|
|
2020-12-02 21:14:28 +00:00
|
|
|
export function loading<T>(v: T, actual: TemplateResult): TemplateResult {
|
|
|
|
if (!v) {
|
2021-08-03 15:52:21 +00:00
|
|
|
return html`<ak-empty-state ?loading="${true}" header=${t`Loading`}> </ak-empty-state>`;
|
2020-12-02 21:14:28 +00:00
|
|
|
}
|
|
|
|
return actual;
|
|
|
|
}
|
2021-03-29 16:18:25 +00:00
|
|
|
|
|
|
|
export function camelToSnake(key: string): string {
|
2021-03-29 16:22:15 +00:00
|
|
|
const result = key.replace(/([A-Z])/g, " $1");
|
|
|
|
return result.split(" ").join("_").toLowerCase();
|
2021-03-29 16:18:25 +00:00
|
|
|
}
|
2021-03-31 13:57:59 +00:00
|
|
|
|
|
|
|
export function groupBy<T>(objects: T[], callback: (obj: T) => string): Array<[string, T[]]> {
|
|
|
|
const m = new Map<string, T[]>();
|
2021-08-03 15:52:21 +00:00
|
|
|
objects.forEach((obj) => {
|
2021-03-31 13:57:59 +00:00
|
|
|
const group = callback(obj);
|
|
|
|
if (!m.has(group)) {
|
|
|
|
m.set(group, []);
|
|
|
|
}
|
|
|
|
const tProviders = m.get(group) || [];
|
|
|
|
tProviders.push(obj);
|
|
|
|
});
|
2021-04-04 11:19:34 +00:00
|
|
|
return Array.from(m).sort();
|
2021-03-31 13:57:59 +00:00
|
|
|
}
|
2021-04-03 09:41:11 +00:00
|
|
|
|
|
|
|
export function first<T>(...args: Array<T | undefined | null>): T {
|
|
|
|
for (let index = 0; index < args.length; index++) {
|
|
|
|
const element = args[index];
|
|
|
|
if (element !== undefined && element !== null) {
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new Error(`No compatible arg given: ${args}`);
|
|
|
|
}
|
2021-04-03 12:47:34 +00:00
|
|
|
|
|
|
|
export function hexEncode(buf: Uint8Array): string {
|
|
|
|
return Array.from(buf)
|
|
|
|
.map(function (x) {
|
|
|
|
return ("0" + x.toString(16)).substr(-2);
|
|
|
|
})
|
|
|
|
.join("");
|
|
|
|
}
|
|
|
|
|
|
|
|
export function randomString(len: number): string {
|
|
|
|
const arr = new Uint8Array(len / 2);
|
|
|
|
window.crypto.getRandomValues(arr);
|
|
|
|
return hexEncode(arr);
|
|
|
|
}
|
2021-09-18 13:31:48 +00:00
|
|
|
|
|
|
|
export function dateTimeLocal(date: Date): string {
|
|
|
|
// So for some reason, the datetime-local input field requires ISO Datetime as value
|
|
|
|
// But the standard javascript date.toISOString() returns everything with seconds and
|
|
|
|
// milliseconds, which the input field doesn't like (on chrome, on firefox its fine)
|
|
|
|
// On chrome, setting .valueAsNumber works, but that causes an error on firefox, so go
|
|
|
|
// figure.
|
|
|
|
const parts = date.toISOString().split(":");
|
|
|
|
return `${parts[0]}:${parts[1]}`;
|
|
|
|
}
|