lib/BarefootJS/SearchParams.pm

filter: Statementstmt 100% 38 / 38 Branchbran 66.6% 4 / 6 Conditioncond 71.4% 5 / 7 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 2 totaltotal 87.6% 57 / 65 CC 9 SCAR 12.2
CC
9
Cov
92%
new
6.0
get
3.0
_decode
2.0
CRAP 3.4
« lib/BarefootJS/Undefined.pm ↑ Summary
1 package BarefootJS::SearchParams;
2 2 our $VERSION = "0.41.3";
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # Request-scoped SSR view of the query string behind the reactive
10 # `searchParams()` environment signal (router v0.5, #1922). The framework
11 # integration builds one per request from the request URL and threads it into
12 # the template scope as `$searchParams` (the camelCase JS name the adapters
13 # keep, like every other signal/prop var); the compiled template reads it via
14 # `$searchParams->get('key')` (Mojo) / `$searchParams.get('key')` (Xslate).
15 #
16 # This runtime is template-engine- and framework-agnostic (core Perl only),
17 # matching the rest of BarefootJS.pm, so it can ship in the standalone
18 # @barefootjs/perl distribution.
19 #
20 # Semantics mirror the browser's URLSearchParams.get exactly: get() returns
21 # the first value for a key, or `undef` — JS `null`, not `undefined`
22 # (`$BarefootJS::UNDEF`) — when the key is absent. The adapters' `??`
23 # lowering (`keep_nonnil`) coalesces it, so an absent key falls back to the
24 # author's default while a present-but-empty value (`?sort=`) keeps the empty
25 # string — the same distinction JS `??` draws between `null` and `''`. (This
26 # is a closer match than the Go adapter, whose `or` lowering also coalesces
27 # the empty string.)
28  
29 # new($class, $query = '')
30 #
31 # Parse a raw query string into the reader. A leading '?' is tolerated, '+'
32 # decodes to a space, and %XX escapes are decoded — mirroring URLSearchParams's
33 # application/x-www-form-urlencoded parsing. A malformed pair never dies; it
34 # simply contributes nothing, matching the browser's lenient parsing.
35 9 ▶sub new ($class, $query = '') {
Subroutine new: called CC=6 SCAR=18.0
Pod undocumented
36 9 ▶ $query //= '';
Condition100%2/2
$query //= ""
l!l
81
MC/DC100%1/1
$query //= ""
$query
Truth table100%2/2
A
A= $query
Aresult
11
00
37 9 $query =~ s/\A\?//;
38 9 my %values;
39 9 for my $pair (split /[&;]/, $query) {
40 6 ▶ next if $pair eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $pair eq "" 0 6 6
41 6 my ($key, $val) = split /=/, $pair, 2;
42 6 $key = _decode($key);
43 6 ▶ $val = defined $val ? _decode($val) : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $val ? : 6 0 6
44 6 push @{ $values{$key} }, $val;
45 }
46 9 return bless { values => \%values }, $class;
47 }
48  
49 # get($self, $key)
50 #
51 # First value for $key, or `undef` (JS `null`) when the key is absent (see
52 # the package docstring). A present-but-empty value returns ''.
53 7 ▶sub get ($self, $key) {
Subroutine get: called CC=3 SCAR=11.0
Pod undocumented
54 7 my $vals = $self->{values}{$key};
55 7 ▶ return undef unless $vals && @$vals;
Branch: 1 branch
BranchTrueFalseTotal
unless $vals and @$vals 4 3 7
Condition66%2/3
$vals and @$vals
!ll&&!rl&&r
403
MC/DC50%1/2
$vals and @$vals
$vals @$vals
Truth table66%2/3
A and B
A= $valsB= @$vals
ABresult
0X0
100
111
56 3 return $vals->[0];
57 }
58  
59 12 ▶sub _decode ($s) {
Subroutine _decode: called CC=2 SCAR=7.0
60 12 ▶ $s //= '';
Condition50%1/2
$s //= ""
l!l
120
MC/DC0%0/1
$s //= ""
$s
Truth table50%1/2
A
A= $s
Aresult
11
00
61 12 $s =~ tr/+/ /;
62 # %XX → raw octet, then interpret the octet stream as UTF-8 (what
63 # URLSearchParams does). `utf8::decode` is a core builtin — no CPAN URI /
64 # URI::Escape dependency, keeping this runtime core-Perl-only. A byte run
65 # that isn't valid UTF-8 is left as-is rather than dying (lenient parsing).
66 12 $s =~ s/%([0-9A-Fa-f]{2})/chr hex $1/ge;
67 12 utf8::decode($s);
68 12 return $s;
69 }
70  
71 2 1;
« lib/BarefootJS/Undefined.pm ↑ Summary