Skip to content

Commit

Permalink
deno and node ESM tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SheetJSDev committed Feb 13, 2022
1 parent 46360a1 commit f58dd36
Show file tree
Hide file tree
Showing 26 changed files with 5,124 additions and 58 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/deno.yml
@@ -0,0 +1,32 @@
name: 'Tests: deno 1.x'

on: [pull_request, push]

jobs:
# small test
misc:
runs-on: ubuntu-latest
env:
FMTS: misc
steps:
- uses: actions/checkout@v2
- uses: denoland/setup-deno@main
with:
deno-version: v1.x
- run: deno test --allow-env --allow-read --allow-write test.ts
# full test
full:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: denoland/setup-deno@main
with:
deno-version: v1.x
- uses: ljharb/actions/node/install@main
with:
node-version: '16.'
- run: sudo curl -Lo /usr/bin/rooster https://github.com/SheetJS/rooster/releases/download/v0.2.0/rooster-v0.2.0-linux-amd64
- run: sudo chmod a+x /usr/bin/rooster
- run: make init
- run: 'cd test_files; make all; cd -'
- run: deno test --allow-env --allow-read --allow-write test.ts
5 changes: 5 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,11 @@ This log is intended to keep track of backwards-incompatible changes, including
but not limited to API changes and file location changes. Minor behavioral
changes may not be included if they are not expected to break existing code.

## v0.18.1

* Removed Node ESM build script and folded into standard ESM build
* Removed undocumented aliases including `make_formulae` and `get_formulae`

## v0.18.0

* Browser scripts only expose `XLSX` variable
Expand Down
18 changes: 18 additions & 0 deletions Makefile
Expand Up @@ -128,13 +128,31 @@ pkg: bin/xlsx.njs xlsx.js ## Build pkg standalone executable
test mocha: test.js ## Run test suite
mocha -R spec -t 30000

.PHONY: test-esm
test-esm: test.mjs ## Run Node ESM test suite
npx mocha -r esm -R spec -t 30000 $<

.PHONY: test-deno
test-deno: test.ts ## Run Deno test suite
deno test --allow-env --allow-read --allow-write $<

#* To run tests for one format, make test_<fmt>
#* To run the core test suite, make test_misc
TESTFMT=$(patsubst %,test_%,$(FMT))
.PHONY: $(TESTFMT)
$(TESTFMT): test_%:
FMTS=$* make test

TESTESMFMT=$(patsubst %,test-esm_%,$(FMT))
.PHONY: $(TESTESMFMT)
$(TESTESMFMT): test-esm_%:
FMTS=$* make test-esm

TESTDENOFMT=$(patsubst %,test-deno_%,$(FMT))
.PHONY: $(TESTESMFMT)
$(TESTDENOFMT): test-deno_%:
FMTS=$* make test-deno

.PHONY: travis
travis: ## Run test suite with minimal output
mocha -R dot -t 30000
Expand Down
45 changes: 43 additions & 2 deletions README.md
Expand Up @@ -177,10 +177,15 @@ $ bower install js-xlsx

**Deno**

