Giter Club home page Giter Club logo

Comments (10)

domchristie avatar domchristie commented on August 22, 2024

Are you able to give an example? Thanks.

from humps.

ajainvivek avatar ajainvivek commented on August 22, 2024

Example:

const myArr = [{
 validTill: 'date obj',
 eventId: 'uniq id'
}]

const decamelizeKeys = humps.decamelizeKeys(myArr); // Throws error obj.hasOwnProperty is not a function

// OR

const decamelizeKeys = _.map(myArr, (item) => humps.decamelizeKeys(item)); // Throws error obj.hasOwnProperty is not a function

Environment: Node 6

It works fine on browser.

from humps.

domchristie avatar domchristie commented on August 22, 2024

Sorry I am unable to reproduce this. I have tested the following in Node 6.9.3 and it works as expected:

> var humps = require('./')
undefined
> const myArr = [{
...  validTill: 'date obj',
...  eventId: 'uniq id'
... }]
undefined
> 
> const decamelizeKeys = humps.decamelizeKeys(myArr);
undefined
> decamelizeKeys
[ { valid_till: 'date obj', event_id: 'uniq id' } ]

from humps.

lloydh avatar lloydh commented on August 22, 2024

Hi,

I came across this issue too trying to run decamelizeKeys on the output object produced by the Joi validator. It turns out that the .hasOwnProperty method gets stripped by Joi.

Since we can't rely on supplied objects having the hasOwnProperty method (or a modified version) I have created a pull request to call hasOwnProperty directly from the Object prototype.

Lloyd

from humps.

lloydh avatar lloydh commented on August 22, 2024

Any chance of merging the fix and making a new release?

from humps.

levivoelz avatar levivoelz commented on August 22, 2024

I'm running into the same issue, but in the browser. The PR above seems to fix it.

from humps.

khisakuni avatar khisakuni commented on August 22, 2024

Any update on this? I'm running into this issue on the browser as well.

from humps.

domchristie avatar domchristie commented on August 22, 2024

obj.hasOwnProperty is a widely recognised method to check object properties, so I would like to understand why this is happening before merging. If anyone can provide a failing demo, I can look into it. Thanks.

from humps.

lloydh avatar lloydh commented on August 22, 2024

Yes, hasOwnProperty is the de facto way to check objects for properties, but unfortunately it's mutable so out in the wild it can't be relied on.

Here are a couple of examples of how you can't rely on the hasOwnProperty method of an object you haven't created.

const foo = Object.create(null);
foo; // {}
foo.bar = 'baz';
foo.hasOwnProperty('bar'); // TypeError: foo.hasOwnProperty is not a function

Object.prototype.hasOwnProperty.call(foo, 'bar'); // true
const foo2 = { bar: 'baz' };
foo2.hasOwnProperty = () => false;
foo2.hasOwnProperty('bar'); // false

Object.prototype.hasOwnProperty.call(foo2, 'bar'); // true

More info...

The comments above demonstrate this is more than just a hypothetical problem and I think that a library with no control over the objects that it receives should follow the principle:
Be conservative in what you do, be liberal in what you accept from others

from humps.

domchristie avatar domchristie commented on August 22, 2024

Perfect, thanks for the explanation! #36 has been merged and released in v2.0.1

from humps.

Related Issues (20)

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.