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

babel-disposable

v0.1.0

Published

[ [GitHub](https://github.com/lyonbot/babel-disposable) | [Demo](https://lyonbot.github.io/babel-disposable) ]

Downloads

6

Readme

babel-disposable

[ GitHub | Demo ]

Concept

Disposable Values

You can add /*#__DISPOSE__*/ magical comment to any objects and arrays, to mark them as disposable.

It's similar to Constant Folding, but is contagious and more aggressive!

We break up the "disposable" values into pieces, and replace each member expressions (like obj.foo.bar) with them. The generated new values will be "disposable" too! You can store it as variables, and continue using it.

Once a variable get disposed, it will disappear from the code.

Disposable Functions

A function can also be disposable. It behaves like Inline Functions of C/C++, but works on JavaScript / TypeScript.

With "disposable" magic and other optimizers like babel-plugin-minify-constant-folding, you can generate concise and readable code, from a patchwork mess.

See example below for more information.

Example

(Deeply) Dispose Objects

const responseSchema = /* #__DISPOSE__ */ {
  fields: {
    name: { type: "string", title: "the movie name" },
    age: { type: "number" },
    tel: { type: "string" },
  },
};

const fieldId = "name";

// const fieldSchema = responseSchema.fields[fieldId];
const { [fieldId]: fieldSchema, ...otherFields } = responseSchema.fields;

print(`Field ${fieldId} is a ${fieldSchema.type}`);
print(fieldSchema);

print("Rest fields: ", Object.keys(otherFields));
const fieldId = "name";
// const fieldSchema = responseSchema.fields[fieldId]

print("Field name is a string");
print(
  /*#__DISPOSE__*/ {
    type: "string",
    title: "the movie name",
  }
);

print("Rest fields: ", /*#__DISPOSE__*/ ["age", "tel"]);

Note: you can insert /*#__DISPOSE__*/ before "name", to make the string literal disposable, and dispose fieldId variable.

Disposable Functions

const responseSchema = /* #__DISPOSE__ */ {
  fields: {
    name: { type: "string", title: "the movie name" },
    age: { type: "number" },
    tel: { type: "string" },
  },
};

function main() {
  return h("div", {}, [
    AutoFormItem("name", responseSchema.fields),
    AutoFormItem("age", responseSchema.fields),
    AutoFormItem("tel", responseSchema.fields),
  ]);
}

/* #__DISPOSE__ */
function AutoInputBox(schema) {
  const { type } = schema;
  if (type === "string") return h("text-input");
  if (type === "number") return h("number-input");
}

/* #__DISPOSE__ */
function AutoFormItem(id, allFields) {
  const field = allFields[id];
  return h("form-item", { title: field.title || id }, [AutoInputBox(field)]);
}
function main() {
  return h("div", {}, [
    h("form-item", { title: "the movie name" }, [h("text-input")]),
    h("form-item", { title: "age" }, [h("number-input")]),
    h("form-item", { title: "tel" }, [h("text-input")]),
  ]);
}

/* ... */

Note: the "disposable" function declarations is omitted in this example output