![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
as-expression
Advanced tools
Inline any chunk of JavaScript, including statements (like if
/else if
/else
chains) as an expression. No more defining variables that get conditionally set!
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 }
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.)
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.
FAQs
Inline any chunk of JS code, including statements, as an expression
The npm package as-expression receives a total of 0 weekly downloads. As such, as-expression popularity was classified as not popular.
We found that as-expression demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.