diff --git a/lib/utils/funding.js b/lib/utils/funding.js index 2c994e0b6b42..dce40147642c 100644 --- a/lib/utils/funding.js +++ b/lib/utils/funding.js @@ -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 } @@ -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() @@ -82,7 +88,7 @@ function getFundingInfo (idealTree, opts) { } if (funding && validFundingUrl(funding)) { - fundingItem.funding = funding + fundingItem.funding = retrieveFunding(funding) length++ } @@ -134,7 +140,7 @@ function getFundingInfo (idealTree, opts) { } if (idealTree && idealTree.funding) { - result.funding = idealTree.funding + result.funding = retrieveFunding(idealTree.funding) } result.dependencies = diff --git a/test/tap/fund.js b/test/tap/fund.js index cc66bea51a05..364dc1b6f817 100644 --- a/test/tap/fund.js +++ b/test/tap/fund.js @@ -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' }) }) }) diff --git a/test/tap/utils.funding.js b/test/tap/utils.funding.js index 60a7a1e67eb5..51b89e5f8d34 100644 --- a/test/tap/utils.funding.js +++ b/test/tap/utils.funding.js @@ -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, @@ -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',