Comparing version 0.1.5 to 1.0.0
64
index.js
@@ -1,9 +0,7 @@ | ||
'use strict'; | ||
'use strict' | ||
module.exports = parse; | ||
parse.format = format; | ||
module.exports = parse | ||
parse.format = format | ||
var semver = require('semver'); | ||
var REGEX_PARSE_ID = /^((?:[^\/])+?)(?:@([^\/]+))?(\/.*)?$/; | ||
var REGEX_PARSE_ID = /^((?:[^\/])+?)(?:@([^\/]+))?(\/.*)?$/ | ||
// On android 2.2, | ||
@@ -16,41 +14,45 @@ // `[^\/]+?` will fail to do the lazy match, but `(?:[^\/]+?)` works. | ||
if (!id) { | ||
throw new TypeError('`id` must be a string.'); | ||
throw new TypeError('`id` must be a string.') | ||
} | ||
var match = id.match(REGEX_PARSE_ID); | ||
var name = match[1]; | ||
var match = id.match(REGEX_PARSE_ID) | ||
var name = match[1] | ||
// 'a/inner' -> 'a@latest/inner' | ||
var version = match[2] | ||
var path = match[3] || ''; | ||
var range; | ||
var path = match[3] || '' | ||
if (version) { | ||
if (!semver.valid(version)) { | ||
range = version; | ||
version = undefined; | ||
} | ||
} | ||
// There always be matches | ||
return { | ||
name: name, | ||
version: version, | ||
range: range, | ||
path: path | ||
}; | ||
}; | ||
return new Pkg(name, version, path) | ||
} | ||
function Pkg (name, version, path) { | ||
this.name = name | ||
this.version = version | ||
this.path = path | ||
} | ||
Pkg.prototype.format = function (){ | ||
return format(this) | ||
} | ||
Pkg.prototype.normalize_url = function () { | ||
return this.name + '/' | ||
+ (this.version || '*') | ||
+ (this.path || '/' + this.name + '.js') | ||
} | ||
function format (obj) { | ||
if (Object(obj) !== obj) { | ||
throw new TypeError('`obj` must be an object.'); | ||
throw new TypeError('`obj` must be an object.') | ||
} | ||
var version = obj.version || obj.range; | ||
var version_slice = version | ||
? '@' + version | ||
: ''; | ||
var version_slice = obj.version | ||
? '@' + obj.version | ||
: '' | ||
return obj.name + version_slice + (obj.path || ''); | ||
return obj.name + version_slice + (obj.path || '') | ||
} |
{ | ||
"name": "neuron-pkg", | ||
"version": "0.1.5", | ||
"version": "1.0.0", | ||
"description": "Parses neuron package id into name, version and path.", | ||
@@ -14,2 +14,3 @@ "main": "index.js", | ||
"keywords": [ | ||
"commonjs", | ||
"neuron", | ||
@@ -37,4 +38,3 @@ "module", | ||
"dependencies": { | ||
"semver": "^5.0.0" | ||
} | ||
} |
[](http://badge.fury.io/js/neuron-pkg) | ||
[](https://travis-ci.org/kaelzhang/neuron-pkg) | ||
[](https://gemnasium.com/kaelzhang/neuron-pkg) | ||
@@ -45,3 +44,3 @@ # neuron-pkg | ||
- obj `Object` | ||
- obj `Object` | ||
@@ -48,0 +47,0 @@ The reverse method of `pkg(id)` |
@@ -1,5 +0,5 @@ | ||
'use strict'; | ||
'use strict' | ||
var expect = require('chai').expect; | ||
var pkg = require('../'); | ||
var expect = require('chai').expect | ||
var pkg = require('../') | ||
@@ -9,79 +9,82 @@ var cases = [ | ||
id: 'a', | ||
obj: { | ||
name: 'a', | ||
version: undefined, | ||
range: undefined, | ||
path: '' | ||
} | ||
name: 'a', | ||
version: undefined, | ||
path: '', | ||
url: 'a/*/a.js' | ||
}, | ||
{ | ||
id: 'a@1.1.0', | ||
obj: { | ||
name: 'a', | ||
version: '1.1.0', | ||
range: undefined, | ||
path: '' | ||
} | ||
name: 'a', | ||
version: '1.1.0', | ||
path: '', | ||
url: 'a/1.1.0/a.js' | ||
}, | ||
{ | ||
id: 'a@1.1.0/a', | ||
obj: { | ||
name: 'a', | ||
version: '1.1.0', | ||
range: undefined, | ||
path: '/a' | ||
} | ||
name: 'a', | ||
version: '1.1.0', | ||
path: '/a', | ||
url: 'a/1.1.0/a' | ||
}, | ||
{ | ||
id: 'a@~1.1.0/a', | ||
obj: { | ||
name: 'a', | ||
range: '~1.1.0', | ||
version: undefined, | ||
path: '/a' | ||
} | ||
name: 'a', | ||
version: '~1.1.0', | ||
path: '/a', | ||
url: 'a/~1.1.0/a' | ||
}, | ||
{ | ||
id: 'a/a', | ||
obj: { | ||
name: 'a', | ||
range: undefined, | ||
version: undefined, | ||
path: '/a' | ||
} | ||
name: 'a', | ||
version: undefined, | ||
path: '/a', | ||
url: 'a/*/a' | ||
}, | ||
{ | ||
id: 'a@hahah/a', | ||
obj: { | ||
name: 'a', | ||
range: 'hahah', | ||
version: undefined, | ||
path: '/a' | ||
} | ||
name: 'a', | ||
version: 'hahah', | ||
path: '/a', | ||
url: 'a/hahah/a' | ||
}, | ||
{ | ||
id: 'a/a.css', | ||
name: 'a', | ||
version: undefined, | ||
path: '/a.css', | ||
url: 'a/*/a.css' | ||
} | ||
]; | ||
] | ||
function expect_type (type, actual, expected) { | ||
expect(type + ':' + actual).to.equal(type + ':' + expected) | ||
} | ||
cases.forEach(function (c) { | ||
describe(c.id, function(){ | ||
it("pkg(id)", function(){ | ||
expect(pkg(c.id)).to.deep.equal(c.obj); | ||
}); | ||
var p = pkg(c.id) | ||
expect_type('name', p.name, c.name) | ||
expect_type('version', p.version, c.version) | ||
expect_type('path', p.path, c.path) | ||
expect_type('.format()', p.format(), c.id) | ||
expect_type('format(pkg)', pkg.format(p), c.id) | ||
expect_type('.normalize_url()', p.normalize_url(p), c.url) | ||
}) | ||
}) | ||
}) | ||
it("pkg.format(obj)", function(){ | ||
expect(pkg.format(c.obj)).to.equal(c.id); | ||
}); | ||
}); | ||
}); | ||
describe("error", function(){ | ||
describe("error", function () { | ||
it("should throw error if id is not a string", function(){ | ||
var error; | ||
var error | ||
try { | ||
pkg(); | ||
pkg() | ||
} catch(e) { | ||
error = e; | ||
error = e | ||
} | ||
expect(error).not.to.equal(); | ||
}); | ||
}); | ||
expect(error).not.to.equal() | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
5870
0
1
52
- Removedsemver@^5.0.0
- Removedsemver@5.7.2(transitive)