index.js
5.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
var licenseIDs = require('spdx-license-ids');
function valid(string) {
return licenseIDs.indexOf(string) > -1;
}
// Common transpositions of license identifier acronyms
var transpositions = [
['APGL', 'AGPL'],
['Gpl', 'GPL'],
['GLP', 'GPL'],
['APL', 'Apache'],
['ISD', 'ISC'],
['GLP', 'GPL'],
['IST', 'ISC'],
['Claude', 'Clause'],
[' or later', '+'],
[' International', ''],
['GNU', 'GPL'],
['GUN', 'GPL'],
['+', ''],
['GNU GPL', 'GPL'],
['GNU/GPL', 'GPL'],
['GNU GLP', 'GPL'],
['GNU General Public License', 'GPL'],
['Gnu public license', 'GPL'],
['GNU Public License', 'GPL'],
['GNU GENERAL PUBLIC LICENSE', 'GPL'],
['MTI', 'MIT'],
['Mozilla Public License', 'MPL'],
['WTH', 'WTF'],
['-License', '']
];
var TRANSPOSED = 0;
var CORRECT = 1;
// Simple corrections to nearly valid identifiers.
var transforms = [
// e.g. 'mit'
function(argument) {
return argument.toUpperCase();
},
// e.g. 'MIT '
function(argument) {
return argument.trim();
},
// e.g. 'M.I.T.'
function(argument) {
return argument.replace(/\./g, '');
},
// e.g. 'Apache- 2.0'
function(argument) {
return argument.replace(/\s+/g, '');
},
// e.g. 'CC BY 4.0''
function(argument) {
return argument.replace(/\s+/g, '-');
},
// e.g. 'LGPLv2.1'
function(argument) {
return argument.replace('v', '-');
},
// e.g. 'Apache 2.0'
function(argument) {
return argument.replace(/,?\s*(\d)/, '-$1');
},
// e.g. 'GPL 2'
function(argument) {
return argument.replace(/,?\s*(\d)/, '-$1.0');
},
// e.g. 'Apache Version 2.0'
function(argument) {
return argument.replace(/,?\s*(V\.|v\.|V|v|Version|version)\s*(\d)/, '-$2');
},
// e.g. 'Apache Version 2'
function(argument) {
return argument.replace(/,?\s*(V\.|v\.|V|v|Version|version)\s*(\d)/, '-$2.0');
},
// e.g. 'ZLIB'
function(argument) {
return argument[0].toUpperCase() + argument.slice(1);
},
// e.g. 'MPL/2.0'
function(argument) {
return argument.replace('/', '-');
},
// e.g. 'Apache 2'
function(argument) {
return argument
.replace(/\s*V\s*(\d)/, '-$1')
.replace(/(\d)$/, '$1.0');
},
// e.g. 'GPL-2.0-'
function(argument) {
return argument.slice(0, argument.length - 1);
},
// e.g. 'GPL2'
function(argument) {
return argument.replace(/(\d)$/, '-$1.0');
},
// e.g. 'BSD 3'
function(argument) {
return argument.replace(/(-| )?(\d)$/, '-$2-Clause');
},
// e.g. 'BSD clause 3'
function(argument) {
return argument.replace(/(-| )clause(-| )(\d)/, '-$3-Clause');
},
// e.g. 'BY-NC-4.0'
function(argument) {
return 'CC-' + argument;
},
// e.g. 'BY-NC'
function(argument) {
return 'CC-' + argument + '-4.0';
},
// e.g. 'Attribution-NonCommercial'
function(argument) {
return argument
.replace('Attribution', 'BY')
.replace('NonCommercial', 'NC')
.replace('NoDerivatives', 'ND')
.replace(/ (\d)/, '-$1')
.replace(/ ?International/, '');
},
// e.g. 'Attribution-NonCommercial'
function(argument) {
return 'CC-' +
argument
.replace('Attribution', 'BY')
.replace('NonCommercial', 'NC')
.replace('NoDerivatives', 'ND')
.replace(/ (\d)/, '-$1')
.replace(/ ?International/, '') +
'-4.0';
}
];
// If all else fails, guess that strings containing certain substrings
// meant to identify certain licenses.
var lastResorts = [
['UNLI', 'Unlicense'],
['WTF', 'WTFPL'],
['2 CLAUSE', 'BSD-2-Clause'],
['2-CLAUSE', 'BSD-2-Clause'],
['3 CLAUSE', 'BSD-3-Clause'],
['3-CLAUSE', 'BSD-3-Clause'],
['AFFERO', 'AGPL-3.0'],
['AGPL', 'AGPL-3.0'],
['APACHE', 'Apache-2.0'],
['ARTISTIC', 'Artistic-2.0'],
['Affero', 'AGPL-3.0'],
['BEER', 'Beerware'],
['BOOST', 'BSL-1.0'],
['BSD', 'BSD-2-Clause'],
['ECLIPSE', 'EPL-1.0'],
['FUCK', 'WTFPL'],
['GNU', 'GPL-3.0'],
['LGPL', 'LGPL-3.0'],
['GPL', 'GPL-3.0'],
['MIT', 'MIT'],
['MPL', 'MPL-2.0'],
['X11', 'X11'],
['ZLIB', 'Zlib']
];
var SUBSTRING = 0;
var IDENTIFIER = 1;
var validTransformation = function(identifier) {
for (var i = 0; i < transforms.length; i++) {
var transformed = transforms[i](identifier);
if (transformed !== identifier && valid(transformed)) {
return transformed;
}
}
return null;
};
var validLastResort = function(identifier) {
var upperCased = identifier.toUpperCase();
for (var i = 0; i < lastResorts.length; i++) {
var lastResort = lastResorts[i];
if (upperCased.indexOf(lastResort[SUBSTRING]) > -1) {
return lastResort[IDENTIFIER];
}
}
return null;
};
var anyCorrection = function(identifier, check) {
for (var i = 0; i < transpositions.length; i++) {
var transposition = transpositions[i];
var transposed = transposition[TRANSPOSED];
if (identifier.indexOf(transposed) > -1) {
var corrected = identifier.replace(
transposed,
transposition[CORRECT]
);
var checked = check(corrected);
if (checked !== null) {
return checked;
}
}
}
return null;
};
module.exports = function(identifier) {
identifier = identifier.replace(/\+$/, '');
if (valid(identifier)) {
return identifier;
}
var transformed = validTransformation(identifier);
if (transformed !== null) {
return transformed;
}
transformed = anyCorrection(identifier, function(argument) {
if (valid(argument)) {
return argument;
}
return validTransformation(argument);
});
if (transformed !== null) {
return transformed;
}
transformed = validLastResort(identifier);
if (transformed !== null) {
return transformed;
}
transformed = anyCorrection(identifier, validLastResort);
if (transformed !== null) {
return transformed;
}
return null;
};