normalize-pkg
Normalize values in package.json using the map-schema library.
Last updated 5 months ago by jonschlinkert .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install normalize-pkg 
SYNC missed versions from official npm registry.

normalize-pkg Donate NPM version NPM monthly downloads NPM total downloads Build Status

Normalize values in package.json using the map-schema library.

Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your :heart: and support.

Table of Contents

<summary> Details</summary>

Install

Install with npm (requires Node.js >= 0.10.0):

$ npm install --save normalize-pkg

Install

Install with bower

$ bower install normalize-pkg --save

Usage

var config = require('./')();
var pkg = config.normalize(require('./package'));

Features

Normalizes most package.json fields, and:

  • converts repository objects to a string
  • stringifies author object
  • stringifies each "person" object in maintainers, contributors and collaborators
  • converts licenses arrays and objects to a license string
  • removes files that don't exist from bin, main and the files array
  • adds cli.js to bin if it exists
  • creates keywords array from name if not defined

See the schema, normalizers, and unit tests for more examples.

Schema

Values are normalized using a schema that is passed to map-schema.

  • only properties that have a corresponding field on the schema will be normalized.
  • any properties that do not have a corresponding field are returned unmodified.

See the .field docs to learn how to add or overwrite a field on the schema.

Defaults

A default value may optionally be defined when a .field is registered. When .normalize is run and a property that is required or recommended by npm is missing, normalize-pkg attempts to create the field if valid data can be found in the repository.

built-in fields have a default value:

  • version: '0.1.0'
  • license: 'MIT'
  • engines: {node: '>= 0.10.0'}

For example:

  • name: the project-name library is used to fill in the name
  • bin: if empty, populated with cli.js or bin if either exists on the file system

Example

The following:

var config = require('./')();

// no package.json is passed, just an empty object
var pkg = config.normalize({});
console.log(pkg);

Results

Since an empty object was passed, normalize-pkg was smart enough to fill in missing fields looking for info in the project. In this case, specifically from parsing .git config and using any defaults defined on the schema.

{ name: 'normalize-pkg',
  version: '0.1.0',
  homepage: 'https://github.com/jonschlinkert/normalize-pkg',
  repository: 'jonschlinkert/normalize-pkg',
  license: 'MIT',
  files: [ 'index.js' ],
  main: 'index.js',
  engines: { node: '>= 0.10.0' } }

API

Params

  • options {Object}

Example

const config = new NormalizePkg();
const pkg = config.normalize({
  author: {
    name: 'Jon Schlinkert',
    url: 'https://github.com/jonschlinkert'
  }
});
console.log(pkg);
//=> {author: 'Jon Schlinkert (https://github.com/jonschlinkert)'}
  • normalize {Function}: function to be called on the value when the .normalize method is called
  • default {any}: default value to be used when the package.json property is undefined.
  • required {Boolean}: define true if the property is required

Params

  • name {String}: Field name (required)
  • type {String|Array}: One or more native javascript types allowed for the property value (required)
  • options {Object}
  • returns {Object}: Returns the instance

Example

const config = new NormalizePkg();

config.field('foo', 'string', {
  default: 'bar'
});

const pkg = config.normalize({});
console.log(pkg);
//=> {foo:  'bar'}

Params

  • pkg {Object}: The package.json object to normalize
  • options {Object}
  • returns {Object}: Returns a normalized package.json object.

Example

const config = new NormalizePkg();
const pkg = config.normalize(require('./package.json'));

Options

options.knownOnly

Type: boolean

Default: undefined

Omit properties from package.json that do not have a field registered on the schema.

var Config = require('normalize-pkg');
var config = new Config({knownOnly: true});

var pkg = config.normalize({name: 'my-project', foo: 'bar'});
console.log(pkg);
//=> {name: 'my-project'}

options.pick

Type: array

Default: undefined

Filter the resulting object to contain only the specified keys.

options.omit

Type: array

