Bug: JavaScript unary operators "+" and "-" do not function as documented for UnitValue objects
According to the Illustrator ExtendScript documentation, applying the unary operators "+" or "-" to a UnitValue object should respectively produce the numeric value or the negated numeric value of the UnitValue. However, they instead respectively produce the UnitValue unmodified or the UnitValue with its value negated.
Either the documentation or the behaviour should be updated to reflect the other.
I observed this behaviour in Illustrator 28.5 on Mac OS Sonoma.
The behaviour may be quickly tested with the following script:
var uv = new UnitValue(2, "px");
alert(
"Results:" +
"\nUnitValue unmodified: \"" + uv + "\" (" + (typeof uv) +
")\n+UnitValue: \"" + (+uv) + "\" (" + (typeof +uv) +
")\n-UnitValue: \"" + (-uv) + "\" (" + (typeof -uv) +
")\nNumber(UnitValue): \"" + (Number(uv)) + "\" (" + (typeof Number(uv)) + ")"
);
-
Nihiltres commented
The issue is important not so much because it is a bad bug to work around but because it is directly misleading. I spent needless time debugging my script because I eliminated other possibilities before testing whether the built-in features functioned as described.
As a minor addendum: the "~" unary operator also produces a modified-UnitValue result instead of a numerical result, and it should have either its behaviour or documentation updated too.
-
Sergey Osokin commented
I agree that the text in the official reference is misleading. Although it's not a problem to convert the object type to a number using Number() or the .as() > new UnitValue(2, "px").as("px") method and then change the sign of the number