From e9e4f847a627afc729dfcf68cedf85c55e9e5a2e Mon Sep 17 00:00:00 2001 From: nebulous Date: Tue, 3 Feb 2015 22:47:38 +0000 Subject: [PATCH] stub for wavelength conversion --- lib/DDG/Goodie/Wavelength.pm | 58 ++++++++++++++++++++++++++++++++++++ t/Wavelength.t | 27 +++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 lib/DDG/Goodie/Wavelength.pm create mode 100644 t/Wavelength.t diff --git a/lib/DDG/Goodie/Wavelength.pm b/lib/DDG/Goodie/Wavelength.pm new file mode 100644 index 000000000..81ad7448c --- /dev/null +++ b/lib/DDG/Goodie/Wavelength.pm @@ -0,0 +1,58 @@ +package DDG::Goodie::Wavelength; +use utf8; +# ABSTRACT: Frequency to wavelength + +use DDG::Goodie; + +use constant SPEED_OF_LIGHT => 299792458; #meters/second +use constant MULTIPLIER => { + hz => 1, + khz => 10**3, + mhz => 10**6, + ghz => 10**9, + thz => 10**12 +}; + +zci answer_type => "wavelength"; +zci is_cached => 1; + +# Metadata. See https://duck.co/duckduckhack/metadata for help in filling out this section. +name "Wavelength"; +description "Frequency to Wavelength translation"; +primary_example_queries "λ 2.4GHz", "144.39 MHz wavelength","lambda 1500kHz"; +category "physical_properties"; +topics "math", "science", "special_interest"; +code_url "https://github.com/nebulous/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/Wavelength.pm"; +attribution github => ["nebulous", "John Lifsey"], + cpan => "nebulous", + twitter => "aresweet"; + +# Triggers +triggers any => "λ", "wavelength", "lambda"; + +# Handle statement +handle remainder => sub { + my ($freq,$units) = m/([\d\.]+)\s*((k|m|g|t)?hz)/i; + return unless $freq and $units; + + my $mul = MULTIPLIER->{lc($units)}; + my $hz_freq = $freq * $mul; + + my $output_value = (SPEED_OF_LIGHT / $hz_freq); + my $output_units = 'Meters'; + + # Fairly common to express higher freqs in cm/mm. + # eg UHF 70cm band, microwave 3mm, etc + if ($output_value<1) { + $output_units = 'Centimeters'; + $output_value *= 100; + if ($output_value<1) { + $output_units = 'Millimeters'; + $output_value *= 10; + } + } + + return "$freq $units λ = $output_value $output_units"; +}; + +1; diff --git a/t/Wavelength.t b/t/Wavelength.t new file mode 100644 index 000000000..2ae4376bc --- /dev/null +++ b/t/Wavelength.t @@ -0,0 +1,27 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use Test::More; +use DDG::Test::Goodie; +use utf8; + +zci answer_type => "wavelength"; +zci is_cached => 1; + +ddg_goodie_test( + [qw( DDG::Goodie::Wavelength )], + # At a minimum, be sure to include tests for all: + # - primary_example_queries: + '1Hz wavelength' => test_zci("1 Hz λ = 299792458 Meters"), + 'lambda 0.001kHz' => test_zci('0.001 kHz λ = 299792458 Meters'), + + 'λ 2.4GHz' => test_zci('1 Hz λ = 299792458 Meters'), + #'144.39 MHz wavelength' => test_zci(''), + #'lambda 1500kHz' => test_zci(''), + # Try to include some examples of queries on which it might + # appear that your answer will trigger, but does not. + 'lambda lambda lambda' => undef, +); + +done_testing;