by justsomeuser · 3y ago · 0 comments
items = (() => {
const results = [];
for (const item of items) {
if (item.length) {
results.push(item.toUpperCase());
} else {
results.push("<empty>");
}
}
return results;
})();
Seems like they are purposely making the JS version extra long. It could be: items = items.map(x => x.length > 0 ? x.toUpperCase() : `<empty>`)
Edit: Just realised the code on the right is the compiled code, not the "equivalent hand written JS".We've added https://github.com/tc39/proposal-pipeline-operator, a variant of https://github.com/tc39/proposal-pattern-matching, a variant of https://github.com/tc39/proposal-string-dedent and others.
Since our goal is to be 99% compatible with ES we'll need to accommodate any proposals that become standard and pick up anything TC39 leaves on the table (rest parameters in any position, etc.)
Personally I couldn't let go of Clojure's other advantages but at least using the syntax would let you step off the syntax churn bus
items = items.map(x => x.toUpperCase() || `<empty>`)This even more terser code will fail if `x.toUpperCase()` fails with an exception (such as when x is not a string).
items = items.map(x => x?.toUpperCase() || `<empty>`)
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...I thought it was the comparable hand written JS.