Skip to content

Commit

Permalink
Fix #300 : Provide enums - initial work
Browse files Browse the repository at this point in the history
  • Loading branch information
faisalman committed Mar 28, 2023
1 parent ba28d33 commit af65fd6
Show file tree
Hide file tree
Showing 5 changed files with 230 additions and 3 deletions.
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,21 @@
"main": "src/ua-parser.js",
"module": "src/ua-parser.mjs",
"exports": {
"." : {
"require": "./src/ua-parser.js",
"import": "./src/ua-parser.mjs"
},
"./enums" : {
"require": "./src/ua-parser-enum.js",
"import": "./src/ua-parser-enum.mjs"
}
},
"files": [
"dist",
"src"
],
"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-ci": "jshint src/ua-parser.js && mocha -R spec test",
"verup": "node ./node_modules/verup",
Expand Down
8 changes: 8 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,14 @@ console.log('Server running at http://127.0.0.1:1337/');

```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
Expand Down
100 changes: 100 additions & 0 deletions src/ua-parser-enum.js
Original file line number Diff line number Diff line change
@@ -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 changes: 103 additions & 0 deletions src/ua-parser-enum.mjs
Original file line number Diff line number Diff line change
@@ -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
}
14 changes: 12 additions & 2 deletions test/es6-test.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { UAParser } from '../src/ua-parser.mjs'
import * as assert from 'assert'
import { UAParser } from 'ua-parser-js';
import { CPUArch, DeviceType, EngineName } from 'ua-parser-js/enums';
import * as assert from 'assert';

describe('Returns', () => {
it('getResult() should returns object', () => {
Expand All @@ -14,4 +15,13 @@ describe('Returns', () => {
os: { name: undefined, version: undefined }
});
});
});

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);
});
});

0 comments on commit af65fd6

Please sign in to comment.