Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(fund): support funding string shorthand
In the approved RFC it was documented that `npm fund` should also
support a shorthand version of the `funding` property using only a
string instead of an object.

This commit fixes it and adds tests to ensure its behavior.

PR-URL: #472
Credit: @ruyadorno
Close: #472
Reviewed-by: @claudiahdz
  • Loading branch information
ruyadorno authored and claudiahdz committed Nov 13, 2019
1 parent 9c7161d commit 938d612
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 9 deletions.
18 changes: 12 additions & 6 deletions lib/utils/funding.js
Expand Up @@ -8,12 +8,10 @@ exports.validFundingUrl = validFundingUrl
// Is the value of a `funding` property of a `package.json`
// a valid type+url for `npm fund` to display?
function validFundingUrl (funding) {
if (!funding || !funding.url) {
return false
}
if (!funding) return false

try {
var parsed = new URL(funding.url)
var parsed = new URL(funding.url || funding)
} catch (error) {
return false
}
Expand Down Expand Up @@ -62,6 +60,14 @@ function getFundingInfo (idealTree, opts) {
)
}

function retrieveFunding (funding) {
return typeof funding === 'string'
? {
url: funding
}
: funding
}

function getFundingDependencies (tree) {
const deps = tree && tree.dependencies
if (!deps) return empty()
Expand All @@ -82,7 +88,7 @@ function getFundingInfo (idealTree, opts) {
}

if (funding && validFundingUrl(funding)) {
fundingItem.funding = funding
fundingItem.funding = retrieveFunding(funding)
length++
}

Expand Down Expand Up @@ -134,7 +140,7 @@ function getFundingInfo (idealTree, opts) {
}

if (idealTree && idealTree.funding) {
result.funding = idealTree.funding
result.funding = retrieveFunding(idealTree.funding)
}

result.dependencies =
Expand Down
4 changes: 1 addition & 3 deletions test/tap/fund.js
Expand Up @@ -92,9 +92,7 @@ const fixture = new Tacks(Dir({
node_modules: Dir({
'sub-bar': getFixturePackage({
name: 'sub-bar',
funding: {
url: 'https://example.com/sponsor'
}
funding: 'https://example.com/sponsor'
})
})
})
Expand Down
67 changes: 67 additions & 0 deletions test/tap/utils.funding.js
Expand Up @@ -35,6 +35,28 @@ test('single item missing funding', (t) => {
t.end()
})

test('funding object missing url', (t) => {
t.deepEqual(
getFundingInfo({ name: 'project',
dependencies: {
'single-item': {
name: 'single-item',
version: '1.0.0',
funding: {
type: 'Foo'
}
}
}}),
{
name: 'project',
dependencies: {},
length: 0
},
'should return empty list'
)
t.end()
})

test('use path if name is missing', (t) => {
t.deepEqual(
getFundingInfo({ name: undefined,
Expand Down Expand Up @@ -86,6 +108,51 @@ test('single item tree', (t) => {
t.end()
})

test('top-level funding info', (t) => {
t.deepEqual(
getFundingInfo({ name: 'project',
funding: 'http://example.com'
}),
{
name: 'project',
funding: {
url: 'http://example.com'
},
dependencies: {},
length: 0
},
'should return top-level item with normalized funding info'
)
t.end()
})

test('use string shorthand', (t) => {
t.deepEqual(
getFundingInfo({ name: 'project',
dependencies: {
'single-item': {
name: 'single-item',
version: '1.0.0',
funding: 'http://example.com'
}
}}),
{
name: 'project',
dependencies: {
'single-item': {
version: '1.0.0',
funding: {
url: 'http://example.com'
}
}
},
length: 1
},
'should return item with normalized funding info'
)
t.end()
})

test('duplicate items along the tree', (t) => {
t.deepEqual(
getFundingInfo({ name: 'project',
Expand Down

0 comments on commit 938d612

Please sign in to comment.