simplify_static_dir/lib/Directory/Simplify/Utils.pm
Dan Church 62f2503cb0
Remove vim modelines
That's what .editorconfig is for.
2023-07-20 13:39:42 -05:00

50 lines
1.1 KiB
Perl

package Directory::Simplify::Utils;
use strict;
use warnings;
sub addcommas {
my @numbers = @_;
my @added;
foreach my $num (@numbers) {
# don't split anything after the decimal
my @parts = split /\./, $num;
while ($parts[0] =~ s/(\d)(\d{3}(?:\D|$))/$1,$2/) {
}
push @added, (join '.', @parts);
}
return wantarray ? @added : $added[0];
}
sub hr_size {
my $sz = shift;
my @sizes = qw/ B KB MB GB TB PB EB ZB YB /;
my $fact = 1024;
my $thresh = 0.1;
my @ret;
foreach my $exp (reverse 0 .. $#sizes) {
if ($sz > (1 - $thresh) * $fact ** $exp) {
@ret = ($sz / $fact ** $exp, $sizes[$exp]);
last;
}
}
# default to ($sz, 'bytes')
unless (@ret) {
@ret = ($sz, $sizes[0]);
}
return wantarray ? @ret : "@ret";
}
sub shell_quote {
# shell-escape argument for inclusion in non-interpolated single quotes
my @words = @_;
my @transformed = map {
(my $out = $_)
=~ s/'/'\\''/g;
"'$out'";
} @words;
return wantarray ? @transformed : $transformed[0];
}
1;