Skip to content

Commit

Permalink
[submodule:helpers] Enable directly pass user-agent as an input to `i…
Browse files Browse the repository at this point in the history
…sAppleSilicon()` / `isBot()` / `isChromeFamily()`
  • Loading branch information
faisalman committed Nov 16, 2024
1 parent b1c7dfc commit 5b375b9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/helpers/ua-parser-helpers.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import { IResult } from "../main/ua-parser";

declare function getDeviceVendor(model: string): string | undefined;
declare function isAppleSilicon(res: IResult, useFeatureDetection?: boolean): boolean;
declare function isBot(res: IResult): boolean;
declare function isChromeFamily(res: IResult): boolean;
declare function isAppleSilicon(resultOrUA: IResult | string): boolean;
declare function isBot(resultOrUA: IResult | string): boolean;
declare function isChromeFamily(resultOrUA: IResult | string): boolean;
declare function isElectron(): boolean;
declare function isFromEU(): boolean;
declare function isFrozenUA(ua: string): boolean;
Expand Down
17 changes: 13 additions & 4 deletions src/helpers/ua-parser-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,22 @@

const { UAParser } = require('../main/ua-parser');
const { CPU, OS, Engine } = require('../enums/ua-parser-enums');
const { Bots } = require('../extensions/ua-parser-extensions');
const { isFromEU } = require('detect-europe-js');
const { isFrozenUA } = require('ua-is-frozen');
const { isStandalonePWA } = require('is-standalone-pwa');

const toResult = (value, head, ext) => typeof value === 'string' ? UAParser(value, head, ext) : value;

const getDeviceVendor = (model) => UAParser(`Mozilla/5.0 (Linux; Android 10; ${model}) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.0.0 Safari/537.36`).device.vendor;

const isAppleSilicon = (res) => {
const isAppleSilicon = (resultOrUA) => {
const res = toResult(resultOrUA);
if (res.os.is(OS.MACOS)) {
if (res.cpu.is(CPU.ARM)) {
return true;
}
if (typeof window !== 'undefined') {
if (typeof resultOrUA !== 'string' && typeof window !== 'undefined') {
try {
const canvas = document.createElement('canvas');
const webgl = canvas.getContext('webgl2') || canvas.getContext('webgl') || canvas.getContext('experimental-webgl');
Expand All @@ -37,9 +41,14 @@ const isAppleSilicon = (res) => {
return false;
}

const isBot = (res) => ['cli', 'crawler', 'fetcher', 'library'].includes(res.browser.type);
const isBot = (resultOrUA) => [
'cli',
'crawler',
'fetcher',
'library'
].includes(toResult(resultOrUA, Bots).browser.type);

const isChromeFamily = (res) => res.engine.is(Engine.BLINK);
const isChromeFamily = (resultOrUA) => toResult(resultOrUA).engine.is(Engine.BLINK);

const isElectron = () => !!(process?.versions?.hasOwnProperty('electron') || // node.js
/ electron\//i.test(navigator?.userAgent)); // browser
Expand Down
9 changes: 9 additions & 0 deletions test/mocha-test-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ describe('isAppleSilicon', () => {
const macIntel = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:97.0) Gecko/20100101 Firefox/97.0';

assert.equal(isAppleSilicon(UAParser(macIntel)), false);
assert.equal(isAppleSilicon(macIntel), false);
assert.equal(isAppleSilicon(UAParser(macARM)), true);
assert.equal(isAppleSilicon(macARM), true);
});
});

Expand All @@ -46,6 +48,11 @@ describe('isBot', () => {
assert.equal(isBot(botParser.setUA(ahrefsBot).getResult()), true);
assert.equal(isBot(botParser.setUA(scrapy).getResult()), true);
assert.equal(isBot(botParser.setUA(thunderbird).getResult()), false);

assert.equal(isBot(ahrefsBot), true);
assert.equal(isBot(firefox), false);
assert.equal(isBot(scrapy), true);
assert.equal(isBot(thunderbird), false);
});
});

Expand All @@ -57,5 +64,7 @@ describe('isChromeFamily', () => {

assert.equal(isChromeFamily(UAParser(edge)), true);
assert.equal(isChromeFamily(UAParser(firefox)), false);
assert.equal(isChromeFamily(edge), true);
assert.equal(isChromeFamily(firefox), false);
});
});

0 comments on commit 5b375b9

Please sign in to comment.