Skip to content
This repository has been archived by the owner on Sep 1, 2023. It is now read-only.

Commit

Permalink
fixed #8, add new api
Browse files Browse the repository at this point in the history
  • Loading branch information
yorkie committed Oct 18, 2014
1 parent 59ee276 commit c7b2936
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 48 deletions.
16 changes: 7 additions & 9 deletions README.md
Expand Up @@ -9,27 +9,25 @@

### API

##### `.Parse([method, options])`
##### `.Parse(filename[, properties])`

* `method` must be one of `pattern` and `trie`, default value is: `pattern`.
* `filename` {String} your 51degrees data, lite or premium.

* `options` must be an object

* `options.filename`: your 51degrees data, lite or premium

* `options.properties`: required properties
* `options` {Array} optional, required properties

for more information, you could move to [51degrees documentation](https://51degrees.com/Support/Documentation)

##### `parser.parse(userAgent)`

* `userAgent` {String}

parse the `userAgent` given by you, and return result of that.

### Usage

```js
var Parser = require('51degrees').Parser;
var psr = new Parser('trie');
var psr = new Parser('51Degrees-Lite.dat');
var userAgent = '...'; // your userAgent in any clients(browser/ios/android)
var ret = psr.parse(userAgent);
console.log(ret);
Expand Down Expand Up @@ -62,7 +60,7 @@ After the above program, you will get:
```js
var properties = require('51degrees').ALL_PROPERTIES;
var userAgent = '...' // your userAgent in any clients(browser/ios/android)
var psr = new Parser('trie', {properties: properties});
var psr = new Parser('51Degrees-Lite.trie', properties);
var ret = psr.parse(userAgent);
console.log(ret);
```
Expand Down
2 changes: 1 addition & 1 deletion benchmark/test-gc.js
@@ -1,6 +1,6 @@
var Parser = require('../index').Parser;
var trieParser = new Parser('trie');
var patternParser = new Parser('pattern');
var patternParser = new Parser('51Degrees-Lite.dat');
var csv = require('csv');
var fs = require("fs");
var path = require('path');
Expand Down
18 changes: 13 additions & 5 deletions benchmark/test-gc2.js
@@ -1,12 +1,20 @@
var path = require('path');
var fs = require('fs');
var Parser = require('../index').Parser;
var properties = ["DeviceType","HardwareFamily","HardwareModel","PlatformName","BrowserVendor","BrowserName","BrowserVersion","IsCrawler","PriceBand","HasTouchScreen"];

var psr = new Parser('pattern', {
properties: properties
});
var properties = [
"DeviceType",
"HardwareFamily",
"HardwareModel",
"PlatformName",
"BrowserVendor",
"BrowserName",
"BrowserVersion",
"IsCrawler",
"PriceBand",
"HasTouchScreen"
];

var psr = new Parser('51Degrees-Lite.dat', properties);
var ua_array = fs.readFileSync(path.join(__dirname, './ua.txt')).toString().split("\n");

var doIt = function(array, cb) {
Expand Down
2 changes: 1 addition & 1 deletion benchmark/test-perf.js
@@ -1,6 +1,6 @@
var Parser = require('../index').Parser;
var trieParser = new Parser('trie');
var patternParser = new Parser('pattern');
var patternParser = new Parser('51Degrees-Lite.dat');
var csv = require('csv');
var fs = require("fs");
var path = require('path');
Expand Down
2 changes: 1 addition & 1 deletion example.js
Expand Up @@ -4,7 +4,7 @@ var Parser = require('./index').Parser;
var properties = require('./index').ALL_PROPERTIES;
var userAgent = 'Mozilla/5.0 (Linux; U; Android 4.2.2; en-us; KFTHWI Build/JDQ39) AppleWebKit/537.36 (KHTML, like Gecko) Silk/3.30 like Chrome/34.0.1847.137 Safari/537.36';

var parser = new Parser('trie', {properties: properties});
var parser = new Parser('51Degrees-Lite.trie', properties);
var ret = parser.parse(userAgent);

console.log(ret);
52 changes: 24 additions & 28 deletions index.js
Expand Up @@ -26,11 +26,10 @@ defined by the Mozilla Public License, v. 2.0.
*/

var util = require('util');
var path = require('path');
var TrieParser = require('./build/Release/trie.node').TrieParser;
var PatternParser = require('./build/Release/pattern.node').PatternParser;
var defaultOptions = {
filename: './51Degrees-Lite',
properties: [
var defaultProperties = [
'Id',
'Canvas',
'CssTransforms',
Expand All @@ -44,39 +43,37 @@ var defaultOptions = {
'Svg',
'TouchEvents',
'WebWorkers'
]
};
];
var extensions = {
'pattern': '.dat',
'trie': '.trie'
};

function Parser(method, options) {
if (!(this instanceof Parser)) {
return new Parser(method, options);
}
if (arguments.length === 1 && typeof method !== 'string') {
options = method;
method = 'pattern';
}
options = options || {};
options.filename = options.filename || defaultOptions.filename;
options.properties = defaultOptions.properties.concat(options.properties || []);
function Parser(filename, properties) {
if (!(this instanceof Parser))
return new Parser(name, options);

if (typeof filename !== 'string')
throw new Error('data filename required');

for (var key in defaultOptions) {
if (options[key] === undefined)
options[key] = defaultOptions[key];
}
if (!properties || properties.length === 0)
properties = properties || defaultProperties;

this._properties = options.properties;
if (method === 'pattern') {
if (!util.isArray(properties))
throw new Error('properties must be an array');

var extname = path.extname(filename);
if (extname === '.trie') {
this.method = 'trie';
this._parser = new TrieParser(filename, properties.join(','));
} else if (extname === '.dat') {
this.method = 'pattern';
this._parser = new PatternParser(filename, properties.join(','));
} else if (extname === '') {
this.method = 'pattern';
this._filename = options.filename + extensions['pattern'];
this._parser = new PatternParser(this._filename, this._properties.join(','));
this._parser = new PatternParser(filename + '.dat', properties.join(','));
} else {
this.method = 'trie';
this._filename = options.filename + extensions['trie'];
this._parser = new TrieParser(this._filename, this._properties.join(','));
throw new Error('could not find data file: ' + filename);
}
}

Expand All @@ -92,7 +89,6 @@ function capitaliseFirstLetter(str) {
}

exports.Parser = Parser;

exports.ALL_PROPERTIES = [
'AnimationTiming',
'BlobBuilder',
Expand Down
6 changes: 3 additions & 3 deletions test.js
Expand Up @@ -31,7 +31,7 @@ var properties = require('./index').ALL_PROPERTIES;
var userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.94 Safari/537.36';

test('pattern', function(t) {
var parser = new Parser('pattern', {properties: properties});
var parser = new Parser('51Degrees-Lite.dat', properties);
var ret = parser.parse(userAgent);
properties.forEach(function(property) {
t.ok(typeof ret[property] !== undefined, property + '> ok');
Expand All @@ -45,7 +45,7 @@ test('pattern overflow', function(t) {
for (var i=0; i<1000; i++) {
ua[i] = 50;
}
var parser = new Parser('pattern', {properties: properties});
var parser = new Parser('51Degrees-Lite', properties);
var throwed = false;
try { parser.parse(ua); }
catch (e) {
Expand All @@ -56,7 +56,7 @@ test('pattern overflow', function(t) {
});

test('trie', function(t) {
var parser = new Parser('trie', {properties: properties});
var parser = new Parser('51Degrees-Lite.trie', properties);
var ret = parser.parse(userAgent);
properties.forEach(function(property) {
t.ok(typeof ret[property] !== undefined, property + '> ok');
Expand Down

0 comments on commit c7b2936

Please sign in to comment.