normalize-package-data
Advanced tools
Comparing version 0.0.3 to 0.0.5
@@ -125,3 +125,6 @@ var semver = require("semver") | ||
} | ||
} | ||
} | ||
else if(data.bugs && typeof data.bugs == "string") { | ||
data.bugs = {url: url} | ||
} | ||
} | ||
@@ -128,0 +131,0 @@ } |
{ | ||
"name": "normalize-package-data", | ||
"version": "0.0.3", | ||
"version": "0.0.5", | ||
"author": "Meryn Stol <merynstol@gmail.com>", | ||
@@ -8,3 +8,3 @@ "description": "Normalizes data that can be found in package.json files.", | ||
"type": "git", | ||
"url": "git://github.com:meryn/read-package-data.git" | ||
"url": "git://github.com/meryn/read-package-data.git" | ||
}, | ||
@@ -20,4 +20,5 @@ "main": "lib/normalize.js", | ||
"tap": "~0.2.5", | ||
"underscore": "~1.4.4" | ||
"underscore": "~1.4.4", | ||
"async": "~0.2.7" | ||
} | ||
} |
@@ -1,3 +0,70 @@ | ||
# read-package-data | ||
# normalize-package-data | ||
This code is based on read-package-json by Isaac Schlueter. | ||
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. | ||
## 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`. | ||
```javascript | ||
normalizeData = require('read-package-data') | ||
packageData = fs.readfileSync("package.json") | ||
normalizeData(packageData) | ||
// packageData is now normalized | ||
``` | ||
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. | ||
```javascript | ||
normalizeData = require('read-package-data') | ||
packageData = fs.readfileSync("package.json") | ||
warnFn = function(msg) { console.error(msg) } | ||
normalizeData(packageData, warnFn) | ||
// packageData is now normalized. Any number of warnings may have been logged. | ||
``` | ||
If you don't provide a warning function, `normalizeData` functions silently. | ||
### Potential exceptions | ||
If the supplied data has an invalid name or version vield, `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 entails | ||
* The value of `name` field gets trimmed | ||
* The value of the 'version` field gets cleaned by `semver.clean`. See [documentation of the semver module](https://github.com/isaacs/node-semver). | ||
* If `repository` field is a string, it will become am object with `url` set to the original string value, and `type` set to `"git"`. | ||
* If `files` field is not an array, it will be removed. | ||
* If `bin` field is a string, then `bin` field will become an object with `name` set to the value of the `name` field, and `bin` set to the original string value. | ||
* If `man` field is a string, it will become an array with the original string as its sole member | ||
* If `keywords` field is string, it is considered to be a list of keywords separated by one or more white-space characters. It gets converted to an array by splitting on `\s+`. | ||
* If `bundledDependencies` field (a typo) exists and `bundleDependencies` field does not, `bundledDependencies` will get renamed to `bundleDependencies`. | ||
* All people fields (`author`, `maintainers`, `contributors`) get converted into objects with name, email and url properties. | ||
* If the value of any of the depedencies fields (`dependencies`, `devDependencies`, `optionalDependencies`) are strings, they get converted into objects with familiar `name=>value` pairs. | ||
* The values in `optionalDependencies` get added to `dependencies`. `optionalDependencies` array is left untouched. | ||
* If `description` field does not exists, but `readme` field does, then (more or less) the first paragraph of text that's found in the readme is taken a value for `description`. | ||
* If `bugs` field is a string, the value of `bugs` field is changed into an object with `url` set to the original string value. | ||
* If `bugs` field does not exist, but `repository` field points to a repository hosted on github, the value of the `bugs` field gets set to an url in the form of https://github.com/[owner-name]/[repo-name]/issues . | ||
### Rules for name field | ||
The value of the name field may not | ||
* start with a period. | ||
* contain the following characters: `/@\s+%` | ||
* contain and characters that would need to be encoded for use in urls. | ||
* resemble the word `node_modules` or `favicon.ico` (case doesn't matter). | ||
### Rules for version field | ||
The value of the version field must be a valid *semver* string, as determined by the `semver.valid` method. See [documentation of the semver module](https://github.com/isaacs/node-semver). | ||
## Credits | ||
This code is based on read-package-json written by Isaac Schlueter. |
@@ -6,14 +6,27 @@ var tap = require("tap") | ||
var _ = require("underscore") | ||
var async = require("async") | ||
tap.test("normalization consistency", function (t) { | ||
var p = path.resolve(__dirname, "./fixtures/read-package-json.json") | ||
fs.readFile (p, function (err, contents) { | ||
if (err) throw err; | ||
var data = JSON.parse(contents.toString()) | ||
normalize(data) | ||
var clonedData = _.clone(data) | ||
normalize(data) | ||
t.deepEqual(data, clonedData, "Normalized normalized is normalized.") | ||
t.end() | ||
}) | ||
}) | ||
var data, clonedData | ||
tap.test("consistent normalization", function(t) { | ||
path.resolve(__dirname, "./fixtures/read-package-json.json") | ||
fs.readdir (__dirname + "/fixtures", function (err, entries) { | ||
verifyConsistency = function(entryName, next) { | ||
filename = __dirname + "/fixtures/" + entryName | ||
fs.readFile(filename, function(err, contents) { | ||
if (err) return next(err) | ||
data = JSON.parse(contents.toString()) | ||
normalize(data) | ||
clonedData = _.clone(data) | ||
normalize(data) | ||
t.deepEqual(data, clonedData, | ||
"Normalization of " + entryName + "is consistent.") | ||
next(null) | ||
}) // fs.readFile | ||
} // verifyConsistency | ||
async.forEach(entries, verifyConsistency, function(err) { | ||
if (err) throw err | ||
t.end() | ||
}) | ||
}) // fs.readdir | ||
}) // tap.test |
27822
20
799
71
3