npm / normalize-package-data

normalizes package metadata, typically found in package.json file.
Other
196 stars 48 forks source link
npm-cli

normalize-package-data

Build Status

normalize-package-data exports a function that normalizes package metadata. This data is typically found in a package.json file, but in principle could come from any source - for example the npm registry.

normalize-package-data is used by read-package-json to normalize the data it reads from a package.json file. In turn, read-package-json is used by npm and various npm-related tools.

Installation

npm install normalize-package-data

Usage

Basic usage is really simple. You call the function that normalize-package-data exports. Let's call it normalizeData.

normalizeData = require('normalize-package-data')
packageData = require("./package.json")
normalizeData(packageData)
// packageData is now normalized

Strict mode

You may activate strict validation by passing true as the second argument.

normalizeData = require('normalize-package-data')
packageData = require("./package.json")
normalizeData(packageData, true)
// packageData is now normalized

If strict mode is activated, only Semver 2.0 version strings are accepted. Otherwise, Semver 1.0 strings are accepted as well. Packages must have a name, and the name field must not have contain leading or trailing whitespace.

Warnings

Optionally, you may pass a "warning" function. It gets called whenever the normalizeData function encounters something that doesn't look right. It indicates less than perfect input data.

normalizeData = require('normalize-package-data')
packageData = require("./package.json")
warnFn = function(msg) { console.error(msg) }
normalizeData(packageData, warnFn)
// packageData is now normalized. Any number of warnings may have been logged.

You may combine strict validation with warnings by passing true as the second argument, and warnFn as third.

When private field is set to true, warnings will be suppressed.

Potential exceptions

If the supplied data has an invalid name or version field, normalizeData will throw an error. Depending on where you call normalizeData, you may want to catch these errors so can pass them to a callback.

What normalization (currently) entails

Rules for name field

If name field is given, the value of the name field must be a string. The string may not:

Rules for version field

If version field is given, the value of the version field must be a valid semver string, as determined by the semver.valid method. See documentation for the semver module.

Rules for license field

The license/licence field should be a valid SPDX license expression or one of the special values allowed by validate-npm-package-license. See documentation for the license field in package.json.

Credits

This package contains code based on read-package-json written by Isaac Z. Schlueter. Used with permission.

License

normalize-package-data is released under the BSD 2-Clause License. Copyright (c) 2013 Meryn Stol