All files license.js

98.7% Statements 76/77
92.45% Branches 49/53
100% Functions 1/1
98.7% Lines 76/77

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 1408x   8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x   8x 8x 8x 8x   8x 10144x 1x           10143x 10143x 9948x         195x 195x     195x 4x     191x       191x 1x     190x 26x     164x 1x     163x   1x     162x 1x     161x 1x     160x 1x     159x 25x     134x 1x     133x 1x 1x   1x 1x     1x     132x 1x     131x 1x     130x 3x 3x     3x 3x     3x     127x 3x 3x   3x 2x   3x     124x 5x     119x   119x 82x   37x      
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 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 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 IS_FILE_REFERENCE = /SEE LICENSE IN (.*)/i;
 
module.exports = function exports(str = 'undefined') {
    if (typeof str !== 'string') {
        throw new Error(`Wrong type for parameter "str" ("${str}"): Must be of type string`, 'lib/license.js', 24);
    }
 
    let match;
    let version;
 
    try {
        spdxExpressionParse(str || '');
        return str;
    } catch (error) {
        // Fail silently and continue
    }
 
    Eif (str) {
        str = str.replace('\n', '');
    }
 
    if (typeof str === 'undefined' || !str || str === 'undefined') {
        return 'Undefined';
    }
 
    Iif (ZERO_PARITY_LICENSE.test(str)) {
        return 'Zero Parity*';
    }
 
    if (ISC_LICENSE.test(str)) {
        return 'ISC*';
    }
 
    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 (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';
    }
 
    match = IS_URL.exec(str) || IS_FILE_REFERENCE.exec(str);
 
    if (match) {
        return 'Custom: ' + match[1];
    } else {
        return null;
    }
};