stub for wavelength conversion

master
nebulous 2015-02-03 22:47:38 +00:00
parent ea04ff4ae7
commit e9e4f847a6
2 changed files with 85 additions and 0 deletions

View File

@ -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;

27
t/Wavelength.t Normal file
View File

@ -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;