txt-reader
TxtReader is a JavaScript library to read text file in browsers based on FileReader API. It can read very large, huge, giant files (GB+). It offloads the file reading operations to Web Worker so that it won't block browser UI rendering even when it is reading a very large file. And you can easily track the reading progress by using promise-like methods. Click here to check the demo
Contents
Installation
npm
Get it via npm by adding txt-reader
to your package.json
or run:
npm install --save txt-reader
CommonJS
Do npm install --save txt-reader
first and then you can use TxtReader
as below:
var TxtReader = TxtReader;
ES2015 or TypeScript
Do npm install --save txt-reader
first and then you can use TxtReader
as below:
HTML script tag
Alternatively you can download this project folder from Github and get the txt-reader.min.js
from txt-reader/dist
, then make a reference in <script>
tag.
The txt-reader.min.js
registers a global variable at window.TxtReader
.
Methods
Instantiate the TxtReader
To start using TxtReader
, we need to create an instance of TxtReader
.
var reader = ;
loadFile(file[ ,iteratorConfig])
After creating instance, we can load any text file into TxtReader
using loadFile()
, this method asynchronously goes through the text file and returns the line number. For large text file, it may take dozens of seconds to complete, we can use .progress()
, .then()
and .catch()
to track the method running progress and results. This method accepts a customized iterator so you can do some initialization jobs towards each line in the text file.
Syntax
reader;
Arguments
Parameter | Type | Description |
---|---|---|
file | File | The text file to be read, only supports UTF-8 encoding. |
iteratorConfig | Object | Optional. The iteratorConfig allows you to run customized iterator function for each line in the text file during loading the file. For detailed usage, please check the iterateLines() method. NOTE that inappropriate usage of iterator may bring performance issue. |
Return value
This method returns an instance of TxtReaderTask
, the TxtReaderTask
implements three promise-like methods: .progress()
, .then()
and .catch()
.
.progress(onProgress)
Parameter | Type | Description |
---|---|---|
onProgress | Function | Function to execute for each task progress update message, taking one argument: progress (Number): Indicating the current task progress in Number from 0-100. |
Appends an onProgress
handler to the TxtReaderTask
and returns the TxtReaderTask
.
.then(onComplete)
Parameter | Type | Description |
---|---|---|
onComplete | Function | Function to execute when a task completes, taking one argument: response (TaskResponse) where you can get the loadFile task execution time taken (millisecond) in response.timeTaken , the text file line number in response.result.lineCount and the iterator this scope in response.result.scope if a customized iterator was specified. |
Appends an onComplete
handler to the TxtReaderTask
and returns the TxtReaderTask
. The TaskResponse
is an object with following structure.
.catch(onFail)
Parameter | Type | Description |
---|---|---|
onFail | Function | Function to execute when a task fails, taking one argument: reason (String): The failure reason |
Appends an onFail
handler to the TxtReaderTask
and returns the TxtReaderTask
.
getLines(start, count[, decode])
Get specific lines from a loaded text file. You need to load the file via loadFile()
before calling this method. Similar to TxtParser.loadFile(file[ ,iteratorConfig])
, this is also an asynchronous function and returns TxtReaderTask
where you can chain .progress()
, .then()
and .catch()
to get the task running progress and result.
Syntax
reader;
Arguments
Parameter | Type | Description |
---|---|---|
start | Number | The line number of the first line to include in the returned array. |
count | Number | The amount of lines to get. |
decode | Boolean | Optional. Default value: true. Whether decode each line to string. |
Return value
Same as loadFile()
method, getLines()
also returns an instance of TxtReaderTask
. The results can be retrieved from response.result
as an array in the onComplete
callback.
getSporadicLines(sporadicLinesMap[, decode])
getLine()
method can only get continuous lines. getSporadicLines()
allows you to get sporadic lines from a loaded text file. You need to load the file via loadFile()
before calling this method. Similar to loadFile()
, this is also an asynchronous function and returns TxtReaderTask
where you can chain .progress()
, .then()
and .catch()
to get the task running progress and result.
Syntax
reader ;
Arguments
Parameter | Type | Description |
---|---|---|
sporadicLinesMap | SporadicLineItem[] | The sporadicLinesMap is an array of SporadicLineItem to tell the reader which lines to get. The SporadicLineItem could be any of following three types:1. A number to indiciate a specific line number 2. A range specified by the start line number and end line number, like: {start: 10, end: 15} 3. A range specified by the start line number and the amount of lines to get, like {start: 20, count: 5} |
decode | Boolean | Optional. Default value: true. Whether decode each line to string. |
Sample: reader.getSporadicLines([1, 5, 7, {start: 100, end: 102}, {start: 1000, count: 4}])
will get lines: 1, 5, 7, 100, 101, 102, 1000, 1001, 1002, 1003
.
Return value
Same as loadFile()
method, getSporadicLines()
also returns an instance of TxtReaderTask
. The results can be retrieved from response.result
as an array in the onComplete
callback, each array item is an object containing two properties: lineNumber and value.
Sample
reader ;
iterateLines(iteratorConfig[, start, count])
This method iterates all lines or a selected range of a loaded text file. You need to load the file via loadFile()
before calling this method. Same as loadFile()
, it is an asynchronous function and returns TxtReaderTask
where you can use .progress()
, .then()
and .catch()
to get the task running progress and result.
Syntax
reader
Arguments
Parameter | Type | Description |
---|---|---|
iteratorConfig | Object | Object to define the iterator method and the this scope of the iterator. |
start | Number | Optional. The line number of the first line to iterate. If you don't specify the start and count arguments, it will iterate all lines in the text file. |
count | Number | Optional. The number of lines to iterate. |
iteratorConfig
The The iteratorConfig
takes two properties:
Property Name | Type | Description |
---|---|---|
eachLine | Function | The iterator function to execute for each line in the selected range, taking three arguments: raw (Uint8Array): the raw data of current line in Uint8Array format, you can use this.decode(raw) in iterator to decode it to readable string.progress (Number): a more accurate progress number of the iterating process for current line lineNumber (Number): the line number of current line |
scope | Object | Optional. You can initialize any properties or methods here and get/set the properties or call the methods via this in the eachLine callback. The modified scope will be returned as response.result in the onComplete callback, the methods will be removed. |
Note: The iterator function will execute in a Web Worker context, it cannot access your current JavaScript running context where you call this method, so please do not include any object/function reference from current context. You can define any helper methods and initial data in the scope
and access them via this
in eachLine
method.
Return value
Same as loadFile()
method, iterateLines()
also returns an instance of TxtReaderTask
. You can predefine any properties in scope
and access the scope
from this
context in your iterator, the scope
will finally be returned as response.result
in the onComplete
callback.
iterateSporadicLines(iteratorConfig, sporadicLinesMap)
iterateLines()
method can only iterate continuous lines. iterateSporadicLines()
allows you to iterate sporadic lines from a loaded text file. You need to load the file via loadFile()
before calling this method. Similar to loadFiles()
, this is also an asynchronous function and returns TxtReaderTask
where you can chain .progress()
, .then()
and .catch()
to get the task running progress and result.
Syntax
reader
Arguments
Parameter | Type | Description |
---|---|---|
iteratorConfig | Object | Object to define the iterator method and the this scope of the iterator. Same as the iteratorConfig in iterateLines() . For detailed definition of this object, please check The iteratorConfig |
sporadicLinesMap | SporadicLineItem[] | Same as the sporadicLinesMap in getSporadicLines() method, check Argument -> sporadicLinesMap for details |
Return value
Same as loadFile()
method, iterateSporadicLines()
also returns an instance of TxtReaderTask
. You can predefine any properties in scope
and access the scope
from this
context in your iterator, the scope
will finally be returned as response.result
in the onComplete
callback.
sniffLines(file, lineNumber[, decode])
sniffLines()
method can sniff first given number lines of a text file without loading this file (knowing the total line count of the file). Unlike getLines()
requires the file to be loaded via loadFile()
first, sniffLines()
method does not require the file to be loaded in prior. This method can be used when sometimes you don't want to load the whole file first but just want to sniff the first few lines of the file. Similar to getLines()
, it is an asynchronous function and returns TxtReaderTask
where you can chain .progress()
, .then()
and .catch()
to get the task running progress and result.
Note: you can call this method on TxtReader
no matter it already loaded a file or not, the pass in file
will not be loaded or replace current loaded file in TxtReader
.
Syntax
reader;
Arguments
Parameter | Type | Description |
---|---|---|
file | File | The text file to be sniffed, only supports UTF-8 encoding |
lineNumber | Number | How many lines to sniff. If you specify a number larger than the actual line count of the file, it will return all the lines of the file. |
decode | Boolean | Optional. Default value: true. Whether decode each line to string. |
Return value
Same as loadFile()
method, sniffLines()
also returns an instance of TxtReaderTask
. The results can be retrieved from response.result
as an array in the onComplete
callback.
Properties
lineCount
Type: Number
Returns the line number of the loaded text file. If no text file has been loaded, it returns 0. This is a readonly property.
utf8decoder
Type: An object of TextDecoder in UTF-8 encoding.
If the browser natively supports TextDecoder, then it equals to new TextDeocder('utf-8')
, otherwise consider it as a polyfill. This is a helper method in case you need to decode ArrayBuffer to text.
Sample
var reader = ;var file = documentfiles0; reader ; reader ; { reader ; reader ; reader ; reader ;} readerutf8decoder === 'a' // true
Browser Compatibility
txt-reader
can run on most major browsers that support FileReader API and Web Worker.
- Chrome
- Edge
- Firefox
- IE11