|
17 | 17 | * [Ignoring Empty Rows](#csv-parse-ignoring-empty-rows)
|
18 | 18 | * [Transforming Rows](#csv-parse-transforming)
|
19 | 19 | * [Validating Rows](#csv-parse-validation)
|
| 20 | + * [Max Rows](#max-rows) |
| 21 | + * [Skip Rows](#skip-rows) |
| 22 | + * [Skip Lines](#skip-lines) |
20 | 23 |
|
21 | 24 | <a name="parsing-options"></a>
|
22 | 25 | ## Options
|
|
45 | 48 | * `rtrim: {boolean} = false`: Set to `true` to right trim all fields.
|
46 | 49 | * `ltrim: {boolean} = false`: Set to `true` to left trim all fields.
|
47 | 50 | * `encoding: {string} = 'utf8'`: Passed to [StringDecoder](https://nodejs.org/api/string_decoder.html#string_decoder_new_stringdecoder_encoding) when decoding incoming buffers. Change if incoming content is not 'utf8' encoded.
|
| 51 | +* `maxRows: {number} = 0`: If number is `> 0` the specified number of rows will be parsed.(e.g. `100` would return the first 100 rows of data). |
| 52 | +* `skipRows: {number} = 0`: If number is `> 0` the specified number of **parsed** rows will be skipped. |
| 53 | +* `skipLines: {number} = 0`: If number is `> 0` the specified number of lines will be skipped. |
48 | 54 |
|
49 | 55 | <a name="parsing-events"></a>
|
50 | 56 | ## Events
|
@@ -585,3 +591,123 @@ Valid [row={"firstName":"timmy","lastName":"yukon"}]
|
585 | 591 | Parsed 2 rows
|
586 | 592 | ```
|
587 | 593 |
|
| 594 | +<a name="max-rows"></a> |
| 595 | +[`examples/parsing/max_rows.example.example.js`](../examples/parsing/max_rows.example.js) |
| 596 | + |
| 597 | +In the following example there are 10 rows, but only 5 will be parsed because of the `maxRows` option. |
| 598 | + |
| 599 | +```javascript |
| 600 | +const rows = [ |
| 601 | + 'header1,header2\n', |
| 602 | + 'col1,col1\n', |
| 603 | + 'col2,col2\n', |
| 604 | + 'col3,col3\n', |
| 605 | + 'col4,col4\n', |
| 606 | + 'col5,col5\n', |
| 607 | + 'col6,col6\n', |
| 608 | + 'col7,col7\n', |
| 609 | + 'col8,col8\n', |
| 610 | + 'col9,col9\n', |
| 611 | + 'col10,col10', |
| 612 | +]; |
| 613 | + |
| 614 | +const stream = csv |
| 615 | + .parse({ headers: true, maxRows: 5 }) |
| 616 | + .on('error', error => console.error(error)) |
| 617 | + .on('data', row => console.log(row)) |
| 618 | + .on('end', rowCount => console.log(`Parsed ${rowCount} rows`)); |
| 619 | + |
| 620 | +rows.forEach(row => stream.write(row)); |
| 621 | +stream.end(); |
| 622 | +``` |
| 623 | + |
| 624 | +Expected output |
| 625 | + |
| 626 | +``` |
| 627 | +{ header1: 'col1', header2: 'col1' } |
| 628 | +{ header1: 'col2', header2: 'col2' } |
| 629 | +{ header1: 'col3', header2: 'col3' } |
| 630 | +{ header1: 'col4', header2: 'col4' } |
| 631 | +{ header1: 'col5', header2: 'col5' } |
| 632 | +Parsed 5 rows |
| 633 | +``` |
| 634 | + |
| 635 | +<a name="skip-rows"></a> |
| 636 | +[`examples/parsing/skip_rows.example.example.js`](../examples/parsing/skip_rows.example.js) |
| 637 | + |
| 638 | +In the following example the first 2 rows are skipped. |
| 639 | + |
| 640 | +**NOTE** Notice how the header row is not skipped, only the row. |
| 641 | + |
| 642 | +```javascript |
| 643 | +const rows = [ |
| 644 | + 'header1,header2\n', |
| 645 | + 'col1,col1\n', |
| 646 | + 'col2,col2\n', |
| 647 | + 'col3,col3\n', |
| 648 | + 'col4,col4\n', |
| 649 | + 'col5,col5\n', |
| 650 | + 'col6,col6\n', |
| 651 | +]; |
| 652 | + |
| 653 | +const stream = csv |
| 654 | + .parse({ headers: true, skipRows: 2 }) |
| 655 | + .on('error', error => console.error(error)) |
| 656 | + .on('data', row => console.log(row)) |
| 657 | + .on('end', rowCount => console.log(`Parsed ${rowCount} rows`)); |
| 658 | + |
| 659 | +rows.forEach(row => stream.write(row)); |
| 660 | +stream.end(); |
| 661 | +``` |
| 662 | + |
| 663 | +Expected output |
| 664 | + |
| 665 | +``` |
| 666 | +{ header1: 'col3', header2: 'col3' } |
| 667 | +{ header1: 'col4', header2: 'col4' } |
| 668 | +{ header1: 'col5', header2: 'col5' } |
| 669 | +{ header1: 'col6', header2: 'col6' } |
| 670 | +Parsed 4 rows |
| 671 | +``` |
| 672 | + |
| 673 | +<a name="skip-lines"></a> |
| 674 | +[`examples/parsing/skip_lines.example.example.js`](../examples/parsing/skip_lines.example.js) |
| 675 | + |
| 676 | +In the following example the first 2 lines are skipped. |
| 677 | + |
| 678 | +**NOTE** Notice how the headers come from the third line because the first two are skipped. |
| 679 | + |
| 680 | +```javascript |
| 681 | +const csv = require('../../'); |
| 682 | + |
| 683 | +const rows = [ |
| 684 | + 'skip1_header1,skip1_header2\n', |
| 685 | + 'skip2_header1,skip2_header2\n', |
| 686 | + 'header1,header2\n', |
| 687 | + 'col1,col1\n', |
| 688 | + 'col2,col2\n', |
| 689 | + 'col3,col3\n', |
| 690 | + 'col4,col4\n', |
| 691 | +]; |
| 692 | + |
| 693 | +const stream = csv |
| 694 | + .parse({ headers: true, skipLines: 2 }) |
| 695 | + .on('error', error => console.error(error)) |
| 696 | + .on('data', row => console.log(row)) |
| 697 | + .on('end', rowCount => console.log(`Parsed ${rowCount} rows`)); |
| 698 | + |
| 699 | +rows.forEach(row => stream.write(row)); |
| 700 | +stream.end(); |
| 701 | +``` |
| 702 | + |
| 703 | +Expected output |
| 704 | + |
| 705 | +``` |
| 706 | +{ header1: 'col1', header2: 'col1' } |
| 707 | +{ header1: 'col2', header2: 'col2' } |
| 708 | +{ header1: 'col3', header2: 'col3' } |
| 709 | +{ header1: 'col4', header2: 'col4' } |
| 710 | +Parsed 4 rows |
| 711 | +``` |
| 712 | + |
| 713 | + |
0 commit comments