export const canUseDOM = (): boolean =>
  !!(typeof window !== 'undefined' && window.document);

export const xmlParser = (xmlString: string): Document => {
  const parser = new window.DOMParser();
  const xmlDoc = parser.parseFromString(xmlString, 'text/xml');
  return xmlDoc;
};

export const getElementsByTagName = (
  elements: ChildNode,
  tagname: string,
): NodeList | null => {
  const arr = Array.prototype.slice.call(elements.childNodes);
  let element: NodeList | null = null;

  arr.forEach((ele) => {
    if (ele.tagName === tagname) {
      element = ele.childNodes;
    }
  });
  return element;
};

export default canUseDOM;
