1.0.5 • Published 3 years ago

simple-chunk-reader v1.0.5

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

simple-chunk-reader

Simple, buffered, chunk-by-chunk file reader with customizable buffer size.

Install

npm install simple-chunk-reader
yarn add simple-chunk-reader

Usage

const ChunkReader = require("simple-chunk-reader");

const filePath = "./somefile.txt";
const chunkSize = 1024;
const reader = new ChunkReader(filePath, chunkSize);

let content = "";
while (!reader.isDone) {
  content += reader.read();
}

API

The module exports the following functions:

constructor

  • new ChunkReader(path: string, size: number): ChunkReader
ParameterTypeDescription
pathstringThe path or location of your file
sizenumberChunk/buffer size in bytes, default: 1024 (1 kB)

read

  • read(): string

It returns a next chunk of current file stream.

Syntax:

const ChunkReader = require("simple-chunk-reader");

const filePath = "./file.txt";
const chunkSize = 8;
const reader = new ChunkReader(filePath, chunkSize);

while (!reader.isDone) {
  console.log(reader.read());
}

./file.txt

aaaabbbbccccddddeeeeffffgggghhhhiiiijjjjkkkkllllmmmmnnnnoooo

Output:

aaaabbbb
ccccdddd
eeeeffff
gggghhhh
iiiijjjj
kkkkllll
mmmmnnnn
oooo

readMultiple

  • readMultiple(total: number): string[]

It returns a next multiple chunk of current file stream.

Syntax:

const ChunkReader = require("simple-chunk-reader");

const filePath = "./file.txt";
const chunkSize = 8;
const reader = new ChunkReader(filePath, chunkSize);

while (!reader.isDone) {
  console.log(reader.readMultiple(3));
}

./file.txt

aaaabbbbccccddddeeeeffffgggghhhhiiiijjjjkkkkllllmmmmnnnnoooo

Output:

['aaaabbbb', 'ccccdddd', 'eeeeffff']
['gggghhhh', 'iiiijjjj', 'kkkkllll']
['mmmmnnnn', 'oooo']

close

  • close(): void

Close the current file descriptor thereby clearing the file stream that is associated with it.

You need to call this method only if you are done before reading the whole content stream. read() and readMutiple() will call this function automatically when reaching the end of the stream.

Syntax:

const ChunkReader = require("simple-chunk-reader");

const filePath = "./file.txt";
const chunkSize = 8;
const reader = new ChunkReader(filePath, chunkSize);

console.log(reader.readMultiple(2));
console.log(reader.readMultiple(4));
console.log(reader.read());

reader.close();

./file.txt

aaaabbbbccccddddeeeeffffgggghhhhiiiijjjjkkkkllllmmmmnnnnoooo

Output:

['aaaabbbb', 'ccccdddd']
['eeeeffff', 'gggghhhh', 'iiiijjjj', 'kkkkllll']
mmmmnnnn
1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago