Bounds Plugin

Load the file:

<script src="bounds-plugin.js"></script>

This plugin allows you insert a method into the chain to check how many items are in the collection at that point. For example, to ensure that you're removing exactly one element from the document, you can do:

   $(sel).bounds(1,1).remove();

The general syntax is .bound(min,max). The method will complain if the length of the collection is less than min or greater than max. Equality yields no complaint.

If max is omitted, there's no maximum, so if you want to ensure that you are removing at least one element from the document, you do:

   $(sel).bounds(1).remove();

Because this is intended to be a debugging aid, the plugin throws a RangeError if the bounds-check doesn't match.

As a shorthand for a common case, .one() is the same as .bounds(1,1)

As a shorthand for another common case, .some() is the same as .bounds(1)

Tests for my jQuery bounds-checking plug-in

  1. bounds(0), can never produce an error
  2. bounds(1), makes the H1 red
  3. one(), underlines the header above
  4. bounds(1), complains that there's no nav, instead of turning nothing bold
  5. bounds(2), colors all the LI green
  6. bounds(1,1), colors the H1#tests green
  7. bounds(1,1), complains that there are too many LI, instead of making them bold
  8. bounds(1,10) makes all the LI italic
  9. bounds(2,2), having selected the first child, complains
  10. some() makes all the LI underlined

You can also try to mouse-over this paragraph to make it red