master
javathunderman 2016-01-15 22:26:19 +00:00
parent 12b96e9332
commit b924ed491c
8 changed files with 383 additions and 1 deletions

68
lib/DDG/Goodie/Bmi.pm Normal file
View File

@ -0,0 +1,68 @@
package DDG::Goodie::Bmi;
# ABSTRACT: A bmi calculator
#
use DDG::Goodie;
zci answer_type => "bmi";
zci is_cached => 1;
name "Bmi";
description "Pops a bmi calculator";
primary_example_queries "bmi";
category "calculations";
topics "special_interest", "everyday";
code_url "https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/Bmi.pm";
attribution github => ["jrenouard", "Julien Renouard"],
twitter => "jurenouard";
triggers start => "bmi";
triggers any => "bmi calculator", "body mass index calculator";
# Handle statement
handle remainder => sub {
return 'BMI', structured_answer => {
id => 'bmi',
name => 'Body Mass Index Calculatior',
data => {
title => "Body Mass Index Calculation",
text => {
imperial => "Imperial",
metric => "Metric",
height_placeholder => "Height in cm",
weight_placeholder => "Weight",
calculate => "Calculate",
feet => "Feet",
inches => "Inches",
pounds => "Pounds",
range => ["very severely underweight",
"severely underweight",
"underweight",
"normal",
"overweight",
"moderately obese",
"severely obese",
"very severely obese"],
error => "Error, your bmi is not a number, have you filled in the fields?",
your_bmi => "Your bmi is ",
within => "<br/>This is within the <b>",
str_range => "</b> range",
source_line => "<br/><small> source Wikipedia <a href=\"https://en.wikipedia.org/wiki/Body_mass_index\">https://en.wikipedia.org/wiki/Body_mass_index</a></small>"
},
},
meta => {
sourceName => "Wikipedia",
sourceUrl => "https://en.wikipedia.org/wiki/Body_mass_index"
},
templates => {
group => 'text',
item => 0,
options => {
content => 'DDH.bmi.content',
moreAt => 0
}
}
};
};
1;

54
share/goodie/bmi/bmi.css Normal file
View File

@ -0,0 +1,54 @@
form input {
padding: 10px;
font-size: 1.5em;
border: 1px solid #eee;
background-color: #fff;
}
#form_bmi_i input {
width: 120px;
}
.unit {
padding: 0 0 10px 0;
}
.unit .selected {
font-weight: bold;
}
#div_result {
font-size: 1.5em;
padding: 10px 0 0 0;
}
#div_result small {
font-size: 0.6em;
}
#form_bmi_i {
display: none;
}

149
share/goodie/bmi/bmi.js Normal file
View File

@ -0,0 +1,149 @@
DDH.bmi = DDH.bmi || {};
DDH.bmi.build = function(ops) {
var text = ops.data.text;
function getBmi(h, w, unit) {
if(unit == 'metric') {
h = h/100;
bmi = w / (h*h);
}
if(unit == 'imperial') {
bmi = (w*703) / (h*h);
}
bmi = Math.round(bmi*100)/100;
return bmi;
}
function getRange(bmi) {
if(bmi < 15) {
range = text.range[0];
}else if(bmi >= 15 && bmi < 16) {
range = text.range[1];
}else if(bmi >= 16 && bmi < 18.5) {
range = text.range[2];
}else if(bmi >= 18.5 && bmi < 25) {
range = text.range[3];
}else if(bmi >= 25 && bmi < 30) {
range = text.range[4];
}else if(bmi >= 30 && bmi < 35) {
range = text.range[5];
}else if(bmi >= 35 && bmi < 40) {
range = text.range[6];
}else if(bmi >= 40) {
range = text.range[7];
}
return range;
}
function formatResult(bmi) {
if(isNaN(bmi)){
html = text.error;
}else{
html = text.your_bmi + bmi;
html += text.within + getRange(bmi) + text.str_range;
html += text.source_line;
}
$('#div_result').html(html);
}
return {
onShow: function() {
$('#form_bmi_m').on('submit', function(e) {
e.preventDefault();
h = $('#height').val();
w = $('#weight').val();
res = getBmi(h, w, 'metric');
formatResult(res);
});
$('#form_bmi_i').on('submit', function(e) {
e.preventDefault();
h = $('#feet').val()*12;
h += $('#inches').val()*1;
w = $('#pounds').val();
res = getBmi(h, w, 'imperial');
formatResult(res);
});
$('.unit a').on('click', function(e) {
e.preventDefault();
$('#form_bmi_i, #form_bmi_m').toggle();
$('.unit a').toggleClass('selected');
$('#form_bmi_i input[type=number], #form_bmi_m input[type=number]').val('');
$('#div_result').html('');
});
} // ends onShow
};
};

