summaryrefslogtreecommitdiff
path: root/deps/npm/node_modules/spdx/source/spdx.js
blob: 5aece3d5437511a9b347817ee081027f21fad18a (plain)
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
// spdx.js
// =======
// SPDX License Expression Syntax parser

// Validation
// ----------

// Require the generated parser.
var parser = require('./parser.generated.js').parser;

exports.parse = function(argument) {
  return parser.parse(argument);
};

var containsRepeatedSpace = /\s{2,}/;

exports.valid = function(argument) {
  if (
    argument.trim() !== argument ||
    containsRepeatedSpace.test(argument)
  ) {
    return false;
  }
  try {
    parser.parse(argument);
    return true;
  } catch (e) {
    // jison generates parsers that throw errors, while this function
    // mimics `semver.valid` by returning null.
    return null;
  }
};

// Comparison
// ----------

var ranges = require('./ranges.json');

var notALicenseIdentifier = ' is not a simple license identifier';

var rangeComparison = function(comparison) {
  return function(first, second) {
    var firstAST = exports.parse(first);
    if (!firstAST.hasOwnProperty('license')) {
      throw new Error('"' + first + '"' + notALicenseIdentifier);
    }
    var secondAST = exports.parse(second);
    if (!secondAST.hasOwnProperty('license')) {
      throw new Error('"' + second + '"' + notALicenseIdentifier);
    }
    return ranges.some(function(range) {
      var indexOfFirst = range.indexOf(firstAST.license);
      if (indexOfFirst < 0) {
        return false;
      }
      var indexOfSecond = range.indexOf(secondAST.license);
      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.satisfies = (function() {
  var rangesAreCompatible = function(first, second) {
    return (
      first.license === second.license ||
      ranges.some(function(range) {
        return (
          range.indexOf(first.license) > -1 &&
          range.indexOf(second.license)
        );
      })
    );
  };

  var identifierInRange = function(identifier, range) {
    return (
      identifier.license === range.license ||
      exports.gt(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;
        }
      }
    }
  };

  var recurseLeftAndRight = function(first, second) {
    var firstConjunction = first.conjunction;
    if (firstConjunction === 'and') {
      return (
        recurse(first.left, second) &&
        recurse(first.right, second)
      );
    } else if (firstConjunction === 'or') {
      return (
        recurse(first.left, second) ||
        recurse(first.right, second)
      );
    }
  };

  var recurse = function(first, second) {
    if (first.hasOwnProperty('conjunction')) {
      return recurseLeftAndRight(first, second);
    } else if (second.hasOwnProperty('conjunction')) {
      return recurseLeftAndRight(second, first);
    } else {
      return licensesAreCompatible(first, second);
    }
  };

  return function(first, second) {
    return recurse(parser.parse(first), parser.parse(second));
  };
})();

// Reference Data
// --------------

// Require the same license and exception data used by the parser.
exports.licenses = require('spdx-license-ids');
exports.exceptions = require('./exceptions.json');

// Version Metadata
// ----------------

// The License Expression Syntax version
exports.specificationVersion = '2.0';

// This module's semantic version
exports.implementationVersion = '0.4.1';