No Description

Sven Slootweg d08fa54337 Initial commit 3 weeks ago
README.md d08fa54337 Initial commit 3 weeks ago
example.js d08fa54337 Initial commit 3 weeks ago
index.js d08fa54337 Initial commit 3 weeks ago
package.json d08fa54337 Initial commit 3 weeks ago

README.md

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.