The [`sheetjs`](https://deno.land/x/sheetjs) package is available on deno:
The [`sheetjs`](https://deno.land/x/sheetjs) package is hosted by Deno:

```ts
// @deno-types="https://deno.land/x/sheetjs/types/index.d.ts"
import * as XLSX from 'https://deno.land/x/sheetjs/xlsx.mjs'

/* load the codepage support library for extended support with older formats */
import * as cptable from 'https://deno.land/x/sheetjs/dist/cpexcel.full.mjs';
XLSX.set_cptable(cptable);
```

**NodeJS**
Expand Down Expand Up @@ -211,7 +216,7 @@ import * as cpexcel from 'xlsx/dist/cpexcel.full.mjs';
XLSX.set_cptable(cpexcel);
```

**PhotoShop and InDesign**
**Photoshop and InDesign**

`dist/xlsx.extendscript.js` is an ExtendScript build for Photoshop and InDesign
that is included in the `npm` package. It can be directly referenced with a
Expand Down Expand Up @@ -538,9 +543,11 @@ The [`demos` directory](demos/) includes sample projects for:
- [`webpack 2.x`](demos/webpack/)

**Platforms and Integrations**
- [`deno`](demos/deno/)
- [`electron application`](demos/electron/)
- [`nw.js application`](demos/nwjs/)
- [`Chrome / Chromium extensions`](demos/chrome/)
- [`Download a Google Sheet locally`](demos/google-sheet/)
- [`Adobe ExtendScript`](demos/extendscript/)
- [`Headless Browsers`](demos/headless/)
- [`canvas-datagrid`](demos/datagrid/)
Expand Down Expand Up @@ -609,6 +616,23 @@ const workbook = read(buf);

</details>

<details>
<summary><b>Local file in a Deno application</b> (click to show)</summary>

`readFile` uses `Deno.readFileSync` under the hood:

```js
// @deno-types="https://deno.land/x/sheetjs/types/index.d.ts"
import * as XLSX from 'https://deno.land/x/sheetjs/xlsx.mjs'

const workbook = XLSX.readFile("test.xlsx");
```

Applications reading files must be invoked with the `--allow-read` flag. The
[`deno` demo](demos/deno/) has more examples

</details>

<details>
<summary><b>User-submitted file in a web page ("Drag-and-Drop")</b> (click to show)</summary>

Expand Down Expand Up @@ -1467,6 +1491,23 @@ const workbook = writeFileSync("out.xlsb", buf);

</details>

<details>
<summary><b>Local file in a Deno application</b> (click to show)</summary>

`writeFile` uses `Deno.writeFileSync` under the hood:

```js
// @deno-types="https://deno.land/x/sheetjs/types/index.d.ts"
import * as XLSX from 'https://deno.land/x/sheetjs/xlsx.mjs'

XLSX.writeFile(workbook, "test.xlsx");
```

Applications writing files must be invoked with the `--allow-write` flag. The
[`deno` demo](demos/deno/) has more examples

</details>

<details>
<summary><b>Local file in a PhotoShop or InDesign plugin</b> (click to show)</summary>

Expand Down
2 changes: 1 addition & 1 deletion bits/05_buf.js
Expand Up @@ -66,7 +66,7 @@ function utf8decode(content/*:string*/) {
o[widx++] = (128|(c&63));
} else if(c >= 0xD800 && c < 0xE000) {
c = (c&1023)+64;
var d = str.charCodeAt(++ridx)&1023;
var d = content.charCodeAt(++ridx)&1023;
o[widx++] = (240|((c>>8)&7));
o[widx++] = (128|((c>>2)&63));
o[widx++] = (128|((d>>6)&15)|((c&3)<<4));
Expand Down
3 changes: 2 additions & 1 deletion bits/23_binutils.js
Expand Up @@ -63,7 +63,7 @@ if(has_buf/*:: && typeof Buffer !== 'undefined'*/) {
}

/* from js-xls */
if(typeof cptable !== 'undefined') {
function cpdoit() {
__utf16le = function(b/*:RawBytes|CFBlob*/,s/*:number*/,e/*:number*/) { return cptable.utils.decode(1200, b.slice(s,e)).replace(chr0, ''); };
__utf8 = function(b/*:RawBytes|CFBlob*/,s/*:number*/,e/*:number*/) { return cptable.utils.decode(65001, b.slice(s,e)); };
__lpstr = function(b/*:RawBytes|CFBlob*/,i/*:number*/) { var len = __readUInt32LE(b,i); return len > 0 ? cptable.utils.decode(current_ansi, b.slice(i+4, i+4+len-1)) : "";};
Expand All @@ -72,6 +72,7 @@ if(typeof cptable !== 'undefined') {
__lpp4 = function(b/*:RawBytes|CFBlob*/,i/*:number*/) { var len = __readUInt32LE(b,i); return len > 0 ? cptable.utils.decode(1200, b.slice(i+4,i+4+len)) : "";};
__8lpp4 = function(b/*:RawBytes|CFBlob*/,i/*:number*/) { var len = __readUInt32LE(b,i); return len > 0 ? cptable.utils.decode(65001, b.slice(i+4,i+4+len)) : "";};
}
if(typeof cptable !== 'undefined') cpdoit();

var __readUInt8 = function(b/*:RawBytes|CFBlob*/, idx/*:number*/)/*:number*/ { return b[idx]; };
var __readUInt16LE = function(b/*:RawBytes|CFBlob*/, idx/*:number*/)/*:number*/ { return (b[idx+1]*(1<<8))+b[idx]; };
Expand Down
12 changes: 6 additions & 6 deletions bits/75_xlml.js
Expand Up @@ -585,19 +585,19 @@ function parse_xlml_xml(d, _opts)/*:Workbook*/ {
break;
case 'header' /*case 'Header'*/:
if(!cursheet['!margins']) default_margins(cursheet['!margins']={}, 'xlml');
cursheet['!margins'].header = parsexmltag(Rn[0]).Margin;
if(!isNaN(+parsexmltag(Rn[0]).Margin)) cursheet['!margins'].header = +parsexmltag(Rn[0]).Margin;
break;
case 'footer' /*case 'Footer'*/:
if(!cursheet['!margins']) default_margins(cursheet['!margins']={}, 'xlml');
cursheet['!margins'].footer = parsexmltag(Rn[0]).Margin;
if(!isNaN(+parsexmltag(Rn[0]).Margin)) cursheet['!margins'].footer = +parsexmltag(Rn[0]).Margin;
break;
case 'pagemargins' /*case 'PageMargins'*/:
var pagemargins = parsexmltag(Rn[0]);
if(!cursheet['!margins']) default_margins(cursheet['!margins']={},'xlml');
if(pagemargins.Top) cursheet['!margins'].top = pagemargins.Top;
if(pagemargins.Left) cursheet['!margins'].left = pagemargins.Left;
if(pagemargins.Right) cursheet['!margins'].right = pagemargins.Right;
if(pagemargins.Bottom) cursheet['!margins'].bottom = pagemargins.Bottom;
if(!isNaN(+pagemargins.Top)) cursheet['!margins'].top = +pagemargins.Top;
if(!isNaN(+pagemargins.Left)) cursheet['!margins'].left = +pagemargins.Left;
if(!isNaN(+pagemargins.Right)) cursheet['!margins'].right = +pagemargins.Right;
if(!isNaN(+pagemargins.Bottom)) cursheet['!margins'].bottom = +pagemargins.Bottom;
break;
case 'displayrighttoleft' /*case 'DisplayRightToLeft'*/:
if(!Workbook.Views) Workbook.Views = [];
Expand Down
1 change: 1 addition & 0 deletions bits/87_read.js
Expand Up @@ -67,6 +67,7 @@ function readSync(data/*:RawData*/, opts/*:?ParseOpts*/)/*:Workbook*/ {
reset_cp();
var o = opts||{};
if(typeof ArrayBuffer !== 'undefined' && data instanceof ArrayBuffer) return readSync(new Uint8Array(data), (o = dup(o), o.type = "array", o));
if(typeof Uint8Array !== 'undefined' && data instanceof Uint8Array && !o.type) o.type = typeof Deno !== "undefined" ? "buffer" : "array";
var d = data, n = [0,0,0,0], str = false;
if(o.cellStyles) { o.cellNF = true; o.sheetStubs = true; }
_ssfopts = {};
Expand Down
8 changes: 7 additions & 1 deletion bits/88_write.js
Expand Up @@ -48,7 +48,12 @@ function write_zip_type(wb/*:Workbook*/, opts/*:?WriteOpts*/)/*:any*/ {
default: throw new Error("Unrecognized type " + o.type);
}
var out = z.FullPaths ? CFB.write(z, {fileType:"zip", type: /*::(*/{"nodebuffer": "buffer", "string": "binary"}/*:: :any)*/[oopts.type] || oopts.type, compression: !!o.compression}) : z.generate(oopts);
if(typeof Deno !== "undefined" && typeof out == "string") out = new Uint8Array(s2ab(out));
if(typeof Deno !== "undefined") {
if(typeof out == "string") {
if(o.type == "binary" || o.type == "base64") return out;
out = new Uint8Array(s2ab(out));
}
}
/*jshint -W083 */
if(o.password && typeof encrypt_agile !== 'undefined') return write_cfb_ctr(encrypt_agile(out, o.password), o); // eslint-disable-line no-undef
/*jshint +W083 */
Expand All @@ -72,6 +77,7 @@ function write_string_type(out/*:string*/, opts/*:WriteOpts*/, bom/*:?string*/)/
case "file": return write_dl(opts.file, o, 'utf8');
case "buffer": {
if(has_buf) return Buffer_from(o, 'utf8');
else if(typeof TextEncoder !== "undefined") return new TextEncoder().encode(o);
else return write_string_type(o, {type:'binary'}).split("").map(function(c) { return c.charCodeAt(0); });
}
}
Expand Down
4 changes: 0 additions & 4 deletions bits/90_utils.js
Expand Up @@ -252,10 +252,6 @@ var utils/*:any*/ = {
decode_cell: decode_cell,
decode_range: decode_range,
format_cell: format_cell,
get_formulae: sheet_to_formulae,
make_csv: sheet_to_csv,
make_json: sheet_to_json,
make_formulae: sheet_to_formulae,
sheet_add_aoa: sheet_add_aoa,
sheet_add_json: sheet_add_json,
sheet_add_dom: sheet_add_dom,
Expand Down
1 change: 1 addition & 0 deletions demos/README.md
Expand Up @@ -40,6 +40,7 @@ can be installed with Bash on Windows or with `cygwin`.
- [`webpack 2.x`](webpack/)

**Platforms and Integrations**
- [`deno`](deno/)
- [`electron application`](electron/)
- [`nw.js application`](nwjs/)
- [`Chrome / Chromium extensions`](chrome/)
Expand Down
1 change: 1 addition & 0 deletions demos/deno/.gitignore
@@ -0,0 +1 @@
sheet2csv
5 changes: 5 additions & 0 deletions demos/deno/Makefile
@@ -1,8 +1,13 @@
TESTS= x mjs jspm
UNSTABLE= node

.PHONY: test
test: $(UNSTABLE) $(TESTS)

.PHONY: sheet2csv
sheet2csv: sheet2csv.ts
deno compile -r --allow-read $<

$(TESTS): %: %.ts doit.ts
deno run --allow-read --allow-write $<

Expand Down
14 changes: 14 additions & 0 deletions demos/deno/README.md
Expand Up @@ -37,12 +37,26 @@ Deno.writeFileSync("test.xlsb", u8);

## Demos

**Complete Example**

`sheet2csv.ts` is a complete command-line tool for generating CSV text from
workbooks. Building the application is incredibly straightforward:

```bash
$ deno compile -r --allow-read sheet2csv.ts # build the sheet2csv binary
$ ./sheet2csv test.xlsx # print the first worksheet as CSV
$ ./sheet2csv test.xlsx s5s # print worksheet "s5s" as CSV
```

**Module Import Scenarios**

All demos attempt to read a file and write a new file. [`doit.ts`](./doit.ts)
accepts the `XLSX` module as an argument.

- `x` imports the ESM build without the codepage library:

```ts
// @deno-types="https://deno.land/x/sheetjs/types/index.d.ts"
import * as XLSX from 'https://deno.land/x/sheetjs/xlsx.mjs';
```

Expand Down
21 changes: 21 additions & 0 deletions demos/deno/sheet2csv.ts
@@ -0,0 +1,21 @@
/*! sheetjs (C) 2013-present SheetJS -- http://sheetjs.com */
// @deno-types="https://deno.land/x/sheetjs/types/index.d.ts"
import * as XLSX from 'https://deno.land/x/sheetjs/xlsx.mjs';
import * as cptable from 'https://deno.land/x/sheetjs/dist/cpexcel.full.mjs';
XLSX.set_cptable(cptable);

const filename = Deno.args[0];
if(!filename) {
console.error("usage: sheet2csv <filename> [sheetname]");
Deno.exit(1);
}

const workbook = XLSX.readFile(filename);
const sheetname = Deno.args[1] || workbook.SheetNames[0];

if(!workbook.Sheets[sheetname]) {
console.error(`error: workbook missing sheet ${sheetname}`);
Deno.exit(1);
}

console.log(XLSX.utils.sheet_to_csv(workbook.Sheets[sheetname]));
9 changes: 7 additions & 2 deletions docbits/10_install.md
Expand Up @@ -61,10 +61,15 @@ $ bower install js-xlsx

**Deno**

The [`sheetjs`](https://deno.land/x/sheetjs) package is available on deno:
The [`sheetjs`](https://deno.land/x/sheetjs) package is hosted by Deno:

```ts
// @deno-types="https://deno.land/x/sheetjs/types/index.d.ts"
import * as XLSX from 'https://deno.land/x/sheetjs/xlsx.mjs'

/* load the codepage support library for extended support with older formats */
import * as cptable from 'https://deno.land/x/sheetjs/dist/cpexcel.full.mjs';
XLSX.set_cptable(cptable);
```

**NodeJS**
Expand Down Expand Up @@ -95,7 +100,7 @@ import * as cpexcel from 'xlsx/dist/cpexcel.full.mjs';
XLSX.set_cptable(cpexcel);
```

**PhotoShop and InDesign**
**Photoshop and InDesign**

`dist/xlsx.extendscript.js` is an ExtendScript build for Photoshop and InDesign
that is included in the `npm` package. It can be directly referenced with a
Expand Down
2 changes: 2 additions & 0 deletions docbits/16_demos.md
Expand Up @@ -25,9 +25,11 @@ The [`demos` directory](demos/) includes sample projects for:
- [`webpack 2.x`](demos/webpack/)

**Platforms and Integrations**
- [`deno`](demos/deno/)
- [`electron application`](demos/electron/)
- [`nw.js application`](demos/nwjs/)
- [`Chrome / Chromium extensions`](demos/chrome/)
- [`Download a Google Sheet locally`](demos/google-sheet/)
- [`Adobe ExtendScript`](demos/extendscript/)
- [`Headless Browsers`](demos/headless/)
- [`canvas-datagrid`](demos/datagrid/)
Expand Down
17 changes: 17 additions & 0 deletions docbits/20_import.md
Expand Up @@ -56,6 +56,23 @@ const workbook = read(buf);

</details>

<details>
<summary><b>Local file in a Deno application</b> (click to show)</summary>

`readFile` uses `Deno.readFileSync` under the hood:

```js
// @deno-types="https://deno.land/x/sheetjs/types/index.d.ts"
import * as XLSX from 'https://deno.land/x/sheetjs/xlsx.mjs'

const workbook = XLSX.readFile("test.xlsx");
```

Applications reading files must be invoked with the `--allow-read` flag. The
[`deno` demo](demos/deno/) has more examples

</details>

<details>
<summary><b>User-submitted file in a web page ("Drag-and-Drop")</b> (click to show)</summary>

Expand Down

0 comments on commit f58dd36

Please sign in to comment.