A module to read text files (including extra large ones) in the browser and in Node.js line by line without loading whole file to the memory.
It accepts both HTML5 File for client side and file path for server side in Node.JS.
Installation:
npm install line-navigator --save
Check it out live in Tonic: tonicdev.com/npm/line-navigator.
Features:
- Modular: can be used in vanilla JS, Node.JS, Browserify.JS and as AMD module.
- No file size limit: LineNavigator doesn't try to read all file to the memory unlike ordinary FileReader.readAsText() which lags for big files and crashes for files larger than ~400 MB.
- Random access by index: repetitive access is optimized.
- Embedded search tools: allows searching by regular expessions, highlight matches, etc.
- Position as per cent: allows showing nice representation in the UI.
- All line endings supported: all types of line endings are supported even mixed together:
\n,\r\n.
Contents:
- Sources as either NPM package or as standalone files (file-wrapper.js and line-navigator.js)
- Examples of usage in vanilla JS, Browserify.JS, Require.JS and Node.JS
- Functional and unit tests
All examples contain all methods invocation and comments, so you can use them as a reference.
Creates an instance of LineNavigator.
var navigator = new LineNavigator(file[, options]);
Where:
fileHTML5 File for client side or a string with file path for server side.optionsdictionary which can contain the following keys:options.encodingencoding name, default is 'utf8'options.chunkSizesize of chunk, default is 1024 * 4options.throwOnLongLinesreturn error when line is longer than chunkSize, otherwise it will be threated as several lines
Reads optimal amount of lines (which depends on chunkSize).
navigator.readSomeLines(indexToStartWith, function (err, index, lines, isEof, progress) {
...
});
Where (including callback arguments):
indexToStartWithindex of first line to readerrwill be undefined if no error happenesindexcallback's representation ofindexToStartWithlinesan array of strings, where index of first one isindexToStartWithisEofa boolean which is true if end of file is reachedprogressa 0-100 per cent position of the last line in the chunk
Reads exact amount of lines.
navigator.readLines(indexToStartWith, numberOfLines, function (err, index, lines, isEof, progress) {
...
});
Where (including callback arguments):
indexToStartWithan index of first line to readnumberOfLinesa number of lines wantederrwill be undefined if no error happenesindexcallback's representation ofindexToStartWithlinesan array of strings, where index of first one isindexToStartWithisEofa boolean which is true if end of file is reachedprogressa 0-100 per cent position of the last line in chunk Method will not return an error if the file contains less lines than requested, just less lines.
Finds first lines starting from given index which matches regex pattern.
navigator.find(regex, indexToStartWith, function(err, index, match) {
...
});
Where (including callback arguments):
regexregular expression to search forindexToStartWithan index of first line to readerrwill be undefined if no error happenesindexcallback's representation ofindexToStartWithmatcha dictionary with the following structure:match.linefull line textmatch.offsetposition of the match itself in this linematch.lengthlength of the match itself in this line
Finds all matches in file.
navigator.findAll(regex, indexToStartWith, limit, function (err, index, limitHit, results) {
...
});
Where (including callback arguments):
regexregular expression to search forindexToStartWithan index of first line to readlimitmax number of matcheserrwill be undefined if no error happenesindexcallback's representation ofindexToStartWithresultsarray of matches, where each contains following:results[0].indexindex of this lineresults[0].linefull line textresults[0].offsetposition of the match itself in this lineresults[0].lengthlength of the match itself in this line