diff --git a/.travis.yml b/.travis.yml index efe94c0..60755d4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 \ No newline at end of file + depth: 10 diff --git a/index.js b/index.js index 1499c67..6ca9310 100644 --- a/index.js +++ b/index.js @@ -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) -} diff --git a/package.json b/package.json index 4636595..c17234e 100644 --- a/package.json +++ b/package.json @@ -54,5 +54,9 @@ "sort", "test", "wildcard" - ] + ], + "dependencies": { + "for-own": "^0.1.3", + "is-match": "^0.2.0" + } } diff --git a/test.js b/test.js index ffae3e8..60ddb8c 100644 --- a/test.js +++ b/test.js @@ -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({}); });