From de30f7dc38f2ab0ca7f0ddd6aea5e24d9702b85d Mon Sep 17 00:00:00 2001 From: Alex 'AdUser' Z Date: Tue, 24 Jul 2018 16:35:11 +1000 Subject: [PATCH] * Subtitle::Format : add sort_events() --- lib/Subtitle/Format.pm | 14 ++++++++++++++ t/format.t | 4 +++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/Subtitle/Format.pm b/lib/Subtitle/Format.pm index 470a89a..66da324 100644 --- a/lib/Subtitle/Format.pm +++ b/lib/Subtitle/Format.pm @@ -35,6 +35,14 @@ sub events { return wantarray ? @{ $self->{events} } : $self->{events}; } +sub sort_events { + my ($self) = @_; + my @sorted = sort { + $a->t_start <=> $b->t_start + } @{ $self->events; }; + $self->events(\@sorted); +} + sub from_string { my ($self, $text) = @_; my @lines = split /\r?\n/o, $text; @@ -102,6 +110,12 @@ Collected messages can be accessed like this: Access to subtitle events +=head2 C + + $obj->sort_events; + +Internally sort events by start time (ascending) + =head2 C =head2 C diff --git a/t/format.t b/t/format.t index 7f82571..0d917b6 100644 --- a/t/format.t +++ b/t/format.t @@ -1,7 +1,7 @@ use strict; use warnings; -use Test::More tests => 5; +use Test::More tests => 6; require_ok('Subtitle::Format'); @@ -21,6 +21,8 @@ require_ok('Subtitle::Format'); my $obj = Subtitle::Dummy->new; isa_ok($obj, 'Subtitle::Dummy'); +can_ok($obj, qw(new parse build log events sort_events + from_file to_file from_string to_string)); is(scalar @{ $obj->{log} }, 0); $obj->log(warn => "something");