Author image Robert Acock 🤞🌏
and 1 contributors

NAME

Coerce::Types::Standard - Coercing

VERSION

Version 0.000007

SYNOPSIS

        package Headache;

        use Coerce::Types::Standard qw/Str HashRef ArrayRef StrToHash StrToArray/;

        attributes(
                [qw/sleep/] => [StrToHash->by(' '), {coe}]
        );

Exports

Coerce::Types::Standard extends Types::Standard it exports Nothing by default. The following outlines the additional types that can be exported using this module.

StrToArray

Accepts a string and coerces it into a ArrayRef, You can specify how to split the string by instantiating with *by*.

        StrToArray->by('--')->coerce('mid--flight--documenter');
        *-*-*-*-*-*-*
        [qw/mid flight documenter/]
        

StrToHash

Accepts a string and coerces it into a HashRef, You can specify how to split the string by instantiating with *by*.

        StrToHash->by(", ")->coerce('I, drink, way, too, much, coffee');
        *-*-*-*-*-*-*
        {
                I => 'drink',
                way => 'too',
                much => 'coffee',
        }

StrSR

Accepts a string and coerces via a search and replace regex generated by the arrayref passed into *by*, optionally the second argument can also be a callback function.

        StrSR->by(['-', ':'])->coerce('beach_bar');
        *-*-*-*-*-*-*
        'beach:bar'

ArrayToHash

Accepts an ArrayRef and coerces it into a HashRef, the default behaviour here is to just dereference the array into a hash.

        ArrayToHash->coerce([qw/north south east west/]);
        *-*-*-*-*-*-*
        {
                north => 'south',
                east => 'west'
        }

You can also instantiate this object via *by* and passing in a *mode*, currently the following are your options.

odd

Only build my hash out of the odd **index** of the array (1, 3, 5, 7 .....)

        ArrayToHash->by('odd')->coerce([qw/zero one two three/]);
        *-*-*-*-*-*-*
        {
                one => 'three',
        }
even

Only build my hash out of the even **index** of the array (0, 2, 4, 6 .....)

        ArrayToHash->by('even')->coerce([qw/zero one two three/]);
        *-*-*-*-*-*-*
        {
                zero => 'two',
        }
reverse

Reverse the default behavior so keys are values and values are keys.

        ArrayToHash->by('reverse')->coerce([qw/north south east west/]);
        *-*-*-*-*-*-*
        {
                south => 'north',
                west => 'east'
        }
flat

Should convert any struct into a one level Hash.

        ArrayToHash->by('flat')->coerce([{ ux => 'ui', analyst => [qw/document support meeting/] }, [qw/sysAdmin backend db deploy/]]); 
        *-*-*-*-*-*-*
        {
                analyst => 'document',
                ux => 'ui',
                support => 'meeting',
                db => 'deploy',
                sysAdmin => 'backend'
        }
merge

Simple single level merge of an array of hash references.

        ArrayToHash->by('merge')->coerce([{ simple => 'merge' }, { simple => 'life' }]);
        *-*-*-*-*-*-*
        {
                simple => 'life'
        }

HashToArray

Accepts a HashRef and coerces it into a ArrayRef, the default behaviour here is to just dereference the hash into a array.

        HashToArray->coerce({ 
                Malaysia => 'KL', 
                Austrailia => 'Sydney',   
                Indonesia => 'Bali',
        });
        *-*-*-*-*-*-*
        [qw/Austrailia Sydney Indonesia Bali Malaysia KL/]

You can also instantiate this object via *by* and passing in a *mode*, currently the following are your options.

keys

Only coerce the hash references keys into an array refernce.

        HashToArray->by('keys')->coerce({ 
                Malaysia => 'KL', 
                Austrailia => 'Sydney',   
                Indonesia => 'Bali',
        });
        *-*-*-*-*-*-*
        [qw/Austrailia Indonesia Malaysia/]
values

Only coerce the hash references values into an array refernce.

        HashToArray->by('values')->coerce({ 
                Malaysia => 'KL', 
                Austrailia => 'Sydney',   
                Indonesia => 'Bali',
        });
        *-*-*-*-*-*-*
        [qw/Bali KL Sydney/]
flat

Should convert any struct into a single level Array.

        HashToArray->by('flat')->coerce({ ux => ['ui'], analyst => [qw/document support meeting/] });   
        *-*-*-*-*-*-*
        [
                qw/analyst document support meeting ux ui/
        ]

HTML

Accepts a String and coerces it into entity encoded/decoded string, the default behaviour here is to entity encode the given string.

        HTML->coerce('okay&'); 
        *-*-*-*-*-*-*
        okay&

You can also instantiate this object via *by* and passing in a *mode*, currently the following are your options.

encode_entity

Entity encode the given string.

        HTML->by('encode_entity')->coerce('okay&');
        *-*-*-*-*-*-*
        okay&
