1 var licenseIDs = require('spdx-license-ids');
3 function valid(string) {
4 return licenseIDs.indexOf(string) > -1;
7 // Common transpositions of license identifier acronyms
18 [' International', ''],
25 ['GNU General Public License', 'GPL'],
26 ['Gnu public license', 'GPL'],
27 ['GNU Public License', 'GPL'],
28 ['GNU GENERAL PUBLIC LICENSE', 'GPL'],
30 ['Mozilla Public License', 'MPL'],
38 // Simple corrections to nearly valid identifiers.
42 return argument.toUpperCase();
46 return argument.trim();
50 return argument.replace(/\./g, '');
54 return argument.replace(/\s+/g, '');
58 return argument.replace(/\s+/g, '-');
62 return argument.replace('v', '-');
66 return argument.replace(/,?\s*(\d)/, '-$1');
70 return argument.replace(/,?\s*(\d)/, '-$1.0');
72 // e.g. 'Apache Version 2.0'
74 return argument.replace(/,?\s*(V\.|v\.|V|v|Version|version)\s*(\d)/, '-$2');
76 // e.g. 'Apache Version 2'
78 return argument.replace(/,?\s*(V\.|v\.|V|v|Version|version)\s*(\d)/, '-$2.0');
82 return argument[0].toUpperCase() + argument.slice(1);
86 return argument.replace('/', '-');
91 .replace(/\s*V\s*(\d)/, '-$1')
92 .replace(/(\d)$/, '$1.0');
96 return argument.slice(0, argument.length - 1);
100 return argument.replace(/(\d)$/, '-$1.0');
104 return argument.replace(/(-| )?(\d)$/, '-$2-Clause');
106 // e.g. 'BSD clause 3'
108 return argument.replace(/(-| )clause(-| )(\d)/, '-$3-Clause');
112 return 'CC-' + argument;
116 return 'CC-' + argument + '-4.0';
118 // e.g. 'Attribution-NonCommercial'
121 .replace('Attribution', 'BY')
122 .replace('NonCommercial', 'NC')
123 .replace('NoDerivatives', 'ND')
124 .replace(/ (\d)/, '-$1')
125 .replace(/ ?International/, '');
127 // e.g. 'Attribution-NonCommercial'
131 .replace('Attribution', 'BY')
132 .replace('NonCommercial', 'NC')
133 .replace('NoDerivatives', 'ND')
134 .replace(/ (\d)/, '-$1')
135 .replace(/ ?International/, '') +
140 // If all else fails, guess that strings containing certain substrings
141 // meant to identify certain licenses.
143 ['UNLI', 'Unlicense'],
145 ['2 CLAUSE', 'BSD-2-Clause'],
146 ['2-CLAUSE', 'BSD-2-Clause'],
147 ['3 CLAUSE', 'BSD-3-Clause'],
148 ['3-CLAUSE', 'BSD-3-Clause'],
149 ['AFFERO', 'AGPL-3.0'],
150 ['AGPL', 'AGPL-3.0'],
151 ['APACHE', 'Apache-2.0'],
152 ['ARTISTIC', 'Artistic-2.0'],
153 ['Affero', 'AGPL-3.0'],
154 ['BEER', 'Beerware'],
155 ['BOOST', 'BSL-1.0'],
156 ['BSD', 'BSD-2-Clause'],
157 ['ECLIPSE', 'EPL-1.0'],
160 ['LGPL', 'LGPL-3.0'],
171 var validTransformation = function(identifier) {
172 for (var i = 0; i < transforms.length; i++) {
173 var transformed = transforms[i](identifier);
174 if (transformed !== identifier && valid(transformed)) {
181 var validLastResort = function(identifier) {
182 var upperCased = identifier.toUpperCase();
183 for (var i = 0; i < lastResorts.length; i++) {
184 var lastResort = lastResorts[i];
185 if (upperCased.indexOf(lastResort[SUBSTRING]) > -1) {
186 return lastResort[IDENTIFIER];
192 var anyCorrection = function(identifier, check) {
193 for (var i = 0; i < transpositions.length; i++) {
194 var transposition = transpositions[i];
195 var transposed = transposition[TRANSPOSED];
196 if (identifier.indexOf(transposed) > -1) {
197 var corrected = identifier.replace(
199 transposition[CORRECT]
201 var checked = check(corrected);
202 if (checked !== null) {
210 module.exports = function(identifier) {
211 identifier = identifier.replace(/\+$/, '');
212 if (valid(identifier)) {
215 var transformed = validTransformation(identifier);
216 if (transformed !== null) {
219 transformed = anyCorrection(identifier, function(argument) {
220 if (valid(argument)) {
223 return validTransformation(argument);
225 if (transformed !== null) {
228 transformed = validLastResort(identifier);
229 if (transformed !== null) {
232 transformed = anyCorrection(identifier, validLastResort);
233 if (transformed !== null) {