views:

555

answers:

3

Does anyone know of a jQuery plugin that has 'helpers' or extensions like those found in the YAHOO.lang namespace?

I have in mind functions such as:

isNull
isDefined
isString
isFunction

I would also appreciate the same kind of thing for strings and arrays, such as Contains, StartsWith (I know these are easy to write, I'm just looking for a plugin that encompasses them all).

It's not in the YAHOO.lang namespace but also form related extensions- determining a radiobox's value (from the one checked), a form element's type in a friendly name.

Specifically a plugin with fluent API rather than selector based such as

$("input[@type=radio][@checked]")

Again I'm aware they're easy to implement but I don't want to reinvent the wheel.

+2  A: 

jQuery 1.3.2 has isFunction and isArray built in (see snippet below). The code for isString is staightforward (typeof obj === "string"), as is isNull (obj === null) and isDefined (obj !== undefined) - so I would just code that inline instead of using a function.

// See test/unit/core.js for details concerning isFunction.
// Since version 1.3, DOM methods and functions like alert
// aren't supported. They return false on IE (#2968).
isFunction: function( obj ) {
    return toString.call(obj) === "[object Function]";
},

isArray: function( obj ) {
    return toString.call(obj) === "[object Array]";
},
Kevin Hakanson
typeof String("foo") === "object"
Dinoboff
A: 

I decided to write two new plugins myself. Here's the two projects:

Form extensions

Example:

// elementExists is also added
if ($("#someid").elementExists())
  alert("found it");

// Select box related
$("#mydropdown").isDropDownList();

// Can be any of the items from a list of radio boxes - it will use the name
$("#randomradioboxitem").isRadioBox("myvalue");
$("#radioboxitem").isSelected("myvalue");

General extensions

These are modelled on Prototype/Mochikit functions such as isNull.

Example:

$.isNumber(42);

var x;
$.isUndefined(x);
$.isNullOrUndefined(x);
$.isString(false);

$.emptyString("the quick brown fox");
$.startsWith("the quick brown fox","the");
$.formatString("Here is the {0} and {2}","first","second");

Both have over 50 unit tests that come as part of the download. Hopefully they'll be useful to people finding this page.

Chris S
+1  A: 

Underscore.js or _.js, if you prefer is a library which contains these functions.

CoolAJ86