Permalink
Browse files

regexp replacements on returns, pattern example and added docs

  • Loading branch information...
Edward Hotchkiss
Edward Hotchkiss committed Nov 30, 2011
1 parent aafa81d commit 291235c694611cd62cdb7c52312e6f5011069e78
Showing with 44 additions and 6 deletions.
  1. +14 −0 README.md
  2. +18 −0 examples/patterns.js
  3. +11 −5 lib/version.js
  4. +1 −1 package.json
View
@@ -21,4 +21,18 @@ version.fetch('express', function(error, version) {
console.log('express current version: ', version);
};
});
+```
+
+### Example (RegExp formating)
+
+```javascript
+var version = require('version');
+
+version.fetch('express', { from : /^/gi, to : "v" }, function(error, version) {
+ if (error) {
+ console.error(error);
+ } else {
+ console.log('express current version with formating:', version);
+ };
+});
```
View
@@ -0,0 +1,18 @@
+
+/*
+ pattern/replace `version` example
+ fetch current version of express, and use a RegExp with
+ a string to search/replace the version before returning it
+*/
+
+var version = require('version');
+
+version.fetch('express', { from : /^/gi, to : "v" }, function(error, version) {
+ if (error) {
+ console.error(error);
+ } else {
+ console.log('express current version with formating:', version);
+ };
+});
+
+/* EOF */
View
@@ -7,7 +7,7 @@
var version = module.exports;
-// mikeal's enhanced http.request
+// enhanced http.request
var request = require('request');
/*
@@ -18,11 +18,12 @@ var request = require('request');
@returns {Object} error {String} version
*/
-version.fetch = function(name, callback, options) {
- this.fetchNode(name, callback);
+version.fetch = function(name, options, callback) {
+ this.fetchNode(name, options, callback);
};
-version.fetchNode = function(name, callback) {
+version.fetchNode = function(name, options, callback) {
+ callback = callback || options;
request('https://registry.npmjs.org/' + name + '/latest', function(error, response, body) {
if (error) {
callback(error, null);
@@ -32,7 +33,12 @@ version.fetchNode = function(name, callback) {
try {
data = JSON.parse(body);
var _version = data['version'];
- callback(null, _version);
+ if (options && options.from && options.to) {
+ _version = _version.replace(options.from, options.to);
+ callback(null, _version);
+ } else {
+ callback(null, _version);
+ };
} catch(error) {
callback(error, null);
}
View
@@ -2,7 +2,7 @@
"author": "Edward Hotchkiss <e@ingk.com>",
"name": "Version",
"description": "NodeJS package.json version number fetcher",
- "version": "0.0.2",
+ "version": "0.0.3",
"repository": {
"type": "git",
"url": "git://github.com/edwardhotchkiss/version.git"

0 comments on commit 291235c

Please sign in to comment.