UNPKG

filter-object

Version:

Filter an object by its keys or values. Returns a copy of an object filtered to have only keys or values that match the given glob patterns.

102 lines (64 loc) 4.41 kB
# filter-object [![NPM version](https://img.shields.io/npm/v/filter-object.svg?style=flat)](https://www.npmjs.com/package/filter-object) [![NPM monthly downloads](https://img.shields.io/npm/dm/filter-object.svg?style=flat)](https://npmjs.org/package/filter-object) [![NPM total downloads](https://img.shields.io/npm/dt/filter-object.svg?style=flat)](https://npmjs.org/package/filter-object) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/filter-object.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/filter-object) > Filter an object by its keys or values. Returns a copy of an object filtered to have only keys or values that match the given glob patterns. ## Install Install with [npm](https://www.npmjs.com/): ```sh $ npm install --save filter-object ``` ## Usage ### Filter with glob patterns ```js var filter = require('filter-object'); console.log(filter({a: 'a', b: 'b', c: 'c'}, '*')); //=> {a: 'a', b: 'b', c: 'c'} console.log(filter({a: 'a', b: 'b', c: 'c'}, 'b')); //=> {b: 'b'} console.log(filter({foo: 'a', bar: 'b', baz: 'c'}, 'b*')); //=> {bar: 'b', baz: 'c'} console.log(filter({a: 'a', b: 'b', c: 'c'}, '{b,c}')); //=> {b: 'b', c: 'c'} console.log(filter({a: 'a', b: 'b', c: 'c'}, ['a', 'b'])); //=> {a: 'a', b: 'b'} ``` **Negation patterns** ```js console.log(filter({foo: 'a', bar: 'b', baz: 'c'}, ['!b*'])); //=> { foo: 'a' } console.log(filter({a: {b: {foo: 'a', bar: 'b', baz: 'c'}}}, ['!a.b.b*'])); //=> {a: {b: {foo: 'a'}}} ``` ### options Options are passed to [glob-object](https://github.com/jonschlinkert/glob-object) and/or [filter-values](https://github.com/jonschlinkert/filter-values) ```js filter({foo: 'a', bar: 'b', baz: 'c'}, ['*', '!b*'], options); ``` See [glob-object](https://github.com/jonschlinkert/glob-object) and/or [filter-values](https://github.com/jonschlinkert/filter-values) for the full range of options and available features. ## About ### Related projects * [filter-keys](https://www.npmjs.com/package/filter-keys): Filter the keys of an object using glob patterns. | [homepage](https://github.com/jonschlinkert/filter-keys "Filter the keys of an object using glob patterns.") * [filter-values](https://www.npmjs.com/package/filter-values): Filter an object values using glob patterns or with a `callback` function returns true. | [homepage](https://github.com/jonschlinkert/filter-values "Filter an object values using glob patterns or with a `callback` function returns true. ") * [micromatch](https://www.npmjs.com/package/micromatch): Glob matching for javascript/node.js. A drop-in replacement and faster alternative to minimatch and multimatch. | [homepage](https://github.com/micromatch/micromatch "Glob matching for javascript/node.js. A drop-in replacement and faster alternative to minimatch and multimatch.") * [rename-keys](https://www.npmjs.com/package/rename-keys): Modify the names of the own enumerable properties (keys) of an object. | [homepage](https://github.com/jonschlinkert/rename-keys "Modify the names of the own enumerable properties (keys) of an object.") * [sort-object](https://www.npmjs.com/package/sort-object): Sort the keys in an object. | [homepage](https://github.com/doowb/sort-object "Sort the keys in an object.") ### Contributing Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). ### Building docs _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_ To generate the readme, run the following command: ```sh $ npm install -g verbose/verb#dev verb-generate-readme && verb ``` ### Running tests Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command: ```sh $ npm install && npm test ``` ### Author **Jon Schlinkert** * [github/jonschlinkert](https://github.com/jonschlinkert) * [twitter/jonschlinkert](https://twitter.com/jonschlinkert) ### License Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert). Released under the [MIT License](LICENSE). *** _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on June 19, 2017._