New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tools.parseUnitString and tools.parseAngle #167
Conversation
* @returns {Object} { amount, unit } | ||
*/ | ||
parseUnitString: function (str) { | ||
var amount = parseFloat(str.match(/^\-?\d+/)), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think parseFloat works quite well without the regex.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
haha. very good point. It was early when I did that, I swear.
I like the idea of supporting units. |
|
That makes sense. |
* Parses the angle string to radians | ||
* https://developer.mozilla.org/en-US/docs/CSS/angle | ||
* | ||
* @param {String} angle The angle/unit string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
parseAngle
should also allow numbers (that are handled as radians). That would allow us to parse the parameter independent from the type. var radians = tools.parseAngle(param);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch.
Simple enough as if (typeof angle === 'number') return angle;
or should there be more robust handling of sorts (not sure what that would be...)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm tempted to let parseUnitString
only return the unit and not "amount". For three reasons:
parseFloat
is nothing we need to put in a helper function. It is already a well tested helper function itself and can safely be used.- We don't need to allocate another object (to be able to return 2 values)
- It saves use from using if-else
Code:
extractUnit: function (any) {
// make sure it's a string and remove trailing whitespace
var unit = String(any).replace(/\s+$/, '');
// returns extracted unit or empty string
return unit.match(/[a-z]*$/i)[0];
}
parseAngle: function(angle) {
var radians;
var amount = parseFloat(angle);
var unit = extractUnit(angle);
switch(unit) {
case 'deg':
radians = amount * PI / 180;
break;
…
case '': // default
case 'rad':
radians = amount;
break;
}
return radians;
}
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
I don’t know why I had completely blanked and not thought of 1. I also disliked it very much for number two so that is a big one. Should we handle someone having white space between the number and unit as well?
'7 deg'
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extractUnit("7 deg")
// "deg"
That's fine, isn't it? Your regex does already the work of ignoring leading whitespace.
👍 Any other cases we can think of to test and handle? |
* @returns {Number} The angle in radians | ||
*/ | ||
parseAngle: function(angle) { | ||
var unit = tools.parseUnitString(angle), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please put radians
at the beginning. Variables that are not explicitly assigned should be listed first. I know there aren't any coding-styles available yet for bonsai.. but soon :)
......annnnnnnnd done. I hadn’t even realized that nuance in the bonsai style. I’ve been trying to keep my additions like they belong in the family :) |
* @param {String} any The unit string. E.g. '90deg', '7em' | ||
* @returns {String} unit | ||
*/ | ||
parseUnitString: function (any) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since parseUnitString
can not just parse strings but also arrays (parseUnitString(["7em"])
) for example wouldn't it be better to rename parseUnitString
to extractUnit
and change the JSDOC's "@ param" to {any}
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup. That does make sense. I didn’t even think of the example of passing an array in. Update coming.
* @returns {String} unit | ||
*/ | ||
extractUnit: function (any) { | ||
if (typeof any === 'undefined' || typeof any === 'number') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that we need that check. What about "", null, [], {}, …
? I see your point but I don't think that we should expect undefined
and number
of all types. The cast in L357 takes care of invalid values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so do some test after var unit = ...
to make sure we return the empty string if given a poor argument?
String(undefined) //=> "undefined"
String(null) //=> "null"
String({}) //=> "[object Object]"
String([]) //=> ''
String(['singleDepth']) //=> 'singleDepth'
String(['bigger', 'array', 4]) //=> 'bigger,array,4'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No. That's sth the author/dev need to fix. That's why I suggested to remove the type-checks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 I can roll with that. Removed the checks, tests are still passing.
Oh and please add a line to the changelog :) |
And thanks for your endurance! This is going to be a really nice feature! |
The endurance is easy over here! I built it up working on matrices, getters/setters, and compounding transformations that I only half understand. |
Pushed. |
tools.extractUnit and tools.parseAngle
Thanks for this addition @iamdustan ! 👍 |
:) We made it! |
tools.extractUnit and tools.parseAngle
Awesome! |
Building upon these previous conversations, here are some converting methods to start accepting angles in any format throughout the project.
#142 (comment)
#165 (comment)
+@basecode