diff options
author | Yuchen Pei <hi@ypei.me> | 2022-07-29 11:51:13 +1000 |
---|---|---|
committer | Yuchen Pei <hi@ypei.me> | 2022-07-29 11:51:13 +1000 |
commit | 2401ca4d60e56b2160b1163c056b94a5642dd7b2 (patch) | |
tree | 6baf99a435d35a1e85de8b38bfc518f09bddfeea | |
parent | dc7a197b2827760992d2052a3dc397f52c293de2 (diff) |
clean up bg/externalicenses.js sans optimizedocument
fixing a bug introduced in the previous commit too
-rw-r--r-- | bg/ExternalLicenses.js | 48 |
1 files changed, 15 insertions, 33 deletions
diff --git a/bg/ExternalLicenses.js b/bg/ExternalLicenses.js index fa02ae5..16ea52f 100644 --- a/bg/ExternalLicenses.js +++ b/bg/ExternalLicenses.js @@ -26,23 +26,14 @@ 'use strict'; const { licenses } = require('../license_definitions') -const licensesByLabel = Object.entries(licenses).reduce((acc, [id, license]) => { - return { - ...acc, - [license.identifier.toUpperCase()]: license, - [id.toUpperCase()]: license, - [license.licenseName.toUpperCase()]: license - }; -}, {}); -const licensesByUrl = Object.entries(licenses).reduce((acc, [_, license]) => { - return { - ...acc, - ...(license.canonicalUrl.reduce((result, url) => { - return { ...result, [url]: license } - }, {})) - }; -}, {}); - +const licensesByLabel = new Map(Object.entries(licenses).map(([id, license]) => + [ + [license.identifier.toUpperCase(), license], + [id.toUpperCase(), license], + [license.licenseName.toUpperCase(), license] + ]).flat()); +const licensesByUrl = new Map(Object.values(licenses).map(license => + license.canonicalUrl.map(url => [url, license])).flat()); for (const [id, license] of Object.entries(licenses)) { if (!license.identifier) { license.identifier = id; @@ -70,28 +61,19 @@ const ExternalLicenses = { if (!(scriptInfo && scriptInfo.licenseLinks.length)) { return null; } - scriptInfo.licenses = new Set(); + scriptInfo.licenses = new Set(scriptInfo.licenseLinks.map( + ({ label, url }) => { + const uLabel = label.trim().toUpperCase(); + const license = licensesByLabel.get(uLabel) || licensesByUrl.get(url) || + licensesByLabel.get(uLabel.replace(/^GNU-|-(?:OR-LATER|ONLY)$/, '')); + return license ? [license] : []; + }).flat()); scriptInfo.toString = () => { const licenseIds = [...this.licenses].map(l => l.identifier).sort().join(', '); return licenseIds ? `Free license${this.licenses.size > 1 ? 's' : ''} (${licenseIds})` : 'Unknown license(s)'; } - const match = (map, key) => { - console.debug(map); - if (map.has(key)) { - scriptInfo.licenses.add(map.get(key)); - return true; - } - return false; - }; - - for (const { label, url } of scriptInfo.licenseLinks) { - const uLabel = label.trim().toUpperCase(); - match(licensesByLabel, uLabel) || - match(licensesByUrl, url) || - match(licensesByLabel, uLabel.replace(/^GNU-|-(?:OR-LATER|ONLY)$/, '')); - } scriptInfo.free = scriptInfo.licenses.size > 0; return scriptInfo; }, |