Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(fund): support funding string shorthand #472

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I would change this syntax to a one liner; { url: funding }. When I read this now it makes me think it's a block scope and something interesting is happening.

}
: 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