Skip to content

Commit

Permalink
Close #17 PR: Add support for ^1.2.3 and ~1.2.3 versions. Fixes #16
Browse files Browse the repository at this point in the history
  • Loading branch information
rsp authored and sindresorhus committed Dec 9, 2015
1 parent 6f9a9da commit 1137b9e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
10 changes: 10 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
var got = require('got');
var registryUrl = require('registry-url');
var rc = require('rc');
var semver = require('semver');

module.exports = function (name, version) {
var scope = name.split('/')[0];
Expand Down Expand Up @@ -29,6 +30,15 @@ module.exports = function (name, version) {
if (version === 'latest') {
data = data.versions[data['dist-tags'].latest];
} else if (version) {
if (!data.versions[version]) {
var versions = Object.keys(data.versions);
version = semver.maxSatisfying(versions, version);

if (!version) {
throw new Error('Version doesn\'t exist');
}
}

data = data.versions[version];

if (!data) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"dependencies": {
"got": "^5.0.0",
"rc": "^1.1.2",
"registry-url": "^3.0.3"
"registry-url": "^3.0.3",
"semver": "^5.1.0"
},
"devDependencies": {
"ava": "*",
Expand Down
6 changes: 6 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ packageJson('@company/package', 'latest').then(json => {
You can optionally specify a version (e.g. `1.0.0`) or `latest`.
If you don't specify a version you'll get the [main entry](http://registry.npmjs.org/pageres/) containing all versions.

The version can also be in any format supported by the [semver](https://www.npmjs.com/package/semver) module. For example:

- `1` - get the latest `1.x.x`
- `1.2` - get the latest `1.2.x`
- `^1.2.3` - get the latest `1.x.x` but at least `1.2.3`
- `~1.2.3` - get the latest `1.2.x` but at least `1.2.3`

## Related

Expand Down
5 changes: 5 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ test('specific version', async t => {
t.is(json.version, '0.1.0');
});

test('incomplete version x', async t => {
const json = await fn('pageres', '0');
t.is(json.version.substr(0,2), '0.');
});

test('scoped - full', async t => {
const json = await fn('@sindresorhus/df');
t.is(json.name, '@sindresorhus/df');
Expand Down

0 comments on commit 1137b9e

Please sign in to comment.