All files / models NavigationCollection.js

5.56% Statements 1/18
0% Branches 0/8
0% Functions 0/6
5.88% Lines 1/17
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 38 39 4019x                                                                              
const checkQueryString = (url) => {
  if (/\?url=/.test(url)) {
    return decodeURIComponent(url.split('=')[1]);
  }
  return url;
};
 
function formatItem(item) {
  const openInNew = item.isNewWindow === 'True';
  const url = openInNew ? checkQueryString(item.AbsolutePortalURL) : item.AbsolutePortalURL;
  return {
    label: item.Label,
    description: item.Description,
    url,
    openInNew,
    imageUrl: item.ImageURL,
  };
}
 
export default class NavigationCollection {
  constructor(obj) {
    this.name = obj.NavCollection.Name;
    this.label = obj.NavCollection.Label;
    this.contents = obj.NavCollection.Contents.NavItem;
    this.parseContents();
  }
 
  parseContents() {
    if (!Array.isArray(this.contents)) {
      this.contents = [this.contents];
    }
    this.contents = this.contents.map((content) => {
      if (content.NavItem) {
        return { label: content.Label, contents: content.NavItem.map(item => formatItem(item)) };
      }
      return formatItem(content);
    });
  }
}