Giter Club home page Giter Club logo

omit-empty's Introduction

omit-empty NPM version NPM monthly downloads NPM total downloads Linux Build Status

Recursively omit empty properties from an object. Omits empty objects, arrays, strings or zero.

Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your ❤️ and support.

Install

Install with npm:

$ npm install --save omit-empty

Heads up!

Breaking changes in v1.0.0! See the Release History.

Usage

const omitEmpty = require('omit-empty');

console.log(omitEmpty({ a: 'a', b: '' }));
//=> { a: 'a' }

console.log(omitEmpty({ a: 'a', b: { c: 'c', d: '' } }));
//=> { a: 'a', b: { c: 'c' } }

console.log(omitEmpty({ a: ['a'], b: [] }));
//=> { a: ['a'] }

console.log(omitEmpty({ a: 0, b: 1 }));
//=> { a: 0, b: 1 }

// set omitZero to true, to evaluate "0" as falsey
console.log(omitEmpty({ a: 0, b: 1 }, { omitZero: true }));
//=> { b: 1 }

About

Contributing

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

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

Related projects

You might also be interested in these projects:

Contributors

Commits Contributor
31 jonschlinkert
1 kakadiadarpan

Author

Jon Schlinkert

License

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


This file was generated by verb-generate-readme, v0.8.0, on December 10, 2018.

omit-empty's People

Contributors

jonschlinkert avatar kakadiadarpan 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

Watchers

 avatar  avatar  avatar  avatar  avatar

omit-empty's Issues

$init keyname appeares on result automatically

If all the items inside the object are Boolean then "$init": true will be added to the output automatically

omitEmpty({ a: true, b: false}) -> { a: true, b: false, $init: true }

Any comments why this happens?

Doesn't keep Symbol properties

Hello! When omitEmpty is called with an object that has Symbols as property keys, those keys are missing from the result:

const omitEmpty = require('omit-empty')
const assert = require('assert')

let gte = Symbol('gte'),
  lt = Symbol.for('lt')

test('should clean some symbols', () => {
  let actual = omitEmpty({
    userId: 1,
    age: {
      [gte]: null,
      [lt]: 20
    }
  })

  let expected = {
    userId: 1, 
    age: {
      [lt]: 2
    }
  }

  // fails: the returned object is just { userId: 1 }
  assert.deepEqual(actual, expected)
})

This happens because Object.keys() only sees string keys, in this line:

for (let key of Object.keys(value)) {

This could be fixed by using Object.getOwnPropertySymbols() in addition to Object.keys().

(The reason I'm using symbols as property keys is because of Sequelize's recommended query practices).

Unable to install "omit-empty:0.4.0" using Bower

Hi,

I am trying to install "omit-empty": "0.4.0" in angular based project.

Error:
bower omit-empty#0.4.0 ENOTFOUND Package omit-empty not found
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------

Could you please provide a hint here, Also Should we consider index.js file to be added in angular project to utilize omit-empty module.

Thanks!

Doesn't omit last object

Hello,

when I pass this structure to omitEmpty :

const test = {
  baz: [
    { foo: "foo" },
    { bar: '' }
  ]
}

it doesn't suppress the last object { bar: '' }

omitFalse option

Just like the removeZero option there are other falsy values that could be omitted like the explicit false.

Also removes Dates

omitEmpty({foo: new Date()});

returns an empty object, is this intended?

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.