Last active
March 26, 2017 19:39
-
-
Save moollaza/ce1472183ddf8fd4f947 to your computer and use it in GitHub Desktop.
DuckDuckHack Webcast - Making a "Wordcount" Goodie Instant Answer with JavaScript & Templates
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
DDH.word_count = DDH.word_count || {}; | |
(function(DDH) { | |
"use strict"; | |
console.log("DDH.word_count.build"); // remove this before submitting pull request | |
DDH.word_count.build = function(ops) { | |
$.each(ops.data.list, function(k, v) { | |
ops.data.list[k] = { | |
word: v.word, | |
length: v.word.length | |
}; | |
}); | |
if (!ops.data.list_view){ | |
ops.data.title = "There is only 1 word"; | |
ops.templates = { group: "text" }; | |
return ops; | |
} | |
}; | |
})(DDH); |
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
package DDG::Goodie::WordCount; | |
use DDG::Goodie; | |
use strict; | |
zci answer_type => 'word_count'; | |
zci is_cached => 1; | |
triggers start => 'word count', 'wordcount', 'how many words in'; | |
handle remainder => sub { | |
my $remainder = $_; | |
return unless $remainder; | |
my @words = split " ", $remainder; | |
my $count = scalar @words; | |
my @list = map { word => $_ }, @words; | |
my $list_view = $count > 1; | |
return "plain text response", | |
structured_answer => { | |
id => 'word_count', | |
name => 'Answer', | |
data => { | |
title => "There are $count words", | |
subtitle => "Wordcount for: $remainder", | |
list => \@list, | |
list_view => $list_view | |
}, | |
templates => { | |
group => 'list', | |
options => { | |
list_content => "DDH.word_count.list_content" | |
} | |
} | |
}; | |
}; | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment