I was reading through this: https://github.com/pburtchaell/redux-promise-middleware/blob/master/src/index.js
I know that ...
is being used as Object spread. I know that !!
is used to convert anything into a boolean with the same truthiness.
However knowing this what do they mean when they're put together like ...!!
? I have trouble understanding the last line here:
{
...resolveAction,
...isAction(rejected) ? rejected : {
...!!rejected && { payload: rejected }
}
...resolveAction
just spreads the keys of resolveAction
....isAction(rejected) ?
will check if rejected
resolves an action and then will spread it. (Not sure about this one either)rejected
to object if true{...!!rejected && { payload: rejected }
????????????????How is ...!!
even valid syntax? There are two options:
If it spreads the object first, then !!
would be applied to all the spread keys
If !!
is applied first it's a boolean value and it can't be spread.
So does it make no sense whatsoever, or am I missing something because given that code, I assume it's trying to spread a boolean value.
ECMAScript syntax is relaxed to enable it to serve as an easy-to-use scripting language. For example, a variable is not required to have its type declared nor are types associated with properties, and defined functions are not required to have their declarations appear textually before calls to them.
JavaScript ES6 (also known as ECMAScript 2015 or ECMAScript 6) is the newer version of JavaScript that was introduced in 2015. ECMAScript is the standard that JavaScript programming language uses. ECMAScript provides the specification on how JavaScript programming language should work.
ES5 lets you define object methods with a syntax that looks like getting or setting a property.
JavaScript Values The JavaScript syntax defines two types of values: Fixed values and variable values. Fixed values are called literals. Variable values are called variables.
Okay so after downloading the npm module and going through the transpiled code I found the line:
return dispatch(isThunk(rejected) ? rejected.bind(null, resolveAction) : _extends({}, resolveAction, isAction(rejected) ? rejected : _extends({}, !!rejected && { payload: rejected })));
Of which the relevant part is here:
_extends({}, !!rejected && { payload: rejected })
Basically if !!rejected
is true then it'll spread the payload into the object. If it's not _extends({}, false)
just returns {}
.
The key to this working is that ...
has less precedence than any other operator in the entire line. With that in mind you can begin to make sense of it.
The relevant question is What is "x && foo()"?
The weird syntax
{...!!rejected && { payload: rejected }}
is parsed as
{ ... ((!!rejected) && { payload: rejected }) }
Indeed the !!
does cast rejected
to a boolean, then it is evaluated to the object when truthy. A better way to write this would be
{ ...(rejected ? {payload: rejected} : null) }
And of course the whole inner object literal is superfluous. It could just be
{
...resolveAction,
...(isAction(rejected) ? rejected : (rejected ? {payload: rejected} : null))
}
(omit parentheses as you see fit)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With