browser-line-reader
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -5,3 +5,3 @@ module.exports = { | ||
'plugin:@typescript-eslint/recommended', // Uses the recommended rules from the @typescript-eslint/eslint-plugin | ||
'prettier/@typescript-eslint', // Uses eslint-config-prettier to disable ESLint rules from @typescript-eslint/eslint-plugin that would conflict with prettier | ||
'prettier', // Uses eslint-config-prettier to disable ESLint rules from @typescript-eslint/eslint-plugin that would conflict with prettier | ||
'plugin:prettier/recommended', // Enables eslint-plugin-prettier and displays prettier errors as ESLint errors. Make sure this is always the last configuration in the extends array. | ||
@@ -8,0 +8,0 @@ ], |
@@ -1,10 +0,6 @@ | ||
module.exports = { | ||
module.exports = { | ||
semi: true, | ||
trailingComma: 'es5', | ||
singleQuote: true, | ||
printWidth: 120, | ||
tabWidth: 4, | ||
useTabs: true, | ||
endOfLine: 'lf', | ||
arrowParens: 'always', | ||
}; |
{ | ||
"name": "browser-line-reader", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "A line by line async file reader for the browser", | ||
@@ -10,4 +10,7 @@ "main": "./src/index.js", | ||
"build": "tsc", | ||
"prepare": "npm run build" | ||
"prepare": "husky install && npm run build" | ||
}, | ||
"engines": { | ||
"node": ">= 12.22" | ||
}, | ||
"repository": { | ||
@@ -32,28 +35,22 @@ "type": "git", | ||
"devDependencies": { | ||
"@types/jest": "^26.0.9", | ||
"@typescript-eslint/eslint-plugin": "^3.8.0", | ||
"@typescript-eslint/parser": "^3.8.0", | ||
"eslint": "^7.6.0", | ||
"eslint-config-prettier": "^6.11.0", | ||
"eslint-plugin-prettier": "^3.1.4", | ||
"husky": "^4.2.5", | ||
"jest": "^26.3.0", | ||
"lint-staged": "^10.2.11", | ||
"prettier": "^2.0.5", | ||
"ts-jest": "^26.1.4", | ||
"typescript": "^3.9.7" | ||
"@types/jest": "^29.2.3", | ||
"@typescript-eslint/eslint-plugin": "^5.44.0", | ||
"@typescript-eslint/parser": "^5.44.0", | ||
"eslint": "^8.28.0", | ||
"eslint-config-prettier": "^8.5.0", | ||
"eslint-plugin-prettier": "^4.2.1", | ||
"husky": "^8.0.2", | ||
"jest": "^29.3.1", | ||
"jest-environment-jsdom": "^29.3.1", | ||
"lint-staged": "^13.0.3", | ||
"ls-engines": "^0.7.0", | ||
"prettier": "^2.8.0", | ||
"ts-jest": "^29.0.3", | ||
"typescript": "^4.9.3" | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "lint-staged" | ||
} | ||
}, | ||
"lint-staged": { | ||
"*.ts": [ | ||
"*.{js,ts}": [ | ||
"eslint --fix", | ||
"prettier --write", | ||
"git add" | ||
], | ||
"*.js": [ | ||
"eslint --fix", | ||
"git add" | ||
] | ||
@@ -63,14 +60,11 @@ }, | ||
"transform": { | ||
"^.+\\.tsx?$": "ts-jest" | ||
"^.+\\.ts$": "ts-jest" | ||
}, | ||
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$", | ||
"testRegex": "./tests/.*\\.spec\\.ts$", | ||
"moduleFileExtensions": [ | ||
"ts", | ||
"tsx", | ||
"js", | ||
"jsx", | ||
"json", | ||
"node" | ||
] | ||
"js" | ||
], | ||
"testEnvironment": "jsdom" | ||
} | ||
} |
# browser-line-reader | ||
An asynchronous line by line file reader for the browser. | ||
This is a project I have been working on since I have found | ||
the lack of asynchronous, promise aware, typescript based file reader | ||
solutions lacking when it comes to the browser. | ||
This project addresses the lack of asynchronous, promise aware, typescript based file reader solutions specifically | ||
crafted for the browser. | ||
This module is designed to read a File object, using the standard | ||
FileReader, one line at a time. In order to achieve this, the file is | ||
actually subject to multiple reads, in "chunks", which can contain | ||
any number of lines. | ||
This module reads a `File` object, using the standard `FileReader`, one line at a time. | ||
In order to achieve this, the file is subject to multiple reads, in 'chunks', which can contain any number of lines. | ||
What this means is that there is no need for the entire file to be stored | ||
in browser memory, and lines can be processed and then discarded. I find | ||
that this is especially useful when reading in large files spanning many | ||
hundreds of megabytes. | ||
The advantage of this method is that there is no need for the entire file to be stored in browser memory, | ||
and lines can be processed and then discarded. | ||
This is especially useful when reading in large files spanning many hundreds of megabytes. | ||
@@ -24,13 +20,39 @@ ## Installation | ||
## Usage | ||
The following types are used in the type signatures below: | ||
Currently, there is only one function accessible, ```readLines```, | ||
which will read all the lines of a file, one by one. The usage | ||
can be described by the below code example: | ||
```typescript | ||
interface Options { | ||
encoding?: string; | ||
} | ||
type LineReaderCallback = (line: string) => void; | ||
``` | ||
To get started, import the `LineReader` class as a default export. | ||
```typescript | ||
import LineReader from 'browser-line-reader'; | ||
``` | ||
### constructor | ||
Type: `(file: File) => LineReader` | ||
The `LineReader` class constructor accepting the File object to read. | ||
```typescript | ||
const myFile: File = new File(['My name is...'], 'SlimShady'); | ||
const lineReader: LineReader = new LineReader(myFile); | ||
``` | ||
### readLines | ||
Type: `(callback?: LineReaderCallback) => Promise<number>` | ||
Read all the lines of a file, one by one. | ||
Accepts a callback with one parameter of type `string`, which is the line just read. | ||
Returns a promise containing the total number of lines read. | ||
Throws an error passed through from the internal `FileReader.prototype.onerror` event. | ||
```typescript | ||
lineReader.readLines((line: string) => { | ||
// Efficiently store this line in a good data structure | ||
console.log(line); | ||
@@ -44,20 +66,22 @@ }).then((numLinesRead: number) => { | ||
- The line reader accepts one argument of type ```File```, | ||
which is the File object to read. | ||
- The ```readLines``` function accepts a callback with | ||
one parameter of type ```string```, which is the line just read. | ||
- The ```readLines``` function returns a promise with return type | ||
```Promise<number|string>```, where on success, the total number of | ||
lines read is returned, and on failure, an error message. | ||
### readNLines | ||
Type: `(nLines: number, callback?: LineReaderCallback) => Promise<number>` | ||
## TODO | ||
Read the first `n` lines of a file of `k` lines, or all lines if `n > k` or `n < 0`. | ||
Return and exceptions are identical to `Linereader.prototype.readLines`. | ||
There are a number of items I would still like to implement. | ||
This list is growing and feel free to send me suggestions if you | ||
find this tool useful. | ||
```typescript | ||
lineReader.readNLines(10, (line: string) => { | ||
console.log(line); | ||
}); | ||
``` | ||
## Missing Features | ||
- Support for different kinds of line separators | ||
- Support for separate read header action | ||
- Support for reading a certain number of lines | ||
- Efficiency optimisations | ||
- Benchmarking against standard FileReader API | ||
- Benchmarking against standard FileReader API | ||
- Add contributing guidelines | ||
Please suggest or implement these or any other features you feel are missing. |
@@ -5,4 +5,4 @@ "use strict"; | ||
function LineReader(file, options) { | ||
if (options === void 0) { options = { encoding: 'UTF-8' }; } | ||
var _this = this; | ||
if (options === void 0) { options = { encoding: 'UTF-8' }; } | ||
this.fileReader = new FileReader(); | ||
@@ -19,2 +19,5 @@ this.readPosition = 0; | ||
LineReader.prototype.readLines = function (callback) { | ||
return this.readNLines(-1, callback); | ||
}; | ||
LineReader.prototype.readNLines = function (nLines, callback) { | ||
var _this = this; | ||
@@ -25,9 +28,12 @@ var count = 0; | ||
var size = lines.length; | ||
if (typeof callback === 'function') { | ||
var index = -1; | ||
while (++index < size) { | ||
var index = -1; | ||
while (++index < size && (count < nLines || nLines < 0)) { | ||
count++; | ||
if (typeof callback === 'function') { | ||
callback(lines[index]); | ||
} | ||
} | ||
count += size; | ||
if (count === nLines) { | ||
_this.emit('end'); | ||
} | ||
_this.step(); | ||
@@ -34,0 +40,0 @@ }); |
export interface Options { | ||
encoding?: string; | ||
} | ||
export type LineReaderCallback = (line: string) => void; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13800
13
137
86
14