Skip to content

fisker/promisify-file

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Promisify File

Greenkeeper badge

Usage

Basic syntax

import PromisifyFile from 'promisify-file'
;(async () => {
  const testFile = new File(
    ['hello from promisify-file.txt'],
    'promisify-file.txt',
    {
      type: 'text/plain',
      lastModified: Date.now(),
    },
  )

  const promisifyFile = new PromisifyFile(file)

  try {
    console.log(await promisifyFile.text()) // print `hello from promisify-file.txt`
  } catch (err) {
    console.error(err)
  }
})()

options

{
  name: fileName,
  type: fileMimeType,
  encoding: textFileEncoding,
  quality: imageFileQuality,
  lastModified: fileLastModified
}

constructor

syntax:

let promisifyFile = new PromisifyFile(data, options)

data must be a Blob(include File), for more data type support see PromisifyFile.from

static methods

PromisifyFile.from

returns a Promise that resolves with a PromisifyFile Object

syntax:

let promisifyFile = await PromisifyFile.from(data, options)

supported data types:

methods

PromisifyFile.prototype.{arrayBuffer, text, dataURL, binaryString}

returns a Promise that resolves with the result FileReader.readAs{ArrayBuffer, Text, DataURL, BinaryString}

syntax:

promisifyFile.arrayBuffer()

// optional `encoding` use in `FileReader.readAsText`
promisifyFile.text([encoding])

promisifyFile.dataURL()

promisifyFile.binaryString()

Notice: result of arrayBuffer / text / dataURL / binaryString is cached

PromisifyFile.prototype.{int8Array, uint8Array, uint8ClampedArray, int16Array, uint16Array, int32Array, uint32Array, float32Array, float64Array}

returns a Promise that resolves with a TypedArray created with blob arrayBuffer

optional arguments byteOffset and length same as TypedArray syntax

syntax:

promisifyFile.int8Array([byteOffset [, length]])

promisifyFile.uint8Array([byteOffset [, length]])

promisifyFile.uint8ClampedArray([byteOffset [, length]])

promisifyFile.int16Array([byteOffset [, length]])

promisifyFile.uint16Array([byteOffset [, length]])

promisifyFile.uint32Array([byteOffset [, length]])

promisifyFile.float32Array([byteOffset [, length]])

promisifyFile.float64Array([byteOffset [, length]])

links:

PromisifyFile.prototype.dataView

returns a Promise that resolves with a DataView created with blob arrayBuffer

optional arguments byteOffset and length same as DataView syntax

syntax:

promisifyFile.dataView([byteOffset [, length]])

links:

PromisifyFile.prototype.blob

returns a Promise that resolves with a new Blob created with orignal blob

optional argument options same as Blob syntax

syntax:

promisifyFile.blob([options]])

links:

PromisifyFile.prototype.file

returns a Promise that resolves with a new File created with orignal blob

optional arguments name and options same as File syntax

syntax:

promisifyFile.file([name [, options]])

Notice: if name is omitted, and the orignal Blob don't has a name. a TypeError throws

links:

PromisifyFile.prototype.json

returns a Promise that resolves with the result of parsing blob text as JSON

syntax:

promisifyFile.json([encoding [, reviver]])

links:

PromisifyFile.prototype.url

returns a Promise that resolves with the result of URL.createObjectURL create with blob

syntax:

promisifyFile.url()

links:

PromisifyFile.prototype.image

returns a Promise that resolves with loaded HTMLImageElement create with blob

syntax:

promisifyFile.image()

links:

PromisifyFile.prototype.imageBitmap

returns a Promise that resolves with ImageBitmap that createImageBitmap returns

syntax:

promisifyFile.imageBitmap([options])

promisifyFile.imageBitmap(sx, sy, sw, sh[, options])

links:

PromisifyFile.prototype.imageData

returns a Promise that resolves with ImageData containing the image data of orignal image blob

syntax:

promisifyFile.imageData([sx = 0[, sy = 0[, sw = image.width [, sh = image.height]]]])

links:

PromisifyFile.prototype.{document, xml, svg, html}

returns a Promise that resolves with DOMParser.parseFromString parsing blob text

syntax:

promisifyFile.document([encoding [, overrideMimeType]])
promisifyFile.xml([encoding]) // shortcut for promisifyFile.document(encoding, 'application/xml')
promisifyFile.svg([encoding]) // shortcut for promisifyFile.document(encoding, 'image/svg+xml')
promisifyFile.html([encoding]) // shortcut for promisifyFile.document(encoding, 'text/html')

Notice: Note that if the parsing process fails, the DOMParser does not throw an exception, but PromisifyFile.document throws an Error

links:

relative projects