Skip to content

Commit

Permalink
Merge 2b28326 into 99c778e
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Oct 18, 2017
2 parents 99c778e + 2b28326 commit 0363408
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
9 changes: 9 additions & 0 deletions npa.js
Expand Up @@ -18,6 +18,15 @@ const isFilename = /[.](?:tgz|tar.gz|tar)$/i
function npa (arg, where) {
let name
let spec
if (typeof arg === 'object' && (!(arg instanceof Result) || where)) {
if (arg.name && arg.rawSpec) {
return npa.resolve(arg.name, arg.rawSpec, where || arg.where)
} else {
return npa(arg.raw, where || arg.where)
}
} else if (typeof arg === 'object') {
return arg
}
const nameEndsAt = arg[0] === '@' ? arg.slice(1).indexOf('@') + 1 : arg.indexOf('@')
const namePart = nameEndsAt > 0 ? arg.slice(0, nameEndsAt) : arg
if (isURL.test(arg)) {
Expand Down
11 changes: 11 additions & 0 deletions test/basic.js
Expand Up @@ -420,6 +420,17 @@ require('tap').test('basic', function (t) {
// t.has(res, tests[arg], arg + ' matches expectations')
})

var objSpec = {name: 'foo', rawSpec: '1.2.3'}
t.equal(npa(objSpec, '/whatnot').toString(), 'foo@1.2.3', 'parsed object')

objSpec = {raw: './foo/bar', where: '/here'}
t.equal(npa(objSpec).fetchSpec, '/here/foo/bar', '`where` is reused')

objSpec = {raw: './foo/bar', where: '/here'}
t.equal(npa(objSpec, '/whatnot').fetchSpec, '/whatnot/foo/bar', '`where` arg overrides the one in the spec object')

t.equal(npa(npa('foo@1.2.3')).toString(), 'foo@1.2.3', 'spec is passthrough')

// Completely unreasonable invalid garbage throws an error
t.throws(function () {
t.comment(npa('this is not a \0 valid package name or url'))
Expand Down

0 comments on commit 0363408

Please sign in to comment.