Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

index.js 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. ['Universal Permissive License', 'UPL'],
  48. ['WTH', 'WTF'],
  49. ['-License', '']
  50. ]
  51. var TRANSPOSED = 0
  52. var CORRECT = 1
  53. // Simple corrections to nearly valid identifiers.
  54. var transforms = [
  55. // e.g. 'mit'
  56. function (argument) {
  57. return argument.toUpperCase()
  58. },
  59. // e.g. 'MIT '
  60. function (argument) {
  61. return argument.trim()
  62. },
  63. // e.g. 'M.I.T.'
  64. function (argument) {
  65. return argument.replace(/\./g, '')
  66. },
  67. // e.g. 'Apache- 2.0'
  68. function (argument) {
  69. return argument.replace(/\s+/g, '')
  70. },
  71. // e.g. 'CC BY 4.0''
  72. function (argument) {
  73. return argument.replace(/\s+/g, '-')
  74. },
  75. // e.g. 'LGPLv2.1'
  76. function (argument) {
  77. return argument.replace('v', '-')
  78. },
  79. // e.g. 'Apache 2.0'
  80. function (argument) {
  81. return argument.replace(/,?\s*(\d)/, '-$1')
  82. },
  83. // e.g. 'GPL 2'
  84. function (argument) {
  85. return argument.replace(/,?\s*(\d)/, '-$1.0')
  86. },
  87. // e.g. 'Apache Version 2.0'
  88. function (argument) {
  89. return argument
  90. .replace(/,?\s*(V\.|v\.|V|v|Version|version)\s*(\d)/, '-$2')
  91. },
  92. // e.g. 'Apache Version 2'
  93. function (argument) {
  94. return argument
  95. .replace(/,?\s*(V\.|v\.|V|v|Version|version)\s*(\d)/, '-$2.0')
  96. },
  97. // e.g. 'ZLIB'
  98. function (argument) {
  99. return argument[0].toUpperCase() + argument.slice(1)
  100. },
  101. // e.g. 'MPL/2.0'
  102. function (argument) {
  103. return argument.replace('/', '-')
  104. },
  105. // e.g. 'Apache 2'
  106. function (argument) {
  107. return argument
  108. .replace(/\s*V\s*(\d)/, '-$1')
  109. .replace(/(\d)$/, '$1.0')
  110. },
  111. // e.g. 'GPL-2.0', 'GPL-3.0'
  112. function (argument) {
  113. if (argument.indexOf('3.0') !== -1) {
  114. return argument + '-or-later'
  115. } else {
  116. return argument + '-only'
  117. }
  118. },
  119. // e.g. 'GPL-2.0-'
  120. function (argument) {
  121. return argument + 'only'
  122. },
  123. // e.g. 'GPL2'
  124. function (argument) {
  125. return argument.replace(/(\d)$/, '-$1.0')
  126. },
  127. // e.g. 'BSD 3'
  128. function (argument) {
  129. return argument.replace(/(-| )?(\d)$/, '-$2-Clause')
  130. },
  131. // e.g. 'BSD clause 3'
  132. function (argument) {
  133. return argument.replace(/(-| )clause(-| )(\d)/, '-$3-Clause')
  134. },
  135. // e.g. 'New BSD license'
  136. function (argument) {
  137. return argument.replace(/\b(Modified|New|Revised)(-| )?BSD((-| )License)?/i, 'BSD-3-Clause')
  138. },
  139. // e.g. 'Simplified BSD license'
  140. function (argument) {
  141. return argument.replace(/\bSimplified(-| )?BSD((-| )License)?/i, 'BSD-2-Clause')
  142. },
  143. // e.g. 'Free BSD license'
  144. function (argument) {
  145. return argument.replace(/\b(Free|Net)(-| )?BSD((-| )License)?/i, 'BSD-2-Clause-$1BSD')
  146. },
  147. // e.g. 'Clear BSD license'
  148. function (argument) {
  149. return argument.replace(/\bClear(-| )?BSD((-| )License)?/i, 'BSD-3-Clause-Clear')
  150. },
  151. // e.g. 'Old BSD License'
  152. function (argument) {
  153. return argument.replace(/\b(Old|Original)(-| )?BSD((-| )License)?/i, 'BSD-4-Clause')
  154. },
  155. // e.g. 'BY-NC-4.0'
  156. function (argument) {
  157. return 'CC-' + argument
  158. },
  159. // e.g. 'BY-NC'
  160. function (argument) {
  161. return 'CC-' + argument + '-4.0'
  162. },
  163. // e.g. 'Attribution-NonCommercial'
  164. function (argument) {
  165. return argument
  166. .replace('Attribution', 'BY')
  167. .replace('NonCommercial', 'NC')
  168. .replace('NoDerivatives', 'ND')
  169. .replace(/ (\d)/, '-$1')
  170. .replace(/ ?International/, '')
  171. },
  172. // e.g. 'Attribution-NonCommercial'
  173. function (argument) {
  174. return 'CC-' +
  175. argument
  176. .replace('Attribution', 'BY')
  177. .replace('NonCommercial', 'NC')
  178. .replace('NoDerivatives', 'ND')
  179. .replace(/ (\d)/, '-$1')
  180. .replace(/ ?International/, '') +
  181. '-4.0'
  182. }
  183. ]
  184. var licensesWithVersions = spdxLicenseIds
  185. .map(function (id) {
  186. var match = /^(.*)-\d+\.\d+$/.exec(id)
  187. return match
  188. ? [match[0], match[1]]
  189. : [id, null]
  190. })
  191. .reduce(function (objectMap, item) {
  192. var key = item[1]
  193. objectMap[key] = objectMap[key] || []
  194. objectMap[key].push(item[0])
  195. return objectMap
  196. }, {})
  197. var licensesWithOneVersion = Object.keys(licensesWithVersions)
  198. .map(function makeEntries (key) {
  199. return [key, licensesWithVersions[key]]
  200. })
  201. .filter(function identifySoleVersions (item) {
  202. return (
  203. // Licenses has just one valid version suffix.
  204. item[1].length === 1 &&
  205. item[0] !== null &&
  206. // APL will be considered Apache, rather than APL-1.0
  207. item[0] !== 'APL'
  208. )
  209. })
  210. .map(function createLastResorts (item) {
  211. return [item[0], item[1][0]]
  212. })
  213. licensesWithVersions = undefined
  214. // If all else fails, guess that strings containing certain substrings
  215. // meant to identify certain licenses.
  216. var lastResorts = [
  217. ['UNLI', 'Unlicense'],
  218. ['WTF', 'WTFPL'],
  219. ['2 CLAUSE', 'BSD-2-Clause'],
  220. ['2-CLAUSE', 'BSD-2-Clause'],
  221. ['3 CLAUSE', 'BSD-3-Clause'],
  222. ['3-CLAUSE', 'BSD-3-Clause'],
  223. ['AFFERO', 'AGPL-3.0-or-later'],
  224. ['AGPL', 'AGPL-3.0-or-later'],
  225. ['APACHE', 'Apache-2.0'],
  226. ['ARTISTIC', 'Artistic-2.0'],
  227. ['Affero', 'AGPL-3.0-or-later'],
  228. ['BEER', 'Beerware'],
  229. ['BOOST', 'BSL-1.0'],
  230. ['BSD', 'BSD-2-Clause'],
  231. ['CDDL', 'CDDL-1.1'],
  232. ['ECLIPSE', 'EPL-1.0'],
  233. ['FUCK', 'WTFPL'],
  234. ['GNU', 'GPL-3.0-or-later'],
  235. ['LGPL', 'LGPL-3.0-or-later'],
  236. ['GPLV1', 'GPL-1.0-only'],
  237. ['GPL-1', 'GPL-1.0-only'],
  238. ['GPLV2', 'GPL-2.0-only'],
  239. ['GPL-2', 'GPL-2.0-only'],
  240. ['GPL', 'GPL-3.0-or-later'],
  241. ['MIT +NO-FALSE-ATTRIBS', 'MITNFA'],
  242. ['MIT', 'MIT'],
  243. ['MPL', 'MPL-2.0'],
  244. ['X11', 'X11'],
  245. ['ZLIB', 'Zlib']
  246. ].concat(licensesWithOneVersion)
  247. var SUBSTRING = 0
  248. var IDENTIFIER = 1
  249. var validTransformation = function (identifier) {
  250. for (var i = 0; i < transforms.length; i++) {
  251. var transformed = transforms[i](identifier).trim()
  252. if (transformed !== identifier && valid(transformed)) {
  253. return transformed
  254. }
  255. }
  256. return null
  257. }
  258. var validLastResort = function (identifier) {
  259. var upperCased = identifier.toUpperCase()
  260. for (var i = 0; i < lastResorts.length; i++) {
  261. var lastResort = lastResorts[i]
  262. if (upperCased.indexOf(lastResort[SUBSTRING]) > -1) {
  263. return lastResort[IDENTIFIER]
  264. }
  265. }
  266. return null
  267. }
  268. var anyCorrection = function (identifier, check) {
  269. for (var i = 0; i < transpositions.length; i++) {
  270. var transposition = transpositions[i]
  271. var transposed = transposition[TRANSPOSED]
  272. if (identifier.indexOf(transposed) > -1) {
  273. var corrected = identifier.replace(
  274. transposed,
  275. transposition[CORRECT]
  276. )
  277. var checked = check(corrected)
  278. if (checked !== null) {
  279. return checked
  280. }
  281. }
  282. }
  283. return null
  284. }
  285. module.exports = function (identifier, options) {
  286. options = options || {}
  287. var upgrade = options.upgrade === undefined ? true : !!options.upgrade
  288. function postprocess (value) {
  289. return upgrade ? upgradeGPLs(value) : value
  290. }
  291. var validArugment = (
  292. typeof identifier === 'string' &&
  293. identifier.trim().length !== 0
  294. )
  295. if (!validArugment) {
  296. throw Error('Invalid argument. Expected non-empty string.')
  297. }
  298. identifier = identifier.trim()
  299. if (valid(identifier)) {
  300. return postprocess(identifier)
  301. }
  302. var noPlus = identifier.replace(/\+$/, '').trim()
  303. if (valid(noPlus)) {
  304. return postprocess(noPlus)
  305. }
  306. var transformed = validTransformation(identifier)
  307. if (transformed !== null) {
  308. return postprocess(transformed)
  309. }
  310. transformed = anyCorrection(identifier, function (argument) {
  311. if (valid(argument)) {
  312. return argument
  313. }
  314. return validTransformation(argument)
  315. })
  316. if (transformed !== null) {
  317. return postprocess(transformed)
  318. }
  319. transformed = validLastResort(identifier)
  320. if (transformed !== null) {
  321. return postprocess(transformed)
  322. }
  323. transformed = anyCorrection(identifier, validLastResort)
  324. if (transformed !== null) {
  325. return postprocess(transformed)
  326. }
  327. return null
  328. }
  329. function upgradeGPLs (value) {
  330. if ([
  331. 'GPL-1.0', 'LGPL-1.0', 'AGPL-1.0',
  332. 'GPL-2.0', 'LGPL-2.0', 'AGPL-2.0',
  333. 'LGPL-2.1'
  334. ].indexOf(value) !== -1) {
  335. return value + '-only'
  336. } else if ([
  337. 'GPL-1.0+', 'GPL-2.0+', 'GPL-3.0+',
  338. 'LGPL-2.0+', 'LGPL-2.1+', 'LGPL-3.0+',
  339. 'AGPL-1.0+', 'AGPL-3.0+'
  340. ].indexOf(value) !== -1) {
  341. return value.replace(/\+$/, '-or-later')
  342. } else if (['GPL-3.0', 'LGPL-3.0', 'AGPL-3.0'].indexOf(value) !== -1) {
  343. return value + '-or-later'
  344. } else {
  345. return value
  346. }
  347. }