Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added maor parsers

  • Loading branch information...
commit f7e983cdebd731b6c9f0a605f076a7cfc83d17c5 1 parent 52e8ac8
@3rd-Eden authored
Showing with 99 additions and 17 deletions.
  1. +3 −0  .npmignore
  2. +52 −1 lib/useragent.js
  3. +44 −16 tests/parser.test.js
View
3  .npmignore
@@ -0,0 +1,3 @@
+tests
+benchmark
+node_modules
View
53 lib/useragent.js
@@ -165,6 +165,7 @@ parse.os_length = parse.os.length;
* Int3rNetz when we want to. We will be using the compiled version by default
* but users can opt-in for updates.
*
+ * @param {Boolean} refresh Refresh the dataset from the remote
* @api public
*/
@@ -220,6 +221,40 @@ function is (useragent) {
}
/**
+ * Converts the result of Agent.toString() back to a new Agent.
+ *
+ * @param {String} prettyAgent
+ * @returns {Agent}
+ * @api public
+ */
+
+function fromString (prettyAgent) {
+ var parts = prettyAgent.split(' / ')
+ , partition = parts[0].split(' ')
+ , versions = partition[1].split('.');
+
+ // return a new user agent instance
+ return new Agent(partition[0]
+ , versions[0]
+ , versions[1]
+ , partition.length == 3 ? partitition[2] : versions[2]
+ , parts.length == 2 ? parts[1] : null
+ );
+}
+
+/**
+ * Transforms a JSON structure back to a new Agent
+ *
+ * @param {Object} data The JSON output of a Agent.toJSON()
+ * @returns {Agent}
+ * @api public
+ */
+
+function fromJSON (data) {
+ return new Agent(data.family, data.major, data.minor, data.patch, data.os);
+}
+
+/**
* Library version.
*/
@@ -242,7 +277,23 @@ updater.Agent = Agent;
updater.parse = parse;
/**
- * Expose our quick useragent tester
+ * Expose our agent string parser.
+ *
+ * @api public
+ */
+
+updater.fromString = fromString;
+
+/**
+ * Expose our agent json parser.
+ *
+ * @api public
+ */
+
+updater.fromJSON = fromJSON;
+
+/**
+ * Expose our quick useragent tester.
*
* @api public
*/
View
60 tests/parser.test.js
@@ -28,24 +28,52 @@ module.exports = {
agent.toJSON().should.equal('{"family":"Other","major":"0","minor":"0","patch":"0","os":"Other"}');
},
- 'correctly parse chromes user agent': function () {
- var agent = useragent.parse(ua);
-
- agent.family.should.equal('Chrome');
- agent.major.should.equal('15');
- agent.minor.should.equal('0');
- agent.patch.should.equal('874');
-
- agent.os.should.equal('Mac OS X');
- agent.toVersion().should.equal('15.0.874');
- agent.toString().should.equal('Chrome 15.0.874 / Mac OS X');
- agent.toAgent().should.equal('Chrome 15.0.874');
- agent.toJSON().should.equal('{"family":"Chrome","major":"15","minor":"0","patch":"874","os":"Mac OS X"}');
- },
+ 'correctly parse chromes user agent': function () {
+ var agent = useragent.parse(ua);
+
+ agent.family.should.equal('Chrome');
+ agent.major.should.equal('15');
+ agent.minor.should.equal('0');
+ agent.patch.should.equal('874');
+
+ agent.os.should.equal('Mac OS X');
+ agent.toVersion().should.equal('15.0.874');
+ agent.toString().should.equal('Chrome 15.0.874 / Mac OS X');
+ agent.toAgent().should.equal('Chrome 15.0.874');
+ agent.toJSON().should.equal('{"family":"Chrome","major":"15","minor":"0","patch":"874","os":"Mac OS X"}');
+ },
- 'simple detections': function () {
+ 'simple detections': function () {
useragent.is(ua).chrome.should.be_true;
useragent.is(ua).webkit.should.be_true;
useragent.is(ua).firefox.should.be_false;
- }
+ },
+
+ 'reparse from agent string': function () {
+ var agent = useragent.fromString('Chrome 15.0.874 / Mac OS X');
+
+ agent.family.should.equal('Chrome');
+ agent.major.should.equal('15');
+ agent.minor.should.equal('0');
+ agent.patch.should.equal('874');
+
+ agent.os.should.equal('Mac OS X');
+ agent.toVersion().should.equal('15.0.874');
+ agent.toString().should.equal('Chrome 15.0.874 / Mac OS X');
+ agent.toAgent().should.equal('Chrome 15.0.874');
+ agent.toJSON().should.equal('{"family":"Chrome","major":"15","minor":"0","patch":"874","os":"Mac OS X"}');
+ },
+
+ 'reparse from agent string without operating system': function () {
+ var agent = useragent.fromString('Chrome 15.0.874');
+
+ agent.family.should.equal('Chrome');
+ agent.major.should.equal('15');
+ agent.minor.should.equal('0');
+ agent.patch.should.equal('874');
+
+ agent.os.should.equal('Other');
+ agent.toVersion().should.equal('15.0.874');
+ agent.toAgent().should.equal('Chrome 15.0.874');
+ }
};
Please sign in to comment.
Something went wrong with that request. Please try again.