all files / util/ platform.js

62.07% Statements 18/29
68.42% Branches 13/19
100% Functions 1/1
62.07% Lines 18/29
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                                                                                          405× 405×     405× 405× 405× 405×   405×           405×               405×                   405×     405×         405× 405× 405×       405×            
/**
  @module
 
  Platform utilities such as browser detection etc.
 
  @example
 
  ```js
  import platform from 'substance/util/platform'
  ```
*/
const platform = {
 
  inBrowser: false,
 
  /**
    True if user agent is Internet Explorer or Microsoft Edge.
  */
  isIE: false,
  /**
    True if user agent is Firefox
  */
 
  isFF: false,
 
  isWebkit: false,
 
  /*
    Major version
 
    ATTENTION: at the moment only extracted for IE
  */
  version: -1,
 
  // TODO: make sure that this is implemented correctly
 
  isWindows: false,
 
  isMac: false,
 
  // in tests we change the state of this to emulate execuatio under certain conditions
  // to reset to defaults we call this function
  _reset: detect
}
 
function detect() {
 
  Eif (typeof window !== 'undefined') {
    platform.inBrowser = true
 
    // Detect Internet Explorer / Edge
    const ua = window.navigator.userAgent
    const msie = ua.indexOf('MSIE ')
    const trident = ua.indexOf('Trident/')
    const edge = ua.indexOf('Edge/')
 
    Iif (msie > 0) {
      // IE 10 or older => return version number
      platform.isIE = true
      platform.version = 10
      // TODO: if we need someday, this would be the exact version number
      // parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10)
    } else Iif (trident > 0) {
      // IE 11 => return version number
      platform.isIE = true
      platform.version = 11
      platform.isTrident = true
      // TODO: if we need someday, this would be the exact version number
      // var rv = ua.indexOf('rv:')
      // parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10)
    } else Iif (edge > 0) {
      // IE 12 => return version number
      platform.isIE = true
      platform.isEdge = true
      platform.version = 12
      // TODO: if we need someday, this would be the exact version number
      parseInt(ua.substring(edge + 5, ua.indexOf('.', edge)), 10)
    }
 
    // Detect Firefox
    platform.isFF = window.navigator.userAgent.toLowerCase().indexOf('firefox') > -1
 
    // TODO: explicit detection of Webkit&/Blink
    platform.isWebkit = !platform.isFF && !platform.isIE
  } else {
    platform.inBrowser = false
  }
 
  Eif (platform.inBrowser) {
    platform.isWindows = (window.navigator !== undefined && window.navigator.appVersion && window.navigator.appVersion.indexOf("Win") !== -1)
    platform.isMac = (window.navigator !== undefined && window.navigator.platform.indexOf('Mac') >= 0)
  }
 
  // TOOD: is there a more reliable way to detect NodeJS?
  Iif (typeof process !== 'undefined') {
    platform.inNodeJS = true
  }
}
 
detect()
 
export default platform