| 1 | package Data::Transfigure::HashFilter::Undef 1.03; | ||
| 2 | use v5.26; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | # ABSTRACT: filters out undefined hash values whose keys match a pattern | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | Data::Transfigure::HashFilter::Undef - filters out undefined hash values whose | ||
| 10 | keys match a pattern | ||
| 11 | |||
| 12 | =head1 DESCRIPTION | ||
| 13 | |||
| 14 | C<Data::Transfigure::HashFilter::Undef> removes the key and corresponding value | ||
| 15 | from the hash in cases where the value is undefined and the key matches a given | ||
| 16 | regular expression. Matching keys can also be rewritten at the same time. | ||
| 17 | |||
| 18 | =cut | ||
| 19 | |||
| 20 | use Object::Pad; | ||
| 21 | |||
| 22 | use Data::Transfigure::Tree; | ||
| 23 | class Data::Transfigure::HashFilter::Undef : does(Data::Transfigure::Tree) { | ||
| 24 | |||
| 25 | =head1 FIELDS | ||
| 26 | |||
| 27 | =head2 key_pattern (optional param) | ||
| 28 | |||
| 29 | A compiled regular expression (C<qr/.../>) identifying keys that this | ||
| 30 | transfigurator can apply to. If a capture group is included in the regex, the | ||
| 31 | key is rewritten to its content (additional capture groups are ignored). | ||
| 32 | |||
| 33 | Default: C<qr/(.*)\?/> | ||
| 34 | |||
| 35 | =cut | ||
| 36 | |||
| 37 | field $key_pattern : param = qr/(.*)\?$/; | ||
| 38 | |||
| 39 | my sub hv_filter_undef($entity, $pat) { | ||
| 40 | if (ref($entity) eq 'HASH') { | ||
| 41 | foreach (keys($entity->%*)) { | ||
| 42 | if ($_ =~ $pat) { | ||
| 43 | my $v = delete($entity->{$_}); | ||
| 44 | $entity->{$1 || $_} = $v if (defined($v)); | ||
| 45 | } | ||
| 46 | __SUB__->($entity->{$_}, $pat) if (exists($entity->{$_})); | ||
| 47 | } | ||
| 48 | } elsif (ref($entity) eq 'ARRAY') { | ||
| 49 | __SUB__->($_, $pat) foreach ($entity->@*); | ||
| 50 | } | ||
| 51 | return $entity; | ||
| 52 | } | ||
| 53 | |||
| 54 | sub BUILDARGS ($class, %args) { | ||
| 55 | $class->SUPER::BUILDARGS( | ||
| 56 | handler => sub ($entity) { | ||
| 57 | return hv_filter_undef($entity, $args{key_pattern} // qr/(.*)\?$/); | ||
| 58 | } | ||
| 59 | ); | ||
| 60 | } | ||
| 61 | |||
| 62 | } | ||
| 63 | |||
| 64 | =pod | ||
| 65 | |||
| 66 | =head1 AUTHOR | ||
| 67 | |||
| 68 | Mark Tyrrell C<< <mark@tyrrminal.dev> >> | ||
| 69 | |||
| 70 | =head1 LICENSE | ||
| 71 | |||
| 72 | Copyright (c) 2024 Mark Tyrrell | ||
| 73 | |||
| 74 | Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| 75 | of this software and associated documentation files (the "Software"), to deal | ||
| 76 | in the Software without restriction, including without limitation the rights | ||
| 77 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| 78 | copies of the Software, and to permit persons to whom the Software is | ||
| 79 | furnished to do so, subject to the following conditions: | ||
| 80 | |||
| 81 | The above copyright notice and this permission notice shall be included in all | ||
| 82 | copies or substantial portions of the Software. | ||
| 83 | |||
| 84 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 85 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| 86 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| 87 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| 88 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| 89 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| 90 | SOFTWARE. | ||
| 91 | |||
| 92 | =cut | ||
| 93 | |||
| 94 | 1; | ||
| 95 | |||
| 96 | __END__ |