-
Notifications
You must be signed in to change notification settings - Fork 11.9k
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
Refactoring using helpers.options.resolve #5965
Conversation
Wow. Thanks for doing this. Great job and very thorough! My only question is should we make this method public. It seems like the API is relatively unlikely to change and I think it's one that we would want to use in lots of our plugins. |
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.
While at it, I would alias helpers.getHoverColor
too (and move it global where aliased locally).
Just something to NOT think about below this line, look at resolve
instead. 😄
Some variants that come to mind:
fallbackAtIndex
, fallbackNum
, fallbackNumAtIndex
.
And from that a generaliaztion:
_fallback: function(fn) {
var args = [].slice.call(arguments);
var fnArgs = args.slice(1, fn.length - 1);
var value = args[fn.length - 1];
var i, ilen;
for (i = fn.length, ilen = args.length; i < ilen; ++i) {
value = fn.apply(null, fnArgs.concat([value, args[i]]));
}
return value;
},
_numberOrDefault: function(num, def) { return isNaN(num) ? def : num; };
_numberAtIndexOrDefault: function(index, num, def) {
return _numberOrDefault(isArray(num) ? num[index] : num, isArray(def) ? def[index] : def); };
would be used like
v = _fallback(valueOrDefault, v1, v2, v3, ...);
v = _fallback(_numberAtIndexOrDefault, index, v1, v2, v3);
Note: valueAtIndexOrDefault
has its arguments in wrong order for this.
@kurkle I'm not fan of all these helpers, they are error prone because they will silently skip invalid options. For example,
|
@nagix A bit late but I'm just realizing that
Maybe we should use You could even use it for case like: hitRadius: !isNaN(custom.hitRadius) ? custom.hitRadius : valueAtIndexOrDefault(dataset.pointHitRadius, index, pointOptions.hitRadius)
// become
hitRadius: resolve([custom.hitRadius, dataset.pointHitRadius, pointOptions.hitRadius], undefined, index); @benmccann and |
I like the idea of using function fallback() {
return resolve(Array.prototype.slice(arguments));
} If it's not too complicated, I would just call resolve directly to avoid a loop over all the arguments for each call. |
Considering we are going to introduce scriptable options, using |
For better readability, I would move aliases to global only if they are used in more than one method/function. |
I have made the suggested changes. The size difference from master is now -1,550 bytes. |
I think I would always declare them outside to mimic partial ES6 imports:
But also because it makes easier to figure out when one is already declared, because ESLint will report a duplicated variable in this case. If locally aliased, then we could most of the time declare it multiple time locally (especially for newcomers). |
@simonbrunel Ok, that makes sense. I will make changes shortly. |
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.
Looks really good, thanks @nagix
Build is failing :\ |
The reason of |
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 like it!
This PR adds
helpers.options._fallback
method that takes an arbitrary number of arguments and returns the first defined value as discussed in #5961.In controller code, options such as
custom.radius
andcustom.borderWidth
don't fallback correctly when they are defined but falsy values, so this has been fixed.Several helper methods are aliased globally, and the size of min.js is reduced by 922 bytes. Also, the variable
_isPointInArea
is renamed toisPointInArea
as per #5937 (comment).Edit: After some discussion, we decide to use
helpers.options.resolve
instead of introducing a new helper method.