Skip to content

Commit ba0ae6a

Browse files
authoredOct 18, 2017
handle case where arg is an object but not an npa instance (#29)
1 parent 99c778e commit ba0ae6a

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed
 

‎npa.js

+9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ const isFilename = /[.](?:tgz|tar.gz|tar)$/i
1818
function npa (arg, where) {
1919
let name
2020
let spec
21+
if (typeof arg === 'object') {
22+
if (arg instanceof Result && (!where || where === arg.where)) {
23+
return arg
24+
} else if (arg.name && arg.rawSpec) {
25+
return npa.resolve(arg.name, arg.rawSpec, where || arg.where)
26+
} else {
27+
return npa(arg.raw, where || arg.where)
28+
}
29+
}
2130
const nameEndsAt = arg[0] === '@' ? arg.slice(1).indexOf('@') + 1 : arg.indexOf('@')
2231
const namePart = nameEndsAt > 0 ? arg.slice(0, nameEndsAt) : arg
2332
if (isURL.test(arg)) {

‎test/basic.js

+16
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,22 @@ require('tap').test('basic', function (t) {
420420
// t.has(res, tests[arg], arg + ' matches expectations')
421421
})
422422

423+
var objSpec = {name: 'foo', rawSpec: '1.2.3'}
424+
t.equal(npa(objSpec, '/whatnot').toString(), 'foo@1.2.3', 'parsed object')
425+
426+
objSpec = {raw: './foo/bar', where: '/here'}
427+
t.equal(npa(objSpec).fetchSpec, '/here/foo/bar', '`where` is reused')
428+
429+
objSpec = {raw: './foo/bar', where: '/here'}
430+
t.equal(npa(objSpec, '/whatnot').fetchSpec, '/whatnot/foo/bar', '`where` arg overrides the one in the spec object')
431+
432+
t.equal(npa(npa('foo@1.2.3')).toString(), 'foo@1.2.3', 'spec is passthrough')
433+
434+
var parsedSpec = npa('./foo', './here')
435+
t.equal(npa(parsedSpec), parsedSpec, 'reused if no where')
436+
t.equal(npa(parsedSpec, './here'), parsedSpec, 'reused if where matches')
437+
t.notEqual(npa(parsedSpec, './there'), parsedSpec, 'new instance if where does not match')
438+
t.notEqual(npa(parsedSpec, './there').fetchSpec, '/there/foo', 'new instance has new where')
423439
// Completely unreasonable invalid garbage throws an error
424440
t.throws(function () {
425441
t.comment(npa('this is not a \0 valid package name or url'))

0 commit comments

Comments
 (0)
Please sign in to comment.