mirror of
https://github.com/faisalman/ua-parser-js.git
synced 2025-09-27 07:58:45 +03:00
Fix #300 : Provide enums - initial work
This commit is contained in:
parent
ba28d33d51
commit
af65fd6960
@ -144,15 +144,21 @@
|
|||||||
"main": "src/ua-parser.js",
|
"main": "src/ua-parser.js",
|
||||||
"module": "src/ua-parser.mjs",
|
"module": "src/ua-parser.mjs",
|
||||||
"exports": {
|
"exports": {
|
||||||
|
"." : {
|
||||||
"require": "./src/ua-parser.js",
|
"require": "./src/ua-parser.js",
|
||||||
"import": "./src/ua-parser.mjs"
|
"import": "./src/ua-parser.mjs"
|
||||||
},
|
},
|
||||||
|
"./enums" : {
|
||||||
|
"require": "./src/ua-parser-enum.js",
|
||||||
|
"import": "./src/ua-parser-enum.mjs"
|
||||||
|
}
|
||||||
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"dist",
|
"dist",
|
||||||
"src"
|
"src"
|
||||||
],
|
],
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "uglifyjs src/ua-parser.js -o dist/ua-parser.min.js --comments '/^ UA/' && uglifyjs src/ua-parser.js -o dist/ua-parser.pack.js --comments '/^ UA/' --compress --mangle && node -e \"const fs=require('fs');fs.writeFileSync('src/ua-parser.mjs','// Generated ESM version of UAParser.js\\n// Source file: /src/ua-parser.js\\n\\nconst window = undefined;\\n\\n'+fs.readFileSync('src/ua-parser.js','utf-8').replace(/\\(func[\\s\\S]+strict\\';/ig,'').replace(/\\/[\\/\\s]+export[\\s\\S]+/ig,'export {UAParser};'),'utf-8')\"",
|
"build": "uglifyjs src/ua-parser.js -o dist/ua-parser.min.js --comments '/^ UA/' && uglifyjs src/ua-parser.js -o dist/ua-parser.pack.js --comments '/^ UA/' --compress --mangle && node -e \"const fs=require('fs');fs.writeFileSync('src/ua-parser.mjs','// Generated ESM version of UAParser.js\\n// Source file: /src/ua-parser.js\\n\\nconst window = undefined;\\n\\n'+fs.readFileSync('src/ua-parser.js','utf-8').replace(/\\(func[\\s\\S]+strict\\';/ig,'').replace(/\\/[\\/\\s]+export[\\s\\S]+/ig,'export {UAParser};'),'utf-8');fs.writeFileSync('src/ua-parser-enum.mjs','// Generated ESM version of UAParser.js enums\\n// Source file: /src/ua-parser-enum.js\\n\\n'+fs.readFileSync('src/ua-parser-enum.js','utf-8').replace(/module\\.exports =/ig,'export'),'utf-8')\"",
|
||||||
"test": "jshint src/ua-parser.js && mocha -R nyan test",
|
"test": "jshint src/ua-parser.js && mocha -R nyan test",
|
||||||
"test-ci": "jshint src/ua-parser.js && mocha -R spec test",
|
"test-ci": "jshint src/ua-parser.js && mocha -R spec test",
|
||||||
"verup": "node ./node_modules/verup",
|
"verup": "node ./node_modules/verup",
|
||||||
|
@ -477,6 +477,14 @@ console.log('Server running at http://127.0.0.1:1337/');
|
|||||||
|
|
||||||
```js
|
```js
|
||||||
import { UAParser } from 'ua-parser-js';
|
import { UAParser } from 'ua-parser-js';
|
||||||
|
import { CPUArch, DeviceType } from 'ua-parser-js/enums';
|
||||||
|
|
||||||
|
const { cpu, device } = UAParser('Mozilla/5.0 (X11; U; Linux armv7l; en-GB; rv:1.9.2a1pre) Gecko/20090928 Firefox/3.5 Maemo Browser 1.4.1.22 RX-51 N900');
|
||||||
|
|
||||||
|
console.log(browser.name) // Maemo Browser
|
||||||
|
console.log(cpu.is(CPUArch.ARM)) // true
|
||||||
|
console.log(device.is(DeviceType.MOBILE)) // true
|
||||||
|
console.log(device.model) // N900
|
||||||
```
|
```
|
||||||
|
|
||||||
## Using TypeScript
|
## Using TypeScript
|
||||||
|
100
src/ua-parser-enum.js
Normal file
100
src/ua-parser-enum.js
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
///////////////////////////////////////////////
|
||||||
|
/* Enums for UAParser.js v2.0
|
||||||
|
https://github.com/faisalman/ua-parser-js
|
||||||
|
Author: Faisal Salman <f@faisalman.com>
|
||||||
|
MIT License */
|
||||||
|
//////////////////////////////////////////////
|
||||||
|
|
||||||
|
const BrowserName = Object.freeze({
|
||||||
|
CHROME : 'Chrome',
|
||||||
|
EDGE : 'Edge',
|
||||||
|
SAFARI : 'Safari',
|
||||||
|
FIREFOX : 'Firefox',
|
||||||
|
OPERA : 'Opera',
|
||||||
|
MOBILE_CHROME : 'Mobile Chrome',
|
||||||
|
MOBILE_SAFARI : 'Mobile Safari',
|
||||||
|
MOBILE_FIREFOX : 'Mobile Firefox',
|
||||||
|
ANDROID_BROWSER : 'Android Browser'
|
||||||
|
|
||||||
|
// TODO : test!
|
||||||
|
});
|
||||||
|
|
||||||
|
const CPUArch = Object.freeze({
|
||||||
|
IA32 : 'ia32',
|
||||||
|
AMD64 : 'amd64',
|
||||||
|
IA64 : 'ia64',
|
||||||
|
ARM : 'arm',
|
||||||
|
ARM64 : 'arm64',
|
||||||
|
ARMHF : 'armhf',
|
||||||
|
_68K : '68k',
|
||||||
|
AVR : 'avr',
|
||||||
|
IRIX : 'irix',
|
||||||
|
IRIX64 : 'irix64',
|
||||||
|
MIPS : 'mips',
|
||||||
|
MIPS64 : 'mips64',
|
||||||
|
PPC : 'ppc',
|
||||||
|
SPARC : 'sparc',
|
||||||
|
SPARC64 : 'sparc64'
|
||||||
|
});
|
||||||
|
|
||||||
|
const DeviceType = Object.freeze({
|
||||||
|
MOBILE : 'mobile',
|
||||||
|
TABLET : 'tablet',
|
||||||
|
SMARTTV : 'smarttv',
|
||||||
|
CONSOLE : 'console',
|
||||||
|
WEARABLE: 'wearable',
|
||||||
|
EMBEDDED: 'embedded'
|
||||||
|
});
|
||||||
|
|
||||||
|
const DeviceVendor = Object.freeze({
|
||||||
|
APPLE : 'Apple',
|
||||||
|
SAMSUNG : 'Samsung',
|
||||||
|
HUAWEI : 'Huawei',
|
||||||
|
XIAOMI : 'Xiaomi',
|
||||||
|
OPPO : 'OPPO',
|
||||||
|
VIVO : 'Vivo',
|
||||||
|
REALME : 'Realme',
|
||||||
|
LENOVO : 'Lenovo',
|
||||||
|
LG : 'LG'
|
||||||
|
|
||||||
|
// TODO : test!
|
||||||
|
});
|
||||||
|
|
||||||
|
const EngineName = Object.freeze({
|
||||||
|
AMAYA : 'Amaya',
|
||||||
|
BLINK : 'Blink',
|
||||||
|
EDGEHTML: 'EdgeHTML',
|
||||||
|
FLOW : 'Flow',
|
||||||
|
GECKO : 'Gecko',
|
||||||
|
GOANNA : 'Goanna',
|
||||||
|
ICAB : 'iCab',
|
||||||
|
KHTML : 'KHTML',
|
||||||
|
LINKS : 'Links',
|
||||||
|
LYNX : 'Lynx',
|
||||||
|
NETFRONT: 'NetFront',
|
||||||
|
NETSURF : 'NetSurf',
|
||||||
|
PRESTO : 'Presto',
|
||||||
|
TASMAN : 'Tasman',
|
||||||
|
TRIDENT : 'Trident',
|
||||||
|
W3M : 'w3m',
|
||||||
|
WEBKIT : 'WebKit'
|
||||||
|
});
|
||||||
|
|
||||||
|
const OSName = Object.freeze({
|
||||||
|
WINDOWS : 'Windows',
|
||||||
|
LINUX : 'Linux',
|
||||||
|
MACOS : 'macOS',
|
||||||
|
IOS : 'iOS',
|
||||||
|
ANDROID : 'Android'
|
||||||
|
|
||||||
|
// TODO : test!
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
BrowserName,
|
||||||
|
CPUArch,
|
||||||
|
DeviceType,
|
||||||
|
DeviceVendor,
|
||||||
|
EngineName,
|
||||||
|
OSName
|
||||||
|
}
|
103
src/ua-parser-enum.mjs
Normal file
103
src/ua-parser-enum.mjs
Normal file
@ -0,0 +1,103 @@
|
|||||||
|
// Generated ESM version of UAParser.js enums
|
||||||
|
// Source file: /src/ua-parser-enum.js
|
||||||
|
|
||||||
|
///////////////////////////////////////////////
|
||||||
|
/* Enums for UAParser.js v2.0
|
||||||
|
https://github.com/faisalman/ua-parser-js
|
||||||
|
Author: Faisal Salman <f@faisalman.com>
|
||||||
|
MIT License */
|
||||||
|
//////////////////////////////////////////////
|
||||||
|
|
||||||
|
const BrowserName = Object.freeze({
|
||||||
|
CHROME : 'Chrome',
|
||||||
|
EDGE : 'Edge',
|
||||||
|
SAFARI : 'Safari',
|
||||||
|
FIREFOX : 'Firefox',
|
||||||
|
OPERA : 'Opera',
|
||||||
|
MOBILE_CHROME : 'Mobile Chrome',
|
||||||
|
MOBILE_SAFARI : 'Mobile Safari',
|
||||||
|
MOBILE_FIREFOX : 'Mobile Firefox',
|
||||||
|
ANDROID_BROWSER : 'Android Browser'
|
||||||
|
|
||||||
|
// TODO : test!
|
||||||
|
});
|
||||||
|
|
||||||
|
const CPUArch = Object.freeze({
|
||||||
|
IA32 : 'ia32',
|
||||||
|
AMD64 : 'amd64',
|
||||||
|
IA64 : 'ia64',
|
||||||
|
ARM : 'arm',
|
||||||
|
ARM64 : 'arm64',
|
||||||
|
ARMHF : 'armhf',
|
||||||
|
_68K : '68k',
|
||||||
|
AVR : 'avr',
|
||||||
|
IRIX : 'irix',
|
||||||
|
IRIX64 : 'irix64',
|
||||||
|
MIPS : 'mips',
|
||||||
|
MIPS64 : 'mips64',
|
||||||
|
PPC : 'ppc',
|
||||||
|
SPARC : 'sparc',
|
||||||
|
SPARC64 : 'sparc64'
|
||||||
|
});
|
||||||
|
|
||||||
|
const DeviceType = Object.freeze({
|
||||||
|
MOBILE : 'mobile',
|
||||||
|
TABLET : 'tablet',
|
||||||
|
SMARTTV : 'smarttv',
|
||||||
|
CONSOLE : 'console',
|
||||||
|
WEARABLE: 'wearable',
|
||||||
|
EMBEDDED: 'embedded'
|
||||||
|
});
|
||||||
|
|
||||||
|
const DeviceVendor = Object.freeze({
|
||||||
|
APPLE : 'Apple',
|
||||||
|
SAMSUNG : 'Samsung',
|
||||||
|
HUAWEI : 'Huawei',
|
||||||
|
XIAOMI : 'Xiaomi',
|
||||||
|
OPPO : 'OPPO',
|
||||||
|
VIVO : 'Vivo',
|
||||||
|
REALME : 'Realme',
|
||||||
|
LENOVO : 'Lenovo',
|
||||||
|
LG : 'LG'
|
||||||
|
|
||||||
|
// TODO : test!
|
||||||
|
});
|
||||||
|
|
||||||
|
const EngineName = Object.freeze({
|
||||||
|
AMAYA : 'Amaya',
|
||||||
|
BLINK : 'Blink',
|
||||||
|
EDGEHTML: 'EdgeHTML',
|
||||||
|
FLOW : 'Flow',
|
||||||
|
GECKO : 'Gecko',
|
||||||
|
GOANNA : 'Goanna',
|
||||||
|
ICAB : 'iCab',
|
||||||
|
KHTML : 'KHTML',
|
||||||
|
LINKS : 'Links',
|
||||||
|
LYNX : 'Lynx',
|
||||||
|
NETFRONT: 'NetFront',
|
||||||
|
NETSURF : 'NetSurf',
|
||||||
|
PRESTO : 'Presto',
|
||||||
|
TASMAN : 'Tasman',
|
||||||
|
TRIDENT : 'Trident',
|
||||||
|
W3M : 'w3m',
|
||||||
|
WEBKIT : 'WebKit'
|
||||||
|
});
|
||||||
|
|
||||||
|
const OSName = Object.freeze({
|
||||||
|
WINDOWS : 'Windows',
|
||||||
|
LINUX : 'Linux',
|
||||||
|
MACOS : 'macOS',
|
||||||
|
IOS : 'iOS',
|
||||||
|
ANDROID : 'Android'
|
||||||
|
|
||||||
|
// TODO : test!
|
||||||
|
});
|
||||||
|
|
||||||
|
export {
|
||||||
|
BrowserName,
|
||||||
|
CPUArch,
|
||||||
|
DeviceType,
|
||||||
|
DeviceVendor,
|
||||||
|
EngineName,
|
||||||
|
OSName
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
import { UAParser } from '../src/ua-parser.mjs'
|
import { UAParser } from 'ua-parser-js';
|
||||||
import * as assert from 'assert'
|
import { CPUArch, DeviceType, EngineName } from 'ua-parser-js/enums';
|
||||||
|
import * as assert from 'assert';
|
||||||
|
|
||||||
describe('Returns', () => {
|
describe('Returns', () => {
|
||||||
it('getResult() should returns object', () => {
|
it('getResult() should returns object', () => {
|
||||||
@ -15,3 +16,12 @@ describe('Returns', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('Enums', () => {
|
||||||
|
it('Can use enum', () => {
|
||||||
|
const { cpu, device, engine } = UAParser('Mozilla/5.0 (X11; U; Linux armv7l; en-GB; rv:1.9.2a1pre) Gecko/20090928 Firefox/3.5 Maemo Browser 1.4.1.22 RX-51 N900');
|
||||||
|
assert.strictEqual(cpu.is(CPUArch.ARM), true);
|
||||||
|
assert.strictEqual(device.is(DeviceType.MOBILE), true);
|
||||||
|
assert.strictEqual(engine.is(EngineName.GECKO), true);
|
||||||
|
});
|
||||||
|
});
|
Loading…
x
Reference in New Issue
Block a user