NAME
coerce-with-sah - Coerce data
VERSION
This document describes version 0.485 of coerce-with-sah (from Perl distribution App-SahUtils), released on 2024-08-06.
SYNOPSIS
coerce-with-sah --help (or -h, -?)
coerce-with-sah --version (or -v)
coerce-with-sah [(--coerce-rule=str)+|--coerce-rules-json=json] [--coerce-to=str] [--compiler=str|-C=str] [(--config-path=path)+|--no-config] [--config-profile=profile|-P] [--data-as-json=str] [--data-as-perl=str] [--data-with-result|-d] [--format=name|--json] [--linenum|-l] [--multiple-data-as-json=str] [--multiple-data-as-perl=str] [--(no)naked-res] [--no-env] [--page-result[=program]|--view-result[=program]] [--return-type=str|-r=str] [--show-code|-c] [--show-rules] -- <type>
See examples in the "EXAMPLES" section.
OPTIONS
*
marks required options.
Action selection options
- --show-code, -c
-
Don't coerce data, show generated coercer code only.
- --show-rules
-
Don't coerce data, show coerce rules that will be used.
Coercer specification options
- --coerce-rule=s@
-
(No description)
Can be specified multiple times.
- --coerce-rules-json=s
-
See
--coerce-rule
. - --coerce-to=s
-
(No description)
- --compiler=s, -C
-
Select compiler.
Default value:
"perl"
Valid values:
["perl","js"]
- --return-type=s, -r
-
Default value:
"val"
Valid values:
["val","bool_coerced+val","bool_coerced+str_errmsg+val"]
- --type=s*
-
(No description)
Can also be specified as the 1st command-line argument.
Configuration options
- --config-path=s
-
Set path to configuration file.
Can actually be specified multiple times to instruct application to read from multiple configuration files (and merge them).
Can be specified multiple times.
- --config-profile=s, -P
-
Set configuration profile to use.
A single configuration file can contain profiles, i.e. alternative sets of values that can be selected. For example:
[profile=dev] username=foo pass=beaver [profile=production] username=bar pass=honey
When you specify
--config-profile=dev
,username
will be set tofoo
andpassword
tobeaver
. When you specify--config-profile=production
,username
will be set tobar
andpassword
tohoney
. - --no-config
-
Do not use any configuration file.
If you specify
--no-config
, the application will not read any configuration file.
Data specification options
- --data-as-json=s
-
Data as JSON.
- --data-as-perl=s
-
Data as Perl code.
- --multiple-data-as-json=s
-
Multiple data as JSON code, JSON data must be an array.
- --multiple-data-as-perl=s
-
Multiple data as Perl code, perl code should return arrayref.
Environment options
- --no-env
-
Do not read environment for default options.
If you specify
--no-env
, the application wil not read any environment variable.
Output options
- --data-with-result, -d
-
Show data alongside with coerced result.
The default is to show the coerced result only.
- --format=s
-
Choose output format, e.g. json, text.
Default value:
undef
Output can be displayed in multiple formats, and a suitable default format is chosen depending on the application and/or whether output destination is interactive terminal (i.e. whether output is piped). This option specifically chooses an output format.
- --json
-
Set output format to json.
- --linenum, -l
-
When showing source code, add line numbers.
- --no-naked-res
-
When outputing as JSON, add result envelope.
By default, when outputing as JSON, the full enveloped result is returned, e.g.:
[200,"OK",[1,2,3],{"func.extra"=>4}]
The reason is so you can get the status (1st element), status message (2nd element) as well as result metadata/extra result (4th element) instead of just the result (3rd element). However, sometimes you want just the result, e.g. when you want to pipe the result for more post-processing. In this case you can use
--naked-res
so you just get:[1,2,3]
- --page-result
-
Filter output through a pager.
This option will pipe the output to a specified pager program. If pager program is not specified, a suitable default e.g.
less
is chosen. - --view-result
-
View output using a viewer.
This option will first save the output to a temporary file, then open a viewer program to view the temporary file. If a viewer program is not chosen, a suitable default, e.g. the browser, is chosen.
Other options
COMPLETION
This script has shell tab completion capability with support for several shells.
bash
To activate bash completion for this script, put:
complete -C coerce-with-sah coerce-with-sah
in your bash startup (e.g. ~/.bashrc). Your next shell session will then recognize tab completion for the command. Or, you can also directly execute the line above in your shell to activate immediately.
It is recommended, however, that you install modules using cpanm-shcompgen which can activate shell completion for scripts immediately.
tcsh
To activate tcsh completion for this script, put:
complete coerce-with-sah 'p/*/`coerce-with-sah`/'
in your tcsh startup (e.g. ~/.tcshrc). Your next shell session will then recognize tab completion for the command. Or, you can also directly execute the line above in your shell to activate immediately.
It is also recommended to install shcompgen (see above).
other shells
For fish and zsh, install shcompgen as described above.
FAQ
When there is an error (e.g. in generating coercer code, in coercing) the program returns undef/null, how do I see the error message?
Pass `--no-naked-res` to see the error code and error message. The default is naked for simpler output.
CONFIGURATION FILE
This script can read configuration files. Configuration files are in the format of IOD, which is basically INI with some extra features.
By default, these names are searched for configuration filenames (can be changed using --config-path
): /home/u1/.config/coerce-with-sah.conf, /home/u1/coerce-with-sah.conf, or /etc/coerce-with-sah.conf.
All found files will be read and merged.
To disable searching for configuration files, pass --no-config
.
You can put multiple profiles in a single file by using section names like [profile=SOMENAME]
or [SOMESECTION profile=SOMENAME]
. Those sections will only be read if you specify the matching --config-profile SOMENAME
.
You can also put configuration for multiple programs inside a single file, and use filter program=NAME
in section names, e.g. [program=NAME ...]
or [SOMESECTION program=NAME]
. The section will then only be used when the reading program matches.
You can also filter a section by environment variable using the filter env=CONDITION
in section names. For example if you only want a section to be read if a certain environment variable is true: [env=SOMEVAR ...]
or [SOMESECTION env=SOMEVAR ...]
. If you only want a section to be read when the value of an environment variable equals some string: [env=HOSTNAME=blink ...]
or [SOMESECTION env=HOSTNAME=blink ...]
. If you only want a section to be read when the value of an environment variable does not equal some string: [env=HOSTNAME!=blink ...]
or [SOMESECTION env=HOSTNAME!=blink ...]
. If you only want a section to be read when the value of an environment variable includes some string: [env=HOSTNAME*=server ...]
or [SOMESECTION env=HOSTNAME*=server ...]
. If you only want a section to be read when the value of an environment variable does not include some string: [env=HOSTNAME!*=server ...]
or [SOMESECTION env=HOSTNAME!*=server ...]
. Note that currently due to simplistic parsing, there must not be any whitespace in the value being compared because it marks the beginning of a new section filter or section name.
To load and configure plugins, you can use either the -plugins
parameter (e.g. -plugins=DumpArgs
or -plugins=DumpArgs@before_validate_args
), or use the [plugin=NAME ...]
sections, for example:
[plugin=DumpArgs]
-event=before_validate_args
-prio=99
[plugin=Foo]
-event=after_validate_args
arg1=val1
arg2=val2
which is equivalent to setting -plugins=-DumpArgs@before_validate_args@99,-Foo@after_validate_args,arg1,val1,arg2,val2
.
List of available configuration parameters:
coerce_rules (see --coerce-rule)
coerce_to (see --coerce-to)
compiler (see --compiler)
data_as_json (see --data-as-json)
data_as_perl (see --data-as-perl)
data_with_result (see --data-with-result)
format (see --format)
linenum (see --linenum)
multiple_data_as_json (see --multiple-data-as-json)
multiple_data_as_perl (see --multiple-data-as-perl)
naked_res (see --naked-res)
return_type (see --return-type)
show_code (see --show-code)
show_rules (see --show-rules)
type (see --type)
ENVIRONMENT
COERCE_WITH_SAH_OPT
String. Specify additional command-line options.
FILES
/home/u1/.config/coerce-with-sah.conf
/home/u1/coerce-with-sah.conf
/etc/coerce-with-sah.conf
EXAMPLES
Example #1
% coerce-with-sah date --coerce-to DateTime --data-as-perl '"2016-05-22"'
do {
my $a = bless({
formatter => undef,
local_c => {
day => 22,
day_of_quarter => 52,
day_of_week => 7,
day_of_year => 143,
hour => 0,
minute => 0,
month => 5,
quarter => 2,
second => 0,
year => 2016,
},
local_rd_days => 736106,
local_rd_secs => 0,
locale => bless({
am_pm_abbreviated => ["AM", "PM"],
available_formats => {
"Bh" => "h B",
"Bhm" => "h:mm B",
"Bhms" => "h:mm:ss B",
"d" => "d",
"E" => "ccc",
"EBhm" => "E h:mm B",
"EBhms" => "E h:mm:ss B",
"Ed" => "d E",
"EHm" => "E HH:mm",
"Ehm" => "E h:mm\x{202F}a",
"Ehm-alt-ascii" => "E h:mm a",
"Ehms" => "E h:mm:ss\x{202F}a",
"EHms" => "E HH:mm:ss",
"Ehms-alt-ascii" => "E h:mm:ss a",
"Gy" => "y G",
"GyMd" => "M/d/y G",
"GyMMM" => "MMM y G",
"GyMMMd" => "MMM d, y G",
"GyMMMEd" => "E, MMM d, y G",
"H" => "HH",
"h" => "h\x{202F}a",
"h-alt-ascii" => "h a",
"Hm" => "HH:mm",
"hm" => "h:mm\x{202F}a",
"hm-alt-ascii" => "h:mm a",
"hms" => "h:mm:ss\x{202F}a",
"Hms" => "HH:mm:ss",
"hms-alt-ascii" => "h:mm:ss a",
"Hmsv" => "HH:mm:ss v",
"hmsv" => "h:mm:ss\x{202F}a v",
"hmsv-alt-ascii" => "h:mm:ss a v",
"Hmv" => "HH:mm v",
"hmv" => "h:mm\x{202F}a v",
"hmv-alt-ascii" => "h:mm a v",
"M" => "L",
"Md" => "M/d",
"MEd" => "E, M/d",
"MMM" => "LLL",
"MMMd" => "MMM d",
"MMMEd" => "E, MMM d",
"MMMMd" => "MMMM d",
"MMMMW-count-one" => "'week' W 'of' MMMM",
"MMMMW-count-other" => "'week' W 'of' MMMM",
"ms" => "mm:ss",
"y" => "y",
"yM" => "M/y",
"yMd" => "M/d/y",
"yMEd" => "E, M/d/y",
"yMMM" => "MMM y",
"yMMMd" => "MMM d, y",
"yMMMEd" => "E, MMM d, y",
"yMMMM" => "MMMM y",
"yQQQ" => "QQQ y",
"yQQQQ" => "QQQQ y",
"yw-count-one" => "'week' w 'of' Y",
"yw-count-other" => "'week' w 'of' Y",
},
code => "en-US",
date_format_full => "EEEE, MMMM d, y",
date_format_long => "MMMM d, y",
date_format_medium => "MMM d, y",
date_format_short => "M/d/yy",
datetime_format_full => "{1}, {0}",
datetime_format_long => "{1}, {0}",
datetime_format_medium => "{1}, {0}",
datetime_format_short => "{1}, {0}",
day_format_abbreviated => ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
day_format_narrow => ["M", "T", "W", "T", "F", "S", "S"],
day_format_wide => [
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday",
],
day_stand_alone_abbreviated => ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
day_stand_alone_narrow => ["M", "T", "W", "T", "F", "S", "S"],
day_stand_alone_wide => [
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday",
],
default_date_format_length => "medium",
default_time_format_length => "medium",
era_abbreviated => ["BC", "AD"],
era_narrow => ["B", "A"],
era_wide => ["Before Christ", "Anno Domini"],
first_day_of_week => 7,
glibc_date_1_format => "%a %b %e %r %Z %Y",
glibc_date_format => "%m/%d/%Y",
glibc_datetime_format => "%a %d %b %Y %r %Z",
glibc_time_12_format => "%I:%M:%S %p",
glibc_time_format => "%r",
language => "English",
locale_data => {
am_pm_abbreviated => 'fix',
available_formats => 'fix',
code => "en-US",
date_format_full => "EEEE, MMMM d, y",
date_format_long => "MMMM d, y",
date_format_medium => "MMM d, y",
date_format_short => "M/d/yy",
datetime_format_full => "{1}, {0}",
datetime_format_long => "{1}, {0}",
datetime_format_medium => "{1}, {0}",
datetime_format_short => "{1}, {0}",
day_format_abbreviated => 'fix',
day_format_narrow => 'fix',
day_format_wide => 'fix',
day_stand_alone_abbreviated => 'fix',
day_stand_alone_narrow => 'fix',
day_stand_alone_wide => 'fix',
era_abbreviated => 'fix',
era_narrow => 'fix',
era_wide => 'fix',
first_day_of_week => 7,
glibc_date_1_format => "%a %b %e %r %Z %Y",
glibc_date_format => "%m/%d/%Y",
glibc_datetime_format => "%a %d %b %Y %r %Z",
glibc_time_12_format => "%I:%M:%S %p",
glibc_time_format => "%r",
language => "English",
month_format_abbreviated => [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
],
month_format_narrow => ["J", "F", "M", "A", "M", "J", "J", "A", "S", "O", "N", "D"],
month_format_wide => [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
],
month_stand_alone_abbreviated => [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
],
month_stand_alone_narrow => ["J", "F", "M", "A", "M", "J", "J", "A", "S", "O", "N", "D"],
month_stand_alone_wide => [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
],
name => "English United States",
native_language => "English",
native_name => "English United States",
native_script => undef,
native_territory => "United States",
native_variant => undef,
quarter_format_abbreviated => ["Q1" .. "Q4"],
quarter_format_narrow => [1 .. 4],
quarter_format_wide => ["1st quarter", "2nd quarter", "3rd quarter", "4th quarter"],
quarter_stand_alone_abbreviated => ["Q1" .. "Q4"],
quarter_stand_alone_narrow => [1 .. 4],
quarter_stand_alone_wide => ["1st quarter", "2nd quarter", "3rd quarter", "4th quarter"],
script => undef,
territory => "United States",
time_format_full => "h:mm:ss\x{202F}a zzzz",
time_format_long => "h:mm:ss\x{202F}a z",
time_format_medium => "h:mm:ss\x{202F}a",
time_format_short => "h:mm\x{202F}a",
variant => undef,
version => 44,
},
month_format_abbreviated => 'fix',
month_format_narrow => 'fix',
month_format_wide => 'fix',
month_stand_alone_abbreviated => 'fix',
month_stand_alone_narrow => 'fix',
month_stand_alone_wide => 'fix',
name => "English United States",
native_language => "English",
native_name => "English United States",
native_script => undef,
native_territory => "United States",
native_variant => undef,
quarter_format_abbreviated => 'fix',
quarter_format_narrow => 'fix',
quarter_format_wide => 'fix',
quarter_stand_alone_abbreviated => 'fix',
quarter_stand_alone_narrow => 'fix',
quarter_stand_alone_wide => 'fix',
script => undef,
territory => "United States",
time_format_full => "h:mm:ss\x{202F}a zzzz",
time_format_long => "h:mm:ss\x{202F}a z",
time_format_medium => "h:mm:ss\x{202F}a",
time_format_short => "h:mm\x{202F}a",
variant => undef,
version => 44,
}, "DateTime::Locale::FromData"),
offset_modifier => 0,
rd_nanosecs => 0,
tz => bless({
is_olson => 1,
max_year => 2033,
name => "Asia/Jakarta",
spans => [
["-Inf", 58904383968, "-Inf", 58904409600, 25632, 0, "LMT"],
[
58904383968,
60683964000,
58904409600,
60683989632,
25632,
0,
"BMT",
],
[
60683964000,
60962776800,
60683990400,
60962803200,
26400,
0,
"+0720",
],
[
60962776800,
61259041800,
60962803800,
61259068800,
27000,
0,
"+0730",
],
[
61259041800,
61369628400,
61259074200,
61369660800,
32400,
0,
"+09",
],
[
61369628400,
61451800200,
61369655400,
61451827200,
27000,
0,
"+0730",
],
[
61451800200,
61514870400,
61451829000,
61514899200,
28800,
0,
"+08",
],
[
61514870400,
61946267400,
61514897400,
61946294400,
27000,
0,
"+0730",
],
[61946267400, "Inf", 61946292600, "Inf", 25200, 0, "WIB"],
],
}, "DateTime::TimeZone::Asia::Jakarta"),
utc_rd_days => 736105,
utc_rd_secs => 61200,
utc_year => 2017,
}, "DateTime");
$a->{locale}{locale_data}{am_pm_abbreviated} = $a->{locale}{am_pm_abbreviated};
$a->{locale}{locale_data}{available_formats} = $a->{locale}{available_formats};
$a->{locale}{locale_data}{day_format_abbreviated} = $a->{locale}{day_format_abbreviated};
$a->{locale}{locale_data}{day_format_narrow} = $a->{locale}{day_format_narrow};
$a->{locale}{locale_data}{day_format_wide} = $a->{locale}{day_format_wide};
$a->{locale}{locale_data}{day_stand_alone_abbreviated} = $a->{locale}{day_stand_alone_abbreviated};
$a->{locale}{locale_data}{day_stand_alone_narrow} = $a->{locale}{day_stand_alone_narrow};
$a->{locale}{locale_data}{day_stand_alone_wide} = $a->{locale}{day_stand_alone_wide};
$a->{locale}{locale_data}{era_abbreviated} = $a->{locale}{era_abbreviated};
$a->{locale}{locale_data}{era_narrow} = $a->{locale}{era_narrow};
$a->{locale}{locale_data}{era_wide} = $a->{locale}{era_wide};
$a->{locale}{month_format_abbreviated} = $a->{locale}{locale_data}{month_format_abbreviated};
$a->{locale}{month_format_narrow} = $a->{locale}{locale_data}{month_format_narrow};
$a->{locale}{month_format_wide} = $a->{locale}{locale_data}{month_format_wide};
$a->{locale}{month_stand_alone_abbreviated} = $a->{locale}{locale_data}{month_stand_alone_abbreviated};
$a->{locale}{month_stand_alone_narrow} = $a->{locale}{locale_data}{month_stand_alone_narrow};
$a->{locale}{month_stand_alone_wide} = $a->{locale}{locale_data}{month_stand_alone_wide};
$a->{locale}{quarter_format_abbreviated} = $a->{locale}{locale_data}{quarter_format_abbreviated};
$a->{locale}{quarter_format_narrow} = $a->{locale}{locale_data}{quarter_format_narrow};
$a->{locale}{quarter_format_wide} = $a->{locale}{locale_data}{quarter_format_wide};
$a->{locale}{quarter_stand_alone_abbreviated} = $a->{locale}{locale_data}{quarter_stand_alone_abbreviated};
$a->{locale}{quarter_stand_alone_narrow} = $a->{locale}{locale_data}{quarter_stand_alone_narrow};
$a->{locale}{quarter_stand_alone_wide} = $a->{locale}{locale_data}{quarter_stand_alone_wide};
$a;
}
Coerce multiple data
% coerce-with-sah date --coerce-to 'float(epoch)' --multiple-data-as-perl '["2016-05-15", "1463328281"]'
[1463245200, 1463328281]
Add one or more coerce rules
% coerce-with-sah duration --coerce-rule From_str::human --coerce-rule From_str::iso8601 --data-as-perl '"P1Y2M"'
36817200
Show source code
% coerce-with-sah duration --coerce-to 'float(secs)' -c
require Scalar::Util;
require Time::Duration::Parse::AsHash;
sub {
my $data = shift;
unless (defined $data) {
return undef;
}
do { if ($data =~ /\A([0-9]{1,2}):([0-9]{1,2})(?::([0-9]{1,2})(\.[0-9]{1,9})?)?\z/) { my $res = do { if ($1 > 23) { ["Invalid hour '$1', must be between 0-23"] } elsif ($2 > 59) { ["Invalid minute '$2', must be between 0-59"] } elsif (defined $3 && $3 > 59) { ["Invalid second '$3', must be between 0-59"] } else { [undef, $1*3600 + $2*60 + (defined $3 ? $3 : 0) + (defined $4 ? $4 : 0)] } }; $res->[0] ? undef : $res->[1] } else { (!ref($data) && $data =~ /\A[0-9]+(?:.[0-9]+)\z/) ? ($data) : (Scalar::Util::blessed($data) && $data->isa('DateTime::Duration')) ? (($data->years * 365.25*86400 + $data->months * 30.4375*86400 + $data->weeks * 7*86400 + $data->days * 86400 + $data->hours * 3600 + $data->minutes * 60 + $data->seconds + $data->nanoseconds * 1e-9)) : (!ref($data) && $data =~ /\AP(?:([0-9]+(?:\.[0-9]+)?)Y)? (?:([0-9]+(?:\.[0-9]+)?)M)? (?:([0-9]+(?:\.[0-9]+)?)W)? (?:([0-9]+(?:\.[0-9]+)?)D)? (?: T (?:([0-9]+(?:\.[0-9]+)?)H)? (?:([0-9]+(?:\.[0-9]+)?)M)? (?:([0-9]+(?:\.[0-9]+)?)S)? )?\z/x) ? ((($1||0)*365.25*86400 + ($2||0)*30.4375*86400 + ($3||0)*7*86400 + ($4||0)*86400 + ($5||0)*3600 + ($6||0)*60 + ($7||0))) : do { if (!ref($data) && $data =~ /\d.*[a-z]/) { my $res = do { my $p; eval { $p = Time::Duration::Parse::AsHash::parse_duration($data) }; my $err = $@; if ($err) { $err =~ s/ at .+//s; ["Invalid duration: $err"] } else { [undef, ($p->{years}||0) * 365.25*86400 + ($p->{months}||0) * 30.4375*86400 + ($p->{weeks}||0) * 7*86400 + ($p->{days}||0) * 86400 + ($p->{hours}||0) * 3600 + ($p->{minutes}||0) * 60 + ($p->{seconds}||0)] } }; $res->[0] ? undef : $res->[1] } else { $data } } } };
}
Show source code, with line number
% coerce-with-sah duration --coerce-to 'DateTime::Duration' -c -l
1|require DateTime::Duration;
2|require Scalar::Util;
3|require Time::Duration::Parse::AsHash;
4|sub {
5| my $data = shift;
6| unless (defined $data) {
7| return undef;
8| }
9| do { if ($data =~ /\A([0-9]{1,2}):([0-9]{1,2})(?::([0-9]{1,2})(\.[0-9]{1,9})?)?\z/) { my $res = do { if ($1 > 23) { ["Invalid hour '$1', must be between 0-23"] } elsif ($2 > 59) { ["Invalid minute '$2', must be between 0-59"] } elsif (defined $3 && $3 > 59) { ["Invalid second '$3', must be between 0-59"] } else { [undef, DateTime::Duration->new(hours => $1, minutes => $2, seconds => (defined $3 ? $3 : 0), nanoseconds => (defined $4 ? $4 * 1e9 : 0))] } }; $res->[0] ? undef : $res->[1] } else { (!ref($data) && $data =~ /\A[0-9]+(?:.[0-9]+)\z/) ? (DateTime::Duration->new(seconds => $data)) : (Scalar::Util::blessed($data) && $data->isa('DateTime::Duration')) ? ($data) : (!ref($data) && $data =~ /\AP(?:([0-9]+(?:\.[0-9]+)?)Y)? (?:([0-9]+(?:\.[0-9]+)?)M)? (?:([0-9]+(?:\.[0-9]+)?)W)? (?:([0-9]+(?:\.[0-9]+)?)D)? (?: T (?:([0-9]+(?:\.[0-9]+)?)H)? (?:([0-9]+(?:\.[0-9]+)?)M)? (?:([0-9]+(?:\.[0-9]+)?)S)? )?\z/x) ? (DateTime::Duration->new( (years=>$1) x !!defined($1), (months=>$2) x !!defined($2), (weeks=>$3) x !!defined($3), (days=>$4) x !!defined($4), (hours=>$5) x !!defined($5), (minutes=>$6) x !!defined($6), (seconds=>$7) x !!defined($7))) : do { if (!ref($data) && $data =~ /\d.*[a-z]/) { my $res = do { my $p; eval { $p = Time::Duration::Parse::AsHash::parse_duration($data) }; my $err = $@; if ($err) { $err =~ s/ at .+//s; ["Invalid duration: $err"] } else { [undef, DateTime::Duration->new( (years=>$p->{years}) x !!defined($p->{years}), (months=>$p->{months}) x !!defined($p->{months}), (weeks=>$p->{weeks}) x !!defined($p->{weeks}), (days=>$p->{days}) x !!defined($p->{days}), (hours=>$p->{hours}) x !!defined($p->{hours}), (minutes=>$p->{minutes}) x !!defined($p->{minutes}), (seconds=>$p->{seconds}) x !!defined($p->{seconds}))] } }; $res->[0] ? undef : $res->[1] } else { $data } } } };
10|}
Show source code (JavaScript)
% coerce-with-sah date -C js -c
function (data) {
if (data === undefined || data === null) {
return null;
}
return ((typeof(data)=='number' && data >= 100000000 && data <= 2147483648) ? ((new Date(data * 1000))) : (function() { if ((data instanceof Date)) { var _tmp1 = isNaN(data) ? ['Invalid date', data] : [null, data]; if (_tmp1[0]) { return null } else { return _tmp1[1] } } else { return (function() { if (typeof(data)=='string') { var _tmp1 = (function (_m) { _m = new Date(data); if (isNaN(_m)) { return ['Invalid date', _m] } else { return [null, _m] } })(); if (_tmp1[0]) { return null } else { return _tmp1[1] } } else { return data } })() } })());
}
HOMEPAGE
Please visit the project's homepage at https://metacpan.org/release/App-SahUtils.
SOURCE
Source repository is at https://github.com/perlancar/perl-App-SahUtils.
AUTHOR
perlancar <perlancar@cpan.org>
CONTRIBUTING
To contribute, you can send patches by email/via RT, or send pull requests on GitHub.
Most of the time, you don't need to build the distribution yourself. You can simply modify the code, then test via:
% prove -l
If you want to build the distribution (e.g. to try to install it locally on your system), you can install Dist::Zilla, Dist::Zilla::PluginBundle::Author::PERLANCAR, Pod::Weaver::PluginBundle::Author::PERLANCAR, and sometimes one or two other Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond that are considered a bug and can be reported to me.
COPYRIGHT AND LICENSE
This software is copyright (c) 2024, 2023, 2022, 2020, 2019, 2018, 2017, 2016, 2015 by perlancar <perlancar@cpan.org>.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
BUGS
Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=App-SahUtils
When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.