GithubHelp home page GithubHelp logo

filter-obj's Introduction

filter-obj

Filter object keys and values into a new object

Install

npm install filter-obj

Usage

import {includeKeys, excludeKeys} from 'filter-obj';

const object = {
	foo: true,
	bar: false
};

const newObject = includeKeys(object, (key, value) => value === true);
//=> {foo: true}

const newObject2 = includeKeys(object, ['bar']);
//=> {bar: false}

const newObject = excludeKeys(object, (key, value) => value === true);
//=> {bar: false}

const newObject3 = excludeKeys(object, ['bar']);
//=> {foo: true}

API

includeKeys(source, filter)

includeKeys(source, keys)

excludeKeys(source, filter)

excludeKeys(source, keys)

source

Type: object

The source object to filter properties from.

filter

Type: (sourceKey: string | symbol, sourceValue: unknown, source: object) => boolean

A predicate function that determines whether a property should be filtered.

keys

Type: Array<string | symbol>

An array of property keys to be filtered.

Related

  • map-obj - Map object keys and values into a new object

filter-obj's People

Contributors

bendingbender avatar ehmicky avatar johansteffner avatar ninevra avatar richienb avatar samverschueren avatar sindresorhus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

filter-obj's Issues

Symbol properties should be kept

The README documents that symbol properties are not kept. However, this leads to some behavior that users might not expect:

const symbol = Symbol('example')
const obj = { [symbol]: true, a: 1, b: 2 }
const objCopy = excludeKeys(obj, ['b'])
objCopy[symbol] // undefined. Unexpected: the consumer only wanted to exclude 'b'

const objCopyTwo = includeKeys(obj, [symbol, 'a'])
objCopyTwo[symbol] // undefined. Unexpected: the consumer intended to keep symbol

I can submit a PR.

How Filter child objects present within root json object??

For Instance, let consider we have a JSON.

const person = {
  name: "John Doe",
  age: 27,
  kids:[
    {
      name: "James Doe",
      age: 7
    },
    {
      name: "Mary Doe",
      age: 7
    }
  ]
}

how to write the filter-obj so that I can get this solution.

filterObject(person, ['name', 'kids??'])
I tested it locally neither kids.name, kids[name], kids{name} worked!!

const person = {
  name: "John Doe",
  kids:[
    {
      name: "James Doe"
    },
    {
      name: "Mary Doe"
    }
  ]
}

Use `Object.getOwnPropertyDescriptors`

Instead of using Reflect.ownKeys and then Object.getOwnPropertyDescriptor (one), why not just get Object.getOwnPropertyDescriptors (multiple) and loop that?

Similarly, you could use Object.defineProperties instead of Object.defineProperty to create the object in one pass after mapping the result of the previous function.

This changes whether a property is writable or configurable

The new properties returned by this library are always writable and configurable, even when the original properties were not. While non-writable or non-configurable properties are sometimes problematic, there are sometimes legitimate reasons for it. This seems to be an unintentional side effect.

const obj = Object.defineProperty({}, 'prop', { value: true, enumerable: true, writable: false, configurable: false })
const objCopy = includeKeys(obj, ['prop'])
console.log(Object.getOwnPropertyDescriptor(objCopy, 'prop'))
// { value: true, writable: true, enumerable: true, configurable: true }

Additionally, when a property is using get/set, they are currently removed.

const obj = Object.defineProperty({}, 'prop', { get: () => Math.random(), enumerable: true })
console.log(obj.prop, obj.prop) // Different random numbers
const objCopy = includeKeys(obj, ['prop'])
console.log(objCopy.prop, objCopy.prop) // Same random numbers

Instead of retrieving then copying the property values, the descriptors should be used instead.

const value = object[key];

const value = object[key];

Note: I can submit a PR.

Keeping non-enumerable properties

At the moment, non-enumerable properties are removed from the return value.

On one hand:

  • Most functional utilities iterate on objects using standard for ... in, Object.keys(...), etc. which ignore non-enumerable properties. Object spreading { ...object } itself removes non-enumerable properties.
  • The concept of a property being non-enumerable implies that it should not be iterated when being copied over.
  • Non-enumerable properties are much less common on plain objects than on class instances, and this library is meant for plain objects.

On the other hand, some users might not expect non-enumerable properties removal as a side effect of this library. For example:

  • excludeKeys(object, ['one']): this would remove object.two if it is non-enumerable, even though the user operation only intended to exclude object.one.
  • includeKeys(object, ['one']): this would not include object.own if it is non-enumerable, even though the user explicitly requested to include it.
  • For predicate functions, the user intent is less clear.

@sindresorhus What are your thoughts on this?

Allow any type of iterable

A consumer may want to use a Set instead of an array for filtering keys. We could allow for iterables to be provided.

Own `'__proto__'` keys assign to the prototype

If the input object contains an own property with the key '__proto__' and the filter does not reject it, rather than create a corresponding own property on the target object, filterObject sets the prototype of the target object to the property's value.

One possible fix is to use Object.defineProperty() for creating properties, rather than assignment.

I'll submit a PR adding test cases and the proposed fix.

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.