Default: undefined

Remove the specified keys from the resulting object.

options.fields

Pass a fields object on the options to customize any fields on the schema (also see options.extend):

var pkg = config.normalize(require('./package'), {
  extend: true,
  fields: {
    name: {
      normalize: function() {
        return 'bar'
      }
    }
  }
});

console.log(pkg.name);
//=> 'bar'

options.extend

Type: boolean

Default: undefined

Used with options.field, pass true if you want to extend a field that is already defined on the schema.

var pkg = config.normalize(require('./package'), {
  extend: true,
  fields: {
    name: {
      normalize: function() {
        return 'bar'
      }
    }
  }
});

console.log(pkg.name);
//=> 'bar'

About

<summary> Contributing</summary>

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

<summary> Running Tests</summary>

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

$ npm install && npm test
<summary> Building docs</summary>

(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)

To generate the readme, run the following command:

$ npm install -g verbose/verb#dev verb-generate-readme && verb

Related projects

You might also be interested in these projects:

update: Be scalable! Update is a new, open source developer framework and CLI for automating updates… more | homepage

Contributors

Commits Contributor
154 jonschlinkert
16 doowb
2 pdehaan

Author

Jon Schlinkert

License

Copyright © 2020, Jon Schlinkert. Released under the MIT License.


This file was generated by verb-generate-readme, v0.8.0, on March 01, 2020.

Current Tags

  • 0.5.0                                ...           latest (5 months ago)

46 Versions

  • 0.5.0                                ...           5 months ago
  • 0.4.1                                ...           3 years ago
  • 0.4.0                                ...           4 years ago
  • 0.3.20                                ...           4 years ago
  • 0.3.19                                ...           4 years ago
  • 0.3.18                                ...           4 years ago
  • 0.3.17                                ...           4 years ago
  • 0.3.16                                ...           4 years ago
  • 0.3.15                                ...           4 years ago
  • 0.3.14                                ...           4 years ago
  • 0.3.13                                ...           4 years ago
  • 0.3.12                                ...           4 years ago
  • 0.3.11                                ...           4 years ago
  • 0.3.10                                ...           4 years ago
  • 0.3.9                                ...           4 years ago
  • 0.3.8                                ...           4 years ago
  • 0.3.7                                ...           4 years ago
  • 0.3.6                                ...           4 years ago
  • 0.3.5                                ...           4 years ago
  • 0.3.4                                ...           4 years ago
  • 0.3.3                                ...           4 years ago
  • 0.3.2                                ...           4 years ago
  • 0.3.1                                ...           4 years ago
  • 0.3.0                                ...           4 years ago
  • 0.2.5                                ...           5 years ago
  • 0.2.4                                ...           5 years ago
  • 0.2.3                                ...           5 years ago
  • 0.2.2                                ...           5 years ago
  • 0.2.1                                ...           5 years ago
  • 0.2.0                                ...           5 years ago
  • 0.1.7                                ...           6 years ago
  • 0.1.6                                ...           6 years ago
  • 0.1.5                                ...           6 years ago
  • 0.1.4                                ...           6 years ago
  • 0.1.3                                ...           6 years ago
  • 0.1.2                                ...           6 years ago
  • 0.1.1                                ...           6 years ago
  • 0.1.0                                ...           6 years ago
  • 0.0.9                                ...           6 years ago
  • 0.0.8                                ...           6 years ago
  • 0.0.6                                ...           6 years ago
  • 0.0.5                                ...           6 years ago
  • 0.0.4                                ...           6 years ago
  • 0.0.3                                ...           6 years ago
  • 0.0.2                                ...           6 years ago
  • 0.0.1                                ...           6 years ago
Maintainers (2)
Downloads
Today 3
This Week 520
This Month 583
Last Day 139
Last Week 765
Last Month 2,974
Dependencies (15)
Dev Dependencies (4)

Copyright 2014 - 2017 © taobao.org |