OpusVL-Text-Util
view release on metacpan or search on metacpan
lib/OpusVL/Text/Util.pm view on Meta::CPAN
package OpusVL::Text::Util;
use 5.014;
use strict;
use warnings;
our @ISA = qw/Exporter/;
our @EXPORT_OK = qw/truncate_text wrap_text string_to_id missing_array_items not_blank split_words line_split mask_text split_camel_case/;
use Array::Utils qw/intersect array_minus/;
use Scalar::Util qw/looks_like_number/;
# ABSTRACT: Simple text utilities
our $VERSION = '0.10';
sub truncate_text
{
my $string = shift;
my $length = shift;
return $string if length($string) < $length;
if($string =~ /^(.{0,$length}\w\b)/)
{
return $1 . '...';
}
else
{
return substr $string, 0, $length;
}
}
sub wrap_text
{
my $string = shift;
my $length = shift;
my $separator = shift || "\n";
return $string if length($string) < $length;
my @lines = $string =~ /\G(.{0,$length}\w\b|.*\w\b)\s*/g;
return join $separator, @lines;
}
sub string_to_id
{
my $text = shift;
my $r = $text =~ s/\s+/_/gr;
$r =~ s/[^\w_]//g;
return lc $r;
}
sub line_split
{
my $text = shift;
my @lines = split /\r\n|\r|\n/, $text;
return @lines
}
sub missing_array_items
{
my ($mandatory_fields, $actual_fields) = @_;
( run in 0.787 second using v1.01-cache-2.11-cpan-71847e10f99 )