countries: hardcode codes reserved for WIPO
[sheet.git] / browser.plp
index 8c6b5ed..5380cef 100644 (file)
@@ -68,13 +68,12 @@ if (my ($somerow) = values %{ $caniuse->{data} }) {
                $versions{$browser} = [ sort { paddedver($a) cmp paddedver($b) } keys %$row ];
        }
 }
-my @browsers = grep { $versions{$_} }
-       qw(trident ie gecko firefox webkit_saf safari ios_saf webkit_chr chrome android presto opera op_mob op_mini);
+my @browsers = keys %versions;
 
 print <<'';
 <p id="intro">Alternate rendition of Fyrd's <a href="http://caniuse.com/">when can I use...</a> page
 
-my ($canihas, $usage);
+my ($canihas, $usage, $minusage);
 given ($get{usage} // 'wm') {
        when (!$_) {
                # none
@@ -94,11 +93,15 @@ given ($get{usage} // 'wm') {
        $ref .= " $_" for $canihas->{-date} || ();
        print "\nwith $ref browser usage statistics";
 }
-if ($usage) {
-       # first() does not work inside given >:(
-       while (my ($browser, $row) = each %$canihas) {
+if ($usage) { # first() does not work inside given >:(
+       # adapt version usage to actual support data
+       my %engineuse;  # prefix => usage sum
+       for my $browser (keys %versions) {
+               my $row = $canihas->{$browser} // {};
                my $verlist = $versions{$browser} or next;
                my %supported = map { $_ => 1 } @$verlist;
+
+               # cascade unknown versions
                $row->{$_} //= undef for @$verlist;  # ensure stable keys during iteration
                while (my ($version, $usage) = each %$row) {
                        next if defined $supported{$version};
@@ -107,7 +110,46 @@ if ($usage) {
                        $row->{$next} += $usage;
                        $row->{$version} = 0;  # balance browser total
                }
+
+               # build row list for each version
+               if ($minusage = $get{threshold} // .5) {
+                       my @vershown;  # $verlist replacement
+                       my ($rowusage, @verrow) = (0);  # replacement row tracking
+                       for (@$verlist) {
+                               push @verrow, $_;  # queue each version
+                               if (($rowusage += $row->{$_}) >= $minusage) {
+                                       push @vershown, [@verrow];   # add row
+                                       ($rowusage, @verrow) = (0);  # reset row tracking
+                               }
+                       }
+                       push @vershown, \@verrow if @verrow;  # always add latest
+                       @$verlist = @vershown;
+               }
+               else {
+                       @$verlist = map { [$_] } @$verlist;
+               }
+
+               # reusable aggregates (grouped by prefix (engine) and browser)
+               $engineuse{ $caniuse->{agents}->{$browser}->{prefix} } +=
+               $row->{-total} = sum(values %$row);
        }
+
+       # order browser columns by usage grouped by engine
+       @browsers = sort {
+               $engineuse{ $caniuse->{agents}->{$b}->{prefix} } <=>
+               $engineuse{ $caniuse->{agents}->{$a}->{prefix} }
+                       ||
+               $canihas->{$b}->{-total} <=> $canihas->{$a}->{-total}
+       } @browsers;
+}
+else {
+       # order browser columns by name grouped by engine
+       @browsers = sort {
+               $caniuse->{agents}->{$b}->{prefix} cmp
+               $caniuse->{agents}->{$a}->{prefix}
+                       ||
+               $a cmp $b
+       } @browsers;
 }
 :>.
 </p>
@@ -131,8 +173,8 @@ my $usagemax = (max(map { ref $_ eq 'HASH' && sum(values %$_) } values %$canihas
 
 my $usagepct = 1;  # score multiplier for 0..100 result
 # normalise usage percentage to only include shown browsers
-$usagepct = 100 / featurescore({  # yes for every possible version
-       map { $_ => { map {$_ => 'y'} @{$versions{$_}} } } keys %versions
+$usagepct = 100.01 / featurescore({  # yes for every possible version
+       map { $_ => { map {$_ => 'y'} map { @{$_} } @{$versions{$_}} } } keys %versions
 });
 
 print '<table class="mapped">';
@@ -151,7 +193,7 @@ my $header = join('',
                                $_, @{ $caniuse->{agents}->{$_} }{'prefix', 'type'},
                        ),
                        join(' ',
-                               sprintf('%.1f%%', sum(values %{ $canihas->{$_} }) * $usagepct),
+                               sprintf('%.1f%%', $canihas->{$_}->{-total} * $usagepct),
                                $name,
                        ),
                        do {
@@ -167,17 +209,18 @@ print '<thead>', $header;
 print "\n<tr>";
 print '<td>' x 3;
 for my $browser (@browsers) {
-       for my $_ (@{ $versions{$browser} }) {
-               my $release = $caniuse->{agents}->{$browser}->{verrelease}->{$_};
+       for (@{ $versions{$browser} }) {
+               my $lastver = $_->[-1];
+               my $release = $caniuse->{agents}->{$browser}->{verrelease}->{$lastver};
                my $future = defined $release;
                printf('<td title="%s"%s>%s',
                        join(' ',
-                               sprintf('%.1f%%', $canihas->{$browser}->{$_} * $usagepct),
+                               sprintf('%.1f%%', sum(@{ $canihas->{$browser} }{@$_}) * $usagepct),
                                $future ? 'development' : (),
-                               "version $_",
+                               'version ' . join(', ', @{$_}),
                        ),
                        $future && ' class="ex"',
-                       showversions($_),
+                       showversions($lastver),
                );
        }
 }
@@ -278,13 +321,13 @@ sub saybrowsercols {
                my $compare = (
                        !defined $ver ? undef :      # last column if nameless
                        ref $data ne 'HASH' ? '' :   # unclassified if no support hash
-                       $data->{$ver} // $prev       # known or inherit from predecessor
-                       // (grep { defined } @{$data}{ @{ $versions{$browser} } })[0]
+                       $data->{ $ver->[-1] } // $prev  # known or inherit from predecessor
+                       // (grep { defined } @{$data}{ map { $_->[0] } @{ $versions{$browser} } })[0]
                           ~~ 'n' && 'n'             # first known version is unsupported
                        || 'u'                       # unsure
                );
                unless (!defined $prev or $prev ~~ $compare) {
-                       my $usage = sum(map { $canihas->{$browser}->{$_} } @span);
+                       my $usage = sum(@{ $canihas->{$browser} }{ map { @{$_} } @span });
                        printf '<td class="%s" colspan="%d" title="%s">%s',
                                join(' ',
                                        X => $CSTATS{$prev},
@@ -301,11 +344,11 @@ sub saybrowsercols {
                                        map { split / /, $_ }
                                        $prev
                                )),
-                               showversions(@span),
+                               showversions($span[0]->[0], @span > 1 ? ($span[-1]->[-1]) : ()),
                        undef $prev;
                        @span = ();
                }
-               push @span, $ver;
+               push @span, $ver && [ grep { $data->{ $_ } eq $data->{ $ver->[-1] } } @{$ver} ];
                $prev = $compare;
        }
 }
@@ -385,6 +428,8 @@ sub showversions {
                        <:= defined $get{style} && 'set to ' :><em><:= $style :></em>
                <li><strong>usage</strong> source is
                        <:= !defined $get{usage} && 'default ' :><:= defined $usage ? "<em>$usage</em>" : 'not included (<em>0</em>)' :>
+               <li>usage <strong>threshold</strong> is
+                       <:= defined $get{threshold} && 'changed to ' :><em><:= $minusage :>%</em>
                </ul>
        </div>
 </div>