Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
catamphetamine committed Nov 11, 2021
1 parent 7384141 commit e0d0226
Show file tree
Hide file tree
Showing 14 changed files with 106 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
* Added [TypeScript](https://github.com/catamphetamine/read-excel-file/issues/71) definitions.
-->

5.2.22 / 11.11.2021
==================

* [Added](https://github.com/catamphetamine/read-excel-file/issues/100) `/web-worker` export

5.2.11 / 08.10.2021
==================

Expand Down
39 changes: 38 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,47 @@ readXlsxFile('/path/to/file').then((rows) => {

// Readable Stream.
readXlsxFile(fs.createReadStream('/path/to/file')).then((rows) => {
...
// `rows` is an array of rows
// each row being an array of cells.
})
```

### Web Worker

```js
const worker = new Worker('web-worker.js')

worker.onmessage = function(event) {
// `event.data` is an array of rows
// each row being an array of cells.
console.log(event.data)
}

worker.onerror = function(event) {
console.error(event.message)
}

const input = document.getElementById('input')

input.addEventListener('change', () => {
worker.postMessage(input.files[0])
})
```

##### `web-worker.js`

```js
import readXlsxFile from 'read-excel-file/web-worker'

onmessage = function(event) {
readXlsxFile(event.data).then((rows) => {
// `rows` is an array of rows
// each row being an array of cells.
postMessage(rows)
})
}
```

## JSON

To convert table rows to JSON objects, pass a `schema` option to `readXlsxFile()`. It will return `{ rows, errors }` object instead of just `rows`.
Expand Down
2 changes: 1 addition & 1 deletion node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"version": "1.0.0",
"main": "index.commonjs.js",
"module": "index.js",
"types.test": "./index.d.ts.test",
"types": "./index.d.ts",
"sideEffects": false
}
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "read-excel-file",
"version": "5.2.21",
"version": "5.2.22",
"description": "Read small to medium `*.xlsx` files in a browser or Node.js. Parse to JSON with a strict schema.",
"module": "index.js",
"sideEffects": false,
"main": "index.commonjs.js",
"types.test": "./index.d.ts.test",
"types": "./index.d.ts",
"scripts": {
"test": "mocha --require @babel/register --colors --bail --reporter spec --require ./test/setup.js \"./{,!(node_modules|commonjs|modules)/**/}*.test.js\" --recursive",
"test-coverage": "istanbul cover -x \"commonjs/**\" -x \"modules/**\" -x \"*.test.js\" node_modules/mocha/bin/_mocha -- --compilers js:babel-core/register --colors --reporter dot --require ./test/setup.js \"./{,!(node_modules|commonjs|modules)/**/}*.test.js\" --recursive",
Expand Down
2 changes: 1 addition & 1 deletion schema/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"version": "1.0.0",
"main": "index.commonjs.js",
"module": "index.js",
"types.test": "./index.d.ts.test",
"types": "./index.d.ts",
"sideEffects": false
}
2 changes: 1 addition & 1 deletion source/read/readXlsxFileNode.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import xml from '../xml/xmlNode'
import xml from '../xml/xml'

import unpackXlsxFile from './unpackXlsxFileNode'
import readXlsxFileContents from './readXlsxFileContents'
Expand Down
16 changes: 16 additions & 0 deletions source/read/readXlsxFileWebWorker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import xml from '../xml/xml'

import unpackXlsxFile from './unpackXlsxFileBrowser'
import readXlsxFileContents from './readXlsxFileContents'

/**
* Reads XLSX file into a 2D array of cells in a web worker.
* @param {file} file - The file.
* @param {object?} options
* @param {(number|string)?} options.sheet - Excel document sheet to read. Defaults to `1`. Will only read this sheet and skip others.
* @return {Promise} Resolves to a 2D array of cells: an array of rows, each row being an array of cells.
*/
export default function readXlsxFile(file, options = {}) {
return unpackXlsxFile(file)
.then((entries) => readXlsxFileContents(entries, xml, options))
}
7 changes: 7 additions & 0 deletions source/xml/xml.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import XMLDOM from '@xmldom/xmldom'

export default {
createDocument(content) {
return new XMLDOM.DOMParser().parseFromString(content)
}
}
13 changes: 13 additions & 0 deletions test/exports.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import readXlsxFileBrowser, { parseExcelDate } from '../index.js'
import readXlsxFileNode, { parseExcelDate as parseExcelDateNode } from '../node'
import readXlsxFileWebWorker, { parseExcelDate as parseExcelDateWebWorker } from '../node'

describe(`exports`, () => {
it(`should export ES6`, () => {
// Browser
readXlsxFileBrowser.should.be.a('function')
parseExcelDate.should.be.a('function')

// Web Worker
readXlsxFileWebWorker.should.be.a('function')
parseExcelDateWebWorker.should.be.a('function')

// Node.js
readXlsxFileNode.should.be.a('function')
parseExcelDateNode.should.be.a('function')
Expand All @@ -21,6 +26,14 @@ describe(`exports`, () => {
Read.default.should.be.a('function')
Read.parseExcelDate.should.be.a('function')

// Web Worker.

const WebWorker = require('../web-worker')

WebWorker.should.be.a('function')
WebWorker.default.should.be.a('function')
WebWorker.parseExcelDate.should.be.a('function')

// Node.js

const Node = require('../node')
Expand Down
6 changes: 6 additions & 0 deletions web-worker/index.commonjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
exports = module.exports = require('../commonjs/read/readXlsxFileWebWorker').default
exports['default'] = require('../commonjs/read/readXlsxFileWebWorker').default
exports.parseExcelDate = require('../commonjs/read/parseDate').default
exports.Integer = require('../commonjs/types/Integer').default
exports.Email = require('../commonjs/types/Email').default
exports.URL = require('../commonjs/types/URL').default
1 change: 1 addition & 0 deletions web-worker/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '../index.d';
5 changes: 5 additions & 0 deletions web-worker/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export { default as default } from '../modules/read/readXlsxFileNode'
export { default as parseExcelDate } from '../modules/read/parseDate'
export { default as Integer } from '../modules/types/Integer'
export { default as Email } from '../modules/types/Email'
export { default as URL } from '../modules/types/URL'
9 changes: 9 additions & 0 deletions web-worker/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"private": true,
"name": "read-excel-file/web-worker",
"version": "1.0.0",
"main": "index.commonjs.js",
"module": "index.js",
"types": "./index.d.ts",
"sideEffects": false
}

0 comments on commit e0d0226

Please sign in to comment.