Comparing version 0.1.0 to 0.1.1
108
matchkeys.js
@@ -22,17 +22,4 @@ /** | ||
// Create Array.isArray() if it's not already natively available. | ||
if(!Array.isArray) { | ||
Array.isArray = function (vArg) { | ||
return Object.prototype.toString.call(vArg) === "[object Array]"; | ||
}; | ||
} | ||
var isArray = function (src) { | ||
return Array.isArray(src) && src instanceof Array; | ||
}; | ||
// Ensure config has keywords | ||
function loadPkg(config) { | ||
var result = {} | ||
@@ -46,3 +33,3 @@ result.keywords = [] | ||
// populate keywords, if any | ||
if(isArray(config.keywords)) { | ||
if(_.isArray(config.keywords)) { | ||
result.keywords = config.keywords; | ||
@@ -52,3 +39,2 @@ } else { | ||
} | ||
return result; | ||
@@ -58,14 +44,2 @@ } | ||
/** | ||
* Compare an array of keywords against multiple arrays of keywords. | ||
* @param {Array} keys Keywords to test against. | ||
* @param {Array} multiArrays Array of objects, each with a keywords property/array | ||
* @return {Array} Array of matching keywords | ||
*/ | ||
exports.multiKeys = function(keys, multiArrays) { | ||
return _.filter(multiArrays, function(arr) { | ||
return _.intersection(keys.keywords || [], arr.keywords || []).length > 0; | ||
}); | ||
}; | ||
/** | ||
* Compare two arrays of keywords | ||
@@ -76,6 +50,12 @@ * @param {Array} a Array of keywords to test against. | ||
*/ | ||
exports.keys = function(a, b) { | ||
return _.intersection(a.keywords || [], b.keywords || []); | ||
exports.match = function(keywords, a, b) { | ||
if(typeof(keywords) !== 'string') { | ||
b = a; | ||
a = keywords; | ||
keywords = 'keywords'; | ||
} | ||
return _.intersection(a[keywords] || [], b[keywords] || []); | ||
}; | ||
/** | ||
@@ -87,4 +67,9 @@ * Compare two arrays of keywords and return true if ANY keywords match. | ||
*/ | ||
exports.isKeywordMatch = function(a, b) { | ||
return _.intersection(a.keywords || [], b.keywords || []).length > 0; | ||
exports.isMatch = function(keywords, a, b) { | ||
if(typeof(keywords) !== 'string') { | ||
b = a; | ||
a = keywords; | ||
keywords = 'keywords'; | ||
} | ||
return _.intersection(a[keywords] || [], b[keywords] || []).length > 0; | ||
}; | ||
@@ -94,14 +79,20 @@ | ||
/** | ||
* Resolve paths to keywords based on keyword matches | ||
* @param {[type]} keys [description] | ||
* @param {[type]} config [description] | ||
* @return {[type]} [description] | ||
* Compare an array of keywords against multiple arrays of keywords. | ||
* @param {Array} keys Keywords to test against. | ||
* @param {Array} arrs Multple arrays. More specifically, an array of objects, | ||
* each with a keywords property/array | ||
* @return {Array} Array of matching keywords | ||
*/ | ||
exports.resolve = function (keys, config) { | ||
config = loadPkg(config); | ||
return exports.matchkeys(keys, config).map(function (keywords) { | ||
return resolveDep.resolvePath(keywords); | ||
exports.matchPkgs = function(keywords, arr, arrs) { | ||
if(typeof(keywords) !== 'string') { | ||
arrs = arr; | ||
arr = keywords; | ||
keywords = 'keywords'; | ||
} | ||
return _.filter(arrs, function(arr) { | ||
return _.intersection(arr[keywords] || [], arr[keywords] || []).length > 0; | ||
}); | ||
}; | ||
/** | ||
@@ -121,2 +112,15 @@ * Return any keywords in the given list that match keywords in config.keywords. | ||
/** | ||
* Resolve paths to keywords based on keyword matches | ||
* @param {[type]} keys [description] | ||
* @param {[type]} config [description] | ||
* @return {[type]} [description] | ||
*/ | ||
exports.resolve = function (patterns, config) { | ||
config = loadPkg(config); | ||
return _.compact(_.flatten(exports.filter(patterns, config).map(function (pattern) { | ||
return resolveDep.dep(pattern).join(); | ||
}))); | ||
}; | ||
/** | ||
* Return the resolved filepaths to any npm modules that match the given list of keywords. | ||
@@ -127,6 +131,22 @@ * @param {[type]} patterns [description] | ||
*/ | ||
exports.filterResolve = function (patterns, config) { | ||
return exports.filter(patterns, config).map(function (pattern) { | ||
return resolveDep.resolvePath(pattern); | ||
}); | ||
}; | ||
exports.resolveDev = function (patterns, config) { | ||
config = loadPkg(config); | ||
return _.compact(_.flatten(exports.filter(patterns, config).map(function (pattern) { | ||
return resolveDep.dev(pattern).join(); | ||
}))); | ||
}; | ||
/** | ||
* Return the resolved filepaths to any npm modules that match the given list of keywords. | ||
* @param {[type]} patterns [description] | ||
* @param {[type]} config [description] | ||
* @return {[type]} [description] | ||
*/ | ||
exports.resolveAll = function (patterns, config) { | ||
config = loadPkg(config); | ||
return _.compact(_.flatten(exports.filter(patterns, config).map(function (pattern) { | ||
return resolveDep.all(pattern).join(); | ||
}))); | ||
}; | ||
{ | ||
"name": "matchkeys", | ||
"description": "Return the resolved filepaths to named npm module dependencies based on keywords in package.json. Minimatch patterns can be used, and Lo-Dash templates can be used to load filepaths into Grunt config.", | ||
"version": "0.1.0", | ||
"description": "Package.json utility for matching, comparing or filtering keywords against one or more arrays of keywords.", | ||
"version": "0.1.1", | ||
"homepage": "https://github.com/jonschlinkert/matchkeys", | ||
@@ -29,3 +29,2 @@ "author": { | ||
"lodash": "~1.3.1", | ||
"chalk": "~0.2.1", | ||
"resolve-dep": "~0.1.2" | ||
@@ -37,17 +36,5 @@ }, | ||
"keywords": [ | ||
"foo", | ||
"bar", | ||
"files", | ||
"lodash", | ||
"glob", | ||
"chalk", | ||
"grunt config", | ||
"load-grunt-tasks", | ||
"grunt", | ||
"matchdep", | ||
"lo-dash templates", | ||
"load", | ||
"lodash templates", | ||
"keywords", | ||
"filter", | ||
"match", | ||
"minimatch", | ||
"pattern", | ||
@@ -54,0 +41,0 @@ "require resolve", |
156
README.md
# matchkeys [![NPM version](https://badge.fury.io/js/matchkeys.png)](http://badge.fury.io/js/matchkeys) | ||
> Return an array of resolved filepaths for specified npm module dependencies. Minimatch patterns can be used. | ||
> A package.json utility for comparing keywords across multiple files. | ||
@@ -10,8 +10,158 @@ | ||
```js | ||
var match = require('matchkeys').keys(arr1, arr2); | ||
console.log(match); | ||
var keys = require('matchkeys'); | ||
keys.match(arrayOne, arrayTwo)); | ||
``` | ||
## Usage Example | ||
```js | ||
var keys = require('matchkeys'); | ||
var pkg = JSON.parse(fs.readFileSync('package.json', 'utf8')); | ||
// => keywords: ['gruntplugin', 'handlebars-helper', 'assemble'] | ||
// | ||
var pkgTwo = JSON.parse(fs.readFileSync('package.json', 'utf8')); | ||
// => keywords: ['foo', 'bar', 'assemble'] | ||
console.log(keys.match(pkg, pkgTwo)); | ||
// => ['assemble'] | ||
``` | ||
## Methods | ||
### matchPkgs | ||
Match an array of keywords to multiple arrays of keywords returning the `package.json` object for each match. | ||
```js | ||
keys.matchPkgs(Array, ArrayofObjects)); | ||
``` | ||
Parameters: | ||
* `Array`: The `package.json` containing the `keywords` property to match against. | ||
* `Array`: An array of `package.json` files, each containing an array of keywords. | ||
_Note that you do not need to specify the `keywords` property, just the object containing the keywords property_. | ||
#### example | ||
Given the following: | ||
```js | ||
var keys = require('matchkeys'); | ||
var one = ['apple', 'lime', 'watermelon'] | ||
var two = [ | ||
{ | ||
name: 'citrus', | ||
keywords: ['lemon', 'lime', 'orange'] | ||
}, | ||
{ | ||
name: 'grains', | ||
keywords: ['wheat', 'oats', 'barley'] | ||
}, | ||
{ | ||
name: 'fruit', | ||
keywords: ['apple', 'peach', 'strawberry'] | ||
} | ||
]; | ||
keys.matchPkgs(one, two)); | ||
``` | ||
Returns: | ||
```js | ||
[ | ||
{ | ||
name: 'citrus', | ||
keywords: ['lemon', 'lime', 'orange'] | ||
}, | ||
{ | ||
name: 'fruit', | ||
keywords: ['apple', 'peach', 'strawberry'] | ||
} | ||
] | ||
``` | ||
### isMatch | ||
Same as `matchPkgs` but returns `true` or `false`. | ||
```js | ||
var keys = require('matchkeys'); | ||
keys.isMatch(Array, ArrayofObjects)); | ||
``` | ||
Parameters: | ||
* `Array`: The keywords to match against. | ||
* `Array`: Array of objects (`package.json` files), each containing an array of keywords. | ||
Using the same example as `keys.matchPkgs`, this would return: | ||
```js | ||
[false, false, true] | ||
``` | ||
### filter | ||
Returns a list of keywords matching the given minimatch pattern. | ||
```js | ||
var keys = require('matchkeys'); | ||
keys.filter('*') | ||
``` | ||
### resolve | ||
```js | ||
keys.resolve('*') | ||
// specify a different config object besides package.json | ||
keys.resolve('*', config)) | ||
``` | ||
Returns the resolved paths to any npm modules that: | ||
1. Are listed in the `dependencies` of the project's `package.json`, and | ||
1. A keyword is defined matching the name of the module | ||
### resolveDev | ||
```js | ||
keys.resolveDev('*') | ||
// specify a different config object besides package.json | ||
keys.resolveDev('*', config)) | ||
``` | ||
Returns the resolved paths to any npm modules that: | ||
1. Are listed in the `devDependencies` of the project's `package.json`, and | ||
1. A keyword is defined matching the name of the module | ||
### resolveDev | ||
```js | ||
keys.resolveAll('*') | ||
// specify a different config object besides package.json | ||
keys.resolveAll('*', config)) | ||
``` | ||
Returns the resolved paths to any npm modules that: | ||
1. Are listed in the `dependencies` or `devDependencies` of the project's `package.json`, and | ||
1. A keyword is defined matching the name of the module | ||
## Contributing | ||
@@ -18,0 +168,0 @@ In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. |
@@ -13,40 +13,54 @@ /** | ||
var path = require('path'); | ||
var chalk = require('chalk'); | ||
var _ = require('lodash'); | ||
// This module. | ||
var keys = require('../'); | ||
var match = require('../') | ||
// Create our test objects and keywords arrays. | ||
var config = JSON.parse(fs.readFileSync(path.join(process.cwd(), './package.json')), 'utf8'); | ||
// TODO: assert | ||
var multiplePkgs = JSON.parse(fs.readFileSync(path.join(process.cwd(), './test/fixtures/multi.json')), 'utf8'); | ||
var singlePkg = JSON.parse(fs.readFileSync(path.join(process.cwd(), './test/fixtures/package.json')), 'utf8'); | ||
function readJSON(src) { | ||
return JSON.parse(fs.readFileSync(path.join(process.cwd(), src)), 'utf8'); | ||
} | ||
// Create our test objects and keywords arrays. | ||
var multiplePkgs = readJSON('./test/fixtures/multi.json'); | ||
var singlePkg = readJSON('./test/fixtures/package.json'); | ||
var multiArr = [{keywords: ['A', 'B', 'C', 'D', 'E', 'F'] }, {keywords: ['A', 'B', 'C', 'D', 'I', 'F'] }]; | ||
var first = {keywords: ['A', 'B', 'C', 'D', 'E', 'F'] }; | ||
var second = {keywords: ['C', 'E', 'G', 'I', 'J'] }; | ||
var multiArr = [ | ||
{ | ||
keywords: ['A', 'B', 'C', 'D', 'E', 'F'] | ||
}, | ||
{ | ||
keywords: ['A', 'B', 'C', 'D', 'I', 'F'] | ||
} | ||
]; | ||
var test = { | ||
keywords: ['baz', 'bar', 'foo', 'chalk'] | ||
var first = { | ||
keywords: ['A', 'B', 'C', 'D', 'E', 'F'] | ||
}; | ||
var second = { | ||
keywords: ['C', 'E', 'G', 'I', 'J'] | ||
}; | ||
var config = { | ||
keywords: ['baz', 'bar', 'foo', 'node-foo'] | ||
}; | ||
console.log("matchMany", match.multiKeys(singlePkg, multiplePkgs)); | ||
console.log("matchOne", match.keys(first, second)); | ||
console.log("isMatchOne", match.isKeywordMatch(first, second)); | ||
console.log("TEST", match.resolve(test, singlePkg)); | ||
console.log("TEST", match.resolve(test)); | ||
console.log("TEST", match.filter('*')); | ||
console.log("TEST", match.filterResolve('node-foo')); | ||
console.log("TEST", match.filterResolve('load-*')); | ||
console.log(chalk.cyan("keys.match"), keys.match(first, second)); | ||
console.log(chalk.cyan("keys.matchPkgs"), keys.matchPkgs(singlePkg, multiplePkgs)); | ||
console.log(chalk.cyan("keys.isMatch"), keys.isMatch(first, second)); | ||
console.log(chalk.cyan("keys.filter"), keys.filter('*')); | ||
console.log(chalk.cyan("keys.filter"), keys.filter('*', config)); | ||
console.log(chalk.cyan("keys.resolve"), keys.resolve('*')); | ||
console.log(chalk.cyan("keys.resolve"), keys.resolve('*', config)); | ||
console.log(chalk.cyan("keys.resolveDev"), keys.resolveDev('*')); | ||
console.log(chalk.cyan("keys.resolveDev"), keys.resolveDev('*', config)); | ||
console.log(chalk.cyan("keys.resolveAll"), keys.resolveAll('*')); | ||
console.log(chalk.cyan("keys.resolveAll"), keys.resolveAll('*', config)); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14722
2
228
189
- Removedchalk@~0.2.1
- Removedansi-styles@0.2.0(transitive)
- Removedchalk@0.2.1(transitive)
- Removedhas-color@0.1.7(transitive)