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 - npm Package Compare versions

Comparing version 0.1.7 to 0.2.0

lib/fields/username.js

276

index.js

@@ -1,236 +0,102 @@

/*!
* normalize-pkg <https://github.com/jonschlinkert/normalize-pkg>
*
* Copyright (c) 2014 Jon Schlinkert, contributors.
* Licensed under the MIT license.
*/
'use strict';
var keywords = require('normalize-keywords');
var isObject = require('is-plain-object');
var extend = require('mixin-deep');
var typeOf = require('kind-of');
var schema = require('./lib/schema');
var utils = require('./lib/utils');
var log = require('verbalize');
log.runner = 'normalize-pkg';
var mapping = {'license': 'licenses'};
var normalize = module.exports = {};
/**
* Author
*
* @param {Object} pkg
* @return {Object} normalized author
* Normalize package.json with the given `options`
*/
normalize.author = function (pkg, options) {
options = extend({}, options);
pkg = extend({}, pkg);
function normalize(pkg, options) {
options = options || {};
pkg = pkg || {};
var name = '';
var url = '';
if (isObject(pkg.author)) {
utils.msg.isCorrect('author');
if (pkg.analyze === false) {
console.log('normalize-pkg: "analyze: false" is defined in package.json.');
return pkg;
} else if (pkg.author == null) {
if (pkg.authors && pkg.authors.length === 1) {
utils.msg.fixingProperty('author');
name = pkg.authors[0].name;
url = pkg.authors[0].url;
delete pkg.authors;
} else {
utils.msg.isMissing('author');
utils.msg.addingProperty('author');
}
} else if (typeOf(pkg.author)) {
utils.msg.isMissing('author');
utils.msg.fixingProperty('author');
name = pkg.author;
} else {
return utils.msg.isMalformed('author');
}
pkg.author = {
name: name,
url: url
};
pkg = rename(pkg, options.mapping || mapping);
return pkg;
};
var defaults = schema(options);
var opts = utils.merge({}, defaults, options);
var keys = Object.keys(opts);
var diff = utils.omit(pkg, keys);
var fns = [];
var ctx = utils.merge({}, pkg);
utils.define(ctx, 'set', function (key, val) {
utils.set(this, key, val, options);
return this;
});
/**
* Repository
*
* @param {Object} pkg
* @return {Object} normalized repository
*/
for (var key in defaults) {
if (!pkg.hasOwnProperty(key) && options.extend === false) {
continue;
}
normalize.repo = function (pkg, options) {
options = options || {};
var type = '', url = '';
var val = utils.extend({}, defaults[key], options[key]);
var value = pkg[key];
if (isObject(pkg.repository)) {
utils.msg.isCorrect('repository');
return pkg;
if (typeof val.value === 'function') {
var res = val.value.call(ctx, key, value, pkg);
if (typeof res === 'function') {
fns.push({name: key, fn: res});
} else {
pkg[key] = res;
}
}
} else if (typeOf(pkg.repository) === 'undefined') {
if (pkg.repositories && pkg.repositories.length === 1) {
utils.msg.isMissing('repository');
utils.msg.addingProperty('repository');
type = pkg.repositories[0].type;
url = pkg.repositories[0].url;
delete pkg.repositories;
} else {
utils.msg.isMissing('repository');
utils.msg.addingProperty('repository');
if (!pkg[key]) {
if (pkg[key] === null) {
delete pkg[key];
} if (val.add) {
pkg[key] = val.value;
} else if (!pkg[key] && typeof val.default !== 'undefined') {
pkg[key] = val.default;
} else {
delete pkg[key];
}
}
} else if (typeOf(pkg.repository) === 'string') {
utils.msg.fixingProperty('repository');
url = pkg.repository;
if (utils.contains(pkg.repository, 'git')) {
type = 'git';
if (pkg[key] && utils.typeOf(pkg[key]) !== val.type) {
throw new TypeError('expected ' + key + ' to be type: ' + val.type);
}
}
} else {
// If none of the above, something is amiss
return utils.msg.isMalformed('repository');
if (fns.length) {
fns.forEach(function(field) {
var key = field.name;
var fn = field.fn;
fn(key, pkg[key], pkg, defaults);
});
}
pkg.repository = {
type: type,
url: url
};
// sort keys
var res = {};
var len = keys.length;
var i = -1;
return pkg;
};
/**
* Bugs
*
* @param {Object} pkg
* @return {Object} normalized bugs
*/
normalize.bugs = function (pkg, options) {
options = options || {};
var url = '';
if (isObject(pkg.bugs)) {
utils.msg.isCorrect('bugs');
return pkg;
} else if (typeOf(pkg.bugs) === 'undefined') {
utils.msg.isMissing('bugs');
utils.msg.addingProperty('bugs');
} else if (typeOf(pkg.bugs) === 'string') {
utils.msg.fixingProperty('bugs');
url = pkg.bugs;
} else {
// If none of the above, something is amiss
return utils.msg.isMalformed('bugs');
while (++i < len) {
var key = keys[i];
if (pkg.hasOwnProperty(key)) {
res[key] = pkg[key];
}
}
pkg.bugs = {
url: url
};
return pkg;
utils.merge(res, diff);
return res;
};
/**
* License
*
* @param {Object} pkg
* @return {Object} normalized license
*/
normalize.license = function (pkg, options) {
options = options || {};
var type = '', url = '';
// Already formatted as an array, return.
if (pkg.licenses && pkg.licenses.length > 0) {
utils.msg.isCorrect('licenses');
return pkg;
} else if (isObject(pkg.license)) {
utils.msg.fixingProperty('license');
type = pkg.license.type;
url = pkg.license.url;
delete pkg.license;
} else if (typeOf(pkg.license) === 'string') {
utils.msg.fixingProperty('license');
if (options.license && options.license === true) {
return pkg;
function rename(pkg, mapping) {
for (var key in mapping) {
var val = mapping[key];
if (pkg.hasOwnProperty(val)) {
pkg[key] = pkg[val];
delete pkg[val];
}
var inferred = utils.inferLicenseURL(pkg.license);
type = inferred.type;
url = inferred.url;
delete pkg.license;
} else if (typeOf(pkg.license) === 'undefined') {
utils.msg.isMissing('license');
utils.msg.addingProperty('license');
} else {
// If none of the above, something is amiss
utils.msg.isMalformed('license');
}
pkg.licenses = [{
type: type,
url: url
}];
return pkg;
};
}
/**
* Keywords
*
* @param {Object} pkg
* @return {Object} normalized keywords
*/
normalize.keywords = function(pkg, options) {
pkg.keywords = keywords(pkg.keywords || []);
return pkg;
};
/**
* All
*
* @param {Object} pkg
* @return {Object} normalized values
*/
normalize.all = function(pkg, options) {
pkg = normalize.author(pkg, options);
pkg = normalize.repo(pkg, options);
pkg = normalize.bugs(pkg, options);
pkg = normalize.license(pkg, options);
pkg = normalize.keywords(pkg, options);
return pkg;
};
module.exports = normalize;

@@ -1,106 +0,54 @@

const log = require('verbalize');
log.runner = 'normalize-pkg';
'use strict';
/**
* Return `true` if the given string contains a string.
*
* @param {String} haystack
* @param {String} needle
*
* @return {Boolean}
* Lazily-required module dependencies (makes the application
* faster)
*/
var contains = exports.contains = function(haystack, needle) {
return !!~haystack.search(needle);
};
var utils = require('lazy-cache')(require);
/**
* Creates an array excluding all values of the
* provided arrays
* Temporarily re-assign `require` to trick browserify and
* webpack into reconizing lazy dependencies.
*
* @return {Array} array of unique keywords
* This tiny bit of ugliness has the huge dual advantage of
* only loading modules that are actually called at some
* point in the lifecycle of the application, whilst also
* allowing browserify and webpack to find modules that
* are depended on but never actually called.
*/
exports.difference = function(all, common) {
common.forEach(function(obj) {
var word = obj.word;
while (all.indexOf(word) !== -1) {
all.splice(all.indexOf(word), 1);
}
});
return all;
};
var fn = require;
require = utils;
/**
* Infer the correct license url based on the given string.
* This should probably also normalize `type`
*
* @param {[type]} str [description]
*
* @return {[type]} [description]
* Lazily required module dependencies
*/
exports.inferLicenseURL = function(str) {
var type = str,
url;
require('extend-shallow', 'extend');
require('mixin-deep', 'merge');
require('object.omit', 'omit');
require('define-property', 'define');
require('kind-of', 'typeOf');
require('project-name');
require('parse-author');
require('stringify-author');
require('remote-origin-url', 'remote');
require('parse-github-url', 'parseUrl');
require('matched', 'glob');
require('array-unique', 'unique');
require('set-value', 'set');
require('get-value', 'get');
require('semver');
if (contains(str, '://')) {
url = str;
}
/**
* Restore `require`
*/
if (contains(str, 'MIT')) {
type = 'MIT';
url = url || 'http://opensource.org/licenses/MIT';
}
require = fn;
if (contains(str, 'Apache')) {
type = 'Apache-2.0';
url = url || 'http://www.apache.org/licenses/LICENSE-2.0.html';
}
if (contains(str, 'GPL')) {
if (contains(str, '2')) {
type = 'GPL-2.0';
url = url || 'http://www.gnu.org/licenses/gpl-2.0.txt';
}
if (contains(str, '3')) {
type = 'GPL-3.0';
url = url || 'http://www.gnu.org/licenses/gpl-3.0.txt';
}
}
return {
type: type,
url: url
};
};
/**
* Logging
* Expose `utils` modules
*/
exports.msg = {
isCorrect: function (val) {
return log.verbose.run(val, log.green('OK'));
},
isMalformed: function (val) {
return log.verbose.error('package.json seems malformed. Cannot determine the status of `' + val + '`');
},
isMissing: function (val) {
return log.verbose.warn(' normalize-pkg [' + val + ']', log.sep, log.red('missing'));
},
fixingProperty: function (val) {
return log.verbose.warn(' normalize-pkg [' + val + ']', log.sep, log.green('fixed'));
},
addingProperty: function (val) {
return log.verbose.success(' normalize-pkg [' + val + ']', log.sep, log.green('added'));
}
};
module.exports = utils;
{
"name": "normalize-pkg",
"version": "0.1.7",
"description": "Normalize values in package.json to improve compatibility, programmatic readability and usefulness with third party libs.",
"author": {
"name": "Jon Schlinkert",
"url": "https://github.com/jonschlinkert"
},
"version": "0.2.0",
"homepage": "https://github.com/jonschlinkert/normalize-pkg/",
"licenses": [
{
"type": "MIT",
"url": "https://github.com/jonschlinkert/normalize-pkg/blob/master/LICENSE-MIT"
}
],
"repository": {
"type": "git",
"url": "git://github.com/jonschlinkert/normalize-pkg.git"
},
"author": "Jon Schlinkert (https://github.com/jonschlinkert)",
"repository": "jonschlinkert/normalize-pkg",
"bugs": {
"url": "https://github.com/jonschlinkert/normalize-pkg/issues"
},
"license": "MIT",
"files": [
"index.js",
"lib"
],
"engines": {
"node": ">= 0.10.0"
},
"scripts": {
"test": "mocha"
},
"dependencies": {
"array-unique": "^0.2.1",
"define-property": "^0.2.5",
"extend-shallow": "^2.0.1",
"get-value": "^2.0.0",
"kind-of": "^2.0.1",
"lazy-cache": "^0.2.4",
"matched": "^0.3.2",
"mixin-deep": "^1.1.3",
"object.omit": "^2.0.0",
"parse-author": "^0.2.0",
"parse-github-url": "^0.2.1",
"project-name": "^0.2.1",
"remote-origin-url": "^0.4.0",
"semver": "^5.0.3",
"set-value": "^0.3.0",
"stringify-author": "^0.1.1"
},
"devDependencies": {
"gulp": "^3.9.0",
"gulp-eslint": "^1.0.0",
"gulp-istanbul": "^0.10.2",
"gulp-mocha": "^2.1.3",
"mocha": "*"
},
"keywords": [
"config",
"fix",
"json",
"normalize",
"package",
"package-json",
"pkg",

@@ -31,27 +58,9 @@ "properties",

],
"scripts": {
"test": "mocha -R spec"
},
"main": "index.js",
"engines": {
"node": ">= 0.8.0"
},
"preferGlobal": true,
"bin": {
"normalize": "./bin/normalize"
},
"dependencies": {
"cwd": "~0.3.1",
"is-plain-object": "^0.1.0",
"kind-of": "^0.1.0",
"minimist": "0.0.8",
"mixin-deep": "^0.1.0",
"normalize-keywords": "^0.1.0",
"verbalize": "~0.1.2"
},
"devDependencies": {
"mocha": "*",
"should": "^4.0.4",
"verb": "^0.2.15"
"verb": {
"related": {
"list": [
"update"
]
}
}
}
# normalize-pkg [![NPM version](https://badge.fury.io/js/normalize-pkg.svg)](http://badge.fury.io/js/normalize-pkg)
> Normalize values in package.json to improve compatibility, programmatic readability and usefulness with third party libs.
## Install
#### Install with [npm](npmjs.org):
```bash
npm i normalize-pkg --save-dev
Install with [npm](https://www.npmjs.com/)
```sh
$ npm i normalize-pkg --save
```
## Run tests
Install with [bower](http://bower.io/)
```bash
npm test
```sh
$ bower install normalize-pkg --save
```

@@ -21,138 +21,97 @@

### API
```js
var normalize = require('normalize-pkg');
var pkg = require('./package');
normalize(pkg);
```
### CLI
## Schema
Run `normalize` in the command line to normalize the following properties in `package.json`:
normalize-pkg takes a schema, where each key on the schema represents a property name on package.json. A default schema is used but it can easily be overridden.
* [license](#license)
* [bugs](#bugs)
* [author](#author)
* [repository](#repository)
**Keys**
Schema property keys represent the properties you want on your package.json. For example, here is are a couple of properties from the default schema:
If you want to specify the source `package.json` to normalize, or the destination to write to, you can use this format:
```bash
normalize [src] [dest]
```
Or explicit:
* `-s` | `--src`: normalize the specified source file.
* `-d` | `--dest`: write the file to the specified destination.
## Normalized properties
Currently, only the following values are normalized. If any of the values is missing, a polite warning will be logged, but nothing will be modified.
## license
If `license` is formatted as a string:
```json
```js
{
"license": "MIT"
name: {
// ensure it's a string
type: 'string',
value: function (key, val, config) {
// if you're initializing a new project, the `projectName`
// function tries to intelligently guess that name
return val || utils.projectName(process.cwd());
}
},
// ensure it's a string, use the default if undefined
description: {
type: 'string',
default: ''
},
// ensure it's a string, use the default if undefined
version: {
type: 'string',
default: '0.1.0'
}
}
```
It will be normalized to:
**Schema properties**
```json
{
"type": "MIT",
"url": "http://opensource.org/licenses/MIT"
}
```
The foll
`license.url` is determined based on a `search()` of the string, with basic support for the following:
* `type`: native javascript type. If the property is defined and it's type does not match this value an error is thrown
* `value`: This can be any type of value. If it's a function, it will be called and passed the `key` and `value` currently defined for the property, and the entire `config` as the last argument. If it's any other value, that value will be used and will overwrite any existing value. If you only want to use a value if it's undefined, use `default` instead or a `value` function if conditional logic is required.
* `default`: Set the default value to use when the property is undefined. Only necessary if `value` is not defined.
* `MIT`
* `Apache`
* `GPL`, `2` and `3`
## Options
When an options object is passed as the second argument it will be used to extend or override properties on the existing schema.
## bugs
If `bugs` is formatted as a string:
```json
{
"bugs": "https://github.com/assemble/generator-assemble/issues"
}
```
It will be normalized to:
```json
{
"bugs": {
"url": "https://github.com/assemble/generator-assemble/issues"
```js
normalize(pkg, {
name: {
value: 'foo' // always use 'foo' as the project name
},
version: {
value: function(key, val) {
// validate version with semver or something.
// be creative :) just remember to return the value!
return value;
}
}
}
});
```
## author
## Related projects
If `author` is formatted as a string:
[update](https://www.npmjs.com/package/update): Update the year in all files in a project using glob patterns. | [homepage](https://github.com/jonschlinkert/update)
```js
{
"author": "Jon Schlinkert"
}
```
## Running tests
It will be normalized to:
Install dev dependencies:
```json
{
"author": {
"name": "Jon Schlinkert",
"url": ""
}
}
```sh
$ npm i -d && npm test
```
## repository
## Contributing
If `repository` is formatted as a string:
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/normalize-pkg/issues/new).
```js
{
"repository": "https://github.com/assemble/generator-assemble.git",
}
```
It will be normalized to:
```json
{
"repository": {
"type": "git",
"url": "https://github.com/assemble/generator-assemble.git"
}
}
```
`repository.type` is determined based on a search of the `url` string.
## Author
**Jon Schlinkert**
+ [github/jonschlinkert](https://github.com/jonschlinkert)
+ [twitter/jonschlinkert](http://twitter.com/jonschlinkert)
+ [twitter/jonschlinkert](http://twitter.com/jonschlinkert)
## License
Copyright (c) 2014 Jon Schlinkert, contributors.
Released under the MIT license
Copyright © 2015 Jon Schlinkert
Released under the MIT license.
***
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on September 26, 2014._
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on November 06, 2015._
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