Description
When we need to destruct array or object and change value then we need make something like this
let { first = "data", second } = getSomeObject();
first = computeFirst(first);
// Or create temporary useless variable
const obj = getSomeObject();
const second = obj.second;
const first = computeFirst("first" in obj ? obj.first : "data");
I propose operator which will give ability to us to apply some function to property inside destruction-syntax without mutable or temporary variables
const { first | computeFirst = "data", second } = getSomeObject();
I think that syntax should be discussed, but it seems lie pipe-symbol is well-known operator in bash and in AngularJS/Vue community.
Alternative variants
Swap function and argument:
const { computeFirst | first = "data", second } = getSomeObject();
Use $ as separator:
const { computeFirst $ first = "data", second } = getSomeObject();
Use -> or <- as separator:
const { computeFirst -> first = "data", second } = getSomeObject();
// OR
const { first <- computeFirst = "data", second } = getSomeObject();
Usage
Function arguments conversion:
function getUserResult({ score | BigInt }) { ... }
Node.js environment variables:
const splitted = a => a.split(",").map(a => a.trim());
const {
REPOSITORIES | splitted,
BRANCH = "master",
COMMITS | splitted
} = process.env;
SQL result to JavaScript:
const { createdAt | Date } = getUser(id);