a zip code crypto-currency system good for red ONLY

index.js 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. Copyright 2015 Kyle E. Mitchell
  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. ['GPLV2', 'GPL-2.0-only'],
  217. ['GPL', 'GPL-3.0-or-later'],
  218. ['MIT +NO-FALSE-ATTRIBS', 'MITNFA'],
  219. ['MIT', 'MIT'],
  220. ['MPL', 'MPL-2.0'],
  221. ['X11', 'X11'],
  222. ['ZLIB', 'Zlib']
  223. ].concat(licensesWithOneVersion)
  224. var SUBSTRING = 0
  225. var IDENTIFIER = 1
  226. var validTransformation = function (identifier) {
  227. for (var i = 0; i < transforms.length; i++) {
  228. var transformed = transforms[i](identifier).trim()
  229. if (transformed !== identifier && valid(transformed)) {
  230. return transformed
  231. }
  232. }
  233. return null
  234. }
  235. var validLastResort = function (identifier) {
  236. var upperCased = identifier.toUpperCase()
  237. for (var i = 0; i < lastResorts.length; i++) {
  238. var lastResort = lastResorts[i]
  239. if (upperCased.indexOf(lastResort[SUBSTRING]) > -1) {
  240. return lastResort[IDENTIFIER]
  241. }
  242. }
  243. return null
  244. }
  245. var anyCorrection = function (identifier, check) {
  246. for (var i = 0; i < transpositions.length; i++) {
  247. var transposition = transpositions[i]
  248. var transposed = transposition[TRANSPOSED]
  249. if (identifier.indexOf(transposed) > -1) {
  250. var corrected = identifier.replace(
  251. transposed,
  252. transposition[CORRECT]
  253. )
  254. var checked = check(corrected)
  255. if (checked !== null) {
  256. return checked
  257. }
  258. }
  259. }
  260. return null
  261. }
  262. module.exports = function (identifier) {
  263. var validArugment = (
  264. typeof identifier === 'string' &&
  265. identifier.trim().length !== 0
  266. )
  267. if (!validArugment) {
  268. throw Error('Invalid argument. Expected non-empty string.')
  269. }
  270. identifier = identifier.replace(/\+$/, '').trim()
  271. if (valid(identifier)) {
  272. return upgradeGPLs(identifier)
  273. }
  274. var transformed = validTransformation(identifier)
  275. if (transformed !== null) {
  276. return upgradeGPLs(transformed)
  277. }
  278. transformed = anyCorrection(identifier, function (argument) {
  279. if (valid(argument)) {
  280. return argument
  281. }
  282. return validTransformation(argument)
  283. })
  284. if (transformed !== null) {
  285. return upgradeGPLs(transformed)
  286. }
  287. transformed = validLastResort(identifier)
  288. if (transformed !== null) {
  289. return upgradeGPLs(transformed)
  290. }
  291. transformed = anyCorrection(identifier, validLastResort)
  292. if (transformed !== null) {
  293. return upgradeGPLs(transformed)
  294. }
  295. return null
  296. }
  297. function upgradeGPLs (value) {
  298. if ([
  299. 'GPL-1.0', 'LGPL-1.0', 'AGPL-1.0',
  300. 'GPL-2.0', 'LGPL-2.0', 'AGPL-2.0',
  301. 'LGPL-2.1'
  302. ].indexOf(value) !== -1) {
  303. return value + '-only'
  304. } else if (['GPL-3.0', 'LGPL-3.0', 'AGPL-3.0'].indexOf(value) !== -1) {
  305. return value + '-or-later'
  306. } else {
  307. return value
  308. }
  309. }