-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
infer-license.js
38 lines (32 loc) · 1003 Bytes
/
infer-license.js
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
/* @flow */
import LICENSES from './licenses.js';
function clean(str: string): string {
return str.replace(/[^A-Za-z\s]/g, ' ').replace(/[\s]+/g, ' ').trim().toLowerCase();
}
const REGEXES: {[key: string]: Array<RegExp>} = {
Apache: [/Apache License\b/],
BSD: [/BSD\b/],
ISC: [/The ISC License/, /ISC\b/],
MIT: [/MIT\b/],
Unlicense: [/http:\/\/unlicense.org\//],
WTFPL: [/DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE/, /WTFPL\b/],
};
export default function inferLicense(license: string): ?string {
// check if we have any explicit licenses
const cleanLicense = clean(license);
for (const licenseName in LICENSES) {
const testLicense = LICENSES[licenseName];
if (cleanLicense.search(testLicense) >= 0) {
return licenseName;
}
}
// infer based on some keywords
for (const licenseName in REGEXES) {
for (const regex of REGEXES[licenseName]) {
if (license.search(regex) >= 0) {
return `${licenseName}*`;
}
}
}
return null;
}