split
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
through | 2.3.8 | 4.36 kB | MIT | prod |
Split is an npm package that works as a stream utility to split a text stream into a line stream. This is particularly useful when dealing with a large amount of data that needs line-by-line processing. The matcher for the split can be a string or a regular expression, offering flexibility in defining the chunk separators.
To utilize the Split package in JavaScript, you'll first need to install it via npm, using npm install split
. Then, you can import and use it in your code. Here is a basic example of reading every line from a file:
const fs = require('fs');
const split = require('split');
fs.createReadStream('yourfile.txt')
.pipe(split())
.on('data', function (line) {
// Each chunk is now a separate line of data!
console.log(line);
});
Split also accepts optional parameters allowing you to define a maximum buffer length and whether the last buffer not delimited by a newline or matcher should be emitted. It also allows you to transform each emitted line with a provided function such as JSON.parse
:
fs.createReadStream('yourfile.json')
.pipe(split(JSON.parse))
.on('data', function (obj) {
// Each emitted chunk is now a JavaScript object
console.log(obj);
})
.on('error', function (err) {
// Syntax errors will be handled here
console.log(err);
});
Split's detailed documentation on how to use the package with various options and examples can be found the official GitHub repository. For quicker reference, developers can also look at JavaScript's String#split documentation, which offers a similar usage to the package. Being adept with Split will be beneficial to developers working with stream data, making the process of separating and analyzing such data much more efficient and manageable.