1
share/goodie/bmi/bmi.txt Normal file
View File

@ -0,0 +1 @@
null

View File

@ -0,0 +1,34 @@
<div class="unit">
<a href="#" class="i">{{text.imperial}}</a> | <a href="#" class="m selected">{{text.metric}}</a>
</div>
<form id="form_bmi_m">
<input type="number" min="0" id="height" placeholder="{{text.height_placeholder}}"/>
<input type="number" id="weight" placeholder="{{text.weight_placeholder}}"/>
<input type="submit" value="{{text.calculate}}"/>
</form>
<form id="form_bmi_i">
<input type="number" min="0" id="feet" placeholder="{{text.feet}}"/>
<input type="number" min="0" id="inches" placeholder="{{text.inches}}"/>
<input type="number" id="pounds" placeholder="{{text.pounds}}"/>
<input type="submit" value="{{text.calculate}}"/>
</form>
<div id="div_result">
</div>

View File

@ -26,7 +26,7 @@ DDH.cheat_sheets.build = function(ops) {
return result;
});
var re_brackets = /(?:\[|\{|\}|\])/, // search for [, {, }, or }
var re_brackets = /(?:\[|\{|\}|\])/, // search for [, {, }, or ]
re_whitespace = /\s+/, // search for spaces
re_codeblock = /<code>(.+?)<\/code>/g; // search for <code></code>

View File

@ -0,0 +1,38 @@
{
"id": "hdfs_shell_cheat_sheet",
"name": "HDFS Shell Commands",
"description": "A brief overview of the commands for interacting with HDFS files and directories",
"metadata": {
"sourceName": "Apache Hadoop v2.7.1 documentation",
"sourceUrl": "http://hadoop.apache.org/docs/r2.7.1/hadoop-project-dist/hadoop-common/FileSystemShell.html"
},
"template_type": "terminal",
"section_order": ["Commands"],
"sections": {
"Commands": [{
"key": "hdfs dfs -ls /user/abc",
"val": "List contents of a directory."
}, {
"key": "hdfs dfs -put file.txt /user/abc",
"val": "Upload file from local file system."
}, {
"key": "hdfs dfs -cat /user/abc/file.txt",
"val": "Read contents of a file on HDFS."
}, {
"key": "hdfs dfs -chmod 700 /user/abc/file.txt",
"val": "Change file permission."
}, {
"key": "hdfs dfs -setrep -w <N> /user/adam/songs.txt",
"val": "Set replication factor to N."
}, {
"key": "hdfs dfs -du -h /user/abc/file.txt",
"val": "Check size of a file"
}, {
"key": "hdfs dfs -mv file.txt dir/",
"val": "Move file to subdirectory dir"
}, {
"key": "hdfs dfs -rm file.txt",
"val": "Delete file from HDFS."
}]
}
}

38
t/Bmi.t Normal file
View File

@ -0,0 +1,38 @@
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
use DDG::Test::Goodie;
zci answer_type => "bmi";
zci is_cached => 1;
my $html = '<h3>BMI Calculator</h3>
<div class="unit">
<a href="#" class="i">Imperial</a> | <a href="#" class="m selected">Metric</a>
</div>
<form id="form_bmi_m">
<input type="number" min="0" id="height" placeholder="Height in cm"/>
<input type="number" id="weight" placeholder="Weight"/>
<input type="submit" value="Calculate"/>
</form>
<form id="form_bmi_i">
<input type="number" min="0" id="feet" placeholder="Feet"/>
<input type="number" min="0" id="inches" placeholder="Inches"/>
<input type="number" id="pounds" placeholder="Pounds"/>
<input type="submit" value="Calculate"/>
</form>
<div id="div_result">
</div>';
ddg_goodie_test(
[qw(
DDG::Goodie::Bmi
)],
'bmi' => test_zci('null', html => $html),
);
done_testing;