Skip to content

Commit

Permalink
Type 'c' is now for character data.
Browse files Browse the repository at this point in the history
Also, rather than the weird behavior where integer types return the empty string
for non-integers, just round the input to an integer.
  • Loading branch information
mbostock committed Jun 19, 2015
1 parent db138f0 commit e247671
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 42 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,16 @@ The available *type* values are:

* `e` - exponent notation.
* `f` - fixed point notation.
* `g` - round to significant digits, and then either decimal or exponent notation.
* `r` - round to significant digits, and then decimal notation.
* `s` - round to significant digits, and then decimal notation with an [SI prefix](#locale_formatPrefix).
* `g` - either decimal or exponent notation, rounded to significant digits.
* `r` - decimal notation, rounded to significant digits.
* `s` - decimal notation with an [SI prefix](#locale_formatPrefix), rounded to significant digits.
* `%` - multiply by 100, and then decimal notation with a percent sign.
* `p` - multiply by 100, round to significant digits, and then decimal notation with a percent sign.
* `b` - binary notation; ignores non-integers.
* `o` - octal notation; ignores non-integers.
* `d` - decimal notation; ignores non-integers.
* `x` - hexadecimal notation, using lower-case letters; ignores non-integers.
* `X` - hexadecimal notation, using upper-case letters; ignores non-integers.
* `b` - binary notation, rounded to integer (ignores precision).
* `o` - octal notation, rounded to integer (ignores precision).
* `d` - decimal notation, rounded to integer (ignores precision).
* `x` - hexadecimal notation, using lower-case letters, rounded to integer (ignores precision).
* `X` - hexadecimal notation, using upper-case letters, rounded to integer (ignores precision).
* `c` - converts the integer to the corresponding unicode character before printing.
* `` (none) - like `g`, but trim insignificant trailing zeros.

Expand Down
12 changes: 6 additions & 6 deletions src/formatTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import formatRounded from "./formatRounded";
export default {
"": formatDefault,
"%": function(x, p) { return (x * 100).toFixed(p); },
"b": function(x) { return x.toString(2); },
"c": function(x) { return String.fromCharCode(x); },
"d": function(x) { return x.toString(10); },
"b": function(x) { return Math.round(x).toString(2); },
"c": function(x) { return x + ""; },
"d": function(x) { return Math.round(x).toString(10); },
"e": function(x, p) { return x.toExponential(p); },
"f": function(x, p) { return x.toFixed(p); },
"g": function(x, p) { return x.toPrecision(p); },
"o": function(x) { return x.toString(8); },
"o": function(x) { return Math.round(x).toString(8); },
"p": function(x, p) { return formatRounded(x * 100, p); },
"r": formatRounded,
"s": formatPrefixAuto,
"X": function(x) { return x.toString(16).toUpperCase(); },
"x": function(x) { return x.toString(16); }
"X": function(x) { return Math.round(x).toString(16).toUpperCase(); },
"x": function(x) { return Math.round(x).toString(16); }
};
56 changes: 30 additions & 26 deletions src/localeFormat.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export default function(locale) {
// Is this an integer type?
// Can this type generate exponential notation?
var formatType = formatTypes[type],
integer = /[bcdoxX]/.test(type),
maybeSuffix = !type || /[defgprs%]/.test(type);

// Set the default precision if not specified,
Expand All @@ -49,31 +48,36 @@ export default function(locale) {
: Math.max(0, Math.min(20, precision));

return function(value) {
value = +value;

// Return the empty string for floats formatted as ints.
if (integer && (value % 1)) return "";

// Convert negative to positive, and compute the prefix.
// Note that -0 is not less than 0, but 1 / -0 is!
var valueNegative = (value < 0 || 1 / value < 0) && (value *= -1, true),
valuePrefix = (valueNegative ? (sign === "(" ? sign : "-") : sign === "-" || sign === "(" ? "" : sign) + prefix;

// Perform the initial formatting.
value = formatType(value, precision);

// Compute the suffix.
var valueSuffix = suffix + (type === "s" ? prefixes[8 + prefixExponent / 3] : "") + (valueNegative && sign === "(" ? ")" : "");

// Break the formatted value into the integer “value” part that can be
// grouped, and fractional or exponential “suffix” part that is not.
if (maybeSuffix) {
var i = -1, n = value.length, c;
while (++i < n) {
if (c = value.charCodeAt(i), 48 > c || c > 57) {
valueSuffix = (c === 46 ? decimal + value.slice(i + 1) : value.slice(i)) + valueSuffix;
value = value.slice(0, i);
break;
var valuePrefix = prefix,
valueSuffix = suffix;

if (type === "c") {
valueSuffix = formatType(value) + valueSuffix;
value = "";
} else {
value = +value;

// Convert negative to positive, and compute the prefix.
// Note that -0 is not less than 0, but 1 / -0 is!
var valueNegative = (value < 0 || 1 / value < 0) && (value *= -1, true);

// Perform the initial formatting.
value = formatType(value, precision);

// Compute the prefix and suffix.
valuePrefix = (valueNegative ? (sign === "(" ? sign : "-") : sign === "-" || sign === "(" ? "" : sign) + valuePrefix;
valueSuffix = valueSuffix + (type === "s" ? prefixes[8 + prefixExponent / 3] : "") + (valueNegative && sign === "(" ? ")" : "");

// Break the formatted value into the integer “value” part that can be
// grouped, and fractional or exponential “suffix” part that is not.
if (maybeSuffix) {
var i = -1, n = value.length, c;
while (++i < n) {
if (c = value.charCodeAt(i), 48 > c || c > 57) {
valueSuffix = (c === 46 ? decimal + value.slice(i + 1) : value.slice(i)) + valueSuffix;
value = value.slice(0, i);
break;
}
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions test/format-type-c-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ var tape = require("tape"),
format = require("../");

tape("format(\"c\") unicode character", function(test) {
test.equal(format.format("c")(9731), "☃");
test.equal(format.format("c")("☃"), "☃");
test.equal(format.format("020c")("☃"), "0000000000000000000☃");
test.equal(format.format(" ^20c")("☃"), " ☃ ");
test.equal(format.format("$c")("☃"), "$☃");
test.end();
});

tape("format(\"c\") does not localize a decimal point", function(test) {
test.equal(format.localeFormat({decimal: "/"}).format("c")(46), ".");
test.equal(format.localeFormat({decimal: "/"}).format("c")("."), ".");
test.end();
});

0 comments on commit e247671

Please sign in to comment.