Skip to content

Doesn't keep Symbol properties #9

@stnever

Description

@stnever

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions