Skip to content

Commit

Permalink
regexp replacements on returns, pattern example and added docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Edward Hotchkiss committed Nov 30, 2011
1 parent aafa81d commit 291235c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
14 changes: 14 additions & 0 deletions README.md
Expand Up @@ -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);
};
});
```
18 changes: 18 additions & 0 deletions examples/patterns.js
@@ -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 */
16 changes: 11 additions & 5 deletions lib/version.js
Expand Up @@ -7,7 +7,7 @@

var version = module.exports;

// mikeal's enhanced http.request
// enhanced http.request
var request = require('request');

/*
Expand All @@ -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);
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -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"
Expand Down

0 comments on commit 291235c

Please sign in to comment.