split2
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
Split2 is a powerful Node.js module designed to break up a stream and reassemble it in a way where each line is a standalone chunk. Drawing inspiration from the Split module developed by Dominic Tarr, Split2 differs by utilizing the Transform API from Node.js core rather than operating independently. The functionality of Split2 is versatile as it accepts Strings or RegEx as matcher parameters.
To incorporate Split2 into your project, begin by installing the npm package. After ensuring the module is included in your file with var split2 = require('split2');
, you can then use it as a pipe in a stream of data. The following JS snippet displays every line in a file as a separate chunk:
var fs = require('fs');
var split2 = require('split2');
fs.createReadStream('my_file.txt')
.pipe(split2())
.on('data', function (line) {
console.log(line); // Outputs each line from 'my_file.txt'
});
Split2 also provides a way to handle potential errors, you just need to add an 'error' event listener on your stream:
fs.createReadStream('my_file.txt')
.pipe(split2())
.on('data', function (line) {
console.log(line); // Outputs each line from 'my_file.txt'
})
.on('error', function (err) {
console.error(err); // Outputs any error that occurred
});
And if you're working with JSON objects in a Newline Delimited Json (NDJ) format, Split2 can handle that too. By using JSON.parse
, the module transforms each line into an object:
fs.createReadStream('my_file.ndjson')
.pipe(split2(JSON.parse))
.on('data', function (obj) {
console.log(obj); // Outputs each JSON object from 'my_file.ndjson'
})
.on('error', function (error) {
console.error(error); // Outputs any error that occurred
});
To access the Split2 documentation, you can head over to GitHub repository. The readme file provides comprehensive instructions and examples to get you started. As a reference guide, it includes details about its implementation, compatibility, optional settings, NDJ support, and error handling procedures. If String splitting parameters are of interest, be sure to review the MDN web docs linked directly in the readme.