Created
January 21, 2012 17:46
-
-
Save abernier/1653407 to your computer and use it in GitHub Desktop.
$.fn.segment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(($) -> | |
### | |
Grab a segment of a jQuery collection | |
* start: a zero-based index from which to grab | |
* width: the width of the segment (default: 1) | |
Examples: | |
| A | B | C | D | E | $myList | |
<-> .segment( 0) | |
<-----> .segment( 1, 2) | |
<-> .segment(-1) | |
<-------------> .segment(-1, 4) | |
### | |
$.fn.segment = (start, width = 1) -> | |
length = @length | |
end = undefined | |
width = Math.max(1, width) | |
width = Math.min(length, width) | |
if (start >= 0) | |
start = Math.min(length - width, start) | |
end = start + width; | |
else | |
start = Math.max(-length, start) | |
start = Math.min(-width, start) | |
end = start + width; | |
end = undefined if (end == 0) | |
@slice(start, end) | |
)(jQuery) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ex: http://jsfiddle.net/abernier/Lpcz8/4/