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