Permalink
Browse files

Test (and fixes) for d3.format.

  • Loading branch information...
1 parent 7df63b3 commit 50ebc155cb66a2d941e82c401bc7af96f9940b01 @mbostock mbostock committed Mar 4, 2011
Showing with 324 additions and 75 deletions.
  1. +10 −0 Makefile
  2. +36 −18 d3.js
  3. +40 −40 d3.min.js
  4. +1 −1 src/core/core.js
  5. +34 −16 src/core/format.js
  6. +109 −0 tests/test-format.js
  7. +94 −0 tests/test-format.out
View
@@ -133,6 +133,9 @@ d3.geom.js: \
src/geom/quadtree.js \
src/end.js
+tests: \
+ tests/test-format.test
+
%.min.js: %.js Makefile
@rm -f $@
$(JS_COMPILER) --js $< --js_output_file $@
@@ -142,5 +145,12 @@ d3.js d3%.js: Makefile
cat $(filter %.js,$^) > $@
@chmod a-w $@
+%.test: %.js %.out d3.js
+ @/bin/echo -n "test: $* "
+ @node $< > $*.actual
+ @diff -U 3 $*.out $*.actual && rm -f $*.actual \
+ && echo '\033[1;32mPASS\033[0m' \
+ || echo test: $* '\033[1;31mFAIL\033[0m'
+
clean:
rm -f d3*.js
View
@@ -1,4 +1,4 @@
-(function(){d3 = {version: "1.5.0"}; // semver
+(function(){d3 = {version: "1.5.1"}; // semver
if (!Date.now) Date.now = function() {
return +new Date();
};
@@ -335,42 +335,60 @@ function d3_dispatch(type) {
d3.format = function(specifier) {
var match = d3_format_re.exec(specifier),
fill = match[1] || " ",
- sign = d3_format_signs[match[3]] || d3_format_signs["-"],
+ sign = match[3] || "",
zfill = match[5],
width = +match[6],
comma = match[7],
precision = match[8],
type = match[9];
if (precision) precision = precision.substring(1);
- if (zfill) fill = "0"; // TODO align = "=";
+ if (zfill) {
+ fill = "0"; // TODO align = "=";
+ if (comma) width -= Math.floor((width - 1) / 4);
+ }
if (type == "d") precision = "0";
return function(value) {
var number = +value,
- negative = (number < 0) && (number = -number);
+ negative = (number < 0) && (number = -number) ? "\u2212" : sign;
+
+ // Return the empty string for floats formatted as ints.
if ((type == "d") && (number % 1)) return "";
+
+ // Convert the input value to the desired precision.
if (precision) value = number.toFixed(precision);
else value = "" + number;
- if (comma) {
- var i = value.lastIndexOf("."),
- f = i >= 0 ? value.substring(i) : (i = value.length, ""),
- t = [];
- while (i > 0) t.push(value.substring(i -= 3, i + 3));
- value = t.reverse().join(",") + f;
- }
- var length = (value = sign(negative, value)).length;
- if (length < width) value = new Array(width - length + 1).join(fill) + value;
+
+ // If the fill character is 0, the sign and group is applied after the fill.
+ if (zfill) {
+ var length = value.length + negative.length;
+ if (length < width) value = new Array(width - length + 1).join(fill) + value;
+ if (comma) value = d3_format_group(value);
+ value = negative + value;
+ }
+
+ // Otherwise (e.g., space-filling), the sign and group is applied before.
+ else {
+ if (comma) value = d3_format_group(value);
+ value = negative + value;
+ var length = value.length;
+ if (length < width) value = new Array(width - length + 1).join(fill) + value;
+ }
+
return value;
};
};
// [[fill]align][sign][#][0][width][,][.precision][type]
var d3_format_re = /(?:([^{])?([<>=^]))?([+\- ])?(#)?(0)?([0-9]+)?(,)?(\.[0-9]+)?([a-zA-Z%])?/;
-var d3_format_signs = {
- "+": function(negative, value) { return (negative ? "\u2212" : "+") + value; },
- " ": function(negative, value) { return (negative ? "\u2212" : " ") + value; },
- "-": function(negative, value) { return negative ? "\u2212" + value : value; }
-};
+// Apply comma grouping for thousands.
+function d3_format_group(value) {
+ var i = value.lastIndexOf("."),
+ f = i >= 0 ? value.substring(i) : (i = value.length, ""),
+ t = [];
+ while (i > 0) t.push(value.substring(i -= 3, i + 3));
+ return t.reverse().join(",") + f;
+}
/*
* TERMS OF USE - EASING EQUATIONS
*
Oops, something went wrong.

0 comments on commit 50ebc15

Please sign in to comment.