Skip to content

Instantly share code, notes, and snippets.

@diegok
Created November 26, 2012 22:28
Show Gist options
  • Save diegok/4151080 to your computer and use it in GitHub Desktop.
Save diegok/4151080 to your computer and use it in GitHub Desktop.
sub extract_subcategories {
my ($c, $slug_param, $categories, $facets) = @_;
return [] unless @$categories;
my ($cref) = ref($c) =~ m/::(\w+)$/;
my $level = split '/', $slug_param;
my $slg = Supers::Slugify->new;
my $subcats = [];
my $slug_re = qr/^$slug_param/;
for my $category (@$categories) {
my @path = $slg->cat2path($category->{term});
my $slug = $slg->slug_it(@path);
my $cat = $path[$level];
next unless $cat;
next unless $slug =~ $slug_re;
my $idx = firstidx { $_->{name} eq $cat } @$subcats;
if ($idx > -1) {
my $existing = $subcats->[$idx];
$subcats->[$idx] = { %$existing,
count => $existing->{count} + $category->{count}
};
} else {
my $catslug = $slg->slug_it($slug_param, $cat);
push @$subcats, {
name => $cat,
count => $category->{count},
url => 'bla'
};
}
}
$subcats;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment