Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use is-match instead and a lil' bit stuff #2

Merged
merged 3 commits into from
Jul 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ language: node_js
node_js:
- "0.10"
- "0.12"
- "iojs"
- "1.0"
- "1.8"
- "2.0"
- "2.1"
git:
depth: 10
depth: 10
23 changes: 7 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,26 @@
*/

var forOwn = require('for-own');
var iterator = require('make-iterator');
var mm = require('micromatch');
var matcher = require('is-match');

/**
* Filter an object values using glob patterns
* or with a `callback` function returns true.
*
* @param {Object} `obj`
* @param {Function|Array|String} `filter`
* @param {Object} `thisArg`
* @param {Function|Array|String|RegExp} `filter`
* @param {Object} `options` pass options to `micromatch`
* @return {Object}
*/

module.exports = function filterValues(obj, filter, thisArg) {
var cb = matcher(filter, thisArg);
module.exports = function filterValues(obj, filter, options) {
var isMatch = matcher(filter, options);
var res = {};

forOwn(obj, function (val, key, o) {
if (cb(val, key, o)) {
if (isMatch(val)) {
res[key] = val;
}
});
return res;
};

function matcher(filter, thisArg) {
if (Array.isArray(filter) || typeof filter === 'string') {
return function (val) {
return !!mm(val, filter).length;
};
}
return iterator(filter, thisArg)
}
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,9 @@
"sort",
"test",
"wildcard"
]
],
"dependencies": {
"for-own": "^0.1.3",
"is-match": "^0.2.0"
}
}
12 changes: 6 additions & 6 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ var filter = require('./');

describe('.filter()', function () {
it('should return values for which the callback returns true.', function () {
var o = filter({a: 'a', b: 'b', c: 'c'}, function (value, key) {
return key === 'b';
var o = filter({a: 'a', b: 'b', c: 'c'}, function (value) {
return value === 'b';
});
o.should.eql({ b: 'b' });
});

it('should omit values for which the callback returns false.', function () {
var o = filter({a: 'a', b: 'b', c: 'c'}, function (value, key) {
return key !== 'b';
var o = filter({a: 'a', b: 'b', c: 'c'}, function (value) {
return value !== 'b';
});
o.should.eql({a: 'a', c: 'c'});
});

it('should return an empty object if all values return false.', function () {
var o = filter({a: 'a', b: 'b', c: 'c'}, function (value, key) {
return key === 'foo';
var o = filter({a: 'a', b: 'b', c: 'c'}, function (value) {
return value === 'foo';
});
o.should.eql({});
});
Expand Down