Code coverage report for ./downlinkmax.js

Statements: 100% (24 / 24)      Branches: 100% (17 / 17)      Functions: 100% (1 / 1)      Lines: 100% (16 / 16)      Ignored: none     

All files » ./ » downlinkmax.js
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          1 16                   16       12       1           11 1 1   2 1 1 1 1   3   11         16  
/**
 * @file downlinkmax is 0.26KB Network Information API downlinkMax polyfill micro-library.
 * @author Daniel Lamb <dlamb.open.source@gmail.com>
 */
 
function downlinkmax() {
  var limitless = Infinity,
      nav = navigator,
      speed,
      // deal with vendor prefixing
      connection = nav.connection || nav.mozConnection || nav.webkitConnection || {
        // API not supported
        downlinkMax: limitless
      };
 
  // check that the API doesn't already support downlinkMax
  if (!('downlinkMax' in connection)) {
 
    // assume NOT W3C Editor's Draft 09 October 2014
    // check if API supports bandwidth
    if ('bandwidth' in connection) {
 
      // assume W3C Working Draft 29 November 2012
      // standardize connection.bandwidth value by converting megabytes per second (MB/s) to megabits per second (Mbit/s)
      connection.downlinkMax = connection.bandwidth * 8;
    } else {
 
      // assume W3C Working Draft 07 June 2011
      // convert connection.type value to approximate downlink values
      // speed estimate is based on the median downlink value for common devices in megabits per second (Mbit/s)
      switch (connection.type) {
        case 'none': speed = 0; break;
        case '2g': speed = 0.134; break;
        case 'bluetooth':
        case 'cellular': speed = 2; break;
        case '3g': speed = 8.95; break;
        case '4g': speed = 100; break;
        case 'ethernet': speed = 550; break;
        case 'wifi': speed = 600; break;
        // other, unknown etc.
        default: speed = limitless; break;
      }
      connection.downlinkMax = speed;
    }
  }
 
  // return the maximum downlink speed
  return connection.downlinkMax;
}