Skip to content

Commit

Permalink
Add proper exports support and docs in readme
Browse files Browse the repository at this point in the history
  • Loading branch information
noahm committed Oct 19, 2023
1 parent beff450 commit 8c64aa7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
37 changes: 36 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

Original parsing code from [city41/stepcharts](https://github.com/city41/stepcharts). Props to Matt for building a really sweet site.

Only works in node (server-side) for now but browser support is on the radar.
Works both in node (server-side or CLI) and in browser. Bun and Deno support is untested, but an interesting future to explore!

## Usage

Install with `npm install --save simfile-parser` or `yarn add simfile-parser`

```ts
// in node.js >= 16.9.0

import {
parseAllPacks,
parsePack,
Expand All @@ -34,3 +36,36 @@ calculateStats(aGreatSong.charts["single-challenge"]);
}
*/
```

### Browser support

Support dragging packs directly into a web app by parsing in-browser!

```ts
// requires typescript 5.0 in "Bundler" module resolution mode for typings
import { parsePack } from "simfile-parser/browser";

// necessary to enable data drops
document.body.addEventListener("dragover", function (e) {
e.preventDefault();
});

document.body.addEventListener("drop", async function (e) {
// also necessary to prevent browser navigating to dropped folder
evt.preventDefault();
if (!evt.dataTransfer) {
return;
}
if (evt.dataTransfer.items.length !== 1) {
console.error("too many items dropped, try just one folder");
return;
}

try {
const pack = await parsePack(evt.dataTransfer.items[0]);
console.log(`parsed pack "${pack.name}" with ${pack.songCount} songs`);
} catch (e) {
console.error(e);
}
});
```
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
"main": "./dist/main.js",
"browser": "./dist/browser/index.js",
"types": "./dist/main.d.ts",
"exports": {
".": "./dist/main.js",
"./browser": "./dist/browser/index.js"
},
"bin": "./dist/cli.js",
"sideEffects": "false",
"scripts": {
Expand Down

0 comments on commit 8c64aa7

Please sign in to comment.