-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
142 lines (129 loc) · 4.36 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
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
var compare = require('spdx-compare')
var parse = require('spdx-expression-parse')
var ranges = require('spdx-ranges')
function rangesAreCompatible (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
)
})
)
}
function identifierInRange (identifier, range) {
return (
identifier.license === range.license ||
compare.gt(identifier.license, range.license) ||
compare.eq(identifier.license, range.license)
)
}
function licensesAreCompatible (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 replaceGPLOnlyOrLaterWithRanges (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('-only', '')
delete argument.plus
}
} else if (argument.left && argument.right) {
argument.left = replaceGPLOnlyOrLaterWithRanges(argument.left)
argument.right = replaceGPLOnlyOrLaterWithRanges(argument.right)
}
return argument
}
function endsWith (string, substring) {
return string.indexOf(substring) === string.length - substring.length
}
function licenseString (e) {
if (e.hasOwnProperty('noassertion')) return 'NOASSERTION'
if (e.license) {
return (
e.license +
(e.plus ? '+' : '') +
(e.exception ? ('WITH ' + e.exception) : '')
)
}
}
// Expand the given expression into an equivalent array where each member is an array of licenses AND'd
// together and the members are OR'd together. For example, `(MIT OR ISC) AND GPL-3.0` expands to
// `[[GPL-3.0 AND MIT], [ISC AND MIT]]`. Note that within each array of licenses, the entries are
// normalized (sorted) by license name.
function expand (expression) {
return sort(expandInner(expression))
}
function expandInner (expression) {
if (!expression.conjunction) return [{ [licenseString(expression)]: expression }]
if (expression.conjunction === 'or') return expandInner(expression.left).concat(expandInner(expression.right))
if (expression.conjunction === 'and') {
var left = expandInner(expression.left)
var right = expandInner(expression.right)
return left.reduce(function (result, l) {
right.forEach(function (r) { result.push(Object.assign({}, l, r)) })
return result
}, [])
}
}
function sort (licenseList) {
var sortedLicenseLists = licenseList
.filter(function (e) { return Object.keys(e).length })
.map(function (e) { return Object.keys(e).sort() })
return sortedLicenseLists.map(function (list, i) {
return list.map(function (license) { return licenseList[i][license] })
})
}
function isANDCompatible (parsedExpression, parsedLicenses) {
return parsedExpression.every(function (element) {
return parsedLicenses.some(function (approvedLicense) {
return licensesAreCompatible(element, approvedLicense)
})
})
}
function satisfies (spdxExpression, arrayOfLicenses) {
var parsedExpression = expand(replaceGPLOnlyOrLaterWithRanges(parse(spdxExpression)))
var parsedLicenses = arrayOfLicenses.map(function (l) { return replaceGPLOnlyOrLaterWithRanges(parse(l)) })
for (const parsed of parsedLicenses) {
if (parsed.hasOwnProperty('conjunction')) {
throw new Error('Approved licenses cannot be AND or OR expressions.')
}
}
return parsedExpression.some(function (o) { return isANDCompatible(o, parsedLicenses) })
}
module.exports = satisfies