decode_entity

Entity decode the given string.

        HTML->by('decode_entity')->coerce('okay&');
        *-*-*-*-*-*-*
        okay&

URI

Accepts a String or Array reference and coerces it into a URI object.

        my $uri = URI->coerce('example.lnation.org'); 
        *-*-*-*-*-*-*
        $uri->as_string; # example.lnation.org'

        my $uri = URI->coerce(['example.lnation.org', 'https']); 
        *-*-*-*-*-*-*
        $uri->as_string; # https://example.lnation.org'

        my $uri = URI->coerce(['https://example.lnation.org', { a => 'b' }]); 
        *-*-*-*-*-*-*
        $uri->as_string; # https://example.lnation.org?a=b'

You can also instantiate this object via *by* and passing in a *mode*, currently the following are your options.

schema

Extract the schema from the given url.

        URI->by('schema')->coerce('https://example.lnation.org');
        *-*-*-*-*-*-*
        https
host

Extract the host from the given url.

        URI->by('schema')->coerce('https://example.lnation.org');
        *-*-*-*-*-*-*
        example.lnation.org     
path

Extract the path from the given url.

        URI->by('path')->coerce('https://example.lnation.org/okays');
        *-*-*-*-*-*-*
        /okays
query_string

Extract the query string from the given url.

        URI->by('path')->coerce('https://example.lnation.org?okay=s');
        *-*-*-*-*-*-*
        ?okay=s
query_form

Extract the query form from the given url.

        URI->by('path')->coerce('https://example.lnation.org?okay=s');
        *-*-*-*-*-*-*
        {
                okay => s
        }
fragment

Extract the fragment from the given url.

        URI->by('fragment')->coerce('https://example.lnation.org#okays');
        *-*-*-*-*-*-*
        # okays
escape

URI escape the given string.

        URI->by('escape')->coerce('!$^@$%');
        *-*-*-*-*-*-*
        %21%24%5E%40%24%25
unescape

URI unescape the given string.

        URI->by('unescape')->coerce('%21%24%5E%40%24%25');
        *-*-*-*-*-*-*
        !$^@$%

Count

Accepts a Array or Hash reference and coerces it a count of items/keys.

        my $count = Count->coerce([qw/a b c/]); 
        *-*-*-*-*-*-*
        3

        my $count = Count->coerce({
                a => 'b',
                c => 'd'
        }); 
        *-*-*-*-*-*-*
        2

JSON

Accepts a JSON encoded String and decodes it into a perl structure.

        my $uri = JSON->coerce('{"a":"b"}'); 
        *-*-*-*-*-*-*
        {
                a => "b"
        }

You can also instantiate this object via *by* and passing in a *mode*, currently the following are your options.

encode

JSON encode the passed reference.

        JSON->by('encode')->coerce({
                a => "b"
        });
        *-*-*-*-*-*-*
        {"a":"b"}

        JSON->by(['encode', ['utf8', 'pretty']])->coerce({
                a => "b"
        });
        *-*-*-*-*-*-*
        {
                "a":"b"
        }
decode

JSON decode the given string.

        JSON->by('decode')->coerce('{"a":"b"}');
        *-*-*-*-*-*-*
        {
                a => "b"
        }

        JSON->by(['HashRef', 'decode', ['utf8']])->coerce('{"a":"b"}');
        *-*-*-*-*-*-*
        {
                "a":"b"
        }

AUTHOR

Robert Acock, <thisusedtobeanemail at gmail.com>

BUGS

Please report any bugs or feature requests to bug-coerce-types-standard at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Coerce-Types-Standard. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc Coerce::Types::Standard

You can also look for information at:

ACKNOWLEDGEMENTS

LICENSE AND COPYRIGHT

Copyright 2017 Robert Acock.

This program is free software; you can redistribute it and/or modify it under the terms of the the Artistic License (2.0). You may obtain a copy of the full license at:

http://www.perlfoundation.org/artistic_license_2_0

Any use, modification, and distribution of the Standard or Modified Versions is governed by this Artistic License. By using, modifying or distributing the Package, you accept this license. Do not use, modify, or distribute the Package, if you do not accept this license.

If your Modified Version has been derived from a Modified Version made by someone other than you, you are nevertheless required to ensure that your Modified Version complies with the requirements of this license.

This license does not grant you the right to use any trademark, service mark, tradename, or logo of the Copyright Holder.

This license includes the non-exclusive, worldwide, free-of-charge patent license to make, have made, use, offer to sell, sell, import and otherwise transfer the Package with respect to any patent claims licensable by the Copyright Holder that are necessarily infringed by the Package. If you institute patent litigation (including a cross-claim or counterclaim) against any party alleging that the Package constitutes direct or contributory patent infringement, then this Artistic License to you shall terminate on the date that such litigation is filed.

Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.