Add new helper method: isFromEU() to detect whether user comes from an EU country

This commit is contained in:
Faisal Salman 2024-09-14 12:38:51 +07:00
parent b9f1bf6223
commit db1612401f
5 changed files with 31 additions and 1 deletions

22
package-lock.json generated
View File

@ -22,6 +22,9 @@
} }
], ],
"license": "AGPL-3.0-or-later", "license": "AGPL-3.0-or-later",
"dependencies": {
"detect-europe-js": "^0.1.1"
},
"bin": { "bin": {
"ua-parser-js": "script/cli.js" "ua-parser-js": "script/cli.js"
}, },
@ -1664,6 +1667,25 @@
"integrity": "sha512-bd2L678uiWATM6m5Z1VzNCErI3jiGzt6HGY8OVICs40JQq/HALfbyNJmp0UDakEY4pMMaN0Ly5om/B1VI/+xfQ==", "integrity": "sha512-bd2L678uiWATM6m5Z1VzNCErI3jiGzt6HGY8OVICs40JQq/HALfbyNJmp0UDakEY4pMMaN0Ly5om/B1VI/+xfQ==",
"dev": true "dev": true
}, },
"node_modules/detect-europe-js": {
"version": "0.1.1",
"resolved": "https://registry.npmjs.org/detect-europe-js/-/detect-europe-js-0.1.1.tgz",
"integrity": "sha512-+bUXDf+tI3L4dcEuRdAFa44Amx9aEaJzoZssx7Xis4H1bXWc5fAcOP850BOj0wJPRzOdovOuOVEvrg6T+GflZA==",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/faisalman"
},
{
"type": "opencollective",
"url": "https://opencollective.com/ua-parser-js"
},
{
"type": "paypal",
"url": "https://paypal.me/faisalman"
}
]
},
"node_modules/detect-libc": { "node_modules/detect-libc": {
"version": "2.0.1", "version": "2.0.1",
"resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-2.0.1.tgz", "resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-2.0.1.tgz",

View File

@ -210,6 +210,9 @@
"test:mocha": "mocha test/mocha*js", "test:mocha": "mocha test/mocha*js",
"test:playwright": "playwright test" "test:playwright": "playwright test"
}, },
"dependencies": {
"detect-europe-js": "^0.1.1"
},
"devDependencies": { "devDependencies": {
"@babel/parser": "7.15.8", "@babel/parser": "7.15.8",
"@babel/traverse": "7.23.2", "@babel/traverse": "7.23.2",

View File

@ -7,7 +7,8 @@ const generateMJS = (module) => {
let text = fs.readFileSync(src, 'utf-8'); let text = fs.readFileSync(src, 'utf-8');
replacements.push( replacements.push(
[/const (.+?)\s*=\s*require\(\'(.+)\'\)/ig, 'import $1 from \'$2.mjs\''], [/const (.+?)\s*=\s*require\(\'\.(.+)\'\)/ig, 'import $1 from \'$2.mjs\''],
[/const (.+?)\s*=\s*require\(\'(.+)\'\)/ig, 'import $1 from \'$2\''],
[/module\.exports =/ig, 'export'] [/module\.exports =/ig, 'export']
); );
replacements.forEach(rep => { replacements.forEach(rep => {

View File

@ -7,6 +7,7 @@ import { IResult } from "../main/ua-parser";
declare function getDeviceVendor(model: string): string | undefined; declare function getDeviceVendor(model: string): string | undefined;
declare function isAppleSilicon(res: IResult): boolean; declare function isAppleSilicon(res: IResult): boolean;
declare function isChromeFamily(res: IResult): boolean; declare function isChromeFamily(res: IResult): boolean;
declare function isFromEU(): boolean;
declare function isFrozenUA(ua: string): boolean; declare function isFrozenUA(ua: string): boolean;
declare function isStandalonePWA(): boolean; declare function isStandalonePWA(): boolean;
@ -14,6 +15,7 @@ export {
getDeviceVendor, getDeviceVendor,
isAppleSilicon, isAppleSilicon,
isChromeFamily, isChromeFamily,
isFromEU,
isFrozenUA, isFrozenUA,
isStandalonePWA isStandalonePWA
} }

View File

@ -9,6 +9,7 @@
const { CPU, OS, Engine } = require('../enums/ua-parser-enums'); const { CPU, OS, Engine } = require('../enums/ua-parser-enums');
const { UAParser } = require('../main/ua-parser'); const { UAParser } = require('../main/ua-parser');
const { isFromEU } = require('detect-europe-js');
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 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;
@ -50,6 +51,7 @@ module.exports = {
getDeviceVendor, getDeviceVendor,
isAppleSilicon, isAppleSilicon,
isChromeFamily, isChromeFamily,
isFromEU,
isFrozenUA, isFrozenUA,
isStandalonePWA isStandalonePWA
} }