-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add function to return pacakge purl
Co-authored-by: Jordan Harband <[email protected]> Signed-off-by: Brian DeHamer <[email protected]>
- Loading branch information
Showing
3 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,16 @@ included then the default is `latest`. | |
|
||
**Throws** if the package name is invalid, a dist-tag is invalid or a URL's protocol is not supported. | ||
|
||
### var purl = npa.toPurl(*arg*, *reg*) | ||
|
||
Returns the [purl (package URL)](https://github.com/package-url/purl-spec) form of the given pacakge name/spec. | ||
|
||
* *arg* - A package/version string. For example: `[email protected]` or `@bar/[email protected]`. | ||
* *reg* - Optionally the URL to the package registry. If not specified, assumes the default | ||
`https://registry.npmjs.org`. | ||
|
||
**Throws** if the package name is invalid, or the supplied arg can't be resolved to a purl. | ||
|
||
## RESULT OBJECT | ||
|
||
The objects that are returned by npm-package-arg contain the following | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
'use strict' | ||
var test = require('tap').test | ||
var npa = require('..') | ||
|
||
test('toPurl - valid', function (t) { | ||
// Unscoped package | ||
t.equal(npa.toPurl('[email protected]'), 'pkg:npm/[email protected]') | ||
|
||
// Scoped package | ||
t.equal( | ||
npa.toPurl('@foo/[email protected]'), | ||
'pkg:npm/%40foo/[email protected]') | ||
|
||
// Non-default registry | ||
t.equal( | ||
npa.toPurl({ name: '@foo/bar', rawSpec: '1.0.0' }, 'https://npm.pkg.github.com'), | ||
'pkg:npm/%40foo/[email protected]?repository_url=https://npm.pkg.github.com' | ||
) | ||
|
||
// Default registry | ||
t.equal( | ||
npa.toPurl({ name: '@foo/bar', rawSpec: '1.0.0' }, 'https://registry.npmjs.org'), | ||
'pkg:npm/%40foo/[email protected]' | ||
) | ||
|
||
t.end() | ||
}) | ||
|
||
test('toPurl - invalid', function (t) { | ||
// Invalid version | ||
t.throws(() => npa.toPurl({ name: 'foo/bar', rawSpec: '1.0.0' }), { | ||
code: 'EINVALIDPACKAGENAME', | ||
}) | ||
|
||
// Invalid version | ||
t.throws(() => npa.toPurl('[email protected]'), { | ||
code: 'EINVALIDPURLTYPE', | ||
}) | ||
|
||
// Invalid type | ||
t.throws(() => npa.toPurl('git+ssh://[email protected]/user/foo#1.2.3'), { | ||
code: 'EINVALIDPURLTYPE', | ||
}) | ||
|
||
t.end() | ||
}) |