source: node_modules/split2/README.md

main
Last change on this file was 62b2964, checked in by Klimentina Efremova <klimentina08642@…>, 2 weeks ago

Project Handcraft Marketplace

  • Property mode set to 100644
File size: 3.3 KB
RevLine 
[62b2964]1# Split2(matcher, mapper, options)
2
3![ci](https://github.com/mcollina/split2/workflows/ci/badge.svg)
4
5Break up a stream and reassemble it so that each line is a chunk.
6`split2` is inspired by [@dominictarr](https://github.com/dominictarr) [`split`](https://github.com/dominictarr/split) module,
7and it is totally API compatible with it.
8However, it is based on Node.js core [`Transform`](https://nodejs.org/api/stream.html#stream_new_stream_transform_options).
9
10`matcher` may be a `String`, or a `RegExp`. Example, read every line in a file ...
11
12``` js
13 fs.createReadStream(file)
14 .pipe(split2())
15 .on('data', function (line) {
16 //each chunk now is a separate line!
17 })
18
19```
20
21`split` takes the same arguments as `string.split` except it defaults to '/\r?\n/', and the optional `limit` paremeter is ignored.
22[String#split](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/split)
23
24`split` takes an optional options object on it's third argument, which
25is directly passed as a
26[Transform](https://nodejs.org/api/stream.html#stream_new_stream_transform_options)
27option.
28
29Additionally, the `.maxLength` and `.skipOverflow` options are implemented, which set limits on the internal
30buffer size and the stream's behavior when the limit is exceeded. There is no limit unless `maxLength` is set. When
31the internal buffer size exceeds `maxLength`, the stream emits an error by default. You may also set `skipOverflow` to
32true to suppress the error and instead skip past any lines that cause the internal buffer to exceed `maxLength`.
33
34Calling `.destroy` will make the stream emit `close`. Use this to perform cleanup logic
35
36``` js
37var splitFile = function(filename) {
38 var file = fs.createReadStream(filename)
39
40 return file
41 .pipe(split2())
42 .on('close', function() {
43 // destroy the file stream in case the split stream was destroyed
44 file.destroy()
45 })
46}
47
48var stream = splitFile('my-file.txt')
49
50stream.destroy() // will destroy the input file stream
51```
52
53# NDJ - Newline Delimited Json
54
55`split2` accepts a function which transforms each line.
56
57``` js
58fs.createReadStream(file)
59 .pipe(split2(JSON.parse))
60 .on('data', function (obj) {
61 //each chunk now is a js object
62 })
63 .on("error", function(error) {
64 //handling parsing errors
65 })
66```
67
68However, in [@dominictarr](https://github.com/dominictarr) [`split`](https://github.com/dominictarr/split) the mapper
69is wrapped in a try-catch, while here it is not: if your parsing logic can throw, wrap it yourself. Otherwise, you can also use the stream error handling when mapper function throw.
70
71# License
72
73Copyright (c) 2014-2021, Matteo Collina <hello@matteocollina.com>
74
75Permission to use, copy, modify, and/or distribute this software for any
76purpose with or without fee is hereby granted, provided that the above
77copyright notice and this permission notice appear in all copies.
78
79THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
80WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
81MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
82ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
83WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
84ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
85IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Note: See TracBrowser for help on using the repository browser.