Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #10356 (new enhancement)

Opened 2 years ago

Last modified 1 year ago

[PATCH] Object.hasProperties(), Object.isEmpty()

Reported by: kendsnyder Assigned to: sam
Priority: normal Milestone: 2.x
Component: Prototype Version: edge
Severity: normal Keywords: discuss
Cc:

Description

Ever wanted to know if an object is an empty object (equivalent to {})? Those new to JS often mistakenly assume that {} == {} but not so.

This function simply tries to iterate the object, returning true on the first iteration or false if the object has no properties to iterate.

Use cases include the following: - Performing an ajax request on "finish" only if the cache contains actions to perform - Prompting user to save on page unload only if actions have been taken - Showing an object as an ordered list or select options

One could argue that $H(object).size() == 0 performs the same function, but it is less intuitive and time-consuming for large objects.

Attachments

Object_hasProperties.diff (1.3 kB) - added by kendsnyder on 12/04/07 05:33:06.
Object_isEmpty.diff (1.2 kB) - added by kendsnyder on 12/06/07 04:10:51.
hasProperties function converted to isEmpty

Change History

12/04/07 05:33:06 changed by kendsnyder

  • attachment Object_hasProperties.diff added.

12/04/07 09:30:46 changed by mislav

  • keywords set to discuss.
  • priority changed from normal to low.
  • severity changed from normal to minor.

I use $H(object).any(), but your solution is faster because it doesn't have to construct a new Hash.

About naming: why not Object.empty/isEmpty?

12/04/07 12:37:44 changed by kangax

+1 for isEmpty

12/04/07 14:16:08 changed by kendsnyder

The reason I suggested hasProperties() instead of isEmpty() is that emptiness is different to different people. Many would expect isEmpty() to return true for empty arrays and empty strings.

I come from the PHP world where empty() is all kinds of madness: http://php.net/empty

12/04/07 14:26:31 changed by mislav

We come from different worlds. Don't forget we aren't following PHP conventions, but of a language that's completely different.

12/04/07 15:14:17 changed by kangax

isEmpty seems to reflect most common use case - testing an object for emptiness and just feels more semantic in this context.

Personally, I rarely need to check if an object contains some hypothetical properties - I am solely interested if it is empty or not.

Ruby seems to go the empty? way as well (though I might be missing something)

(follow-up: ↓ 7 ) 12/04/07 15:28:53 changed by mislav

Since JavaScript doesn't support question marks in the method name, I think we (the Core team) agreed one day to prefer "isSomething" convention over just "something".

Object.isEmpty() is the way to go here.

(in reply to: ↑ 6 ) 12/04/07 15:34:55 changed by kangax

  • summary changed from [PATCH] Object.hasProperties() to [PATCH] Object.hasProperties(), Object.isEmpty().

Replying to mislav:

Since JavaScript doesn't support question marks in the method name, I think we (the Core team) agreed one day to prefer "isSomething" convention over just "something". Object.isEmpty() is the way to go here.

That's exactly why I like isEmpty() :)

12/06/07 04:10:51 changed by kendsnyder

  • attachment Object_isEmpty.diff added.

hasProperties function converted to isEmpty

(follow-up: ↓ 10 ) 12/21/07 12:47:22 changed by Tobie

imho, this should go hand in hand with an aliasing of all the empty methods to isEmpty and with a check for that method in Object.isEmpty like so:

isEmpty: function(object) {
  if (object && object.isEmpty) return object.isEmpty();
  for (var property in object) return false; 
  return true;
}

because

Object.isEmpty([])
// -> false

is *really* counter-intuitive.

Thoughts ?

12/21/07 16:35:47 changed by kendsnyder

I completely agree. That is why I proposed calling it hasProperties(). It is simple to tell if other types of objects are empty using length or any(). But the quickest way to detect if an object is empty is $H({}).any() which can be slower than hasProperties().

(in reply to: ↑ 8 ) 04/02/08 02:27:01 changed by kangax

  • priority changed from low to normal.
  • severity changed from minor to normal.

Replying to Tobie:

imho, this should go hand in hand with an aliasing of all the empty methods to isEmpty and with a check for that method in Object.isEmpty like so: {{{ isEmpty: function(object) { if (object && object.isEmpty) return object.isEmpty(); for (var property in object) return false; return true; } }}} because {{{ Object.isEmpty([]) // -> false }}} is *really* counter-intuitive. Thoughts ?

Tobie, it's counter-intuitive because the implementation is somewhat naive : )

// all of these return false

isEmpty([]);
isEmpty(/regexp/);
isEmpty('string');
isEmpty(5);
isEmpty(Infinity);
isEmpty(NaN);
isEmpty(document.createElement)

I think we could do a little better:

function isEmpty(o) {
  if (typeof o == 'object') {
    for (var p in o)
      if (o[p] !== undefined && typeof o[p] != 'function')
        return false;
  }
  return true;
};

04/02/08 02:31:28 changed by kangax

Forgot to mention that the proposed function returns true when passed any of those "objects"