Git Product home page Git Product logo

filter-object's Introduction

filter-object NPM version NPM monthly downloads NPM total downloads Linux Build Status

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:

$ npm install --save filter-object

Usage

Filter with glob patterns

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

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 and/or filter-values

filter({foo: 'a', bar: 'b', baz: 'c'}, ['*', '!b*'], options);

See glob-object and/or filter-values for the full range of options and available features.

About

Related projects

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Building docs

(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)

To generate the readme, run the following command:

$ 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:

$ npm install && npm test

Author

Jon Schlinkert

License

Copyright © 2017, Jon Schlinkert. Released under the MIT License.


This file was generated by verb-generate-readme, v0.6.0, on June 19, 2017.

filter-object's People

Contributors

jonschlinkert avatar

Stargazers

Cat  avatar Yi-Liang Lai avatar Lucas Lopes Trindade avatar  avatar Nathan Jones avatar Andrea Parodi avatar Victor Truong avatar Mayur Bhangale avatar Travis Fischer avatar fishchen avatar streetlamp avatar ivan k avatar Mathieu M-Gosselin avatar Pierre Klink avatar Jerzerak avatar Shaun Befort avatar Kumarajiva avatar Reggi avatar  avatar Chris W. Burke avatar Brian Woodward avatar Keith Williams avatar Charlike Mike Reagent avatar  avatar

Watchers

 avatar Brian Woodward avatar  avatar

filter-object's Issues

Array support

Would you be interested in a PR for array support?

Say we have this object:

{
    name: "Peter",
    familyName: "Morlion",
    hobbies: [{
        name: "Reading",
        since: 1989
    }, {
        name: "Games",
        since: 1994
    }]
}

If I want to filter with ["name", "hobbies.name"] I'll get:

{
    name: "Peter"
}

It would be nice to get:

{
    name: "Peter",
    hobbies: [{
        name: "Reading"
    }, {
        name: "Games"
    }]
}

Vue support

I get this
[Vue warn]: Error in render: "Error: Cannot find module 'kind-of'"

filter by full or * path

filter({
  removeMe: 'now',
  foo: {
    bar: {
      hello: 'hello',
      there: 'there'
    }
  },
  foosiz: 'to-remove'
}, ['foo.*'])

My expectation is that it should remove everything but foo.* but I get:

{ foosiz: 'to-remove' }

suggestion for refactor

what about to filter both by values and keys by default, and give options if you want to filter only values, or only keys, e.g.

'use-strict'

var matcher = require('is-match')

function objectFilter (obj, filter, opts) {
  opts = typeof opts === 'object' ? opts : {}

  var res = {}
  var isMatch = matcher(filter, opts)
  var keys = Object.keys(obj)
  var len = keys.length
  var i = 0

  while (i < len) {
    var key = keys[i++]
    var val = obj[key]

    if (opts.values && !isMatch(val)) {
      continue
    }
    if (opts.keys && !isMatch(key)) {
      continue
    }
    if (!isMatch(val) && !isMatch(key)) {
      continue
    }
    res[key] = val
  }

  return res
}

and use cases

var obj = {
  'foo': 'bar',
  'fos': 'koa',
  'faz': 'far',
  'boo': 'abc',
  'data': 'foo'
}

console.log(objectFilter(obj, 'f*'))
//=> { foo: 'bar', fos: 'koa', faz: 'far', data: 'foo' }

console.log(objectFilter(obj, 'f*', {keys: true}))
//=> { foo: 'bar', fos: 'koa', faz: 'far' }

console.log(objectFilter(obj, 'f*', {values: true}))
//=> { faz: 'far', data: 'foo' }

console.log(objectFilter(obj, '*a'))
//=> { fos: 'koa', data: 'foo' }

console.log(objectFilter(obj, ['*oo', '!f*']))
//=> { boo: 'abc' }

console.log(objectFilter(obj, '*oo', {keys: true}))
//=> { foo: 'bar', boo: 'abc' }

console.log(objectFilter(obj, '*oo', {values: true}))
//=> { data: 'foo' }

console.log(objectFilter(obj, '*o*', {values: true}))
//=> { fos: 'koa', data: 'foo' }

React-Native support?

Re-opening this issue as it is not supporting on react-native.
After importing import filter from 'filter-object'; ,
I'm getting below error
Module 'util' does not exist in the haste module map

Find all keys starting with $

Would this work: filter(obj, '\$') Not sure about glob vs regexp rules for escaping (since $ is normally a special regexp end signifier) ...

wrong behavior with negation patterns

const filter = require('filter-object')

console.log(filter({
  limit: 10,
  startId: 'startId',
  startKey: 'startKey',
  sort: 'sort'
}, [
  '*',
  '!sort',
  '!startId',
  '!startKey',
  '!limit'
])) // { startId: 'startId' }

console.log(filter({
  limit: 10,
  startId: 'startId',
  startKey: 'startKey',
  sort: 'sort'
}, [
  '*',
  '!sort',
  // '!startId',
  '!startKey',
  '!limit'
])) // { startId: 'startId', startKey: 'startKey' } O_O

console.log(filter({
  limit: 10,
  startId: 'startId',
  startKey: 'startKey',
  sort: 'sort'
}, [
  '*',
  '!sort',
  '',
  '!startId',
  '!startKey',
  '!limit'
])) // {} ಠ_ಠ

Are we using it wrong?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.