index.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. Copyright spdx-correct.js contributors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. var parse = require('spdx-expression-parse')
  14. var spdxLicenseIds = require('spdx-license-ids')
  15. function valid (string) {
  16. try {
  17. parse(string)
  18. return true
  19. } catch (error) {
  20. return false
  21. }
  22. }
  23. // Common transpositions of license identifier acronyms
  24. var transpositions = [
  25. ['APGL', 'AGPL'],
  26. ['Gpl', 'GPL'],
  27. ['GLP', 'GPL'],
  28. ['APL', 'Apache'],
  29. ['ISD', 'ISC'],
  30. ['GLP', 'GPL'],
  31. ['IST', 'ISC'],
  32. ['Claude', 'Clause'],
  33. [' or later', '+'],
  34. [' International', ''],
  35. ['GNU', 'GPL'],
  36. ['GUN', 'GPL'],
  37. ['+', ''],
  38. ['GNU GPL', 'GPL'],
  39. ['GNU/GPL', 'GPL'],
  40. ['GNU GLP', 'GPL'],
  41. ['GNU General Public License', 'GPL'],
  42. ['Gnu public license', 'GPL'],
  43. ['GNU Public License', 'GPL'],
  44. ['GNU GENERAL PUBLIC LICENSE', 'GPL'],
  45. ['MTI', 'MIT'],
  46. ['Mozilla Public License', 'MPL'],
  47. ['WTH', 'WTF'],
  48. ['-License', '']
  49. ]
  50. var TRANSPOSED = 0
  51. var CORRECT = 1
  52. // Simple corrections to nearly valid identifiers.
  53. var transforms = [
  54. // e.g. 'mit'
  55. function (argument) {
  56. return argument.toUpperCase()
  57. },
  58. // e.g. 'MIT '
  59. function (argument) {
  60. return argument.trim()
  61. },
  62. // e.g. 'M.I.T.'
  63. function (argument) {
  64. return argument.replace(/\./g, '')
  65. },
  66. // e.g. 'Apache- 2.0'
  67. function (argument) {
  68. return argument.replace(/\s+/g, '')
  69. },
  70. // e.g. 'CC BY 4.0''
  71. function (argument) {
  72. return argument.replace(/\s+/g, '-')
  73. },
  74. // e.g. 'LGPLv2.1'
  75. function (argument) {
  76. return argument.replace('v', '-')
  77. },
  78. // e.g. 'Apache 2.0'
  79. function (argument) {
  80. return argument.replace(/,?\s*(\d)/, '-$1')
  81. },
  82. // e.g. 'GPL 2'
  83. function (argument) {
  84. return argument.replace(/,?\s*(\d)/, '-$1.0')
  85. },
  86. // e.g. 'Apache Version 2.0'
  87. function (argument) {
  88. return argument
  89. .replace(/,?\s*(V\.|v\.|V|v|Version|version)\s*(\d)/, '-$2')
  90. },
  91. // e.g. 'Apache Version 2'
  92. function (argument) {
  93. return argument
  94. .replace(/,?\s*(V\.|v\.|V|v|Version|version)\s*(\d)/, '-$2.0')
  95. },
  96. // e.g. 'ZLIB'
  97. function (argument) {
  98. return argument[0].toUpperCase() + argument.slice(1)
  99. },
  100. // e.g. 'MPL/2.0'
  101. function (argument) {
  102. return argument.replace('/', '-')
  103. },
  104. // e.g. 'Apache 2'
  105. function (argument) {
  106. return argument
  107. .replace(/\s*V\s*(\d)/, '-$1')
  108. .replace(/(\d)$/, '$1.0')
  109. },
  110. // e.g. 'GPL-2.0', 'GPL-3.0'
  111. function (argument) {
  112. if (argument.indexOf('3.0') !== -1) {
  113. return argument + '-or-later'
  114. } else {
  115. return argument + '-only'
  116. }
  117. },
  118. // e.g. 'GPL-2.0-'
  119. function (argument) {
  120. return argument + 'only'
  121. },
  122. // e.g. 'GPL2'
  123. function (argument) {
  124. return argument.replace(/(\d)$/, '-$1.0')
  125. },
  126. // e.g. 'BSD 3'
  127. function (argument) {
  128. return argument.replace(/(-| )?(\d)$/, '-$2-Clause')
  129. },
  130. // e.g. 'BSD clause 3'
  131. function (argument) {
  132. return argument.replace(/(-| )clause(-| )(\d)/, '-$3-Clause')
  133. },
  134. // e.g. 'BY-NC-4.0'
  135. function (argument) {
  136. return 'CC-' + argument
  137. },
  138. // e.g. 'BY-NC'
  139. function (argument) {
  140. return 'CC-' + argument + '-4.0'
  141. },
  142. // e.g. 'Attribution-NonCommercial'
  143. function (argument) {
  144. return argument
  145. .replace('Attribution', 'BY')
  146. .replace('NonCommercial', 'NC')
  147. .replace('NoDerivatives', 'ND')
  148. .replace(/ (\d)/, '-$1')
  149. .replace(/ ?International/, '')
  150. },
  151. // e.g. 'Attribution-NonCommercial'
  152. function (argument) {
  153. return 'CC-' +
  154. argument
  155. .replace('Attribution', 'BY')
  156. .replace('NonCommercial', 'NC')
  157. .replace('NoDerivatives', 'ND')
  158. .replace(/ (\d)/, '-$1')
  159. .replace(/ ?International/, '') +
  160. '-4.0'
  161. }
  162. ]
  163. var licensesWithVersions = spdxLicenseIds
  164. .map(function (id) {
  165. var match = /^(.*)-\d+\.\d+$/.exec(id)
  166. return match
  167. ? [match[0], match[1]]
  168. : [id, null]
  169. })
  170. .reduce(function (objectMap, item) {
  171. var key = item[1]
  172. objectMap[key] = objectMap[key] || []
  173. objectMap[key].push(item[0])
  174. return objectMap
  175. }, {})
  176. var licensesWithOneVersion = Object.keys(licensesWithVersions)
  177. .map(function makeEntries (key) {
  178. return [key, licensesWithVersions[key]]
  179. })
  180. .filter(function identifySoleVersions (item) {
  181. return (
  182. // Licenses has just one valid version suffix.
  183. item[1].length === 1 &&
  184. item[0] !== null &&
  185. // APL will be considered Apache, rather than APL-1.0
  186. item[0] !== 'APL'
  187. )
  188. })
  189. .map(function createLastResorts (item) {
  190. return [item[0], item[1][0]]
  191. })
  192. licensesWithVersions = undefined
  193. // If all else fails, guess that strings containing certain substrings
  194. // meant to identify certain licenses.
  195. var lastResorts = [
  196. ['UNLI', 'Unlicense'],
  197. ['WTF', 'WTFPL'],
  198. ['2 CLAUSE', 'BSD-2-Clause'],
  199. ['2-CLAUSE', 'BSD-2-Clause'],
  200. ['3 CLAUSE', 'BSD-3-Clause'],
  201. ['3-CLAUSE', 'BSD-3-Clause'],
  202. ['AFFERO', 'AGPL-3.0-or-later'],
  203. ['AGPL', 'AGPL-3.0-or-later'],
  204. ['APACHE', 'Apache-2.0'],
  205. ['ARTISTIC', 'Artistic-2.0'],
  206. ['Affero', 'AGPL-3.0-or-later'],
  207. ['BEER', 'Beerware'],
  208. ['BOOST', 'BSL-1.0'],
  209. ['BSD', 'BSD-2-Clause'],
  210. ['CDDL', 'CDDL-1.1'],
  211. ['ECLIPSE', 'EPL-1.0'],
  212. ['FUCK', 'WTFPL'],
  213. ['GNU', 'GPL-3.0-or-later'],
  214. ['LGPL', 'LGPL-3.0-or-later'],
  215. ['GPLV1', 'GPL-1.0-only'],
  216. ['GPL-1', 'GPL-1.0-only'],
  217. ['GPLV2', 'GPL-2.0-only'],
  218. ['GPL-2', 'GPL-2.0-only'],
  219. ['GPL', 'GPL-3.0-or-later'],
  220. ['MIT +NO-FALSE-ATTRIBS', 'MITNFA'],
  221. ['MIT', 'MIT'],
  222. ['MPL', 'MPL-2.0'],
  223. ['X11', 'X11'],
  224. ['ZLIB', 'Zlib']
  225. ].concat(licensesWithOneVersion)
  226. var SUBSTRING = 0
  227. var IDENTIFIER = 1
  228. var validTransformation = function (identifier) {
  229. for (var i = 0; i < transforms.length; i++) {
  230. var transformed = transforms[i](identifier).trim()
  231. if (transformed !== identifier && valid(transformed)) {
  232. return transformed
  233. }
  234. }
  235. return null
  236. }
  237. var validLastResort = function (identifier) {
  238. var upperCased = identifier.toUpperCase()
  239. for (var i = 0; i < lastResorts.length; i++) {
  240. var lastResort = lastResorts[i]
  241. if (upperCased.indexOf(lastResort[SUBSTRING]) > -1) {
  242. return lastResort[IDENTIFIER]
  243. }
  244. }
  245. return null
  246. }
  247. var anyCorrection = function (identifier, check) {
  248. for (var i = 0; i < transpositions.length; i++) {
  249. var transposition = transpositions[i]
  250. var transposed = transposition[TRANSPOSED]
  251. if (identifier.indexOf(transposed) > -1) {
  252. var corrected = identifier.replace(
  253. transposed,
  254. transposition[CORRECT]
  255. )
  256. var checked = check(corrected)
  257. if (checked !== null) {
  258. return checked
  259. }
  260. }
  261. }
  262. return null
  263. }
  264. module.exports = function (identifier, options) {
  265. options = options || {}
  266. var upgrade = options.upgrade === undefined ? true : !!options.upgrade
  267. function postprocess (value) {
  268. return upgrade ? upgradeGPLs(value) : value
  269. }
  270. var validArugment = (
  271. typeof identifier === 'string' &&
  272. identifier.trim().length !== 0
  273. )
  274. if (!validArugment) {
  275. throw Error('Invalid argument. Expected non-empty string.')
  276. }
  277. identifier = identifier.trim()
  278. if (valid(identifier)) {
  279. return postprocess(identifier)
  280. }
  281. var noPlus = identifier.replace(/\+$/, '').trim()
  282. if (valid(noPlus)) {
  283. return postprocess(noPlus)
  284. }
  285. var transformed = validTransformation(identifier)
  286. if (transformed !== null) {
  287. return postprocess(transformed)
  288. }
  289. transformed = anyCorrection(identifier, function (argument) {
  290. if (valid(argument)) {
  291. return argument
  292. }
  293. return validTransformation(argument)
  294. })
  295. if (transformed !== null) {
  296. return postprocess(transformed)
  297. }
  298. transformed = validLastResort(identifier)
  299. if (transformed !== null) {
  300. return postprocess(transformed)
  301. }
  302. transformed = anyCorrection(identifier, validLastResort)
  303. if (transformed !== null) {
  304. return postprocess(transformed)
  305. }
  306. return null
  307. }
  308. function upgradeGPLs (value) {
  309. if ([
  310. 'GPL-1.0', 'LGPL-1.0', 'AGPL-1.0',
  311. 'GPL-2.0', 'LGPL-2.0', 'AGPL-2.0',
  312. 'LGPL-2.1'
  313. ].indexOf(value) !== -1) {
  314. return value + '-only'
  315. } else if ([
  316. 'GPL-1.0+', 'GPL-2.0+', 'GPL-3.0+',
  317. 'LGPL-2.0+', 'LGPL-2.1+', 'LGPL-3.0+',
  318. 'AGPL-1.0+', 'AGPL-3.0+'
  319. ].indexOf(value) !== -1) {
  320. return value.replace(/\+$/, '-or-later')
  321. } else if (['GPL-3.0', 'LGPL-3.0', 'AGPL-3.0'].indexOf(value) !== -1) {
  322. return value + '-or-later'
  323. } else {
  324. return value
  325. }
  326. }