diff --git a/src/env.js b/src/env.js index 39154db..81cb6c6 100644 --- a/src/env.js +++ b/src/env.js @@ -31,7 +31,15 @@ const env = { * @static * @member {Boolean} module:utils/env~env#isEdge */ - isEdge: isEdge( userAgent ) + isEdge: isEdge( userAgent ), + + /** + * Indicates that the application is running in Firefox (Gecko). + * + * @static + * @member {Boolean} module:utils/env~env#isEdge + */ + isGecko: isGecko( userAgent ) }; export default env; @@ -55,3 +63,13 @@ export function isMac( userAgent ) { export function isEdge( userAgent ) { return !!userAgent.match( /edge\/(\d+.?\d*)/ ); } + +/** + * Checks if User Agent represented by the string is Firefox (Gecko). + * + * @param {String} userAgent **Lowercase** `navigator.userAgent` string. + * @returns {Boolean} Whether User Agent is Firefox or not. + */ +export function isGecko( userAgent ) { + return !!userAgent.match( /gecko\/\d+/ ); +} diff --git a/tests/env.js b/tests/env.js index 91fb373..2cae15a 100644 --- a/tests/env.js +++ b/tests/env.js @@ -3,7 +3,7 @@ * For licensing, see LICENSE.md. */ -import env, { isEdge, isMac } from '../src/env'; +import env, { isEdge, isMac, isGecko } from '../src/env'; function toLowerCase( str ) { return str.toLowerCase(); @@ -29,6 +29,12 @@ describe( 'Env', () => { } ); } ); + describe( 'isGecko', () => { + it( 'is a boolean', () => { + expect( env.isGecko ).to.be.a( 'boolean' ); + } ); + } ); + describe( 'isMac()', () => { it( 'returns true for macintosh UA strings', () => { expect( isMac( 'macintosh' ) ).to.be.true; @@ -75,4 +81,26 @@ describe( 'Env', () => { ) ) ).to.be.false; } ); } ); + + describe( 'isGecko()', () => { + it( 'returns true for Firefox UA strings', () => { + expect( isGecko( 'gecko/42' ) ).to.be.true; + expect( isGecko( 'foo gecko/42 bar' ) ).to.be.true; + + expect( isGecko( toLowerCase( + 'mozilla/5.0 (macintosh; intel mac os x 10.13; rv:62.0) gecko/20100101 firefox/62.0' + ) ) ).to.be.true; + } ); + + it( 'returns false for non–Edge UA strings', () => { + expect( isGecko( '' ) ).to.be.false; + expect( isGecko( 'foo' ) ).to.be.false; + expect( isGecko( 'Mozilla' ) ).to.be.false; + + // Chrome + expect( isGecko( 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; + } ); + } ); } );