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 🙏

© 2025 – Pkg Stats / Ryan Hefner

as-expression

v1.0.0

Published

Inline any chunk of JS code, including statements, as an expression

Readme

as-expression

Inline any chunk of JavaScript, including statements (like if/else if/else chains) as an expression. No more defining variables that get conditionally set!

Example

Turns this:

"use strict";

let stringValue = "two";

let numericValue;

if (stringValue === "one") {
	numericValue = 1;
} else if (stringValue === "two") {
	numericValue = 2;
} else if (stringValue === "three") {
	numericValue = 3;
}

let someObject = {
	someBoolean: true,
	someString: "This is a dummy object",
	someNumber: numericValue
};

console.log(someObject);

into this:

"use strict";

const asExpression = require("as-expression");

let stringValue = "two";

let someObject = {
	someBoolean: true,
	someString: "This is a dummy object",
	someNumber: asExpression(() => {
		if (stringValue === "one") {
			return 1;
		} else if (stringValue === "two") {
			return 2;
		} else if (stringValue === "three") {
			return 3;
		}
	})
};

console.log(someObject);

In both cases, the console.log would produce:

{ someBoolean: true,
  someString: 'This is a dummy object',
  someNumber: 2 }

Wait, why an entire library for this? It's like 3 lines of code.

Lines of code don't matter in general, but in this specific case, the point isn't even the complexity - it's the readability.

The point of this library is to function almost like a self-documenting syntax extension; from a glance, you can immediately see what it's supposed to do. It's technically not new syntax, but in how you use it, it does work like it.

You could define an ad-hoc version of this abstraction throughout your code, sure, and it'd have the same benefit. But what would be the point? You'd just end up with 20 copies of the same asExpression helper throughout your codebase and dependency tree, that all do functionally the same thing, but that each take up their own bit of space and need to be reviewed individually.

By having it as a separate package, you'll only ever have one copy of this utility in your codebase, no matter how many dependencies do the same thing. As the API for this library is very unlikely to ever change (and so it'll always remain on the same major version), npm will perfectly deduplicate this library no matter in how many places it is used.

(Of course, this assumes that you don't mess with npm's default behaviour; if you start pinning explicit versions in your package.json - which you should never do, that is what lockfiles are for! - then you may start seeing multiple copies, because npm can no longer deduplicate them without breaking your version constraints.)

API

asExpression(func)

Immediately executes func and returns its return value. This has the effect of giving you a scope that you can return from, with the ability to inline that result in an expression of some sort (like a variable assignment, or an object literal).

That's it.