Comments (4)
So, you are talking about this monkey-patching code:
if((0.9).toFixed() == 0){
// (isIE) toFixed() bug workaround: Rounding fails on IE when most significant digit
// is just after the rounding place and is >=5
var round = number.round;
number.round = function(v, p, m){
var d = Math.pow(10, -p || 0), a = Math.abs(v);
if(!v || a >= d || a * Math.pow(10, p + 1) < 5){
d = 0;
}
return round(v, p, m) + (v > 0 ? d : -d);
};
}
workarounds
I just dealt with a bunch of problems like that, like for example when dojox/mvc overrides some methods in dijit. Some possible workarounds are:
if statement
Instead of monkey patching the function, just use an if() statement:
number.round = function(/*Number*/ value, /*Number?*/ places, /*Number?*/ increment){
// summary: ...
if((0.9).toFixed() == 0){
// (isIE) toFixed() bug workaround: Rounding fails on IE when most significant digit
// is just after the rounding place and is >=5
var d = Math.pow(10, -places || 0), a = Math.abs(value);
if(!value || a >= d || a * Math.pow(10, places + 1) < 5){
d = 0;
}
}
var factor = 10 / (increment || 10);
return (factor * +value).toFixed(places) / factor; // Number
};
It's dead simple, but degrades performance on modern browser. Whether or not the performance change is significant is a different question.
Also note that the bug-test should really be in a has.add() block so we can filter it out for webkit-only builds.
around advice
Use dojo/aspect.around() to modify the behavior of round(). Thankfully the parser doesn't try to interpret around advice.
if((0.9).toFixed() == 0){
// (isIE) toFixed() bug workaround: Rounding fails on IE when most significant digit
// is just after the rounding place and is >=5
aspect.around(number, "round", function(round){
return function(v, p, m){
var d = Math.pow(10, -p || 0), a = Math.abs(v);
if(!v || a >= d || a * Math.pow(10, p + 1) < 5){
d = 0;
}
return round(v, p, m) + (v > 0 ? d : -d);
};
});
}
This seems like the most legible solution, working around the parser problem and improving code readability at the same time, but probably we don't want to introduce a dependency from dojo/number to dojo/aspect.
doc comment trick
Use doc-comments to trick the parser:
if((0.9).toFixed() == 0){
// (isIE) toFixed() bug workaround: Rounding fails on IE when most significant digit
// is just after the rounding place and is >=5
var round = number.round;
number.round = /*===== number.round || =====*/ function(v, p, m){
var d = Math.pow(10, -p || 0), a = Math.abs(v);
if(!v || a >= d || a * Math.pow(10, p + 1) < 5){
d = 0;
}
return round(v, p, m) + (v > 0 ? d : -d);
};
}
Probably the best solution for this particular case.
pseudo function in doc block at the end
Use a pseudo-code doc block after both definitions, like we do for dom.byId():
if(has("ie")){
dom.byId = function(id, doc){
...
};
}else{
dom.byId = function(id, doc){
...
};
}
/*=====
dom.byId = function(id, doc){
// summary:
// Returns DOM node with matching `id` attribute or `null`
// if not found. If `id` is a DomNode, this function is a no-op.
...
};
=====*/
fix
As to fixing the issue, not sure what you want the parser to do? Ignore any code inside of if() statements or ternaries? It just seems complicated/dangerous. For example, consider this code from json.js:
return {
// summary:
// Functions to parse and serialize JSON
parse: has("json-parse") ? JSON.parse : function(str, strict){
// summary:
// Parses a [JSON](http://json.org) string to return a JavaScript object.
...
},
Imagine if the ternary listed to inlined functions. How does the parser know which definition to use?
In any case, seems like we should workaround this issue in the code for now and possibly improve the parser later, since there are lots of other parser bugs that we can't workaround. Agreed?
from js-doc-parse.
No, that is fine if there is a workaround for the parser. What is the proper etiquette for updating comments for documentation purposes?
from js-doc-parse.
I've just been updating them, without any "etiquette". I don't think anyone minds, or that it's practical to coordinate every documentation fix.
from js-doc-parse.
PS: for some reason the "doc comment trick" I listed above didn't work in this case, but I checked in a similar fix:
// Use "doc hint" so the doc parser ignores this new definition of round(), and uses the one above.
/*===== number.round = round; =====*/
from js-doc-parse.
Related Issues (20)
- return query(...) // dojo/NodeList not working HOT 2
- property's summary gets assigned to the property's value HOT 4
- fx.js makes other file lose easing and duration kwargs definition HOT 3
- kwargs definition using declare() to extend from plain function() broken HOT 1
- inline expansion of union types with kwargs doesn't work HOT 2
- comment on line declaring export variable masks summary HOT 3
- fragile regex for union types HOT 1
- inline return types often ignored HOT 1
- properties added from one module to another sometimes don't show up HOT 1
- dojodoc for properties with dashes ignored HOT 1
- js-doc-parser errors HOT 2
- Ubuntu/Canonical process.execPath = /usr/bin/nodejs HOT 1
- Add support for documenting attach points and attach events HOT 2
- tree.json no longer generated HOT 1
- ERR: TypeError: Object true has no method 'match' HOT 6
- parse.bat config = ./config.js produces error "ERR: TypeError: Object true has no method 'match'" HOT 1
- dynamic AMD require
- js-doc-parse traces when require()ing external url
- UMD modules HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from js-doc-parse.