Skip to main content

Bug Tracker

Side navigation

#3330 closed feature (fixed)

Opened September 04, 2008 10:28PM UTC

Closed August 25, 2010 04:58PM UTC

Last modified March 14, 2012 06:58PM UTC

jQuery.typeof and jQuery.isEmpty Utilities

Reported by: projectatomic Owned by:
Priority: major Milestone: 1.4.3
Component: core Version: 1.4.2
Keywords: Cc:
Blocked by: Blocking:
Description

One of the best aspects of jQuery is its light weight. I have really taken advantage of some of its built in Utilities such as jQuery.extend() and jQuery.map(). I am definitely against bloated software, but I find myself writing these functions time and again. They are pretty utilitarian and not too huge. Just a suggestion (code borrowed from Crockford's Remedial JavaScript)

1. A better typeof function that actually returns 'array' for an array instead of 'object':

function typeOf(value) {

var s = typeof value;

if (s === 'object') {

if (value) {

if (typeof value.length === 'number' &&

!(value.propertyIsEnumerable('length')) &&

typeof value.splice === 'function') {

s = 'array';

}

} else {

s = 'null';

}

}

return s;

}

2. A simple isEmpty function that returns a boolean:

function isEmpty(o) {

var i, v;

if (typeOf(o) === 'object') {

for (i in o) {

v = o[i];

if (v !== undefined && typeOf(v) !== 'function') {

return false;

}

}

}

return true;

}

Attachments (0)
Change History (2)

Changed October 30, 2009 11:06PM UTC by mofle comment:1

Anyone have a patch for this?

Changed August 25, 2010 04:58PM UTC by john comment:2

milestone: 1.31.4.3
resolution: → fixed
status: newclosed
version: 1.2.61.4.2