wallet-core/node_modules/spdx-correct/index.js

327 lines
8.1 KiB
JavaScript
Raw Normal View History

2018-09-20 02:56:13 +02:00
/*
Copyright 2015 Kyle E. Mitchell
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
var parse = require('spdx-expression-parse')
var spdxLicenseIds = require('spdx-license-ids')
function valid (string) {
try {
parse(string)
return true
} catch (error) {
return false
}
2016-10-10 03:43:44 +02:00
}
// Common transpositions of license identifier acronyms
var transpositions = [
['APGL', 'AGPL'],
['Gpl', 'GPL'],
['GLP', 'GPL'],
['APL', 'Apache'],
['ISD', 'ISC'],
['GLP', 'GPL'],
['IST', 'ISC'],
['Claude', 'Clause'],
[' or later', '+'],
[' International', ''],
['GNU', 'GPL'],
['GUN', 'GPL'],
['+', ''],
['GNU GPL', 'GPL'],
['GNU/GPL', 'GPL'],
['GNU GLP', 'GPL'],
['GNU General Public License', 'GPL'],
['Gnu public license', 'GPL'],
['GNU Public License', 'GPL'],
['GNU GENERAL PUBLIC LICENSE', 'GPL'],
['MTI', 'MIT'],
['Mozilla Public License', 'MPL'],
['WTH', 'WTF'],
['-License', '']
2018-09-20 02:56:13 +02:00
]
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
var TRANSPOSED = 0
var CORRECT = 1
2016-10-10 03:43:44 +02:00
// Simple corrections to nearly valid identifiers.
var transforms = [
// e.g. 'mit'
2018-09-20 02:56:13 +02:00
function (argument) {
return argument.toUpperCase()
2016-10-10 03:43:44 +02:00
},
// e.g. 'MIT '
2018-09-20 02:56:13 +02:00
function (argument) {
return argument.trim()
2016-10-10 03:43:44 +02:00
},
// e.g. 'M.I.T.'
2018-09-20 02:56:13 +02:00
function (argument) {
return argument.replace(/\./g, '')
2016-10-10 03:43:44 +02:00
},
// e.g. 'Apache- 2.0'
2018-09-20 02:56:13 +02:00
function (argument) {
return argument.replace(/\s+/g, '')
2016-10-10 03:43:44 +02:00
},
// e.g. 'CC BY 4.0''
2018-09-20 02:56:13 +02:00
function (argument) {
return argument.replace(/\s+/g, '-')
2016-10-10 03:43:44 +02:00
},
// e.g. 'LGPLv2.1'
2018-09-20 02:56:13 +02:00
function (argument) {
return argument.replace('v', '-')
2016-10-10 03:43:44 +02:00
},
// e.g. 'Apache 2.0'
2018-09-20 02:56:13 +02:00
function (argument) {
return argument.replace(/,?\s*(\d)/, '-$1')
2016-10-10 03:43:44 +02:00
},
// e.g. 'GPL 2'
2018-09-20 02:56:13 +02:00
function (argument) {
return argument.replace(/,?\s*(\d)/, '-$1.0')
2016-10-10 03:43:44 +02:00
},
// e.g. 'Apache Version 2.0'
2018-09-20 02:56:13 +02:00
function (argument) {
return argument
.replace(/,?\s*(V\.|v\.|V|v|Version|version)\s*(\d)/, '-$2')
2016-10-10 03:43:44 +02:00
},
// e.g. 'Apache Version 2'
2018-09-20 02:56:13 +02:00
function (argument) {
return argument
.replace(/,?\s*(V\.|v\.|V|v|Version|version)\s*(\d)/, '-$2.0')
2016-10-10 03:43:44 +02:00
},
// e.g. 'ZLIB'
2018-09-20 02:56:13 +02:00
function (argument) {
return argument[0].toUpperCase() + argument.slice(1)
2016-10-10 03:43:44 +02:00
},
// e.g. 'MPL/2.0'
2018-09-20 02:56:13 +02:00
function (argument) {
return argument.replace('/', '-')
2016-10-10 03:43:44 +02:00
},
// e.g. 'Apache 2'
2018-09-20 02:56:13 +02:00
function (argument) {
2016-10-10 03:43:44 +02:00
return argument
.replace(/\s*V\s*(\d)/, '-$1')
2018-09-20 02:56:13 +02:00
.replace(/(\d)$/, '$1.0')
},
// e.g. 'GPL-2.0', 'GPL-3.0'
function (argument) {
if (argument.indexOf('3.0') !== -1) {
return argument + '-or-later'
} else {
return argument + '-only'
}
2016-10-10 03:43:44 +02:00
},
// e.g. 'GPL-2.0-'
2018-09-20 02:56:13 +02:00
function (argument) {
return argument + 'only'
2016-10-10 03:43:44 +02:00
},
// e.g. 'GPL2'
2018-09-20 02:56:13 +02:00
function (argument) {
return argument.replace(/(\d)$/, '-$1.0')
2016-10-10 03:43:44 +02:00
},
// e.g. 'BSD 3'
2018-09-20 02:56:13 +02:00
function (argument) {
return argument.replace(/(-| )?(\d)$/, '-$2-Clause')
2016-10-10 03:43:44 +02:00
},
// e.g. 'BSD clause 3'
2018-09-20 02:56:13 +02:00
function (argument) {
return argument.replace(/(-| )clause(-| )(\d)/, '-$3-Clause')
2016-10-10 03:43:44 +02:00
},
// e.g. 'BY-NC-4.0'
2018-09-20 02:56:13 +02:00
function (argument) {
return 'CC-' + argument
2016-10-10 03:43:44 +02:00
},
// e.g. 'BY-NC'
2018-09-20 02:56:13 +02:00
function (argument) {
return 'CC-' + argument + '-4.0'
2016-10-10 03:43:44 +02:00
},
// e.g. 'Attribution-NonCommercial'
2018-09-20 02:56:13 +02:00
function (argument) {
2016-10-10 03:43:44 +02:00
return argument
.replace('Attribution', 'BY')
.replace('NonCommercial', 'NC')
.replace('NoDerivatives', 'ND')
.replace(/ (\d)/, '-$1')
2018-09-20 02:56:13 +02:00
.replace(/ ?International/, '')
2016-10-10 03:43:44 +02:00
},
// e.g. 'Attribution-NonCommercial'
2018-09-20 02:56:13 +02:00
function (argument) {
2016-10-10 03:43:44 +02:00
return 'CC-' +
argument
2018-09-20 02:56:13 +02:00
.replace('Attribution', 'BY')
.replace('NonCommercial', 'NC')
.replace('NoDerivatives', 'ND')
.replace(/ (\d)/, '-$1')
.replace(/ ?International/, '') +
'-4.0'
2016-10-10 03:43:44 +02:00
}
2018-09-20 02:56:13 +02:00
]
var licensesWithVersions = spdxLicenseIds
.map(function (id) {
var match = /^(.*)-\d+\.\d+$/.exec(id)
return match
? [match[0], match[1]]
: [id, null]
})
.reduce(function (objectMap, item) {
var key = item[1]
objectMap[key] = objectMap[key] || []
objectMap[key].push(item[0])
return objectMap
}, {})
var licensesWithOneVersion = Object.keys(licensesWithVersions)
.map(function makeEntries (key) {
return [key, licensesWithVersions[key]]
})
.filter(function identifySoleVersions (item) {
return (
// Licenses has just one valid version suffix.
item[1].length === 1 &&
item[0] !== null &&
// APL will be considered Apache, rather than APL-1.0
item[0] !== 'APL'
)
})
.map(function createLastResorts (item) {
return [item[0], item[1][0]]
})
licensesWithVersions = undefined
2016-10-10 03:43:44 +02:00
// If all else fails, guess that strings containing certain substrings
// meant to identify certain licenses.
var lastResorts = [
['UNLI', 'Unlicense'],
['WTF', 'WTFPL'],
['2 CLAUSE', 'BSD-2-Clause'],
['2-CLAUSE', 'BSD-2-Clause'],
['3 CLAUSE', 'BSD-3-Clause'],
['3-CLAUSE', 'BSD-3-Clause'],
2018-09-20 02:56:13 +02:00
['AFFERO', 'AGPL-3.0-or-later'],
['AGPL', 'AGPL-3.0-or-later'],
2016-10-10 03:43:44 +02:00
['APACHE', 'Apache-2.0'],
['ARTISTIC', 'Artistic-2.0'],
2018-09-20 02:56:13 +02:00
['Affero', 'AGPL-3.0-or-later'],
2016-10-10 03:43:44 +02:00
['BEER', 'Beerware'],
['BOOST', 'BSL-1.0'],
['BSD', 'BSD-2-Clause'],
2018-09-20 02:56:13 +02:00
['CDDL', 'CDDL-1.1'],
2016-10-10 03:43:44 +02:00
['ECLIPSE', 'EPL-1.0'],
['FUCK', 'WTFPL'],
2018-09-20 02:56:13 +02:00
['GNU', 'GPL-3.0-or-later'],
['LGPL', 'LGPL-3.0-or-later'],
['GPLV1', 'GPL-1.0-only'],
['GPLV2', 'GPL-2.0-only'],
['GPL', 'GPL-3.0-or-later'],
['MIT +NO-FALSE-ATTRIBS', 'MITNFA'],
2016-10-10 03:43:44 +02:00
['MIT', 'MIT'],
['MPL', 'MPL-2.0'],
['X11', 'X11'],
['ZLIB', 'Zlib']
2018-09-20 02:56:13 +02:00
].concat(licensesWithOneVersion)
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
var SUBSTRING = 0
var IDENTIFIER = 1
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
var validTransformation = function (identifier) {
2016-10-10 03:43:44 +02:00
for (var i = 0; i < transforms.length; i++) {
2018-09-20 02:56:13 +02:00
var transformed = transforms[i](identifier).trim()
2016-10-10 03:43:44 +02:00
if (transformed !== identifier && valid(transformed)) {
2018-09-20 02:56:13 +02:00
return transformed
2016-10-10 03:43:44 +02:00
}
}
2018-09-20 02:56:13 +02:00
return null
}
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
var validLastResort = function (identifier) {
var upperCased = identifier.toUpperCase()
2016-10-10 03:43:44 +02:00
for (var i = 0; i < lastResorts.length; i++) {
2018-09-20 02:56:13 +02:00
var lastResort = lastResorts[i]
2016-10-10 03:43:44 +02:00
if (upperCased.indexOf(lastResort[SUBSTRING]) > -1) {
2018-09-20 02:56:13 +02:00
return lastResort[IDENTIFIER]
2016-10-10 03:43:44 +02:00
}
}
2018-09-20 02:56:13 +02:00
return null
}
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
var anyCorrection = function (identifier, check) {
2016-10-10 03:43:44 +02:00
for (var i = 0; i < transpositions.length; i++) {
2018-09-20 02:56:13 +02:00
var transposition = transpositions[i]
var transposed = transposition[TRANSPOSED]
2016-10-10 03:43:44 +02:00
if (identifier.indexOf(transposed) > -1) {
var corrected = identifier.replace(
transposed,
transposition[CORRECT]
2018-09-20 02:56:13 +02:00
)
var checked = check(corrected)
2016-10-10 03:43:44 +02:00
if (checked !== null) {
2018-09-20 02:56:13 +02:00
return checked
2016-10-10 03:43:44 +02:00
}
}
}
2018-09-20 02:56:13 +02:00
return null
}
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
module.exports = function (identifier) {
var validArugment = (
typeof identifier === 'string' &&
identifier.trim().length !== 0
)
if (!validArugment) {
throw Error('Invalid argument. Expected non-empty string.')
}
identifier = identifier.replace(/\+$/, '').trim()
2016-10-10 03:43:44 +02:00
if (valid(identifier)) {
2018-09-20 02:56:13 +02:00
return upgradeGPLs(identifier)
2016-10-10 03:43:44 +02:00
}
2018-09-20 02:56:13 +02:00
var transformed = validTransformation(identifier)
2016-10-10 03:43:44 +02:00
if (transformed !== null) {
2018-09-20 02:56:13 +02:00
return upgradeGPLs(transformed)
2016-10-10 03:43:44 +02:00
}
2018-09-20 02:56:13 +02:00
transformed = anyCorrection(identifier, function (argument) {
2016-10-10 03:43:44 +02:00
if (valid(argument)) {
2018-09-20 02:56:13 +02:00
return argument
2016-10-10 03:43:44 +02:00
}
2018-09-20 02:56:13 +02:00
return validTransformation(argument)
})
2016-10-10 03:43:44 +02:00
if (transformed !== null) {
2018-09-20 02:56:13 +02:00
return upgradeGPLs(transformed)
2016-10-10 03:43:44 +02:00
}
2018-09-20 02:56:13 +02:00
transformed = validLastResort(identifier)
2016-10-10 03:43:44 +02:00
if (transformed !== null) {
2018-09-20 02:56:13 +02:00
return upgradeGPLs(transformed)
2016-10-10 03:43:44 +02:00
}
2018-09-20 02:56:13 +02:00
transformed = anyCorrection(identifier, validLastResort)
2016-10-10 03:43:44 +02:00
if (transformed !== null) {
2018-09-20 02:56:13 +02:00
return upgradeGPLs(transformed)
2016-10-10 03:43:44 +02:00
}
2018-09-20 02:56:13 +02:00
return null
}
function upgradeGPLs (value) {
if ([
'GPL-1.0', 'LGPL-1.0', 'AGPL-1.0',
'GPL-2.0', 'LGPL-2.0', 'AGPL-2.0',
'LGPL-2.1'
].indexOf(value) !== -1) {
return value + '-only'
} else if (['GPL-3.0', 'LGPL-3.0', 'AGPL-3.0'].indexOf(value) !== -1) {
return value + '-or-later'
} else {
return value
}
}