App-AdditivesUtils

 view release on metacpan or  search on metacpan

lib/App/AdditivesUtils.pm  view on Meta::CPAN

package App::AdditivesUtils;

use 5.010001;
use strict;
use warnings;

use Capture::Tiny 'capture_stderr';

our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
our $DATE = '2023-02-05'; # DATE
our $DIST = 'App-AdditivesUtils'; # DIST
our $VERSION = '0.004'; # VERSION

our %SPEC;

our %args_common = (
    quantity => {
        # schema => 'physical::mass*', # XXX Perinci::Sub::GetArgs::Argv is not smart enough to coerce from string
        schema => 'str*',
        req => 1,
        pos => 0,
    },
    to_unit => {
        # schema => 'physical::unit', # IU hasn't been added
        schema => 'str*',
        pos => 1,
    },
);

$SPEC{convert_benzoate_unit} = {
    v => 1.1,
    summary => 'Convert a benzoate quantity from one unit to another',
    description => <<'_',

If target unit is not specified, will show all known conversions.

_
    args => {
        %args_common,
    },
    examples => [
        {args=>{quantity=>'ppm'}, summary=>'Show all possible conversions'},
        {args=>{quantity=>'250 ppm', to_unit=>'ppm-as-benzoic-acid'}, summary=>'Convert from ppm (as sodium benzoate) to ppm (as benzoic acid)'},
    ],
};
sub convert_benzoate_unit {
    require Physics::Unit;

    Physics::Unit::InitUnit(
        ['ppm'], '1 mg/kg',
        ['ppm-as-benzoic-acid'], '1.18006878480183 mg/kg', # benzoic acid's molecular weight = 122.12, sodium benzoate's molecular weight = 144.11
        ['ppm-as-sodium-benzoate'], '1 mg/kg',
        ['ppm-as-na-benzoate'], '1 mg/kg',
        ['ppm-as-potassium-benzoate'], '0.899506897197428 mg/kg', # potassium benzoate's molecular weight = 160.21
        ['ppm-as-k-benzoate'], '0.959326321395287 mg/kg',
        ['ppm-as-calcium-benzoate'], '1.02093443377847 mg/kg', # calcium benzoate's molecular weight = 282.31 (2 benzoate groups per molecule)
        ['ppm-as-ca-benzoate'], '1.02093443377847 mg/kg',
    );

    my %args = @_;
    my $quantity = Physics::Unit->new($args{quantity});
    return [412, "Must be a Dimensionless quantity"] unless $quantity->type eq 'Dimensionless';

    if ($args{to_unit}) {
        my $new_amount = $quantity->convert($args{to_unit});
        return [200, "OK", $new_amount];
    } else {



( run in 2.149 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )