Merge branch 'b-3.6.1' into mebil
[kivitendo-erp.git] / SL / Util.pm
index fdcc080..bf2e9ff 100644 (file)
@@ -6,7 +6,7 @@ use parent qw(Exporter);
 
 use Carp;
 
-our @EXPORT_OK = qw(_hashify);
+our @EXPORT_OK = qw(_hashify camelify snakify trim);
 
 sub _hashify {
   my $keep = shift;
@@ -18,6 +18,26 @@ sub _hashify {
           ((1 + $keep) == scalar(@_)) && ((ref($_[$keep]) || '') eq 'HASH') ? %{ $_[$keep] } : @_[$keep..scalar(@_) - 1]);
 }
 
+sub camelify {
+  my ($str) = @_;
+  $str =~ s/_+([[:lower:]])/uc($1)/ge;
+  ucfirst $str;
+}
+
+sub snakify {
+  my ($str) = @_;
+  $str =~ s/_([[:upper:]])/'_' . lc($1)/ge;
+  $str =~ s/(?<!^)([[:upper:]])/'_' . lc($1)/ge;
+  lc $str;
+}
+
+sub trim {
+  my $value = shift;
+  $value    =~ s{^ \p{WSpace}+ }{}xg if defined($value);
+  $value    =~ s{ \p{WSpace}+ $}{}xg if defined($value);
+  return $value;
+}
+
 1;
 __END__
 
@@ -61,6 +81,30 @@ Template code both. Example:
     # Now do stuff, obviously!
   }
 
+=item C<camilify $string>
+
+Returns C<$string> converted from underscore-style to
+camel-case-style, e.g. for the string C<stupid_example_dude> it will
+return C<StupidExampleDude>.
+
+L</snakify> does the reverse.
+
+=item C<snakify $string>
+
+Returns C<$string> converted from camel-case-style to
+underscore-style, e.g. for the string C<EvenWorseExample> it will
+return C<even_worse_example>.
+
+L</camilify> does the reverse.
+
+=item C<trim $string>
+
+Removes all leading and trailing whitespaces from C<$string> and
+returns it. Whitespaces within the string won't be changed.
+
+This function considers everything matching the Unicode character
+property "Whitespace" (C<WSpace>) to be a whitespace.
+
 =back
 
 =head1 BUGS