[@portaljs/ckan][xl] - add ckan package and example using it

This commit is contained in:
Luccas Mateus de Medeiros Gomes
2023-05-23 14:47:25 -03:00
parent eac0a22aa8
commit c82bfdd847
87 changed files with 31067 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
export function getDaysAgo(date: string) {
const today = new Date();
const createdOn = new Date(date);
const msInDay = 24 * 60 * 60 * 1000;
createdOn.setHours(0, 0, 0, 0);
today.setHours(0, 0, 0, 0);
return (+today - +createdOn) / msInDay;
}
export default async function fetchRetry(url: string, n: number): Promise<any> {
const abortController = new AbortController();
const id = setTimeout(() => abortController.abort(), 30000);
const res = await fetch(url, { signal: abortController.signal });
clearTimeout(id);
if (!res.ok && n && n > 0) {
return await fetchRetry(url, n - 1);
}
return res;
}
export function removeTag(tag?: string) {
if (tag === "{{description}}" || !tag) {
return undefined;
}
if (typeof window !== "undefined") {
const div = document.createElement("div");
div.innerHTML = tag;
return div.textContent || div.innerText || "";
}
return tag;
}
//The porpuse of this functoin is converting the schema returned from datastore info which is in the form o a dictionary into an array of key value pairs that can be consumed by the data-explorer
export function convertFieldSchema(
schema: Record<string, string | boolean | number>
) {
function convertToGraphqlString(fieldName: string) {
return fieldName
.replaceAll(" ", "_")
.replaceAll("(", "_")
.replaceAll(")", "_")
.replace(/[^\w\s]|(_)\1/gi, "_");
}
const entries = Object.entries(schema);
return {
fields: entries.map((entry) => ({
name: convertToGraphqlString(entry[0]),
title: entry[0],
type: entry[1],
})),
};
}