New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

normalize-pkg

Package Overview
Dependencies
Maintainers
1
Versions
46
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

normalize-pkg

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

  • 0.3.11
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
26K
increased by1.19%
Maintainers
1
Weekly downloads
 
Created
Source

normalize-pkg NPM version NPM downloads Build Status

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

Install

Install with npm:

$ npm install normalize-pkg --save

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' },
  keywords: [ 'normalize', 'pkg' ] }

API

NormalizePkg

Create an instance of NormalizePkg with the given options.

Params

  • options {Object}

Example

var config = new NormalizePkg();
var pkg = config.normalize({
  author: {
    name: 'Jon Schlinkert',
    url: 'https://github.com/jonschlinkert'
  }
});
console.log(pkg);
//=> {author: 'Jon Schlinkert (https://github.com/jonschlinkert)'}

.field

Add a field to the schema, or overwrite or extend an existing field. The last argument is an options object that supports the following properties:

  • 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

var config = new NormalizePkg();

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

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

.normalize

Iterate over pkg properties and normalize values that have corresponding fields registered on the schema.

Params

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

Example

var config = new NormalizePkg();
var 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'

You might also be interested in these projects:

update: Easily keep anything in your project up-to-date by installing the updaters you want to use… more | homepage

Contributing

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

Building docs

Generate readme and API documentation with verb:

$ npm install verb && npm run docs

Or, if verb is installed globally:

$ verb

Running tests

Install dev dependencies:

$ npm install -d && npm test

Author

Jon Schlinkert

License

Copyright © 2016, Jon Schlinkert. Released under the MIT license.


This file was generated by verb, v0.9.0, on April 26, 2016.

Keywords

FAQs

Package last updated on 27 Apr 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc