Initial commit

master
Sven Slootweg 5 years ago
commit d08fa54337

@ -0,0 +1,85 @@
# 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:
```js
"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:
```js
"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:
```js
{ 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](https://github.com/sindresorhus/ama/issues/10#issuecomment-117766328), 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.

@ -0,0 +1,27 @@
"use strict";
const asExpression = require("./");
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);
/*
{ someBoolean: true,
someString: 'This is a dummy object',
someNumber: 2 }
*/

@ -0,0 +1,5 @@
"use strict";
module.exports = function asExpression(func) {
return func();
};

@ -0,0 +1,9 @@
{
"name": "as-expression",
"description": "Inline any chunk of JS code, including statements, as an expression",
"version": "1.0.0",
"main": "index.js",
"repository": "http://git.cryto.net/joepie91/as-expression.git",
"author": "Sven Slootweg <admin@cryto.net>",
"license": "WTFPL OR CC0-1.0"
}
Loading…
Cancel
Save