Skip to content

Commit 938d612

Browse files
ruyadornoclaudiahdz
authored andcommitted
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
1 parent 9c7161d commit 938d612

File tree

3 files changed

+80
-9
lines changed

3 files changed

+80
-9
lines changed

lib/utils/funding.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@ exports.validFundingUrl = validFundingUrl
88
// Is the value of a `funding` property of a `package.json`
99
// a valid type+url for `npm fund` to display?
1010
function validFundingUrl (funding) {
11-
if (!funding || !funding.url) {
12-
return false
13-
}
11+
if (!funding) return false
1412

1513
try {
16-
var parsed = new URL(funding.url)
14+
var parsed = new URL(funding.url || funding)
1715
} catch (error) {
1816
return false
1917
}
@@ -62,6 +60,14 @@ function getFundingInfo (idealTree, opts) {
6260
)
6361
}
6462

63+
function retrieveFunding (funding) {
64+
return typeof funding === 'string'
65+
? {
66+
url: funding
67+
}
68+
: funding
69+
}
70+
6571
function getFundingDependencies (tree) {
6672
const deps = tree && tree.dependencies
6773
if (!deps) return empty()
@@ -82,7 +88,7 @@ function getFundingInfo (idealTree, opts) {
8288
}
8389

8490
if (funding && validFundingUrl(funding)) {
85-
fundingItem.funding = funding
91+
fundingItem.funding = retrieveFunding(funding)
8692
length++
8793
}
8894

@@ -134,7 +140,7 @@ function getFundingInfo (idealTree, opts) {
134140
}
135141

136142
if (idealTree && idealTree.funding) {
137-
result.funding = idealTree.funding
143+
result.funding = retrieveFunding(idealTree.funding)
138144
}
139145

140146
result.dependencies =

test/tap/fund.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,7 @@ const fixture = new Tacks(Dir({
9292
node_modules: Dir({
9393
'sub-bar': getFixturePackage({
9494
name: 'sub-bar',
95-
funding: {
96-
url: 'https://example.com/sponsor'
97-
}
95+
funding: 'https://example.com/sponsor'
9896
})
9997
})
10098
})

test/tap/utils.funding.js

+67
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,28 @@ test('single item missing funding', (t) => {
3535
t.end()
3636
})
3737

38+
test('funding object missing url', (t) => {
39+
t.deepEqual(
40+
getFundingInfo({ name: 'project',
41+
dependencies: {
42+
'single-item': {
43+
name: 'single-item',
44+
version: '1.0.0',
45+
funding: {
46+
type: 'Foo'
47+
}
48+
}
49+
}}),
50+
{
51+
name: 'project',
52+
dependencies: {},
53+
length: 0
54+
},
55+
'should return empty list'
56+
)
57+
t.end()
58+
})
59+
3860
test('use path if name is missing', (t) => {
3961
t.deepEqual(
4062
getFundingInfo({ name: undefined,
@@ -86,6 +108,51 @@ test('single item tree', (t) => {
86108
t.end()
87109
})
88110

111+
test('top-level funding info', (t) => {
112+
t.deepEqual(
113+
getFundingInfo({ name: 'project',
114+
funding: 'http://example.com'
115+
}),
116+
{
117+
name: 'project',
118+
funding: {
119+
url: 'http://example.com'
120+
},
121+
dependencies: {},
122+
length: 0
123+
},
124+
'should return top-level item with normalized funding info'
125+
)
126+
t.end()
127+
})
128+
129+
test('use string shorthand', (t) => {
130+
t.deepEqual(
131+
getFundingInfo({ name: 'project',
132+
dependencies: {
133+
'single-item': {
134+
name: 'single-item',
135+
version: '1.0.0',
136+
funding: 'http://example.com'
137+
}
138+
}}),
139+
{
140+
name: 'project',
141+
dependencies: {
142+
'single-item': {
143+
version: '1.0.0',
144+
funding: {
145+
url: 'http://example.com'
146+
}
147+
}
148+
},
149+
length: 1
150+
},
151+
'should return item with normalized funding info'
152+
)
153+
t.end()
154+
})
155+
89156
test('duplicate items along the tree', (t) => {
90157
t.deepEqual(
91158
getFundingInfo({ name: 'project',

0 commit comments

Comments
 (0)