Skip to content

Commit

Permalink
use most permissive license color (badges#2196)
Browse files Browse the repository at this point in the history
* use most permissive license color

* revert to possible non-array licenseToColor argument
  • Loading branch information
pnowaczyk authored and platan committed Oct 20, 2018
1 parent 6f589a7 commit 4554f49
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
27 changes: 21 additions & 6 deletions lib/licenses.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const licenseTypes = {
'Zlib',
],
color: 'green',
priority: '2',
},
// copyleft licenses require 'Disclose source' (https://choosealicense.com/appendix/#disclose-source)
// or 'Same license' (https://choosealicense.com/appendix/#same-license)
Expand All @@ -43,24 +44,40 @@ const licenseTypes = {
'OSL-3.0',
],
color: 'orange',
priority: '1',
},
// public domain licenses do not require 'License and copyright notice' (https://choosealicense.com/appendix/#include-copyright)
'public-domain': {
spdxLicenseIds: ['CC0-1.0', 'Unlicense', 'WTFPL'],
color: '7cd958',
priority: '3',
},
}

const licenseToColorMap = {}
Object.keys(licenseTypes).forEach(licenseType => {
const { spdxLicenseIds, color } = licenseTypes[licenseType]
const { spdxLicenseIds, color, priority } = licenseTypes[licenseType]
spdxLicenseIds.forEach(license => {
licenseToColorMap[license] = color
licenseToColorMap[license] = { color, priority }
})
})
const defaultLicenseColor = 'lightgrey'
const licenseToColor = spdxId =>
licenseToColorMap[spdxId] || defaultLicenseColor
const licenseToColor = spdxId => {
if (!Array.isArray(spdxId)) {
return (
(licenseToColorMap[spdxId] && licenseToColorMap[spdxId].color) ||
defaultLicenseColor
)
}
const licenseType = spdxId
.filter(i => licenseToColorMap[i])
.map(i => licenseToColorMap[i])
.reduce((a, b) => (a.priority > b.priority ? a : b), {
color: defaultLicenseColor,
priority: 0,
})
return licenseType.color
}

function renderLicenseBadge({ license, licenses }) {
if (licenses === undefined) {
Expand All @@ -73,8 +90,6 @@ function renderLicenseBadge({ license, licenses }) {

return {
message: licenses.join(', '),
// TODO This does not provide a color when more than one license is
// present. Probably that should be fixed.
color: licenseToColor(licenses),
}
}
Expand Down
12 changes: 11 additions & 1 deletion lib/licenses.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ describe('license helpers', function() {
given('Unlicense').expect('7cd958')
given('unknown-license').expect('lightgrey')
given(null).expect('lightgrey')

given(['CC0-1.0', 'MPL-2.0']).expect('7cd958')
given(['MPL-2.0', 'CC0-1.0']).expect('7cd958')
given(['MIT', 'MPL-2.0']).expect('green')
given(['MPL-2.0', 'MIT']).expect('green')
given(['OFL-1.1', 'MPL-2.0']).expect('orange')
given(['MPL-2.0', 'OFL-1.1']).expect('orange')
given(['CC0-1.0', 'MIT', 'MPL-2.0']).expect('7cd958')
given(['UNKNOWN-1.0', 'MIT']).expect('green')
given(['UNKNOWN-1.0', 'UNKNOWN-2.0']).expect('lightgrey')
})

test(renderLicenseBadge, () => {
Expand All @@ -30,7 +40,7 @@ describe('license helpers', function() {
})
given({ licenses: ['MPL-2.0', 'MIT'] }).expect({
message: 'MPL-2.0, MIT',
color: 'lightgrey',
color: 'green',
})
})
})

0 comments on commit 4554f49

Please sign in to comment.