-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
110 lines (102 loc) · 3.17 KB
/
index.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
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
var compare = require('spdx-compare')
var ranges = require('spdx-ranges')
module.exports = function (first, second) {
if (!Array.isArray(second)) throw new Error('second argument must be an Array')
if (second.some(function (element) {
return !element.hasOwnProperty('license')
})) throw new Error('second argument must contain license expression AST leaves')
if (
!first.hasOwnProperty('license') &&
!first.hasOwnProperty('conjunction')
) throw new Error('first argument must be a license expression AST')
var terms = normalizeGPLIdentifiers(first)
var whitelist = second.map(function (element) {
return normalizeGPLIdentifiers(element)
})
return recurse(terms, whitelist)
}
var rangesAreCompatible = function (first, second) {
return (
first.license === second.license ||
ranges.some(function (range) {
return (
licenseInRange(first.license, range) &&
licenseInRange(second.license, range)
)
})
)
}
function licenseInRange (license, range) {
return (
range.indexOf(license) !== -1 ||
range.some(function (element) {
return (
Array.isArray(element) &&
element.indexOf(license) !== -1
)
})
)
}
var identifierInRange = function (identifier, range) {
return (
identifier.license === range.license ||
compare.gt(identifier.license, range.license) ||
compare.eq(identifier.license, range.license)
)
}
var licensesAreCompatible = function (first, second) {
if (first.exception !== second.exception) {
return false
} else if (second.hasOwnProperty('license')) {
if (second.hasOwnProperty('plus')) {
if (first.hasOwnProperty('plus')) {
// first+, second+
return rangesAreCompatible(first, second)
} else {
// first, second+
return identifierInRange(first, second)
}
} else {
if (first.hasOwnProperty('plus')) {
// first+, second
return identifierInRange(second, first)
} else {
// first, second
return first.license === second.license
}
}
}
}
function normalizeGPLIdentifiers (argument) {
var license = argument.license
if (license) {
if (endsWith(license, '-or-later')) {
argument.license = license.replace('-or-later', '')
argument.plus = true
} else if (endsWith(license, '-only')) {
argument.license = license.replace('-or-later', '')
delete argument.plus
}
} else if (argument.left && argument.right) {
argument.left = normalizeGPLIdentifiers(argument.left)
argument.right = normalizeGPLIdentifiers(argument.right)
}
return argument
}
function endsWith (string, substring) {
return string.indexOf(substring) === string.length - 1
}
function recurse (terms, whitelist) {
var conjunction = terms.conjunction
if (!conjunction) {
return whitelist.some(function (whitelisted) {
return licensesAreCompatible(terms, whitelisted)
})
} else if (conjunction === 'or') {
return recurse(terms.left, whitelist) || recurse(terms.right, whitelist)
} else if (conjunction === 'and') {
return recurse(terms.left, whitelist) && recurse(terms.right, whitelist)
} else {
throw new Error('invalid terms')
}
}