Skip to content
This repository has been archived by the owner on Dec 13, 2019. It is now read-only.
/ file-reader Public archive

Base classes for read big file write by JavaScript

Notifications You must be signed in to change notification settings

Datafruit/file-reader

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status npm version

中文

About

The FileReader class provides native file-reading capabilities for modern browsers.

Sometimes we will read out the needs from large files. such as read a part of the text file to display on the browser. At this point need to read the file as fragmented to avoid the browser stuck.

FileReader is an asynchronous interface, so in the concurrent read need a program to manage these concurrent, pass the contents of the slices to the next operation with order.

You can wait for a slice finished then reading the next piece. because JavaScript is a single-threaded language, so the two ways not essentially different.

Install

npm install next-reader --save-dev

Reader

All Reader extends of BaseReader

ReaderOptions

  • {String} encode - default utf8
  • {Number} read_size - default file.size
  • {Number} concurrency - default 1
  • {Number} chunk_size - file's encode. default utf8, only supportutf8,default 1 << 16

BrowserFileReader(file, [options])

Params

Simple

import { BrowserFileReader } from 'next-reader'
const target = document.querySelector('#file')
target.onchange = () => {
  const file = target.files[0]
  const reader = new BrowserFileReader(file)
  reader.subscribe(
    (data, sequence) => console.log('onNext::', data, sequence),
    (error, already, read_size) => console.log('onError::', error, already, read_size),
    () => console.log('onCompleted')
  )
  reader.read()    // same as reader.readAsArrayBuffer()
  // or
  reader.readAsArrayBuffer()
  // or
  reader.readAsText()
  // or
  reader.readAsDataURL()
}

LineReaderOptions

  • {Boolean} ignore_line_break - default true

ReadLineAsUint8(file, [options])

Params

Simple

import { ReadLineAsUint8 } from 'next-reader'
const Type = ReadLineAsUint8.Type
const target = document.querySelector('#file')
target.onchange = () => {
  const file = target.files[0]
  const reader = new ReadLineAsUint8(file)
  reader.subscribe(
    (record) => {
      const {type, no, size, data} = record   
      if (type === Type.lines) {
        console.log(data.forEach(d => console.log(d)))
      }else if (type ===Type.line) {
        console.log(data, no, size)
      }
    },
    (error, already, read_size) => console.log('onError::', error, already, read_size),
    () => console.log('onCompleted')
  )
  reader.read()
}

ReadLineAsString(file, [options])

Params

Simple is same as ReadLineAsUint8

ReadCsvAsUint8(file, [options])

Params

Simple

import { ReadCsvAsUint8 } from 'next-reader'
const target = document.querySelector('#file')
target.onchange = () => {
  const file = target.files[0]
  const reader = new ReadCsvAsUint8(file)
  reader.subscribe(
    (record) => {
      const { lines, size } = record
      lines.forEach(record => {
        console.log(record.fields, record.size, record.no)
      })
      console.log(size)
    },
    (error, already, read_size) => console.log('onError::', error, already, read_size),
    () => console.log('onCompleted')
  )
  reader.read()
}

ReadCsvWithLines(file, lines, [options])

Params

Simple

import { ReadCsvWithLines } from 'next-reader'
const target = document.querySelector('#file')
target.onchange = () => {
  const file = target.files[0]
  const reader = new ReadCsvWithLines(file, 100)
  reader.subscribe(
    (record) => {
     const { lines, size } = record
       let total = 0
       lines.forEach(record => {
         console.log('No. %s: %s .%s', record.no, record.fields.toString(), record.size)
       })
       console.log('total => %s, size => %s', total, size)
    },
    (error, already, read_size) => console.log('onError::', error, already, read_size),
    () => console.log('onCompleted')
  )
  reader.read()
}

BaseReader

BaseReader extends of BaseObservable and implements AbstractReader

AbstractReader

Implements AbstractObservable

  • read()
  • pause()
  • resume()
  • enqueue()
  • onReadData()
  • onReadError(error, already, read_size)
  • onReadComplete()
  • readable()
  • validate(data)
  • result(data)

BaseObservable

Implements of AbstractObservable

AbstractObservable

  • subscribe(BaseObserverOrOnNext, onError, onComplete)
  • subscribeOnNext(...args)
  • subscribeOnError(...args)
  • subscribeOnComplete(...args)

BaseObserver

Implements AbstractObserver

AbstractObserver

  • onNext(...args)
  • onError(...args)
  • onComplete(...args)
  • destroy()
  • isDestroy()

License

MIT