npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

wastyle

v0.0.5

Published

AStyle code formatter compiled to WebAssembly, for Node.js and the browser.

Downloads

254

Readme

WAstyle

AStyle code formatter compiled to WebAssembly, for Node.js and the browser.

Build Status Dependencies Commitizen friendly code style: prettier License

Install

Install via NPM:

$ yarn add wastyle

Usage

Firstly, you need to initialize AStyle WASM library with init function.

Unfortunately this step couldn't be done automatically since you need make Webpack emit the AStyle WASM binary to your dist directory and tell us its URL. Since we support running in browser mainly, so I don't want to write code to detect Node.js to increase your Webpack bundle size. So in both Node.js and Webpack this step must be done.

In Node.js, call init with a Buffer from fs.readFile or fs.readFileSync:

const fs = require("fs");
const util = require("util");
const wastyle = require("wastyle");

util.promisify(fs.readFile)("wastyle/dist/astyle.wasm")
.then(data => wastyle.init(data))
.then(() => console.log("WAstyle is ready!"))
.catch(err => console.error(err));

In Webpack, I recommend you to use file-loader to emit the AStyle WASM binary to your dist directory and get its URL. See NeekSandhu/onigasm#2.

Note that Webpack has a very simple built-in WASM loader, which will load a WASM file (detected by its magic header) as a compiled and instantiated WASM instance. But it doesn't support passing imports to WASM module. So it WILL fail since the AStyle WASM binary needs WASI to run. To disable the build-in WASM loader and force file-loader to produce the file URL, use this rule: (webpack/webpack#6725)

{
  test: /\.wasm$/,
  loader: "file-loader",
  type: "javascript/auto"
}

In your code, initialize WAstyle with:

import { init, format } from "wastyle";
import astyleBinaryUrl from "wastyle/dist/astyle.wasm";

init(astyleBinaryUrl).then(() => console.log("WAstyle is ready!"));

The init function will compile and instantiate the Astyle WASM binary asynchronously. Will throw an error if failed.

After initialization, call the synchronous function format to use Astyle:

const [success, result] = format("#include <cstdio>\nint main(){int 🦄,a,*b=a,c=🦄*2,*d=nullptr;return -1;}", "pad-oper style=google");
console.log(result);
// #include <cstdio>
// int main() {
//     int 🦄, a, *b = a, c = 🦄 * 2, *d = nullptr;
//     return -1;
// }

Besides, you can use wastyle/dist/wastyle-optimize-size.wasm to reduce your Webpack dist's size.

Performance

Tested on Chrome 80.0.3987.122 on an i7-6600U laptop, formatting this C++ file takes 15ms with wastyle/dist/wastyle.wasm and 40ms with wastyle/dist/wastyle-optimize-size.wasm in average of 100 samples.

Development

You'll need emcc to build the Astyle WASM binary. Follow this guide to install it.

$ yarn build      # Build TypeSrript
$ yarn build:wasm # Build WASM

License

This project is licensed under the MIT license.

Astyle code formatter (Artistic Style) is also licensed under the MIT license. You can find its license in the astyle folder.