File Coverage

blib/lib/App/CSVUtils/csv_sorted_fields.pm
Criterion Covered Total %
statement 17 17 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 23 23 100.0


line stmt bran cond sub pod time code
1             package App::CSVUtils::csv_sorted_fields;
2              
3 1     1   7668 use 5.010001;
  1         3  
4 1     1   6 use strict;
  1         2  
  1         20  
5 1     1   5 use warnings;
  1         2  
  1         26  
6 1     1   6 use Log::ger;
  1         2  
  1         4  
7              
8             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
9             our $DATE = '2023-03-31'; # DATE
10             our $DIST = 'App-CSVUtils'; # DIST
11             our $VERSION = '1.023'; # VERSION
12              
13 1         47 use App::CSVUtils qw(
14             gen_csv_util
15 1     1   257 );
  1         3  
16 1     1   5 use App::CSVUtils::csv_sort_fields;
  1         5  
  1         158  
17              
18             gen_csv_util(
19             name => 'csv_sorted_fields',
20             summary => 'Check that CSV fields are sorted',
21             description => <<'_',
22              
23             This utility checks that fields in the CSV are sorted according to specified
24             sorting rule(s). Example `input.csv`:
25              
26             b,c,a
27             1,2,3
28             4,5,6
29              
30             Example check command:
31              
32             % csv-sorted-fields input.csv; # check if the fields are ascibetically sorted
33             ERROR 400: Fields are NOT sorted
34              
35             Example `input2.csv`:
36              
37             c,b,a
38             1,2,3
39             4,5,6
40              
41             % csv-sorted-fields input2.csv -r
42             Fields are sorted
43              
44             See <prog:csv-sort-fields> for details on sorting options.
45              
46             _
47              
48             writes_csv => 0,
49              
50             tags => ['category:checking'],
51              
52             add_args => {
53             # KEEP SYNC WITH csv_sort_fields
54             %App::CSVUtils::argspecs_sort_fields,
55              
56             quiet => {
57             summary => 'If set to true, do not show messages',
58             schema => 'bool*',
59             cmdline_aliases => {q=>{}},
60             },
61             },
62              
63             # KEEP SYNC WITH csv_sort_fields
64             add_args_rels => {
65             choose_one => ['by_examples', 'by_code', 'by_sortsub'],
66             },
67              
68             on_input_header_row => sub {
69             local $main::_CSV_SORTED_FIELDS = 1;
70             App::CSVUtils::csv_sort_fields::on_input_header_row(@_);
71             },
72              
73             on_input_data_row => sub {
74             local $main::_CSV_SORTED_FIELDS = 1;
75             App::CSVUtils::csv_sort_fields::on_input_data_row(@_);
76             },
77             );
78              
79             1;
80             # ABSTRACT: Check that CSV fields are sorted
81              
82             __END__
83              
84             =pod
85              
86             =encoding UTF-8
87              
88             =head1 NAME
89              
90             App::CSVUtils::csv_sorted_fields - Check that CSV fields are sorted
91              
92             =head1 VERSION
93              
94             This document describes version 1.023 of App::CSVUtils::csv_sorted_fields (from Perl distribution App-CSVUtils), released on 2023-03-31.
95              
96             =head1 FUNCTIONS
97              
98              
99             =head2 csv_sorted_fields
100              
101             Usage:
102              
103             csv_sorted_fields(%args) -> [$status_code, $reason, $payload, \%result_meta]
104              
105             Check that CSV fields are sorted.
106              
107             This utility checks that fields in the CSV are sorted according to specified
108             sorting rule(s). Example C<input.csv>:
109              
110             b,c,a
111             1,2,3
112             4,5,6
113              
114             Example check command:
115              
116             % csv-sorted-fields input.csv; # check if the fields are ascibetically sorted
117             ERROR 400: Fields are NOT sorted
118              
119             Example C<input2.csv>:
120              
121             c,b,a
122             1,2,3
123             4,5,6
124            
125             % csv-sorted-fields input2.csv -r
126             Fields are sorted
127              
128             See L<csv-sort-fields> for details on sorting options.
129              
130             This function is not exported.
131              
132             Arguments ('*' denotes required arguments):
133              
134             =over 4
135              
136             =item * B<by_code> => I<str|code>
137              
138             Sort fields using Perl code.
139              
140             C<$a> and C<$b> (or the first and second argument) will contain C<[$field_name,
141             $field_idx]>.
142              
143             =item * B<by_examples> => I<array[str]>
144              
145             Sort by a list of field names as examples.
146              
147             =item * B<by_sortsub> => I<str>
148              
149             Sort using a Sort::Sub routine.
150              
151             When sorting rows, usually combined with C<--key> because most Sort::Sub routine
152             expects a string to be compared against.
153              
154             When sorting fields, the Sort::Sub routine will get the field name as argument.
155              
156             =item * B<ci> => I<bool>
157              
158             (No description)
159              
160             =item * B<input_escape_char> => I<str>
161              
162             Specify character to escape value in field in input CSV, will be passed to Text::CSV_XS.
163              
164             Defaults to C<\\> (backslash). Overrides C<--input-tsv> option.
165              
166             =item * B<input_filename> => I<filename> (default: "-")
167              
168             Input CSV file.
169              
170             Use C<-> to read from stdin.
171              
172             Encoding of input file is assumed to be UTF-8.
173              
174             =item * B<input_header> => I<bool> (default: 1)
175              
176             Specify whether input CSV has a header row.
177              
178             By default, the first row of the input CSV will be assumed to contain field
179             names (and the second row contains the first data row). When you declare that
180             input CSV does not have header row (C<--no-input-header>), the first row of the
181             CSV is assumed to contain the first data row. Fields will be named C<field1>,
182             C<field2>, and so on.
183              
184             =item * B<input_quote_char> => I<str>
185              
186             Specify field quote character in input CSV, will be passed to Text::CSV_XS.
187              
188             Defaults to C<"> (double quote). Overrides C<--input-tsv> option.
189              
190             =item * B<input_sep_char> => I<str>
191              
192             Specify field separator character in input CSV, will be passed to Text::CSV_XS.
193              
194             Defaults to C<,> (comma). Overrides C<--input-tsv> option.
195              
196             =item * B<input_tsv> => I<true>
197              
198             Inform that input file is in TSV (tab-separated) format instead of CSV.
199              
200             Overriden by C<--input-sep-char>, C<--input-quote-char>, C<--input-escape-char>
201             options. If one of those options is specified, then C<--input-tsv> will be
202             ignored.
203              
204             =item * B<quiet> => I<bool>
205              
206             If set to true, do not show messages.
207              
208             =item * B<reverse> => I<bool>
209              
210             (No description)
211              
212             =item * B<sortsub_args> => I<hash>
213              
214             Arguments to pass to Sort::Sub routine.
215              
216              
217             =back
218              
219             Returns an enveloped result (an array).
220              
221             First element ($status_code) is an integer containing HTTP-like status code
222             (200 means OK, 4xx caller error, 5xx function error). Second element
223             ($reason) is a string containing error message, or something like "OK" if status is
224             200. Third element ($payload) is the actual result, but usually not present when enveloped result is an error response ($status_code is not 2xx). Fourth
225             element (%result_meta) is called result metadata and is optional, a hash
226             that contains extra information, much like how HTTP response headers provide additional metadata.
227              
228             Return value: (any)
229              
230             =head1 HOMEPAGE
231              
232             Please visit the project's homepage at L<https://metacpan.org/release/App-CSVUtils>.
233              
234             =head1 SOURCE
235              
236             Source repository is at L<https://github.com/perlancar/perl-App-CSVUtils>.
237              
238             =head1 AUTHOR
239              
240             perlancar <perlancar@cpan.org>
241              
242             =head1 CONTRIBUTING
243              
244              
245             To contribute, you can send patches by email/via RT, or send pull requests on
246             GitHub.
247              
248             Most of the time, you don't need to build the distribution yourself. You can
249             simply modify the code, then test via:
250              
251             % prove -l
252              
253             If you want to build the distribution (e.g. to try to install it locally on your
254             system), you can install L<Dist::Zilla>,
255             L<Dist::Zilla::PluginBundle::Author::PERLANCAR>,
256             L<Pod::Weaver::PluginBundle::Author::PERLANCAR>, and sometimes one or two other
257             Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond
258             that are considered a bug and can be reported to me.
259              
260             =head1 COPYRIGHT AND LICENSE
261              
262             This software is copyright (c) 2023, 2022, 2021, 2020, 2019, 2018, 2017, 2016 by perlancar <perlancar@cpan.org>.
263              
264             This is free software; you can redistribute it and/or modify it under
265             the same terms as the Perl 5 programming language system itself.
266              
267             =head1 BUGS
268              
269             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=App-CSVUtils>
270              
271             When submitting a bug or request, please include a test-file or a
272             patch to an existing test-file that illustrates the bug or desired
273             feature.
274              
275             =cut