Skip to content

Commit e247671

Browse files
committed
Type 'c' is now for character data.
Also, rather than the weird behavior where integer types return the empty string for non-integers, just round the input to an integer.
1 parent db138f0 commit e247671

File tree

4 files changed

+49
-42
lines changed

4 files changed

+49
-42
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,16 @@ The available *type* values are:
114114

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

src/formatTypes.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ import formatRounded from "./formatRounded";
55
export default {
66
"": formatDefault,
77
"%": function(x, p) { return (x * 100).toFixed(p); },
8-
"b": function(x) { return x.toString(2); },
9-
"c": function(x) { return String.fromCharCode(x); },
10-
"d": function(x) { return x.toString(10); },
8+
"b": function(x) { return Math.round(x).toString(2); },
9+
"c": function(x) { return x + ""; },
10+
"d": function(x) { return Math.round(x).toString(10); },
1111
"e": function(x, p) { return x.toExponential(p); },
1212
"f": function(x, p) { return x.toFixed(p); },
1313
"g": function(x, p) { return x.toPrecision(p); },
14-
"o": function(x) { return x.toString(8); },
14+
"o": function(x) { return Math.round(x).toString(8); },
1515
"p": function(x, p) { return formatRounded(x * 100, p); },
1616
"r": formatRounded,
1717
"s": formatPrefixAuto,
18-
"X": function(x) { return x.toString(16).toUpperCase(); },
19-
"x": function(x) { return x.toString(16); }
18+
"X": function(x) { return Math.round(x).toString(16).toUpperCase(); },
19+
"x": function(x) { return Math.round(x).toString(16); }
2020
};

src/localeFormat.js

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ export default function(locale) {
3737
// Is this an integer type?
3838
// Can this type generate exponential notation?
3939
var formatType = formatTypes[type],
40-
integer = /[bcdoxX]/.test(type),
4140
maybeSuffix = !type || /[defgprs%]/.test(type);
4241

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

5150
return function(value) {
52-
value = +value;
53-
54-
// Return the empty string for floats formatted as ints.
55-
if (integer && (value % 1)) return "";
56-
57-
// Convert negative to positive, and compute the prefix.
58-
// Note that -0 is not less than 0, but 1 / -0 is!
59-
var valueNegative = (value < 0 || 1 / value < 0) && (value *= -1, true),
60-
valuePrefix = (valueNegative ? (sign === "(" ? sign : "-") : sign === "-" || sign === "(" ? "" : sign) + prefix;
61-
62-
// Perform the initial formatting.
63-
value = formatType(value, precision);
64-
65-
// Compute the suffix.
66-
var valueSuffix = suffix + (type === "s" ? prefixes[8 + prefixExponent / 3] : "") + (valueNegative && sign === "(" ? ")" : "");
67-
68-
// Break the formatted value into the integer “value” part that can be
69-
// grouped, and fractional or exponential “suffix” part that is not.
70-
if (maybeSuffix) {
71-
var i = -1, n = value.length, c;
72-
while (++i < n) {
73-
if (c = value.charCodeAt(i), 48 > c || c > 57) {
74-
valueSuffix = (c === 46 ? decimal + value.slice(i + 1) : value.slice(i)) + valueSuffix;
75-
value = value.slice(0, i);
76-
break;
51+
var valuePrefix = prefix,
52+
valueSuffix = suffix;
53+
54+
if (type === "c") {
55+
valueSuffix = formatType(value) + valueSuffix;
56+
value = "";
57+
} else {
58+
value = +value;
59+
60+
// Convert negative to positive, and compute the prefix.
61+
// Note that -0 is not less than 0, but 1 / -0 is!
62+
var valueNegative = (value < 0 || 1 / value < 0) && (value *= -1, true);
63+
64+
// Perform the initial formatting.
65+
value = formatType(value, precision);
66+
67+
// Compute the prefix and suffix.
68+
valuePrefix = (valueNegative ? (sign === "(" ? sign : "-") : sign === "-" || sign === "(" ? "" : sign) + valuePrefix;
69+
valueSuffix = valueSuffix + (type === "s" ? prefixes[8 + prefixExponent / 3] : "") + (valueNegative && sign === "(" ? ")" : "");
70+
71+
// Break the formatted value into the integer “value” part that can be
72+
// grouped, and fractional or exponential “suffix” part that is not.
73+
if (maybeSuffix) {
74+
var i = -1, n = value.length, c;
75+
while (++i < n) {
76+
if (c = value.charCodeAt(i), 48 > c || c > 57) {
77+
valueSuffix = (c === 46 ? decimal + value.slice(i + 1) : value.slice(i)) + valueSuffix;
78+
value = value.slice(0, i);
79+
break;
80+
}
7781
}
7882
}
7983
}

test/format-type-c-test.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ var tape = require("tape"),
22
format = require("../");
33

44
tape("format(\"c\") unicode character", function(test) {
5-
test.equal(format.format("c")(9731), "☃");
5+
test.equal(format.format("c")("☃"), "☃");
6+
test.equal(format.format("020c")("☃"), "0000000000000000000☃");
7+
test.equal(format.format(" ^20c")("☃"), " ☃ ");
8+
test.equal(format.format("$c")("☃"), "$☃");
69
test.end();
710
});
811

912
tape("format(\"c\") does not localize a decimal point", function(test) {
10-
test.equal(format.localeFormat({decimal: "/"}).format("c")(46), ".");
13+
test.equal(format.localeFormat({decimal: "/"}).format("c")("."), ".");
1114
test.end();
1215
});

0 commit comments

Comments
 (0)