├── .gitignore ├── README.md ├── example.js ├── index.js ├── package.json └── test └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # normalize-license-data 2 | 3 | clean up licenses from package.json files and the npm registry 4 | 5 | ## Installation 6 | 7 | Download node at [nodejs.org](http://nodejs.org) and install it, if you haven't already. 8 | 9 | ```sh 10 | npm install normalize-license-data --save 11 | ``` 12 | 13 | ## Usage 14 | 15 | ```js 16 | var normalize = require("normalize-license-data") 17 | 18 | normalize(null) 19 | // => null 20 | 21 | normalize('') 22 | // => null 23 | 24 | normalize('MIT') 25 | // => {name: 'MIT', url: 'http://opensource.org/licenses/MIT'} 26 | 27 | normalize('BSD') 28 | // => {name: 'BSD', url: 'http://opensource.org/licenses/BSD-2-Clause'} 29 | 30 | normalize('unfamiliar') 31 | // => {name: 'unfamiliar'} 32 | 33 | normalize({name: 'wtfpl', url: 'https:///wtfpl.net'}) 34 | // => {name: 'wtfpl', url: 'https:///wtfpl.net'} 35 | 36 | normalize({type: 'wtfpl', url: 'https:///wtfpl.net'}) 37 | // => {name: 'wtfpl', url: 'https:///wtfpl.net'} 38 | 39 | normalize('https://custom-license.com') 40 | // => {name: 'custom-license.com', url: 'https:///custom-license.com'} 41 | 42 | ``` 43 | 44 | ## Tests 45 | 46 | ```sh 47 | npm install 48 | npm test 49 | ``` 50 | 51 | ## Dependencies 52 | 53 | - [is_js](https://github.com/arasatasaygin/is.js): micro check library 54 | - [lodash.clone](https://github.com/lodash/lodash): The modern build of lodash’s `_.clone` as a module. 55 | - [oss-license-name-to-url](https://github.com/npm/oss-license-name-to-url): Convert shorthand OSS license names to opensource.org URLs 56 | - [schemeless](https://github.com/zeke/schemeless): Remove the parts of URLs that humans don't really need to see 57 | 58 | ## Dev Dependencies 59 | 60 | - [code](https://github.com/hapijs/code): assertion library 61 | - [mocha](https://github.com/mochajs/mocha): simple, flexible, fun test framework 62 | - [package-json-to-readme](https://github.com/zeke/package-json-to-readme): Generate a README.md from package.json contents 63 | - [standard](https://github.com/feross/standard): JavaScript Standard Style 64 | 65 | 66 | ## License 67 | 68 | MIT 69 | 70 | _Generated by [package-json-to-readme](https://github.com/zeke/package-json-to-readme)_ 71 | -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | var normalize = require('./') 2 | 3 | normalize(null) 4 | // => null 5 | 6 | normalize('') 7 | // => null 8 | 9 | normalize('MIT') 10 | // => {name: 'MIT', url: 'http://opensource.org/licenses/MIT'} 11 | 12 | normalize('BSD') 13 | // => {name: 'BSD', url: 'http://opensource.org/licenses/BSD-2-Clause'} 14 | 15 | normalize('unfamiliar') 16 | // => {name: 'unfamiliar'} 17 | 18 | normalize({name: 'wtfpl', url: 'https:///wtfpl.net'}) 19 | // => {name: 'wtfpl', url: 'https:///wtfpl.net'} 20 | 21 | normalize({type: 'wtfpl', url: 'https:///wtfpl.net'}) 22 | // => {name: 'wtfpl', url: 'https:///wtfpl.net'} 23 | 24 | normalize('https://custom-license.com') 25 | // => {name: 'custom-license.com', url: 'https:///custom-license.com'} 26 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var clone = require('lodash.clone') 2 | var is = require('is_js') 3 | var schemeless = require('schemeless') 4 | var nameToUrl = require('oss-license-name-to-url') 5 | 6 | module.exports = function normalizeLicense (license) { 7 | if (is.falsy(license)) return null 8 | if (is.empty(license)) return null 9 | 10 | // Prevent modification of the source object 11 | license = clone(license) 12 | 13 | // Legacy, I presume 14 | if (is.array(license)) license = license[0] 15 | 16 | // Convert `type` to `name` 17 | if (is.object(license)) { 18 | if (license.type) { 19 | license.name = license.type 20 | delete license.type 21 | } 22 | // Honor URL if present, otherwise guess 23 | license.url = license.url || nameToUrl(license.name) 24 | return license 25 | } 26 | 27 | if (is.url(license)) { 28 | return {name: schemeless(license), url: license} 29 | } 30 | 31 | if (is.string(license)) { 32 | license = {name: license, url: nameToUrl(license)} 33 | if (!license.url) delete license.url 34 | return license 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "normalize-license-data", 3 | "version": "1.0.1", 4 | "description": "clean up licenses from package.json files and the npm registry", 5 | "main": "index.js", 6 | "scripts": { 7 | "readme": "package-json-to-readme package.json > README.md", 8 | "pretest": "npm run readme", 9 | "test": "standard --format && mocha" 10 | }, 11 | "repository": { 12 | "type": "git", 13 | "url": "https://github.com/npm/normalize-license-data.git" 14 | }, 15 | "keywords": [ 16 | "license", 17 | "npm", 18 | "normalize" 19 | ], 20 | "author": "Zeke Sikelianos (http://zeke.sikelianos.com/)", 21 | "license": "MIT", 22 | "bugs": { 23 | "url": "https://github.com/npm/normalize-license-data/issues" 24 | }, 25 | "homepage": "https://github.com/npm/normalize-license-data", 26 | "devDependencies": { 27 | "code": "^1.4.0", 28 | "mocha": "^2.2.1", 29 | "package-json-to-readme": "^1.4.1", 30 | "standard": "^3.3.1" 31 | }, 32 | "dependencies": { 33 | "is_js": "^0.7.3", 34 | "lodash.clone": "^3.0.1", 35 | "oss-license-name-to-url": "^1.2.0", 36 | "schemeless": "^1.1.1" 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | /* global describe, it */ 2 | 3 | var normalize = require('..') 4 | var expect = require('code').expect 5 | 6 | describe('normalize-license-data', function () { 7 | it('returns null for null values', function () { 8 | expect(normalize(null)).to.equal(null) 9 | }) 10 | 11 | it('returns null for undefined values', function () { 12 | expect(normalize(null)).to.equal(null) 13 | }) 14 | 15 | it('returns null for empty strings', function () { 16 | expect(normalize('')).to.equal(null) 17 | }) 18 | 19 | it('creates opensource.org-based license.url for valid string license names', function () { 20 | var license = normalize('MIT') 21 | expect(license.name).to.equal('MIT') 22 | expect(license.url).to.equal('http://opensource.org/licenses/MIT') 23 | }) 24 | 25 | it('does not create license.url for unrecognized license names', function () { 26 | var license = normalize('something-crazy') 27 | expect(license.name).to.equal('something-crazy') 28 | expect(license.url).to.not.exist() 29 | }) 30 | 31 | it('converts license.type to license.name', function () { 32 | var license = normalize({ 33 | type: 'MIT', 34 | url: 'https://github.com/isaacs/abbrev-js/raw/master/LICENSE' 35 | }) 36 | expect(license.name).to.equal('MIT') 37 | expect(license.type).to.not.exist() 38 | expect(license.url).to.equal('https://github.com/isaacs/abbrev-js/raw/master/LICENSE') 39 | }) 40 | 41 | it('leaves license.url untouched if it exists', function () { 42 | var license = normalize({ 43 | type: 'MIT', 44 | url: 'https://github.com/isaacs/abbrev-js/raw/master/LICENSE' 45 | }) 46 | expect(license.name).to.equal('MIT') 47 | expect(license.url).to.equal('https://github.com/isaacs/abbrev-js/raw/master/LICENSE') 48 | }) 49 | 50 | it('accepts URL strings, using a schemeless value for name', function () { 51 | var license = normalize('https://example.com') 52 | expect(license.name).to.equal('example.com') 53 | expect(license.url).to.equal('https://example.com') 54 | }) 55 | 56 | it('does not mutate the source object', function () { 57 | var original = { 58 | type: 'MIT', 59 | url: 'https://github.com/isaacs/abbrev-js/raw/master/LICENSE' 60 | } 61 | var normalized = normalize(original) 62 | 63 | expect(original.type).to.equal('MIT') 64 | expect(original.name).to.not.exist() 65 | expect(normalized.type).to.not.exist() 66 | expect(normalized.name).to.equal('MIT') 67 | }) 68 | 69 | }) 70 | --------------------------------------------------------------------------------