forked from morficus/license-ls
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhancing how the SPDX ID is extracted from package.json
* hardened the extraction process so it can deal with old packages that do not follow the new “license” field format * expanding the output to include individual parts of the computed “license” output (licenseId, licenseFullName and licenseFilePath) * including module path output when used as a module
- Loading branch information
Showing
9 changed files
with
105 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const { isString, isObject, isArray, compact } = require('lodash') | ||
|
||
/** | ||
* Deal with all the crazy stuff the "license" field in package.json can have and return only the SPDX ID (if any) | ||
* | ||
* @param {*} license | ||
* @returns {string} | ||
*/ | ||
module.exports = function extractLicenseText(license) { | ||
let licenseText | ||
if (isString(license)) { | ||
licenseText = license | ||
|
||
} else if (isArray(license)) { | ||
const text = license.map(extractLicenseText) | ||
licenseText = compact(text).join(' AND ') | ||
|
||
} else if (isObject(license)) { | ||
licenseText = license.type | ||
|
||
} else { | ||
licenseText = '' | ||
} | ||
|
||
return licenseText | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
const extractLicenseText = require('../helpers/extract-license-id') | ||
const test = require('ava') | ||
|
||
test('Can deal with normal things... like text', t => { | ||
const payload = 'MIT' | ||
const expected = 'MIT' | ||
const actual = extractLicenseText(payload) | ||
|
||
t.is(actual, expected) | ||
}) | ||
|
||
test('Can deal with objects', t => { | ||
const payload = { type: 'MIT', url: 'some-some' } | ||
const expected = 'MIT' | ||
const actual = extractLicenseText(payload) | ||
|
||
t.is(actual, expected) | ||
}) | ||
|
||
test('Can deal with an array with a single object', t => { | ||
const payload = [{ type: 'MIT', url: 'some-some' }] | ||
const expected = 'MIT' | ||
const actual = extractLicenseText(payload) | ||
|
||
t.is(actual, expected) | ||
}) | ||
|
||
test('Can deal with an array with multiple objects', t => { | ||
const payload = [{ type: 'MIT', url: 'some-some' }, { type: 'ISC', url: 'some-some' }] | ||
const expected = 'MIT AND ISC' | ||
const actual = extractLicenseText(payload) | ||
|
||
t.is(actual, expected) | ||
}) | ||
|
||
test('Can deal with funky arrays', t => { | ||
const payload = [1, '', 'MIT'] | ||
const expected = 'MIT' | ||
const actual = extractLicenseText(payload) | ||
|
||
t.is(actual, expected) | ||
}) | ||
|
||
test('Can deal with REALLY funky arrays', t => { | ||
const payload = [1, '', 'MIT', { type: 'ISC', url: 'some-url' }] | ||
const expected = 'MIT AND ISC' | ||
const actual = extractLicenseText(payload) | ||
|
||
t.is(actual, expected) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters