"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/spdx-correct/index.js" (8 Mar 2017, 5829 Bytes) of archive /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 var licenseIDs = require('spdx-license-ids');
    2 
    3 function valid(string) {
    4   return licenseIDs.indexOf(string) > -1;
    5 }
    6 
    7 // Common transpositions of license identifier acronyms
    8 var transpositions = [
    9   ['APGL', 'AGPL'],
   10   ['Gpl', 'GPL'],
   11   ['GLP', 'GPL'],
   12   ['APL', 'Apache'],
   13   ['ISD', 'ISC'],
   14   ['GLP', 'GPL'],
   15   ['IST', 'ISC'],
   16   ['Claude', 'Clause'],
   17   [' or later', '+'],
   18   [' International', ''],
   19   ['GNU', 'GPL'],
   20   ['GUN', 'GPL'],
   21   ['+', ''],
   22   ['GNU GPL', 'GPL'],
   23   ['GNU/GPL', 'GPL'],
   24   ['GNU GLP', 'GPL'],
   25   ['GNU General Public License', 'GPL'],
   26   ['Gnu public license', 'GPL'],
   27   ['GNU Public License', 'GPL'],
   28   ['GNU GENERAL PUBLIC LICENSE', 'GPL'],
   29   ['MTI', 'MIT'],
   30   ['Mozilla Public License', 'MPL'],
   31   ['WTH', 'WTF'],
   32   ['-License', '']
   33 ];
   34 
   35 var TRANSPOSED = 0;
   36 var CORRECT = 1;
   37 
   38 // Simple corrections to nearly valid identifiers.
   39 var transforms = [
   40   // e.g. 'mit'
   41   function(argument) {
   42     return argument.toUpperCase();
   43   },
   44   // e.g. 'MIT '
   45   function(argument) {
   46     return argument.trim();
   47   },
   48   // e.g. 'M.I.T.'
   49   function(argument) {
   50     return argument.replace(/\./g, '');
   51   },
   52   // e.g. 'Apache- 2.0'
   53   function(argument) {
   54     return argument.replace(/\s+/g, '');
   55   },
   56   // e.g. 'CC BY 4.0''
   57   function(argument) {
   58     return argument.replace(/\s+/g, '-');
   59   },
   60   // e.g. 'LGPLv2.1'
   61   function(argument) {
   62     return argument.replace('v', '-');
   63   },
   64   // e.g. 'Apache 2.0'
   65   function(argument) {
   66     return argument.replace(/,?\s*(\d)/, '-$1');
   67   },
   68   // e.g. 'GPL 2'
   69   function(argument) {
   70     return argument.replace(/,?\s*(\d)/, '-$1.0');
   71   },
   72   // e.g. 'Apache Version 2.0'
   73   function(argument) {
   74     return argument.replace(/,?\s*(V\.|v\.|V|v|Version|version)\s*(\d)/, '-$2');
   75   },
   76   // e.g. 'Apache Version 2'
   77   function(argument) {
   78     return argument.replace(/,?\s*(V\.|v\.|V|v|Version|version)\s*(\d)/, '-$2.0');
   79   },
   80   // e.g. 'ZLIB'
   81   function(argument) {
   82     return argument[0].toUpperCase() + argument.slice(1);
   83   },
   84   // e.g. 'MPL/2.0'
   85   function(argument) {
   86     return argument.replace('/', '-');
   87   },
   88   // e.g. 'Apache 2'
   89   function(argument) {
   90     return argument
   91       .replace(/\s*V\s*(\d)/, '-$1')
   92       .replace(/(\d)$/, '$1.0');
   93   },
   94   // e.g. 'GPL-2.0-'
   95   function(argument) {
   96     return argument.slice(0, argument.length - 1);
   97   },
   98   // e.g. 'GPL2'
   99   function(argument) {
  100     return argument.replace(/(\d)$/, '-$1.0');
  101   },
  102   // e.g. 'BSD 3'
  103   function(argument) {
  104     return argument.replace(/(-| )?(\d)$/, '-$2-Clause');
  105   },
  106   // e.g. 'BSD clause 3'
  107   function(argument) {
  108     return argument.replace(/(-| )clause(-| )(\d)/, '-$3-Clause');
  109   },
  110   // e.g. 'BY-NC-4.0'
  111   function(argument) {
  112     return 'CC-' + argument;
  113   },
  114   // e.g. 'BY-NC'
  115   function(argument) {
  116     return 'CC-' + argument + '-4.0';
  117   },
  118   // e.g. 'Attribution-NonCommercial'
  119   function(argument) {
  120     return argument
  121       .replace('Attribution', 'BY')
  122       .replace('NonCommercial', 'NC')
  123       .replace('NoDerivatives', 'ND')
  124       .replace(/ (\d)/, '-$1')
  125       .replace(/ ?International/, '');
  126   },
  127   // e.g. 'Attribution-NonCommercial'
  128   function(argument) {
  129     return 'CC-' +
  130       argument
  131       .replace('Attribution', 'BY')
  132       .replace('NonCommercial', 'NC')
  133       .replace('NoDerivatives', 'ND')
  134       .replace(/ (\d)/, '-$1')
  135       .replace(/ ?International/, '') +
  136       '-4.0';
  137   }
  138 ];
  139 
  140 // If all else fails, guess that strings containing certain substrings
  141 // meant to identify certain licenses.
  142 var lastResorts = [
  143   ['UNLI', 'Unlicense'],
  144   ['WTF', 'WTFPL'],
  145   ['2 CLAUSE', 'BSD-2-Clause'],
  146   ['2-CLAUSE', 'BSD-2-Clause'],
  147   ['3 CLAUSE', 'BSD-3-Clause'],
  148   ['3-CLAUSE', 'BSD-3-Clause'],
  149   ['AFFERO', 'AGPL-3.0'],
  150   ['AGPL', 'AGPL-3.0'],
  151   ['APACHE', 'Apache-2.0'],
  152   ['ARTISTIC', 'Artistic-2.0'],
  153   ['Affero', 'AGPL-3.0'],
  154   ['BEER', 'Beerware'],
  155   ['BOOST', 'BSL-1.0'],
  156   ['BSD', 'BSD-2-Clause'],
  157   ['ECLIPSE', 'EPL-1.0'],
  158   ['FUCK', 'WTFPL'],
  159   ['GNU', 'GPL-3.0'],
  160   ['LGPL', 'LGPL-3.0'],
  161   ['GPL', 'GPL-3.0'],
  162   ['MIT', 'MIT'],
  163   ['MPL', 'MPL-2.0'],
  164   ['X11', 'X11'],
  165   ['ZLIB', 'Zlib']
  166 ];
  167 
  168 var SUBSTRING = 0;
  169 var IDENTIFIER = 1;
  170 
  171 var validTransformation = function(identifier) {
  172   for (var i = 0; i < transforms.length; i++) {
  173     var transformed = transforms[i](identifier);
  174     if (transformed !== identifier && valid(transformed)) {
  175       return transformed;
  176     }
  177   }
  178   return null;
  179 };
  180 
  181 var validLastResort = function(identifier) {
  182   var upperCased = identifier.toUpperCase();
  183   for (var i = 0; i < lastResorts.length; i++) {
  184     var lastResort = lastResorts[i];
  185     if (upperCased.indexOf(lastResort[SUBSTRING]) > -1) {
  186       return lastResort[IDENTIFIER];
  187     }
  188   }
  189   return null;
  190 };
  191 
  192 var anyCorrection = function(identifier, check) {
  193   for (var i = 0; i < transpositions.length; i++) {
  194     var transposition = transpositions[i];
  195     var transposed = transposition[TRANSPOSED];
  196     if (identifier.indexOf(transposed) > -1) {
  197       var corrected = identifier.replace(
  198         transposed,
  199         transposition[CORRECT]
  200       );
  201       var checked = check(corrected);
  202       if (checked !== null) {
  203         return checked;
  204       }
  205     }
  206   }
  207   return null;
  208 };
  209 
  210 module.exports = function(identifier) {
  211   identifier = identifier.replace(/\+$/, '');
  212   if (valid(identifier)) {
  213     return identifier;
  214   }
  215   var transformed = validTransformation(identifier);
  216   if (transformed !== null) {
  217     return transformed;
  218   }
  219   transformed = anyCorrection(identifier, function(argument) {
  220     if (valid(argument)) {
  221       return argument;
  222     }
  223     return validTransformation(argument);
  224   });
  225   if (transformed !== null) {
  226     return transformed;
  227   }
  228   transformed = validLastResort(identifier);
  229   if (transformed !== null) {
  230     return transformed;
  231   }
  232   transformed = anyCorrection(identifier, validLastResort);
  233   if (transformed !== null) {
  234     return transformed;
  235   }
  236   return null;
  237 };