expand-pkg
Parse string values in package.json into objects.
Last updated 8 years ago by doowb .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install expand-pkg 
SYNC missed versions from official npm registry.

expand-pkg NPM version NPM downloads Linux Build Status

Parse string values in package.json into objects.

You might also be interested in normalize-pkg.

Install

Install with npm:

$ npm install --save expand-pkg

Usage

var Config = require('./');
var config = new Config();
console.log(config.expand(require('./package')));

Schema

Values are parsed using a schema that is passed to map-schema (builds on the schema from normalize-pkg as a starting point):

  • only properties that have a corresponding field on the schema will be parsed.
  • 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 .expand is run and a property that is required or recommended by npm is missing, expand-pkg attempts to create the field if valid data can be found in the repository.

The following fields are the only built-in fields with default values:

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

API

Config

Create an instance of Config with the given options.

Example

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

Params

  • options {Object}

.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 given package.json value when the .expand 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

Example

var config = new Config();

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

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

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

.expand

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

Example

var config = new Config();
var pkg = config.expand(require('./package.json'));

Params

  • pkg {Object}: The package.json object to expand
  • options {Object}
  • returns {Object}: Returns an expanded package.json object.

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('expand-pkg');
var config = new Config({knownOnly: true});

console.log(config.expand({author: 'Brian Woodward', foo: 'bar'}));
//=> {author: {name: 'Brian Woodward'}}

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.expand(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.expand(require('./package'), {
  extend: true,
  fields: {
    name: {
      normalize: function() {
        return 'bar'
      }
    }
  }
});

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

About

Related projects

Contributing

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

Contributors

Commits Contributor
27 jonschlinkert
4 doowb

Building docs

(This document was generated by verb-generate-readme (a verb generator), please don't edit the readme directly. Any changes to the readme must be made in .verb.md.)

To generate the readme and API documentation with verb:

$ npm install -g verb verb-generate-readme && 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-generate-readme, v0.2.0, on December 02, 2016.

Current Tags

  • 0.1.9                                ...           latest (6 years ago)

10 Versions

  • 0.1.9                                ...           6 years ago
  • 0.1.8                                ...           8 years ago
  • 0.1.7                                ...           8 years ago
  • 0.1.6                                ...           8 years ago
  • 0.1.5                                ...           8 years ago
  • 0.1.4                                ...           8 years ago
  • 0.1.3                                ...           9 years ago
  • 0.1.2                                ...           9 years ago
  • 0.1.1                                ...           9 years ago
  • 0.1.0                                ...           9 years ago
Maintainers (2)
Downloads
Total 0
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (13)
Dev Dependencies (10)

© 2010 - cnpmjs.org x YWFE | Home | YWFE