react / wstein / node_modules / jest-cli / node_modules / cover / node_modules / underscore.string / test / test_underscore / chaining.js
80684 views$(document).ready(function() {12module("Chaining");34test("chaining: map/flatten/reduce", function() {5var lyrics = [6"I'm a lumberjack and I'm okay",7"I sleep all night and I work all day",8"He's a lumberjack and he's okay",9"He sleeps all night and he works all day"10];11var counts = _(lyrics).chain()12.map(function(line) { return line.split(''); })13.flatten()14.reduce(function(hash, l) {15hash[l] = hash[l] || 0;16hash[l]++;17return hash;18}, {}).value();19ok(counts['a'] == 16 && counts['e'] == 10, 'counted all the letters in the song');20});2122test("chaining: select/reject/sortBy", function() {23var numbers = [1,2,3,4,5,6,7,8,9,10];24numbers = _(numbers).chain().select(function(n) {25return n % 2 == 0;26}).reject(function(n) {27return n % 4 == 0;28}).sortBy(function(n) {29return -n;30}).value();31equals(numbers.join(', '), "10, 6, 2", "filtered and reversed the numbers");32});3334test("chaining: reverse/concat/unshift/pop/map", function() {35var numbers = [1,2,3,4,5];36numbers = _(numbers).chain()37.reverse()38.concat([5, 5, 5])39.unshift(17)40.pop()41.map(function(n){ return n * 2; })42.value();43equals(numbers.join(', '), "34, 10, 8, 6, 4, 2, 10, 10", 'can chain together array functions.');44});4546});474849