Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Feature: Add env.isEdge property. #248

Merged
merged 4 commits into from
Jul 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions src/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,20 @@ const userAgent = navigator.userAgent.toLowerCase();
*/
const env = {
/**
* Indicates that application is running on Macintosh.
* Indicates that the application is running on Macintosh.
*
* @static
* @member {Boolean} module:utils/env~env#isMac
*/
isMac: isMac( userAgent )
isMac: isMac( userAgent ),

/**
* Indicates that the application is running in Microsoft Edge.
*
* @static
* @member {Boolean} module:utils/env~env#isEdge
*/
isEdge: isEdge( userAgent )
};

export default env;
Expand All @@ -37,3 +45,13 @@ export default env;
export function isMac( userAgent ) {
return userAgent.indexOf( 'macintosh' ) > -1;
}

/**
* Checks if User Agent represented by the string is Microsoft Edge.
*
* @param {String} userAgent **Lowercase** `navigator.userAgent` string.
* @returns {Boolean} Whether User Agent is Edge or not.
*/
export function isEdge( userAgent ) {
return !!userAgent.match( /edge\/(\d+.?\d*)/ );
}
46 changes: 45 additions & 1 deletion tests/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
* For licensing, see LICENSE.md.
*/

import env, { isMac } from '../src/env';
import env, { isEdge, isMac } from '../src/env';

function toLowerCase( str ) {
return str.toLowerCase();
}

describe( 'Env', () => {
beforeEach( () => {
Expand All @@ -19,10 +23,21 @@ describe( 'Env', () => {
} );
} );

describe( 'isEdge', () => {
it( 'is a boolean', () => {
expect( env.isEdge ).to.be.a( 'boolean' );
} );
} );

describe( 'isMac()', () => {
it( 'returns true for macintosh UA strings', () => {
expect( isMac( 'macintosh' ) ).to.be.true;
expect( isMac( 'foo macintosh bar' ) ).to.be.true;

expect( isMac( toLowerCase(
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) ' +
'Chrome/61.0.3163.100 Safari/537.36'
) ) ).to.be.true;
} );

it( 'returns false for non–macintosh UA strings', () => {
Expand All @@ -31,4 +46,33 @@ describe( 'Env', () => {
expect( isMac( 'foo' ) ).to.be.false;
} );
} );

describe( 'isEdge()', () => {
it( 'returns true for Edge UA strings', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a single real UA string ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah - I've copied from other test. I'm adding some real ones right now ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK... I was too slow 😞

expect( isEdge( 'edge/12' ) ).to.be.true;
expect( isEdge( 'foo edge/12 bar' ) ).to.be.true;

expect( isEdge( toLowerCase(
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) ' +
'Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393'
) ) ).to.be.true;
} );

it( 'returns false for non–Edge UA strings', () => {
expect( isEdge( '' ) ).to.be.false;
expect( isEdge( 'mac' ) ).to.be.false;
expect( isEdge( 'foo' ) ).to.be.false;
expect( isEdge( 'ledge' ) ).to.be.false;
expect( isEdge( 'foo edge bar' ) ).to.be.false;

// Chrome
expect( isEdge( toLowerCase(
'Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36'
) ) ).to.be.false;
// IE11
expect( isEdge( toLowerCase(
'Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko'
) ) ).to.be.false;
} );
} );
} );