File Coverage

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