Code coverage report for browser/src/browser-env.ts

Statements: 0% (0 / 18)      Branches: 0% (0 / 6)      Functions: 0% (0 / 5)      Lines: 0% (0 / 18)      Ignored: none     

All files » browser/src/ » browser-env.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37                                                                         
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);
}