-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
60 lines (54 loc) · 1.62 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
var arrayFindIndex = require('array-find-index')
var parse = require('spdx-expression-parse')
var ranges = require('spdx-ranges')
var notALicenseIdentifier = ' is not a simple license identifier'
var rangeComparison = function (comparison) {
return function (first, second) {
var firstAST = parse(first)
if (!firstAST.hasOwnProperty('license')) {
throw new Error('"' + first + '"' + notALicenseIdentifier)
}
var secondAST = parse(second)
if (!secondAST.hasOwnProperty('license')) {
throw new Error('"' + second + '"' + notALicenseIdentifier)
}
return ranges.some(function (range) {
var firstLicense = firstAST.license
var indexOfFirst = arrayFindIndex(range, function (element) {
return (
element === firstLicense ||
(
Array.isArray(element) &&
element.indexOf(firstLicense) !== -1
)
)
})
if (indexOfFirst < 0) {
return false
}
var secondLicense = secondAST.license
var indexOfSecond = arrayFindIndex(range, function (element) {
return (
element === secondLicense ||
(
Array.isArray(element) &&
element.indexOf(secondLicense) !== -1
)
)
})
if (indexOfSecond < 0) {
return false
}
return comparison(indexOfFirst, indexOfSecond)
})
}
}
exports.gt = rangeComparison(function (first, second) {
return first > second
})
exports.lt = rangeComparison(function (first, second) {
return first < second
})
exports.eq = rangeComparison(function (first, second) {
return first === second
})