From 14d86c827f6a576bd719ba647561021f9090d106 Mon Sep 17 00:00:00 2001 From: Joe Walnes Date: Sat, 10 Nov 2012 17:12:56 -0600 Subject: [PATCH] ResitorColors use 4 bands instead of 5. These seems to be more common. e.g. 4.7K -> * 5 bands (old): yellow(4), purple(7), black(0), brown(x10), tolerance * 4 bands (new): yellow(4), purple(7), red(x100), tolerance --- lib/DDG/Goodie/ResistorColors.pm | 14 ++--- t/ResistorColors.t | 98 +++++++++++++++----------------- 2 files changed, 54 insertions(+), 58 deletions(-) diff --git a/lib/DDG/Goodie/ResistorColors.pm b/lib/DDG/Goodie/ResistorColors.pm index ba5c2f642..0424c8c2b 100644 --- a/lib/DDG/Goodie/ResistorColors.pm +++ b/lib/DDG/Goodie/ResistorColors.pm @@ -50,8 +50,8 @@ my %digits_to_colors = ( handle matches => sub { my $input = shift; my $value = parse_value($input); - if (defined $value) { - $value = round_to_significant_places($value, 3); + if (defined $value && ($value == 0 || ($value <= 99900000000 && $value >= 1))) { + $value = round_to_significant_places($value, 2); my @digits = number_to_color_digits($value); return render($value, \@digits); } @@ -97,13 +97,12 @@ sub round_to_significant_places { # array of color digits (e.g. 4, 7, 0, 3). See %digits_to_colors. sub number_to_color_digits { my $value = shift; - return (0, 0, 0, 0) if $value == 0; # special case + return (0, 0, 0) if $value == 0; # special case my @value_digits = split(//, $value * 100); return ( $value_digits[0] || 0, $value_digits[1] || 0, - $value_digits[2] || 0, - scalar(@value_digits) - 5); + scalar(@value_digits) - 4); }; # Given a numeric value, format it like '3.2M' etc. @@ -147,8 +146,9 @@ sub render { return; } } - $html .= "
Followed by a gap and tolerance color. " - . "More at resisto.rs"; + $html .= "
" + . "" + . "More at resisto.rs"; return $text, html => $html; }; diff --git a/t/ResistorColors.t b/t/ResistorColors.t index 65d7747eb..38367e724 100644 --- a/t/ResistorColors.t +++ b/t/ResistorColors.t @@ -14,62 +14,62 @@ ddg_goodie_test( )], # Check trigger kicks in. - "330 ohms" => test_zci("330\x{2126} (ohms) resistor colors: orange (3), orange (3), black (0), black (\x{00D7}1)", html => qr/./), - "330 ohm" => test_zci("330\x{2126} (ohms) resistor colors: orange (3), orange (3), black (0), black (\x{00D7}1)", html => qr/./), - "330 \x{2126}" => test_zci("330\x{2126} (ohms) resistor colors: orange (3), orange (3), black (0), black (\x{00D7}1)", html => qr/./), - "330ohms" => test_zci("330\x{2126} (ohms) resistor colors: orange (3), orange (3), black (0), black (\x{00D7}1)", html => qr/./), - "330ohm" => test_zci("330\x{2126} (ohms) resistor colors: orange (3), orange (3), black (0), black (\x{00D7}1)", html => qr/./), - "330\x{2126}" => test_zci("330\x{2126} (ohms) resistor colors: orange (3), orange (3), black (0), black (\x{00D7}1)", html => qr/./), + "330 ohms" => test_zci("330\x{2126} (ohms) resistor colors: orange (3), orange (3), brown (\x{00D7}10)", html => qr/./), + "330 ohm" => test_zci("330\x{2126} (ohms) resistor colors: orange (3), orange (3), brown (\x{00D7}10)", html => qr/./), + "330 \x{2126}" => test_zci("330\x{2126} (ohms) resistor colors: orange (3), orange (3), brown (\x{00D7}10)", html => qr/./), + "330ohms" => test_zci("330\x{2126} (ohms) resistor colors: orange (3), orange (3), brown (\x{00D7}10)", html => qr/./), + "330ohm" => test_zci("330\x{2126} (ohms) resistor colors: orange (3), orange (3), brown (\x{00D7}10)", html => qr/./), + "330\x{2126}" => test_zci("330\x{2126} (ohms) resistor colors: orange (3), orange (3), brown (\x{00D7}10)", html => qr/./), # Various multipliers - "472000 ohms" => test_zci("472K\x{2126} (ohms) resistor colors: yellow (4), purple (7), red (2), orange (\x{00D7}1K)", html => qr/./), - "400000 ohms" => test_zci("400K\x{2126} (ohms) resistor colors: yellow (4), black (0), black (0), orange (\x{00D7}1K)", html => qr/./), - "12300 ohms" => test_zci("12.3K\x{2126} (ohms) resistor colors: brown (1), red (2), orange (3), red (\x{00D7}100)", html => qr/./), + "472000 ohms" => test_zci("470K\x{2126} (ohms) resistor colors: yellow (4), purple (7), yellow (\x{00D7}10K)", html => qr/./), + "400000 ohms" => test_zci("400K\x{2126} (ohms) resistor colors: yellow (4), black (0), yellow (\x{00D7}10K)", html => qr/./), + "12300 ohms" => test_zci("12K\x{2126} (ohms) resistor colors: brown (1), red (2), orange (\x{00D7}1K)", html => qr/./), # Rounding - "1.2345 ohms" => test_zci("1.23\x{2126} (ohms) resistor colors: brown (1), red (2), orange (3), silver (\x{00D7}0.01)", html => qr/./), - "1.2355 ohms" => test_zci("1.24\x{2126} (ohms) resistor colors: brown (1), red (2), yellow (4), silver (\x{00D7}0.01)", html => qr/./), - "12.345 ohms" => test_zci("12.3\x{2126} (ohms) resistor colors: brown (1), red (2), orange (3), gold (\x{00D7}0.1)", html => qr/./), - "12.355 ohms" => test_zci("12.4\x{2126} (ohms) resistor colors: brown (1), red (2), yellow (4), gold (\x{00D7}0.1)", html => qr/./), - "123.45 ohms" => test_zci("123\x{2126} (ohms) resistor colors: brown (1), red (2), orange (3), black (\x{00D7}1)", html => qr/./), - "123.55 ohms" => test_zci("124\x{2126} (ohms) resistor colors: brown (1), red (2), yellow (4), black (\x{00D7}1)", html => qr/./), - "1234.5 ohms" => test_zci("1.23K\x{2126} (ohms) resistor colors: brown (1), red (2), orange (3), brown (\x{00D7}10)", html => qr/./), - "1235.5 ohms" => test_zci("1.24K\x{2126} (ohms) resistor colors: brown (1), red (2), yellow (4), brown (\x{00D7}10)", html => qr/./), - "12345 ohms" => test_zci("12.3K\x{2126} (ohms) resistor colors: brown (1), red (2), orange (3), red (\x{00D7}100)", html => qr/./), - "12355 ohms" => test_zci("12.4K\x{2126} (ohms) resistor colors: brown (1), red (2), yellow (4), red (\x{00D7}100)", html => qr/./), - "123450 ohms" => test_zci("123K\x{2126} (ohms) resistor colors: brown (1), red (2), orange (3), orange (\x{00D7}1K)", html => qr/./), - "123550 ohms" => test_zci("124K\x{2126} (ohms) resistor colors: brown (1), red (2), yellow (4), orange (\x{00D7}1K)", html => qr/./), - "1234500 ohms" => test_zci("1.23M\x{2126} (ohms) resistor colors: brown (1), red (2), orange (3), yellow (\x{00D7}10K)", html => qr/./), - "1235500 ohms" => test_zci("1.24M\x{2126} (ohms) resistor colors: brown (1), red (2), yellow (4), yellow (\x{00D7}10K)", html => qr/./), - "12345000 ohms" => test_zci("12.3M\x{2126} (ohms) resistor colors: brown (1), red (2), orange (3), green (\x{00D7}100K)", html => qr/./), - "12355000 ohms" => test_zci("12.4M\x{2126} (ohms) resistor colors: brown (1), red (2), yellow (4), green (\x{00D7}100K)", html => qr/./), - "123450000 ohms" => test_zci("123M\x{2126} (ohms) resistor colors: brown (1), red (2), orange (3), blue (\x{00D7}1M)", html => qr/./), - "123550000 ohms" => test_zci("124M\x{2126} (ohms) resistor colors: brown (1), red (2), yellow (4), blue (\x{00D7}1M)", html => qr/./), - "1234500000 ohms" => test_zci("1230M\x{2126} (ohms) resistor colors: brown (1), red (2), orange (3), purple (\x{00D7}10M)", html => qr/./), - "1235500000 ohms" => test_zci("1240M\x{2126} (ohms) resistor colors: brown (1), red (2), yellow (4), purple (\x{00D7}10M)", html => qr/./), + "1.2345 ohms" => test_zci("1.2\x{2126} (ohms) resistor colors: brown (1), red (2), gold (\x{00D7}0.1)", html => qr/./), + "1.2555 ohms" => test_zci("1.3\x{2126} (ohms) resistor colors: brown (1), orange (3), gold (\x{00D7}0.1)", html => qr/./), + "12.345 ohms" => test_zci("12\x{2126} (ohms) resistor colors: brown (1), red (2), black (\x{00D7}1)", html => qr/./), + "12.555 ohms" => test_zci("13\x{2126} (ohms) resistor colors: brown (1), orange (3), black (\x{00D7}1)", html => qr/./), + "123.45 ohms" => test_zci("120\x{2126} (ohms) resistor colors: brown (1), red (2), brown (\x{00D7}10)", html => qr/./), + "125.55 ohms" => test_zci("130\x{2126} (ohms) resistor colors: brown (1), orange (3), brown (\x{00D7}10)", html => qr/./), + "1234.5 ohms" => test_zci("1.2K\x{2126} (ohms) resistor colors: brown (1), red (2), red (\x{00D7}100)", html => qr/./), + "1255.5 ohms" => test_zci("1.3K\x{2126} (ohms) resistor colors: brown (1), orange (3), red (\x{00D7}100)", html => qr/./), + "12345 ohms" => test_zci("12K\x{2126} (ohms) resistor colors: brown (1), red (2), orange (\x{00D7}1K)", html => qr/./), + "12555 ohms" => test_zci("13K\x{2126} (ohms) resistor colors: brown (1), orange (3), orange (\x{00D7}1K)", html => qr/./), + "123450 ohms" => test_zci("120K\x{2126} (ohms) resistor colors: brown (1), red (2), yellow (\x{00D7}10K)", html => qr/./), + "125550 ohms" => test_zci("130K\x{2126} (ohms) resistor colors: brown (1), orange (3), yellow (\x{00D7}10K)", html => qr/./), + "1234500 ohms" => test_zci("1.2M\x{2126} (ohms) resistor colors: brown (1), red (2), green (\x{00D7}100K)", html => qr/./), + "1255500 ohms" => test_zci("1.3M\x{2126} (ohms) resistor colors: brown (1), orange (3), green (\x{00D7}100K)", html => qr/./), + "12345000 ohms" => test_zci("12M\x{2126} (ohms) resistor colors: brown (1), red (2), blue (\x{00D7}1M)", html => qr/./), + "12555000 ohms" => test_zci("13M\x{2126} (ohms) resistor colors: brown (1), orange (3), blue (\x{00D7}1M)", html => qr/./), + "123450000 ohms" => test_zci("120M\x{2126} (ohms) resistor colors: brown (1), red (2), purple (\x{00D7}10M)", html => qr/./), + "125550000 ohms" => test_zci("130M\x{2126} (ohms) resistor colors: brown (1), orange (3), purple (\x{00D7}10M)", html => qr/./), + "1234500000 ohms" => test_zci("1200M\x{2126} (ohms) resistor colors: brown (1), red (2), gray (\x{00D7}100M)", html => qr/./), + "1255500000 ohms" => test_zci("1300M\x{2126} (ohms) resistor colors: brown (1), orange (3), gray (\x{00D7}100M)", html => qr/./), # kilo and mega multipliers - "27kohm" => test_zci("27K\x{2126} (ohms) resistor colors: red (2), purple (7), black (0), red (\x{00D7}100)", html => qr/./), - "27Kohm" => test_zci("27K\x{2126} (ohms) resistor colors: red (2), purple (7), black (0), red (\x{00D7}100)", html => qr/./), - "27 K ohm" => test_zci("27K\x{2126} (ohms) resistor colors: red (2), purple (7), black (0), red (\x{00D7}100)", html => qr/./), - "4K2 ohm" => test_zci("4.2K\x{2126} (ohms) resistor colors: yellow (4), red (2), black (0), brown (\x{00D7}10)", html => qr/./), - "4.2K ohm" => test_zci("4.2K\x{2126} (ohms) resistor colors: yellow (4), red (2), black (0), brown (\x{00D7}10)", html => qr/./), + "27kohm" => test_zci("27K\x{2126} (ohms) resistor colors: red (2), purple (7), orange (\x{00D7}1K)", html => qr/./), + "27Kohm" => test_zci("27K\x{2126} (ohms) resistor colors: red (2), purple (7), orange (\x{00D7}1K)", html => qr/./), + "27 K ohm" => test_zci("27K\x{2126} (ohms) resistor colors: red (2), purple (7), orange (\x{00D7}1K)", html => qr/./), + "4K2 ohm" => test_zci("4.2K\x{2126} (ohms) resistor colors: yellow (4), red (2), red (\x{00D7}100)", html => qr/./), + "4.2K ohm" => test_zci("4.2K\x{2126} (ohms) resistor colors: yellow (4), red (2), red (\x{00D7}100)", html => qr/./), # Decimal points - "2.9ohm" => test_zci("2.9\x{2126} (ohms) resistor colors: red (2), white (9), black (0), silver (\x{00D7}0.01)", html => qr/./), + "2.9ohm" => test_zci("2.9\x{2126} (ohms) resistor colors: red (2), white (9), gold (\x{00D7}0.1)", html => qr/./), # Negative multipliers - "1 ohm" => test_zci("1\x{2126} (ohm) resistor colors: brown (1), black (0), black (0), silver (\x{00D7}0.01)", html => qr/./), - "29 ohms" => test_zci("29\x{2126} (ohms) resistor colors: red (2), white (9), black (0), gold (\x{00D7}0.1)", html => qr/./), + "1 ohm" => test_zci("1\x{2126} (ohm) resistor colors: brown (1), black (0), gold (\x{00D7}0.1)", html => qr/./), + "29 ohms" => test_zci("29\x{2126} (ohms) resistor colors: red (2), white (9), black (\x{00D7}1)", html => qr/./), # Zero special case - "0 ohms" => test_zci("0\x{2126} (ohms) resistor colors: black (0), black (0), black (0), black (\x{00D7}1)", html => qr/./), + "0 ohms" => test_zci("0\x{2126} (ohms) resistor colors: black (0), black (0), black (\x{00D7}1)", html => qr/./), # Range - "999000M ohms" => test_zci("999000M\x{2126} (ohms) resistor colors: white (9), white (9), white (9), white (\x{00D7}1000M)", html => qr/./), - "999500M ohms" => undef, - "1.1 ohms" => test_zci("1.1\x{2126} (ohms) resistor colors: brown (1), brown (1), black (0), silver (\x{00D7}0.01)", html => qr/./), - "1 ohms" => test_zci("1\x{2126} (ohm) resistor colors: brown (1), black (0), black (0), silver (\x{00D7}0.01)", html => qr/./), + "99000M ohms" => test_zci("99000M\x{2126} (ohms) resistor colors: white (9), white (9), white (\x{00D7}1000M)", html => qr/./), + "99500M ohms" => undef, + "1.1 ohms" => test_zci("1.1\x{2126} (ohms) resistor colors: brown (1), brown (1), gold (\x{00D7}0.1)", html => qr/./), + "1 ohms" => test_zci("1\x{2126} (ohm) resistor colors: brown (1), black (0), gold (\x{00D7}0.1)", html => qr/./), "0.9 ohms" => undef, "-10 ohms" => undef, @@ -80,22 +80,18 @@ ddg_goodie_test( "ohm ma darling" => undef, # Check the HTML. Just once. - "1.58m ohm" => test_zci("1.58M\x{2126} (ohms) resistor colors: brown (1), green (5), gray (8), yellow (\x{00D7}10K)", html => - "1.58MΩ (ohms) resistor colors: " + "1.5m ohm" => test_zci("1.5M\x{2126} (ohms) resistor colors: brown (1), green (5), green (\x{00D7}100K)", html => + "1.5MΩ (ohms) resistor colors: " . "brown (1) " . "green (5) " - . "gray (8) " - . "yellow (×10K)" - . "
Followed by a gap and tolerance color. " - . "More at resisto.rs"), + . "-webkit-border-radius:4px;-moz-border-radius:4px;'>green (×100K)" + . "
More at resisto.rs"), );