All files getLicenseTitle.js

95.83% Statements 92/96
90.32% Branches 56/62
100% Functions 1/1
95.83% Lines 92/96

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 17814x   14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x     14x 14x 14x 14x 14x 14x 14x   14x 13182x 1x                   13181x   13181x     12808x         373x 373x     373x 218x     155x       155x 1x     154x       154x 29x     125x 1x     124x   1x     123x 1x     122x 1x     121x 1x     120x 28x     92x 1x     91x 1x 1x   1x 1x     1x     90x 1x     89x 26x 26x   26x       26x     63x 1x     62x 3x 3x     3x 3x     3x     59x 3x 3x   3x 2x   3x     56x 5x     51x       51x   51x 38x 38x 38x   38x 8x       43x    
const spdxExpressionParse = require('spdx-expression-parse');
 
const MIT_LICENSE = /ermission is hereby granted, free of charge, to any/;
const BSD_LICENSE = /edistribution and use in source and binary forms, with or withou/;
const BSD_SOURCE_CODE_LICENSE = /edistribution and use of this software in source and binary forms, with or withou/;
const WTFPL_LICENSE = /DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE/;
const ISC_LICENSE = /The ISC License/;
const HIPPOCRATIC_LICENSE = /Hippocratic License/;
const MIT = /\bMIT\b/;
const BSD = /\bBSD\b/;
const ISC = /\bISC\b/;
const GPL = /\bGNU GENERAL PUBLIC LICENSE\s*Version ([^,]*)/i;
const LGPL = /(?:LESSER|LIBRARY) GENERAL PUBLIC LICENSE\s*Version ([^,]*)/i;
const APACHE_VERSION = /\bApache License\s*Version ([^,\s]*)/i;
const APACHE = /\bApache License\b/;
const APACHE_VERSION_SHORT = /\bApache ([^,\s]*)/i;
const WTFPL = /\bWTFPL\b/;
const ZERO_PARITY_LICENSE = /\bParity\b/;
// https://creativecommons.org/publicdomain/zero/1.0/
const CC0_1_0 =
    /The\s+person\s+who\s+associated\s+a\s+work\s+with\s+this\s+deed\s+has\s+dedicated\s+the\s+work\s+to\s+the\s+public\s+domain\s+by\s+waiving\s+all\s+of\s+his\s+or\s+her\s+rights\s+to\s+the\s+work\s+worldwide\s+under\s+copyright\s+law,\s+including\s+all\s+related\s+and\s+neighboring\s+rights,\s+to\s+the\s+extent\s+allowed\s+by\s+law.\s+You\s+can\s+copy,\s+modify,\s+distribute\s+and\s+perform\s+the\s+work,\s+even\s+for\s+commercial\s+purposes,\s+all\s+without\s+asking\s+permission./i; // jshint ignore:line
const PUBLIC_DOMAIN = /[Pp]ublic[\-_ ]*[Dd]omain/;
const IS_URL = /(https?:\/\/[-a-zA-Z0-9\/.]*)/;
const CONTAINS_URLS_ENDING_WITH = /(.svg|.gif|.png|.jpg|.jpeg)$/i;
const CONTAINS_LICENSE_TERM = /(license|licence|lizenz)/gi;
const IS_FILE_REFERENCE = /SEE LICENSE IN (.*)/i;
const UNLICENSED = /UNLICENSED/i;
 
module.exports = function getLicenseTitle(str = 'undefined') {
    if (typeof str !== 'string') {
        throw new Error(
            `Wrong type for parameter "str" ("${str}"): Must be of type string`,
            'lib/getLicenseTitle.js',
            24,
        );
    }
 
    let match;
    let version;
 
    try {
        // Simply check if the string is a valid SPDX expression:
        spdxExpressionParse(str);
 
        // No need for additional parsing efforts:
        return str;
    } catch (error) {
        // Fail silently and continue with additional parsing efforts:
    }
 
    Eif (str) {
        str = str.replace('\n', '');
    }
 
    if (!str || str === 'undefined') {
        return 'Undefined';
    }
 
    Iif (ZERO_PARITY_LICENSE.test(str)) {
        return 'Zero Parity*';
    }
 
    if (ISC_LICENSE.test(str)) {
        return 'ISC*';
    }
 
    Iif (HIPPOCRATIC_LICENSE.test(str)) {
        return 'Hippocratic-2.1*';
    }
 
    if (MIT_LICENSE.test(str)) {
        return 'MIT*';
    }
 
    if (BSD_LICENSE.test(str)) {
        return 'BSD*';
    }
 
    if (BSD_SOURCE_CODE_LICENSE.test(str)) {
        // https://spdx.org/licenses/BSD-Source-Code.html
        return 'BSD-Source-Code*';
    }
 
    if (WTFPL_LICENSE.test(str)) {
        return 'WTFPL*';
    }
 
    if (ISC.test(str)) {
        return 'ISC*';
    }
 
    if (MIT.test(str)) {
        return 'MIT*';
    }
 
    if (BSD.test(str)) {
        return 'BSD*';
    }
 
    if (WTFPL.test(str)) {
        return 'WTFPL*';
    }
 
    if (APACHE_VERSION.test(str)) {
        match = APACHE_VERSION.exec(str);
        version = match[1];
 
        Eif (version.length === 1) {
            version = version + '.0';
        }
 
        return 'Apache-' + version + '*';
    }
 
    if (APACHE.test(str)) {
        return 'Apache*';
    }
 
    if (APACHE_VERSION_SHORT.test(str)) {
        match = APACHE_VERSION_SHORT.exec(str);
        version = match[1];
 
        Iif (version.length === 1) {
            version = version + '.0';
        }
 
        return 'Apache-' + version + '*';
    }
 
    if (CC0_1_0.test(str)) {
        return 'CC0-1.0*';
    }
 
    if (GPL.test(str)) {
        match = GPL.exec(str);
        version = match[1];
 
        /*istanbul ignore else*/
        if (version.length === 1) {
            version = version + '.0';
        }
 
        return 'GPL-' + version + '*';
    }
 
    if (LGPL.test(str)) {
        match = LGPL.exec(str);
        version = match[1];
 
        if (version.length === 1) {
            version = version + '.0';
        }
        return 'LGPL-' + version + '*';
    }
 
    if (PUBLIC_DOMAIN.test(str)) {
        return 'Public Domain';
    }
 
    Iif (UNLICENSED.test(str)) {
        return 'UNLICENSED';
    }
 
    match = IS_URL.exec(str) || IS_FILE_REFERENCE.exec(str);
 
    if (match) {
        const matchedUrl = match[1];
        CONTAINS_LICENSE_TERM.lastIndex = 0;
        CONTAINS_URLS_ENDING_WITH.lastIndex = 0;
 
        if (CONTAINS_LICENSE_TERM.test(str) && !CONTAINS_URLS_ENDING_WITH.test(matchedUrl)) {
            return `Custom: ${matchedUrl}`;
        }
    }
 
    return null;
};