import {Supplier} from '@btilford/ts-base-core';
export interface ElementExtractor {
(node: Element): { key: string; value: string } | undefined;
}
export function documentQueryLoader(selector: string, extractor: ElementExtractor, document: { querySelectorAll } | Document): Supplier<Record<string, string>> {
return function queryLoader(): Record<string, string> {
const env: Record<string, string> = {};
const tags = document.querySelectorAll(selector);
tags.forEach(element => {
const result = extractor(element);
if (result) {
env[result.key] = result.value;
}
});
return env;
}
}
export const metaContentExtractor: ElementExtractor = (node => {
let result: { key: string; value: string } | undefined;
if (node.hasAttribute('name') && node.hasAttribute('content')) {
const key: string = node.getAttribute('name') as string;
const value: string = node.getAttribute('content') as string;
result = { key, value };
} else {
result = undefined;
}
return result;
});
export function loadEnvWithMetaTags(namePrefix: string, document: Document): Supplier<Record<string, string>> {
return documentQueryLoader(`meta[name^="${namePrefix}"][content]`, metaContentExtractor, document);
}
|