Skip to content

Ember.isEmpty({}) returns false #5543

Closed

Description

@ultimatemonty

Currently Ember.isEmpty returns false if I pass in an empty object when I would expect it to return true. This feels like a gap in the functionality.

@taras suggested using Ember.keys({}).length === 0 and while this certainly works it doesn't feel intuitive when you have Ember.isEmpty available.

It's trivial to implement - I'd be glad to submit a PR if it's agreed that it is indeed a gap.

Activity

  1. taras commented on Sep 4, 2014

    @taras
    Contributor

    Ember.keys({}).length === 0 could be added to Em.isEmpty for testing objects.

  2. workmanw commented on Sep 4, 2014

    @workmanw

    I think there is a distinction between Ember.isEmpty and Ember.isNone. If you're looking for null checking, you should use Ember.isNone.

    Completely misread. Sorry about that.

  3. ultimatemonty commented on Sep 4, 2014

    @ultimatemonty
    Author

    @workmanw my usecase isn't null checking. I was passing an object to store.find and had a need to remove a specific property on the object if present and then check to see if the object had other properties or if it was empty.

    ...
    
    if (typeof query.procedure !== 'undefined') {
       var proc = query.procedure;
       var httpRequestType = 'GET';
    
       delete query.procedure;
    
       // check to see if there are additional query params
       // Currently Ember.isEmpty(query) will always return false even if query has no additional properties
       if (!Ember.isEmpty(query)) {
          httpRequestType = 'POST';
       }
    }
    
    ...

    Ember.isNone({}) returns false which is correct because the object isn't null - it's empty.

  4. stefanpenner commented on Sep 7, 2014

    @stefanpenner
    Member

    This was decided because we couldn't guarentee the same results across all our browsers. As es3 browsers have no concept of user defined non enumerable methods. At the point in time we where we drop them we could revisit this.

  5. ultimatemonty commented on Sep 7, 2014

    @ultimatemonty
    Author

    thanks @stefanpenner will close for now and use @taras suggested method.

  6. ronco commented on May 21, 2015

    @ronco
    Contributor

    With 2.0 dropping support for IE 8 could we consider fixing this behavior to be true on {}? That would seem more consistent with the behavior for [] and 2.0 would be a good occasion to introduce an API breaking change.

  7. BlueRaja commented on Jun 10, 2015

    @BlueRaja

    Bump, makes no sense that an empty object isn't empty.

  8. timini commented on Jul 2, 2015

    @timini

    Confusing.

  9. stefanpenner commented on Jul 2, 2015

    @stefanpenner
    Member

    We can likely revisit this in 2.0 which drops support for IE8, so now there is a concept of enumerable vs non enumerable properties across all our target platforms.

    In-fact aspects of ember already rely on this concept. {{each-in}

    If someone is interested in championing and RFC we may be able to re-open things thought.

  10. BillBrower commented on Feb 1, 2017

    @BillBrower

    Was this revisited in 2.0?

  11. gnapse commented on Feb 8, 2017

    @gnapse

    I don't think so. And it should have been IMO. It does not make sense that Ember.isEmpty({}) is false.

  12. akshayrawat commented on Mar 10, 2017

    @akshayrawat
  13. joshkg commented on Mar 21, 2018

    @joshkg

    Seems silly to dip into jQuery for this check - can we revisit this issue now that we're 3.0?

  14. snewcomer commented on May 22, 2018

    @snewcomer
    Contributor

    Anybody coming across this - looks like in a 2015 rfc, it was decided that returning true for {} would have desired unintended side effects.

    If that is still the current thought process and the addon is a path forward, I would be happy to put that out there. However, it's super simple to implement ourselves, so perhaps that is why nobody has done so ¯_(ツ)_/¯

    emberjs/rfcs#82

  15. rwjblue commented on May 22, 2018

    @rwjblue
    Member

    Almost certainly Ember.{isEmpty,isBlank,isNone,isPresent} should be deprecated. They don’t really add value over the normal JS that you would write in each case, and they generally have negative cognitive impact on the code in question...

  16. BlueRaja commented on May 22, 2018

    @BlueRaja

    We've been using lodash _.isEmpty(), _.isNil(), etc. which actually work correctly

  17. snewcomer commented on May 22, 2018

    @snewcomer
    Contributor

    I agree. Should I/we put together an RFC? And potentially extracted to an addon as well (for those that gratuitously used them)?

  18. locks commented on May 22, 2018

    @locks
    Contributor
  19. snewcomer commented on Jun 2, 2018

    @snewcomer
    Contributor

    @rwjblue Getting some feedback that these methods will still be bundled in Ember core. How should we handle the deprecation side of things for these util methods?

  20. jfschaff commented on Jun 17, 2019

    @jfschaff

    isEmpty is not meant to check for empty objects:

    https://github.com/emberjs/ember.js/blob/master/packages/ember-metal/lib/is_empty.js#L6-L7

    The function name has just been poorly chosen... IsEmpty should tell you yes when given... an empty "set"...

  21. gtsop commented on Oct 31, 2019

    @gtsop

    @jfschaff @marclundgren Old comment, I referenced the description on the jsdoc that didn't mention checking for an empty object. Currently lives here:

    https://github.com/emberjs/ember.js/blob/master/packages/%40ember/-internals/metal/lib/is_empty.ts#L6-L7

    Not to say that I didn't expect the method to check an empty object as well, just saying that there wasn't such a claim in the first place.

    (updated original comment)

  22. ashish2199 commented on Oct 5, 2020

    @ashish2199

    Where are we on this? Will this ever be incorporated?

  23. locks commented on Oct 5, 2020

    @locks
    Contributor

    @ashish2199 the current proposal is to deprecate the APIs, not change them: emberjs/rfcs#334.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions