1 $(document).ready(function() {
5 test("chaining: map/flatten/reduce", function() {
7 "I'm a lumberjack and I'm okay",
8 "I sleep all night and I work all day",
9 "He's a lumberjack and he's okay",
10 "He sleeps all night and he works all day"
12 var counts = _(lyrics).chain()
13 .map(function(line) { return line.split(''); })
15 .reduce(function(hash, l) {
16 hash[l] = hash[l] || 0;
20 ok(counts['a'] == 16 && counts['e'] == 10, 'counted all the letters in the song');
23 test("chaining: select/reject/sortBy", function() {
24 var numbers = [1,2,3,4,5,6,7,8,9,10];
25 numbers = _(numbers).chain().select(function(n) {
27 }).reject(function(n) {
29 }).sortBy(function(n) {
32 equals(numbers.join(', '), "10, 6, 2", "filtered and reversed the numbers");
35 test("chaining: select/reject/sortBy in functional style", function() {
36 var numbers = [1,2,3,4,5,6,7,8,9,10];
37 numbers = _.chain(numbers).select(function(n) {
39 }).reject(function(n) {
41 }).sortBy(function(n) {
44 equals(numbers.join(', '), "10, 6, 2", "filtered and reversed the numbers");
47 test("chaining: reverse/concat/unshift/pop/map", function() {
48 var numbers = [1,2,3,4,5];
49 numbers = _(numbers).chain()
54 .map(function(n){ return n * 2; })
56 equals(numbers.join(', '), "34, 10, 8, 6, 4, 2, 10, 10", 'can chain together array functions.');