all files / components/ utils.js

90.41% Statements 66/73
85.48% Branches 53/62
100% Functions 15/15
90.41% Lines 66/73
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177                             133×     132× 599× 92×     89×         42× 403×     41×                 79×       13×     12× 12× 12× 1908× 12×     1896×                 14×         320×       29×       28×       27× 27× 27× 27×   27×   27× 27×     27× 27×               34×                   449×   449× 56326× 257×       192×     191×     191×                                 19× 19×              
import AllCountries from './AllCountries';
 
export default {
  arraysEqual(a, b) {
    if (a === b) {
      return true;
    }
    if (a === null || b === null) {
      return false;
    }
    if (a.length !== b.length) {
      return false;
    }
 
    // If you don't care about the order of the elements inside
    // the array, you should sort both arrays here.
 
    for (let i = 0; i < a.length; ++i) {
      if (a[i] !== b[i]) {
        return false;
      }
    }
    return true;
  },
 
  shallowEquals(a, b) {
    if (a === b) {
      return true;
    }
 
    for (const key in a) {
      if (a[key] !== b[key]) {
        if (Array.isArray(a[key]) && Array.isArray(b[key])) {
          if (!this.arraysEqual(a[key], b[key])) {
            return false;
          }
        } else {
          return false;
        }
      }
    }
 
    for (const key in b) {
      if (a.hasOwnProperty(key) === false) {
        return false;
      }
    }
    return true;
  },
 
  trim(str) {
    // Make sure we trim BOM and NBSP
    const rtrim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;
    if (!str) {
      return '';
    }
    return str.replace(rtrim, '');
  },
 
  isNumeric(obj) {
    return obj - parseFloat(obj) >= 0;
  },
 
  retrieveLiIndex(node) {
    if (!node) {
      return -1;
    }
 
    const children = node.parentNode.childNodes;
    let num = 0;
    for (let i = 0, max = children.length; i < max; i++) {
      if (children[i] === node) {
        return num;
      }
 
      if (children[i].nodeType === 1 && children[i].tagName.toLowerCase() === 'li') {
        num++;
      }
    }
    return -1;
  },
 
  // extract the numeric digits from the given string
  getNumeric(s) {
    return s.replace(/\D/g, '');
  },
 
  // check if (uppercase) string a starts with string b
  startsWith(a, b) {
    return (a.substr(0, b.length).toUpperCase() === b);
  },
 
  isWindow(obj) {
    return obj !== null && obj === obj.window;
  },
 
  getWindow(elem) {
    return this.isWindow(elem) ? elem : elem.nodeType === 9 && elem.defaultView;
  },
 
  offset(elem) {
    let docElem = undefined;
    let win = undefined;
    let box = { top: 0, left: 0 };
    const doc = elem && elem.ownerDocument;
 
    docElem = doc.documentElement;
 
    if (Etypeof elem.getBoundingClientRect !== typeof undefined) {
      box = elem.getBoundingClientRect();
    }
 
    win = this.getWindow(doc);
    return {
      top: box.top + win.pageYOffset - docElem.clientTop,
      left: box.left + win.pageXOffset - docElem.clientLeft,
    };
  },
 
  // retrieve outerHeight of element
  getOuterHeight(element) {
    return element.offsetHeight +
           parseFloat(window.getComputedStyle(element).getPropertyValue('margin-top')) +
           parseFloat(window.getComputedStyle(element).getPropertyValue('margin-bottom'));
  },
 
  // find the country data for the given country code
  // the ignoreOnlyCountriesOption is only used during init()
  // while parsing the onlyCountries array
  getCountryData(countries, countryCode, ignoreOnlyCountriesOption,
                 allowFail, errorHandler) {
    const countryList = ignoreOnlyCountriesOption ?
      AllCountries.getCountries() : countries;
    for (let i = 0; i < countryList.length; i++) {
      if (countryList[i].iso2 === countryCode) {
        return countryList[i];
      }
    }
 
    if (allowFail) {
      return null;
    }
 
    if (typeof errorHandler === 'function') {
      errorHandler(countryCode);
    }
 
    return {};
  },
 
  // Copied from http://jaketrent.com/post/addremove-classes-raw-javascript/
  hasClass(el, className) {
    if (Eel.classList) {
      return el.classList.contains(className);
    }
 
    return !!el.className.match(new RegExp(`(\\s|^)${className}(\\s|$)`));
  },
 
  addClass(el, className) {
    if (Eel.classList) {
      el.classList.add(className);
    } else if (!this.hasClass(el, className)) {
      el.className += ` ${className}`;
    }
  },
 
  removeClass(el, className) {
    if (Eel.classList) {
      el.classList.remove(className);
    } else if (this.hasClass(el, className)) {
      const reg = new RegExp(`(\\s|^)${className}(\\s|$)`);
      el.className = el.className.replace(reg, ' ');
    }
  },
};