Merge pull request #476 from b1ake/master
Character Counter (Chars.pm) - More triggers, quoted strings and better testsmaster
commit
81d169bd70
|
@ -3,22 +3,59 @@ package DDG::Goodie::Chars;
|
|||
|
||||
use DDG::Goodie;
|
||||
|
||||
triggers start => 'chars';
|
||||
triggers startend =>
|
||||
'chars',
|
||||
'number of characters',
|
||||
'number of chars',
|
||||
'num chars',
|
||||
'num characters',
|
||||
'char count',
|
||||
'character count',
|
||||
'characters count',
|
||||
'length of string',
|
||||
'length in characters',
|
||||
'length in chars';
|
||||
|
||||
zci is_cached => 1;
|
||||
zci answer_type => "chars";
|
||||
|
||||
primary_example_queries 'chars test';
|
||||
secondary_example_queries 'chars this is a test';
|
||||
description 'count the number of charaters in a query';
|
||||
name 'Chars';
|
||||
name 'Character Counter';
|
||||
description 'Count the number of charaters in a query';
|
||||
primary_example_queries 'chars in "my string"';
|
||||
code_url 'https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/Chars.pm';
|
||||
category 'computing_tools';
|
||||
topics 'programming';
|
||||
|
||||
handle remainder => sub {
|
||||
return "Chars: " .length $_ if $_;
|
||||
return;
|
||||
my ($str) = @_;
|
||||
return if !$str;
|
||||
|
||||
# remove leading word 'in',
|
||||
# e.g. 'chars in mississippi' would just count the string 'mississippi'.
|
||||
$str =~ s/^\s*in\b//;
|
||||
|
||||
# trim spaces at beg and end of string
|
||||
$str =~ s/^\s+|\s+$//g;
|
||||
|
||||
# if nothing left in the string, return without triggering the IA.
|
||||
# this means the remainder contained only the word 'in' and/or spaces.
|
||||
return if !$str;
|
||||
|
||||
# if surrounded by quotation marks (either ' or ")
|
||||
# remove so we don't include them in the count
|
||||
$str =~ s/^["'](.*)["']$/$1/;
|
||||
|
||||
# get the length of the string in characters
|
||||
my $len = length($str);
|
||||
|
||||
# pluralize the word 'character' unless length is 1.
|
||||
# note that this works for length=0, i.e. we'll correctly get '0 characters'.
|
||||
my $characters_pluralized = (length($str) == 1 ? 'character' : 'characters');
|
||||
|
||||
# build the output string
|
||||
my $text_out = qq("$str" is $len $characters_pluralized long.);
|
||||
|
||||
return $text_out;
|
||||
};
|
||||
|
||||
1;
|
||||
|
|
76
t/Chars.t
76
t/Chars.t
|
@ -12,8 +12,78 @@ ddg_goodie_test(
|
|||
[qw(
|
||||
DDG::Goodie::Chars
|
||||
)],
|
||||
'chars test' => test_zci('Chars: 4'),
|
||||
'chars this is a test' => test_zci('Chars: 14'),
|
||||
);
|
||||
|
||||
# string can be inside double quotes, and quotes shouldn't be counted as characters
|
||||
'chars in "my string"' => test_zci('"my string" is 9 characters long.'),
|
||||
|
||||
# string can be inside single quotes, and single quotes shouldn't be counted as characters
|
||||
"chars in 'my string'" => test_zci('"my string" is 9 characters long.'),
|
||||
|
||||
# string shouldn't need quotes
|
||||
'chars in my string' => test_zci('"my string" is 9 characters long.'),
|
||||
|
||||
# extra spaces shouldn't be counted
|
||||
'chars in my string ' => test_zci('"my string" is 9 characters long.'),
|
||||
|
||||
# extra spaces before 'in' should still trigger
|
||||
'chars in my string' => test_zci('"my string" is 9 characters long.'),
|
||||
|
||||
# one character strings should say '1 character long' instead of '1 characters long'
|
||||
'chars in "1"' => test_zci('"1" is 1 character long.'),
|
||||
|
||||
# trigger plus empty quotes should return a length of 0.
|
||||
'chars in ""' => test_zci('"" is 0 characters long.'),,
|
||||
|
||||
#####
|
||||
# triggers that SHOULD NOT load the IA
|
||||
|
||||
# a trigger query with no text should not trigger the IA
|
||||
'chars' => undef,
|
||||
|
||||
# a trigger query plus the word 'in' should not trigger the IA
|
||||
'chars in' => undef,
|
||||
|
||||
# a trigger query plus the word 'in' and spaces should not trigger the IA
|
||||
'chars in ' => undef,
|
||||
|
||||
# searches for TV characters should not load the IA
|
||||
'Sopranos characters' => undef,
|
||||
'characters in the Sopranos' => undef,
|
||||
|
||||
# generic length searches should not load the IA
|
||||
'length of the Nile River' => undef,
|
||||
'Titanic movie length' => undef,
|
||||
|
||||
|
||||
#####
|
||||
# triggers that SHOULD load the IA
|
||||
|
||||
'chars "my string"' => test_zci('"my string" is 9 characters long.'),
|
||||
'chars in "my string"' => test_zci('"my string" is 9 characters long.'),
|
||||
'number of chars in "my string"' => test_zci('"my string" is 9 characters long.'),
|
||||
'"my string" number of chars' => test_zci('"my string" is 9 characters long.'),
|
||||
'number of characters in "my string"' => test_zci('"my string" is 9 characters long.'),
|
||||
'"my string" number of characters' => test_zci('"my string" is 9 characters long.'),
|
||||
'num chars "my string"' => test_zci('"my string" is 9 characters long.'),
|
||||
'"my string" num chars' => test_zci('"my string" is 9 characters long.'),
|
||||
'num chars in "my string"' => test_zci('"my string" is 9 characters long.'),
|
||||
'num characters "my string"' => test_zci('"my string" is 9 characters long.'),
|
||||
'"my string" num characters' => test_zci('"my string" is 9 characters long.'),
|
||||
'num characters in "my string"' => test_zci('"my string" is 9 characters long.'),
|
||||
'char count "my string"' => test_zci('"my string" is 9 characters long.'),
|
||||
'"my string" char count' => test_zci('"my string" is 9 characters long.'),
|
||||
'char count in "my string"' => test_zci('"my string" is 9 characters long.'),
|
||||
'character count "my string"' => test_zci('"my string" is 9 characters long.'),
|
||||
'"my string" character count' => test_zci('"my string" is 9 characters long.'),
|
||||
'character count in "my string"' => test_zci('"my string" is 9 characters long.'),
|
||||
'length of string "my string"' => test_zci('"my string" is 9 characters long.'),
|
||||
'"my string" length of string' => test_zci('"my string" is 9 characters long.'),
|
||||
'length in characters "my string"' => test_zci('"my string" is 9 characters long.'),
|
||||
'"my string" length in characters' => test_zci('"my string" is 9 characters long.'),
|
||||
'length in chars "my string"' => test_zci('"my string" is 9 characters long.'),
|
||||
'"my string" length in chars' => test_zci('"my string" is 9 characters long.'),
|
||||
|
||||
);
|
||||
|
||||
done_testing;
|
||||
|
||||
|
|
Loading…
Reference in New Issue