Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Enumerable plugin for jQuery.
JavaScript Ruby
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
public/javascripts
spec/javascripts
README.rdoc
Rakefile
jquery.enumerable.js

README.rdoc

jquery-enumerable

Enumerable plugin for jQuery.

USAGE

<script src="jquery.min.js" type="text/javascript"></script>
<script src="jquery.enumerable.js" type="text/javascript"></script>

EXAMPLE

var square = $([1, 2, 3, 4, 5]).collect(function(index, value) {
  return value * value;
}); // => [1, 4, 9, 16, 25]

var even = $([1, 2, 3, 4, 5]).select(function(index, value) {
  return value % 2 == 0;
}); // => [2, 4]

var sum = $([1, 2, 3, 4, 5]).inject(0, function(result, index, value) {
  return result + value;
}); // => 15

var included = $([1, 2, 3, 4, 5]).include(1);  // => true
included = $([1, 2, 3, 4, 5]).include(10); // => false

var max = $([1, 10, 100, 1000]).max(); // => 1000
max = $([-3, -2, -1]).maxBy(function(index, value) {
  return value * value;
}); // => -3

var min = $([-1000, -100, -10, 0]).min(); // => -1000
min = $([-4, -2, 1, 3]).minBy(function(index, value) {
  return value * value;
}); // => 1

var numbers = $([1, 2, 3, 4, 5, 6]).partition(function(index, value) {
  return value % 2 == 0;
}); // => [[2, 4, 6], [1, 3, 5]]
var even = numbers[0]; // => [2, 4, 6]
var odd  = numbers[1]; // => [1, 3, 5]

var sorted_by_string = $(["200", "30", "1000"]).sortBy(function(index, value) {
  return value;
}); // => ["1000", "200", "30"]
var sorted_by_number = $(["200", "30", "1000"]).sortBy(function(index, value) {
  return parseInt(value);
}); // => ["30", "200", "1000"]

var zip = $([1, 2, 3]).zip([4, 5, 6], [7, 8, 9]) // => [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
var cube = $([1, 2, 3]).zip([1, 2, 3], [1, 2, 3], function(arr_value) {
  return $(arr_value).inject(1, function(result, index, value) {
    return result * value;
  });
}); // => [1, 8, 27];

The following functions are currently implemented

all
any
collect
select
inject
member
include
max
maxBy
min
minBy
portition
reject
sortBy
zip
pluck

Copyright

Copyright (C) 2011 Kazuya Takeshima

Released under the WTFPL: sam.zoy.org/wtfpl

Something went wrong with that request. Please try again.