Skip to main content
Module

x/lume/utils.js

🔥 Static site generator for Deno 🦕
Very Popular
Go to Latest
File
import { bold, red } from "./deps/colors.js";import { SEP } from "./deps/path.js";
export async function concurrent(iterable, iteratorFn, limit = 200) { const executing = [];
for await (const item of iterable) { const p = iteratorFn(item).then(() => executing.splice(executing.indexOf(p), 1) );
executing.push(p);
if (executing.length >= limit) { await Promise.race(executing); } }
await Promise.all(executing);}
export const mimes = new Map([ [".html", "text/html; charset=utf-8"], [".txt", "text/plain; charset=utf-8"], [".js", "text/javascript; charset=utf-8"], [".css", "text/css; charset=utf-8"], [".json", "application/json; charset=utf-8"], [".ico", "image/x-icon"], [".png", "image/png"], [".jpg", "image/jpg"], [".jpeg", "image/jpg"], [".gif", "image/gif"], [".svg", "image/svg+xml"], [".mp3", "audio/mpeg"], [".mp4", "video/mpeg"], [".xml", "text/xml"], [".woff", "font/woff"], [".woff2", "font/woff2"], [".wasm", "application/wasm"], [".webp", "image/webp"], [".webm", "video/webm"], [".zip", "application/zip"],]);
export function error(context, message, exception) { console.error(bold(red(`${context}:`)), message); if (exception) { console.error(exception); } console.error("");}
export function merge(defaults, user) { const merged = { ...defaults };
if (!user) { return merged; }
for (const [key, value] of Object.entries(user)) { if ( typeof merged[key] === "object" && typeof value === "object" && !Array.isArray(merged[key]) ) { merged[key] = merge(merged[key], value); continue; }
merged[key] = value; }
return merged;}
export function normalizePath(path) { //Is Windows path if (SEP !== "/") { return path.replaceAll(SEP, "/"); }
return path;}
export const slugChars = new Map([ ["á", "a"], ["à", "a"], ["â", "a"], ["ã", "a"], ["ä", "a"], ["å", "a"], ["é", "e"], ["è", "e"], ["ê", "e"], ["ë", "e"], ["ì", "i"], ["í", "i"], ["î", "i"], ["ï", "i"], ["ò", "o"], ["ó", "o"], ["ô", "o"], ["õ", "o"], ["ö", "o"], ["ð", "d"], ["ù", "u"], ["ú", "u"], ["ü", "u"], ["û", "u"], ["ñ", "n"], ["ª", "a"], ["º", "o"], ["ç", "c"],]);
export function slugify(string) { return string.toLowerCase() .replaceAll(/[\s-_]+/g, () => "-") .replaceAll(/[^\w-\/]/g, (char) => slugChars.get(char) || "");}
export function searchByExtension(path, extensions) { for (const [key, value] of extensions) { if (path.endsWith(key)) { return [key, value]; } }}