pax_global_header 0000666 0000000 0000000 00000000064 12750212270 0014510 g ustar 00root root 0000000 0000000 52 comment=44ed4cfd04bba86fb60e8d69be82d77084f56ed4
d3-format-1.0.2/ 0000775 0000000 0000000 00000000000 12750212270 0013304 5 ustar 00root root 0000000 0000000 d3-format-1.0.2/.eslintrc 0000664 0000000 0000000 00000000217 12750212270 0015130 0 ustar 00root root 0000000 0000000 parserOptions:
sourceType: "module"
env:
es6: true
browser: true
extends:
"eslint:recommended"
rules:
no-cond-assign: 0
d3-format-1.0.2/.gitignore 0000664 0000000 0000000 00000000100 12750212270 0015263 0 ustar 00root root 0000000 0000000 *.sublime-workspace
.DS_Store
build/
node_modules
npm-debug.log
d3-format-1.0.2/.npmignore 0000664 0000000 0000000 00000000036 12750212270 0015302 0 ustar 00root root 0000000 0000000 *.sublime-*
build/*.zip
test/
d3-format-1.0.2/LICENSE 0000664 0000000 0000000 00000002703 12750212270 0014313 0 ustar 00root root 0000000 0000000 Copyright 2010-2015 Mike Bostock
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the author nor the names of contributors may be used to
endorse or promote products derived from this software without specific prior
written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
d3-format-1.0.2/README.md 0000664 0000000 0000000 00000032245 12750212270 0014571 0 ustar 00root root 0000000 0000000 # d3-format
Ever noticed how sometimes JavaScript doesn’t display numbers the way you expect? Like, you tried to print tenths with a simple loop:
```js
for (var i = 0; i < 10; i++) {
console.log(0.1 * i);
}
```
And you got this:
```js
0
0.1
0.2
0.30000000000000004
0.4
0.5
0.6000000000000001
0.7000000000000001
0.8
0.9
```
Welcome to [binary floating point](https://en.wikipedia.org/wiki/Double-precision_floating-point_format)! ಠ_ಠ
Yet rounding error is not the only reason to customize number formatting. A table of numbers should be formatted consistently for comparison; above, 0.0 would be better than 0. Large numbers should have grouped digits (e.g., 42,000) or be in scientific or metric notation (4.2e+4, 42k). Currencies should have fixed precision ($3.50). Reported numerical results should be rounded to significant digits (4021 becomes 4000). Number formats should appropriate to the reader’s locale (42.000,00 or 42,000.00). The list goes on.
Formatting numbers for human consumption is the purpose of d3-format, which is modeled after Python 3’s [format specification mini-language](https://docs.python.org/3/library/string.html#format-specification-mini-language) ([PEP 3101](https://www.python.org/dev/peps/pep-3101/)). Revisiting the example above:
```js
var f = d3.format(".1f");
for (var i = 0; i < 10; i++) {
console.log(f(0.1 * i));
}
```
Now you get this:
```js
0.0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
```
But d3-format is much more than an alias for [number.toFixed](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed)! A few more examples:
```js
d3.format(".0%")(0.123); // rounded percentage, "12%"
d3.format("($.2f")(-3.5); // localized fixed-point currency, "(£3.50)"
d3.format("+20")(42); // space-filled and signed, " +42"
d3.format(".^20")(42); // dot-filled and centered, ".........42........."
d3.format(".2s")(42e6); // SI-prefix with two significant digits, "42M"
d3.format("#x")(48879); // prefixed lowercase hexadecimal, "0xbeef"
d3.format(",.2r")(4223); // grouped thousands with two significant digits, "4,200"
```
See [*locale*.format](#locale_format) for a detailed specification, and try running [d3.formatSpecifier](#formatSpecifier) on the above formats to decode their meaning.
## Installing
If you use NPM, `npm install d3-format`. Otherwise, download the [latest release](https://github.com/d3/d3-format/releases/latest). You can also load directly from [d3js.org](https://d3js.org), either as a [standalone library](https://d3js.org/d3-format.v1.min.js) or as part of [D3 4.0](https://github.com/d3/d3). AMD, CommonJS, and vanilla environments are supported. In vanilla, a `d3` global is exported:
```html
```
[Try d3-format in your browser.](https://tonicdev.com/npm/d3-format)
## API Reference
# d3.format(specifier)
An alias for [*locale*.format](#locale_format) on the [default locale](#formatDefaultLocale).
# d3.formatPrefix(specifier, value)
An alias for [*locale*.formatPrefix](#locale_formatPrefix) on the [default locale](#formatDefaultLocale).
# locale.format(specifier)
Returns a new format function for the given string *specifier*. The returned function takes a number as the only argument, and returns a string representing the formatted number. The general form of a specifier is:
```
[[fill]align][sign][symbol][0][width][,][.precision][type]
```
The *fill* can be any character. The presence of a fill character is signaled by the *align* character following it, which must be one of the following:
* `>` - Forces the field to be right-aligned within the available space. (Default behavior).
* `<` - Forces the field to be left-aligned within the available space.
* `^` - Forces the field to be centered within the available space.
* `=` - like `>`, but with any sign and symbol to the left of any padding.
The *sign* can be:
* `-` - nothing for positive and a minus sign for negative. (Default behavior.)
* `+` - a plus sign for positive and a minus sign for negative.
* `(` - nothing for positive and parentheses for negative.
* ` ` (space) - a space for positive and a minus sign for negative.
The *symbol* can be:
* `$` - apply currency symbols per the locale definition.
* `#` - for binary, octal, or hexadecimal notation, prefix by `0b`, `0o`, or `0x`, respectively.
The *zero* (`0`) option enables zero-padding; this implicitly sets *fill* to `0` and *align* to `=`. The *width* defines the minimum field width; if not specified, then the width will be determined by the content. The *comma* (`,`) option enables the use of a group separator, such as a comma for thousands.
Depending on the *type*, the *precision* either indicates the number of digits that follow the decimal point (types `f` and `%`), or the number of significant digits (types ``, `e`, `g`, `r`, `s` and `p`). If the precision is not specified, it defaults to 6 for all types except `` (none), which defaults to 12. Precision is ignored for integer formats (types `b`, `o`, `d`, `x`, `X` and `c`). See [precisionFixed](#precisionFixed) and [precisionRound](#precisionRound) for help picking an appropriate precision.
The available *type* values are:
* `e` - exponent notation.
* `f` - fixed point notation.
* `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, rounded to integer.
* `o` - octal notation, rounded to integer.
* `d` - decimal notation, rounded to integer.
* `x` - hexadecimal notation, using lower-case letters, rounded to integer.
* `X` - hexadecimal notation, using upper-case letters, rounded to integer.
* `c` - converts the integer to the corresponding unicode character before printing.
* `` (none) - like `g`, but trim insignificant trailing zeros.
The type `n` is also supported as shorthand for `,g`. For the `g`, `n` and `` (none) types, decimal notation is used if the resulting string would have *precision* or fewer digits; otherwise, exponent notation is used. For example:
```js
d3.format(".2")(42); // "42"
d3.format(".2")(4.2); // "4.2"
d3.format(".1")(42); // "4e+1"
d3.format(".1")(4.2); // "4"
```
# locale.formatPrefix(specifier, value)
Equivalent to [*locale*.format](#locale_format), except the returned function will convert values to the units of the appropriate [SI prefix](https://en.wikipedia.org/wiki/Metric_prefix#List_of_SI_prefixes) for the specified numeric reference *value* before formatting in fixed point notation. The following prefixes are supported:
* `y` - yocto, 10⁻²⁴
* `z` - zepto, 10⁻²¹
* `a` - atto, 10⁻¹⁸
* `f` - femto, 10⁻¹⁵
* `p` - pico, 10⁻¹²
* `n` - nano, 10⁻⁹
* `µ` - micro, 10⁻⁶
* `m` - milli, 10⁻³
* `` (none) - 10⁰
* `k` - kilo, 10³
* `M` - mega, 10⁶
* `G` - giga, 10⁹
* `T` - tera, 10¹²
* `P` - peta, 10¹⁵
* `E` - exa, 10¹⁸
* `Z` - zetta, 10²¹
* `Y` - yotta, 10²⁴
Unlike [*locale*.format](#locale_format) with the `s` format type, this method returns a formatter with a consistent SI prefix, rather than computing the prefix dynamically for each number. In addition, the *precision* for the given *specifier* represents the number of digits past the decimal point (as with `f` fixed point notation), not the number of significant digits. For example:
```js
var f = d3.formatPrefix(",.0", 1e-6);
f(0.00042); // "420µ"
f(0.0042); // "4,200µ"
```
This method is useful when formatting multiple numbers in the same units for easy comparison. See [precisionPrefix](#precisionPrefix) for help picking an appropriate precision, and [bl.ocks.org/9764126](http://bl.ocks.org/mbostock/9764126) for an example.
# d3.formatSpecifier(specifier)
Parses the specified *specifier*, returning an object with exposed fields that correspond to the [format specification mini-language](#locale_format) and a toString method that reconstructs the specifier. For example, `formatSpecifier("s")` returns:
```js
{
"fill": " ",
"align": ">",
"sign": "-",
"symbol": "",
"zero": false,
"width": undefined,
"comma": false,
"precision": 6,
"type": "s"
}
```
This method is useful for understanding how format specifiers are parsed and for deriving new specifiers. For example, you might compute an appropriate precision based on the numbers you want to format using [precisionFixed](#precisionFixed) and then create a new format:
```js
var s = d3.formatSpecifier("f");
s.precision = precisionFixed(0.01);
var f = d3.format(s);
f(42); // "42.00";
```
# d3.precisionFixed(step)
Returns a suggested decimal precision for fixed point notation given the specified numeric *step* value. The *step* represents the minimum absolute difference between values that will be formatted. (This assumes that the values to be formatted are also multiples of *step*.) For example, given the numbers 1, 1.5, and 2, the *step* should be 0.5 and the suggested precision is 1:
```js
var p = d3.precisionFixed(0.5),
f = d3.format("." + p + "f");
f(1); // "1.0"
f(1.5); // "1.5"
f(2); // "2.0"
```
Whereas for the numbers 1, 2 and 3, the *step* should be 1 and the suggested precision is 0:
```js
var p = d3.precisionFixed(1),
f = d3.format("." + p + "f");
f(1); // "1"
f(2); // "2"
f(3); // "3"
```
Note: for the `%` format type, subtract two:
```js
var p = Math.max(0, d3.precisionFixed(0.05) - 2),
f = d3.format("." + p + "%");
f(0.45); // "45%"
f(0.50); // "50%"
f(0.55); // "55%"
```
# d3.precisionPrefix(step, value)
Returns a suggested decimal precision for use with [*locale*.formatPrefix](#locale_formatPrefix) given the specified numeric *step* and reference *value*. The *step* represents the minimum absolute difference between values that will be formatted, and *value* determines which SI prefix will be used. (This assumes that the values to be formatted are also multiples of *step*.) For example, given the numbers 1.1e6, 1.2e6, and 1.3e6, the *step* should be 1e5, the *value* could be 1.3e6, and the suggested precision is 1:
```js
var p = d3.precisionPrefix(1e5, 1.3e6),
f = d3.formatPrefix("." + p, 1.3e6);
f(1.1e6); // "1.1M"
f(1.2e6); // "1.2M"
f(1.3e6); // "1.3M"
```
# d3.precisionRound(step, max)
Returns a suggested decimal precision for format types that round to significant digits given the specified numeric *step* and *max* values. The *step* represents the minimum absolute difference between values that will be formatted, and the *max* represents the largest absolute value that will be formatted. (This assumes that the values to be formatted are also multiples of *step*.) For example, given the numbers 0.99, 1.0, and 1.01, the *step* should be 0.01, the *max* should be 1.01, and the suggested precision is 3:
```js
var p = d3.precisionRound(0.01, 1.01),
f = d3.format("." + p + "r");
f(0.99); // "0.990"
f(1.0); // "1.00"
f(1.01); // "1.01"
```
Whereas for the numbers 0.9, 1.0, and 1.1, the *step* should be 0.1, the *max* should be 1.1, and the suggested precision is 2:
```js
var p = d3.precisionRound(0.1, 1.1),
f = d3.format("." + p + "r");
f(0.9); // "0.90"
f(1.0); // "1.0"
f(1.1); // "1.1"
```
Note: for the `e` format type, subtract one:
```js
var p = Math.max(0, d3.precisionRound(0.01, 1.01) - 1),
f = d3.format("." + p + "e");
f(0.01); // "1.00e-2"
f(1.01); // "1.01e+0"
```
### Locales
# d3.formatLocale(definition)
Returns a *locale* object for the specified *definition* with [*locale*.format](#locale_format) and [*locale*.formatPrefix](#locale_formatPrefix) methods. The *definition* must include the following properties:
* `decimal` - the decimal point (e.g., `"."`).
* `thousands` - the group separator (e.g., `","`).
* `grouping` - the array of group sizes (e.g., `[3]`), cycled as needed.
* `currency` - the currency prefix and suffix (e.g., `["$", ""]`).
Note that the *thousands* property is a misnomer, as the grouping definition allows groups other than thousands.
# d3.formatDefaultLocale(definition)
Equivalent to [d3.formatLocale](#formatLocale), except it also redefines [d3.format](#format) and [d3.formatPrefix](#formatPrefix) to the new locale’s [*locale*.format](#locale_format) and [*locale*.formatPrefix](#locale_formatPrefix). If you do not set a default locale, it defaults to [U.S. English](https://github.com/d3/d3-format/blob/master/locale/en-US.json).
d3-format-1.0.2/d3-format.sublime-project 0000664 0000000 0000000 00000000271 12750212270 0020126 0 ustar 00root root 0000000 0000000 {
"folders": [
{
"path": ".",
"file_exclude_patterns": [
"*.sublime-workspace"
],
"folder_exclude_patterns": [
"build"
]
}
]
}
d3-format-1.0.2/index.js 0000664 0000000 0000000 00000000624 12750212270 0014753 0 ustar 00root root 0000000 0000000 export {default as formatDefaultLocale, format, formatPrefix} from "./src/defaultLocale";
export {default as formatLocale} from "./src/locale";
export {default as formatSpecifier} from "./src/formatSpecifier";
export {default as precisionFixed} from "./src/precisionFixed";
export {default as precisionPrefix} from "./src/precisionPrefix";
export {default as precisionRound} from "./src/precisionRound";
d3-format-1.0.2/locale/ 0000775 0000000 0000000 00000000000 12750212270 0014543 5 ustar 00root root 0000000 0000000 d3-format-1.0.2/locale/ca-ES.json 0000664 0000000 0000000 00000000135 12750212270 0016325 0 ustar 00root root 0000000 0000000 {
"decimal": ",",
"thousands": ".",
"grouping": [3],
"currency": ["", "\u00a0€"]
}
d3-format-1.0.2/locale/cs-CZ.json 0000664 0000000 0000000 00000000142 12750212270 0016352 0 ustar 00root root 0000000 0000000 {
"decimal": ",",
"thousands": "\u00a0",
"grouping": [3],
"currency": ["", "\u00a0Kč"]
}
d3-format-1.0.2/locale/de-CH.json 0000664 0000000 0000000 00000000135 12750212270 0016315 0 ustar 00root root 0000000 0000000 {
"decimal": ",",
"thousands": "'",
"grouping": [3],
"currency": ["", "\u00a0CHF"]
}
d3-format-1.0.2/locale/de-DE.json 0000664 0000000 0000000 00000000135 12750212270 0016313 0 ustar 00root root 0000000 0000000 {
"decimal": ",",
"thousands": ".",
"grouping": [3],
"currency": ["", "\u00a0€"]
}
d3-format-1.0.2/locale/en-CA.json 0000664 0000000 0000000 00000000125 12750212270 0016317 0 ustar 00root root 0000000 0000000 {
"decimal": ".",
"thousands": ",",
"grouping": [3],
"currency": ["$", ""]
}
d3-format-1.0.2/locale/en-GB.json 0000664 0000000 0000000 00000000126 12750212270 0016325 0 ustar 00root root 0000000 0000000 {
"decimal": ".",
"thousands": ",",
"grouping": [3],
"currency": ["£", ""]
}
d3-format-1.0.2/locale/en-US.json 0000664 0000000 0000000 00000000125 12750212270 0016363 0 ustar 00root root 0000000 0000000 {
"decimal": ".",
"thousands": ",",
"grouping": [3],
"currency": ["$", ""]
}
d3-format-1.0.2/locale/es-ES.json 0000664 0000000 0000000 00000000135 12750212270 0016351 0 ustar 00root root 0000000 0000000 {
"decimal": ",",
"thousands": ".",
"grouping": [3],
"currency": ["", "\u00a0€"]
}
d3-format-1.0.2/locale/es-MX.json 0000664 0000000 0000000 00000000125 12750212270 0016365 0 ustar 00root root 0000000 0000000 {
"decimal": ".",
"thousands": ",",
"grouping": [3],
"currency": ["$", ""]
}
d3-format-1.0.2/locale/fi-FI.json 0000664 0000000 0000000 00000000142 12750212270 0016325 0 ustar 00root root 0000000 0000000 {
"decimal": ",",
"thousands": "\u00a0",
"grouping": [3],
"currency": ["", "\u00a0€"]
}
d3-format-1.0.2/locale/fr-CA.json 0000664 0000000 0000000 00000000132 12750212270 0016322 0 ustar 00root root 0000000 0000000 {
"decimal": ",",
"thousands": "\u00a0",
"grouping": [3],
"currency": ["", "$"]
}
d3-format-1.0.2/locale/fr-FR.json 0000664 0000000 0000000 00000000135 12750212270 0016351 0 ustar 00root root 0000000 0000000 {
"decimal": ",",
"thousands": ".",
"grouping": [3],
"currency": ["", "\u00a0€"]
}
d3-format-1.0.2/locale/he-IL.json 0000664 0000000 0000000 00000000127 12750212270 0016334 0 ustar 00root root 0000000 0000000 {
"decimal": ".",
"thousands": ",",
"grouping": [3],
"currency": ["₪", ""]
}
d3-format-1.0.2/locale/hu-HU.json 0000664 0000000 0000000 00000000141 12750212270 0016360 0 ustar 00root root 0000000 0000000 {
"decimal": ",",
"thousands": "\u00a0",
"grouping": [3],
"currency": ["", "\u00a0Ft"]
}
d3-format-1.0.2/locale/it-IT.json 0000664 0000000 0000000 00000000127 12750212270 0016364 0 ustar 00root root 0000000 0000000 {
"decimal": ",",
"thousands": ".",
"grouping": [3],
"currency": ["€", ""]
}
d3-format-1.0.2/locale/ja-JP.json 0000664 0000000 0000000 00000000127 12750212270 0016337 0 ustar 00root root 0000000 0000000 {
"decimal": ".",
"thousands": ",",
"grouping": [3],
"currency": ["", "円"]
}
d3-format-1.0.2/locale/ko-KR.json 0000664 0000000 0000000 00000000127 12750212270 0016361 0 ustar 00root root 0000000 0000000 {
"decimal": ".",
"thousands": ",",
"grouping": [3],
"currency": ["₩", ""]
}
d3-format-1.0.2/locale/mk-MK.json 0000664 0000000 0000000 00000000141 12750212270 0016346 0 ustar 00root root 0000000 0000000 {
"decimal": ",",
"thousands": ".",
"grouping": [3],
"currency": ["", "\u00a0ден."]
}
d3-format-1.0.2/locale/nl-NL.json 0000664 0000000 0000000 00000000135 12750212270 0016355 0 ustar 00root root 0000000 0000000 {
"decimal": ",",
"thousands": ".",
"grouping": [3],
"currency": ["€\u00a0", ""]
}
d3-format-1.0.2/locale/pl-PL.json 0000664 0000000 0000000 00000000127 12750212270 0016362 0 ustar 00root root 0000000 0000000 {
"decimal": ",",
"thousands": ".",
"grouping": [3],
"currency": ["", "zł"]
}
d3-format-1.0.2/locale/pt-BR.json 0000664 0000000 0000000 00000000126 12750212270 0016361 0 ustar 00root root 0000000 0000000 {
"decimal": ",",
"thousands": ".",
"grouping": [3],
"currency": ["R$", ""]
}
d3-format-1.0.2/locale/ru-RU.json 0000664 0000000 0000000 00000000146 12750212270 0016411 0 ustar 00root root 0000000 0000000 {
"decimal": ",",
"thousands": "\u00a0",
"grouping": [3],
"currency": ["", "\u00a0руб."]
}
d3-format-1.0.2/locale/sv-SE.json 0000664 0000000 0000000 00000000134 12750212270 0016371 0 ustar 00root root 0000000 0000000 {
"decimal": ",",
"thousands": "\u00a0",
"grouping": [3],
"currency": ["", "SEK"]
}
d3-format-1.0.2/locale/uk-UA.json 0000664 0000000 0000000 00000000143 12750212270 0016356 0 ustar 00root root 0000000 0000000 {
"decimal": ",",
"thousands": "\u00a0",
"grouping": [3],
"currency": ["", "\u00a0₴."]
}
d3-format-1.0.2/locale/zh-CN.json 0000664 0000000 0000000 00000000126 12750212270 0016354 0 ustar 00root root 0000000 0000000 {
"decimal": ".",
"thousands": ",",
"grouping": [3],
"currency": ["¥", ""]
}
d3-format-1.0.2/package.json 0000664 0000000 0000000 00000002713 12750212270 0015575 0 ustar 00root root 0000000 0000000 {
"name": "d3-format",
"version": "1.0.2",
"description": "Format numbers for human consumption.",
"keywords": [
"d3",
"d3-module",
"format",
"localization"
],
"homepage": "https://d3js.org/d3-format/",
"license": "BSD-3-Clause",
"author": {
"name": "Mike Bostock",
"url": "http://bost.ocks.org/mike"
},
"main": "build/d3-format.js",
"module": "index",
"jsnext:main": "index",
"repository": {
"type": "git",
"url": "https://github.com/d3/d3-format.git"
},
"scripts": {
"pretest": "rm -rf build && mkdir build && rollup --banner \"$(preamble)\" -f umd -n d3 -o build/d3-format.js -- index.js",
"test": "tape 'test/**/*-test.js' && eslint index.js src",
"prepublish": "npm run test && uglifyjs --preamble \"$(preamble)\" build/d3-format.js -c -m -o build/d3-format.min.js",
"postpublish": "VERSION=`node -e 'console.log(require(\"./package.json\").version)'`; git push && git push --tags && cd ../d3.github.com && git pull && cp ../d3-format/build/d3-format.js d3-format.v1.js && cp ../d3-format/build/d3-format.min.js d3-format.v1.min.js && git add d3-format.v1.js d3-format.v1.min.js && git commit -m \"d3-format ${VERSION}\" && git push && cd - && zip -j build/d3-format.zip -- LICENSE README.md build/d3-format.js build/d3-format.min.js"
},
"devDependencies": {
"d3-queue": "3",
"eslint": "2",
"package-preamble": "0.0",
"rollup": "0.34",
"tape": "4",
"uglify-js": "2"
}
}
d3-format-1.0.2/src/ 0000775 0000000 0000000 00000000000 12750212270 0014073 5 ustar 00root root 0000000 0000000 d3-format-1.0.2/src/defaultLocale.js 0000664 0000000 0000000 00000000551 12750212270 0017176 0 ustar 00root root 0000000 0000000 import formatLocale from "./locale";
var locale;
export var format;
export var formatPrefix;
defaultLocale({
decimal: ".",
thousands: ",",
grouping: [3],
currency: ["$", ""]
});
export default function defaultLocale(definition) {
locale = formatLocale(definition);
format = locale.format;
formatPrefix = locale.formatPrefix;
return locale;
}
d3-format-1.0.2/src/exponent.js 0000664 0000000 0000000 00000000206 12750212270 0016267 0 ustar 00root root 0000000 0000000 import formatDecimal from "./formatDecimal";
export default function(x) {
return x = formatDecimal(Math.abs(x)), x ? x[1] : NaN;
}
d3-format-1.0.2/src/formatDecimal.js 0000664 0000000 0000000 00000001220 12750212270 0017173 0 ustar 00root root 0000000 0000000 // Computes the decimal coefficient and exponent of the specified number x with
// significant digits p, where x is positive and p is in [1, 21] or undefined.
// For example, formatDecimal(1.23) returns ["123", 0].
export default function(x, p) {
if ((i = (x = p ? x.toExponential(p - 1) : x.toExponential()).indexOf("e")) < 0) return null; // NaN, ±Infinity
var i, coefficient = x.slice(0, i);
// The string returned by toExponential either has the form \d\.\d+e[-+]\d+
// (e.g., 1.2e+3) or the form \de[-+]\d+ (e.g., 1e+3).
return [
coefficient.length > 1 ? coefficient[0] + coefficient.slice(2) : coefficient,
+x.slice(i + 1)
];
}
d3-format-1.0.2/src/formatDefault.js 0000664 0000000 0000000 00000000560 12750212270 0017227 0 ustar 00root root 0000000 0000000 export default function(x, p) {
x = x.toPrecision(p);
out: for (var n = x.length, i = 1, i0 = -1, i1; i < n; ++i) {
switch (x[i]) {
case ".": i0 = i1 = i; break;
case "0": if (i0 === 0) i0 = i; i1 = i; break;
case "e": break out;
default: if (i0 > 0) i0 = 0; break;
}
}
return i0 > 0 ? x.slice(0, i0) + x.slice(i1 + 1) : x;
}
d3-format-1.0.2/src/formatGroup.js 0000664 0000000 0000000 00000000733 12750212270 0016741 0 ustar 00root root 0000000 0000000 export default function(grouping, thousands) {
return function(value, width) {
var i = value.length,
t = [],
j = 0,
g = grouping[0],
length = 0;
while (i > 0 && g > 0) {
if (length + g + 1 > width) g = Math.max(1, width - length);
t.push(value.substring(i -= g, i + g));
if ((length += g + 1) > width) break;
g = grouping[j = (j + 1) % grouping.length];
}
return t.reverse().join(thousands);
};
}
d3-format-1.0.2/src/formatPrefixAuto.js 0000664 0000000 0000000 00000001143 12750212270 0017727 0 ustar 00root root 0000000 0000000 import formatDecimal from "./formatDecimal";
export var prefixExponent;
export default function(x, p) {
var d = formatDecimal(x, p);
if (!d) return x + "";
var coefficient = d[0],
exponent = d[1],
i = exponent - (prefixExponent = Math.max(-8, Math.min(8, Math.floor(exponent / 3))) * 3) + 1,
n = coefficient.length;
return i === n ? coefficient
: i > n ? coefficient + new Array(i - n + 1).join("0")
: i > 0 ? coefficient.slice(0, i) + "." + coefficient.slice(i)
: "0." + new Array(1 - i).join("0") + formatDecimal(x, Math.max(0, p + i - 1))[0]; // less than 1y!
}
d3-format-1.0.2/src/formatRounded.js 0000664 0000000 0000000 00000000712 12750212270 0017242 0 ustar 00root root 0000000 0000000 import formatDecimal from "./formatDecimal";
export default function(x, p) {
var d = formatDecimal(x, p);
if (!d) return x + "";
var coefficient = d[0],
exponent = d[1];
return exponent < 0 ? "0." + new Array(-exponent).join("0") + coefficient
: coefficient.length > exponent + 1 ? coefficient.slice(0, exponent + 1) + "." + coefficient.slice(exponent + 1)
: coefficient + new Array(exponent - coefficient.length + 2).join("0");
}
d3-format-1.0.2/src/formatSpecifier.js 0000664 0000000 0000000 00000003065 12750212270 0017557 0 ustar 00root root 0000000 0000000 import formatTypes from "./formatTypes";
// [[fill]align][sign][symbol][0][width][,][.precision][type]
var re = /^(?:(.)?([<>=^]))?([+\-\( ])?([$#])?(0)?(\d+)?(,)?(\.\d+)?([a-z%])?$/i;
export default function(specifier) {
return new FormatSpecifier(specifier);
}
function FormatSpecifier(specifier) {
if (!(match = re.exec(specifier))) throw new Error("invalid format: " + specifier);
var match,
fill = match[1] || " ",
align = match[2] || ">",
sign = match[3] || "-",
symbol = match[4] || "",
zero = !!match[5],
width = match[6] && +match[6],
comma = !!match[7],
precision = match[8] && +match[8].slice(1),
type = match[9] || "";
// The "n" type is an alias for ",g".
if (type === "n") comma = true, type = "g";
// Map invalid types to the default format.
else if (!formatTypes[type]) type = "";
// If zero fill is specified, padding goes after sign and before digits.
if (zero || (fill === "0" && align === "=")) zero = true, fill = "0", align = "=";
this.fill = fill;
this.align = align;
this.sign = sign;
this.symbol = symbol;
this.zero = zero;
this.width = width;
this.comma = comma;
this.precision = precision;
this.type = type;
}
FormatSpecifier.prototype.toString = function() {
return this.fill
+ this.align
+ this.sign
+ this.symbol
+ (this.zero ? "0" : "")
+ (this.width == null ? "" : Math.max(1, this.width | 0))
+ (this.comma ? "," : "")
+ (this.precision == null ? "" : "." + Math.max(0, this.precision | 0))
+ this.type;
};
d3-format-1.0.2/src/formatTypes.js 0000664 0000000 0000000 00000001516 12750212270 0016751 0 ustar 00root root 0000000 0000000 import formatDefault from "./formatDefault";
import formatPrefixAuto from "./formatPrefixAuto";
import formatRounded from "./formatRounded";
export default {
"": formatDefault,
"%": function(x, p) { return (x * 100).toFixed(p); },
"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 Math.round(x).toString(8); },
"p": function(x, p) { return formatRounded(x * 100, p); },
"r": formatRounded,
"s": formatPrefixAuto,
"X": function(x) { return Math.round(x).toString(16).toUpperCase(); },
"x": function(x) { return Math.round(x).toString(16); }
};
d3-format-1.0.2/src/locale.js 0000664 0000000 0000000 00000012177 12750212270 0015700 0 ustar 00root root 0000000 0000000 import exponent from "./exponent";
import formatGroup from "./formatGroup";
import formatSpecifier from "./formatSpecifier";
import formatTypes from "./formatTypes";
import {prefixExponent} from "./formatPrefixAuto";
var prefixes = ["y","z","a","f","p","n","µ","m","","k","M","G","T","P","E","Z","Y"];
function identity(x) {
return x;
}
export default function(locale) {
var group = locale.grouping && locale.thousands ? formatGroup(locale.grouping, locale.thousands) : identity,
currency = locale.currency,
decimal = locale.decimal;
function newFormat(specifier) {
specifier = formatSpecifier(specifier);
var fill = specifier.fill,
align = specifier.align,
sign = specifier.sign,
symbol = specifier.symbol,
zero = specifier.zero,
width = specifier.width,
comma = specifier.comma,
precision = specifier.precision,
type = specifier.type;
// Compute the prefix and suffix.
// For SI-prefix, the suffix is lazily computed.
var prefix = symbol === "$" ? currency[0] : symbol === "#" && /[boxX]/.test(type) ? "0" + type.toLowerCase() : "",
suffix = symbol === "$" ? currency[1] : /[%p]/.test(type) ? "%" : "";
// What format function should we use?
// Is this an integer type?
// Can this type generate exponential notation?
var formatType = formatTypes[type],
maybeSuffix = !type || /[defgprs%]/.test(type);
// Set the default precision if not specified,
// or clamp the specified precision to the supported range.
// For significant precision, it must be in [1, 21].
// For fixed precision, it must be in [0, 20].
precision = precision == null ? (type ? 6 : 12)
: /[gprs]/.test(type) ? Math.max(1, Math.min(21, precision))
: Math.max(0, Math.min(20, precision));
function format(value) {
var valuePrefix = prefix,
valueSuffix = suffix,
i, n, c;
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);
// If the original value was negative, it may be rounded to zero during
// formatting; treat this as (positive) zero.
if (valueNegative) {
i = -1, n = value.length;
valueNegative = false;
while (++i < n) {
if (c = value.charCodeAt(i), (48 < c && c < 58)
|| (type === "x" && 96 < c && c < 103)
|| (type === "X" && 64 < c && c < 71)) {
valueNegative = true;
break;
}
}
}
// 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) {
i = -1, n = value.length;
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;
}
}
}
}
// If the fill character is not "0", grouping is applied before padding.
if (comma && !zero) value = group(value, Infinity);
// Compute the padding.
var length = valuePrefix.length + value.length + valueSuffix.length,
padding = length < width ? new Array(width - length + 1).join(fill) : "";
// If the fill character is "0", grouping is applied after padding.
if (comma && zero) value = group(padding + value, padding.length ? width - valueSuffix.length : Infinity), padding = "";
// Reconstruct the final output based on the desired alignment.
switch (align) {
case "<": return valuePrefix + value + valueSuffix + padding;
case "=": return valuePrefix + padding + value + valueSuffix;
case "^": return padding.slice(0, length = padding.length >> 1) + valuePrefix + value + valueSuffix + padding.slice(length);
}
return padding + valuePrefix + value + valueSuffix;
}
format.toString = function() {
return specifier + "";
};
return format;
}
function formatPrefix(specifier, value) {
var f = newFormat((specifier = formatSpecifier(specifier), specifier.type = "f", specifier)),
e = Math.max(-8, Math.min(8, Math.floor(exponent(value) / 3))) * 3,
k = Math.pow(10, -e),
prefix = prefixes[8 + e / 3];
return function(value) {
return f(k * value) + prefix;
};
}
return {
format: newFormat,
formatPrefix: formatPrefix
};
}
d3-format-1.0.2/src/precisionFixed.js 0000664 0000000 0000000 00000000167 12750212270 0017410 0 ustar 00root root 0000000 0000000 import exponent from "./exponent";
export default function(step) {
return Math.max(0, -exponent(Math.abs(step)));
}
d3-format-1.0.2/src/precisionPrefix.js 0000664 0000000 0000000 00000000276 12750212270 0017607 0 ustar 00root root 0000000 0000000 import exponent from "./exponent";
export default function(step, value) {
return Math.max(0, Math.max(-8, Math.min(8, Math.floor(exponent(value) / 3))) * 3 - exponent(Math.abs(step)));
}
d3-format-1.0.2/src/precisionRound.js 0000664 0000000 0000000 00000000272 12750212270 0017435 0 ustar 00root root 0000000 0000000 import exponent from "./exponent";
export default function(step, max) {
step = Math.abs(step), max = Math.abs(max) - step;
return Math.max(0, exponent(max) - exponent(step)) + 1;
}
d3-format-1.0.2/test/ 0000775 0000000 0000000 00000000000 12750212270 0014263 5 ustar 00root root 0000000 0000000 d3-format-1.0.2/test/defaultLocale-test.js 0000664 0000000 0000000 00000002262 12750212270 0020344 0 ustar 00root root 0000000 0000000 var tape = require("tape"),
d3 = require("../");
var enUs = {
"decimal": ".",
"thousands": ",",
"grouping": [3],
"currency": ["$", ""]
};
var frFr = {
"decimal": ",",
"thousands": ".",
"grouping": [3],
"currency": ["", "\u00a0€"]
};
tape("d3.formatDefaultLocale(definition) returns the new default locale", function(test) {
var locale = d3.formatDefaultLocale(frFr);
try {
test.equal(locale.format("$,.2f")(12345678.90), "12.345.678,90 €");
test.end();
} finally {
d3.formatDefaultLocale(enUs);
}
});
tape("d3.formatDefaultLocale(definition) affects d3.format", function(test) {
var locale = d3.formatDefaultLocale(frFr);
try {
test.equal(d3.format, locale.format);
test.equal(d3.format("$,.2f")(12345678.90), "12.345.678,90 €");
test.end();
} finally {
d3.formatDefaultLocale(enUs);
}
});
tape("d3.formatDefaultLocale(definition) affects d3.formatPrefix", function(test) {
var locale = d3.formatDefaultLocale(frFr);
try {
test.equal(d3.formatPrefix, locale.formatPrefix);
test.equal(d3.formatPrefix(",.2", 1e3)(12345678.90), "12.345,68k");
test.end();
} finally {
d3.formatDefaultLocale(enUs);
}
});
d3-format-1.0.2/test/format-test.js 0000664 0000000 0000000 00000003257 12750212270 0017075 0 ustar 00root root 0000000 0000000 var tape = require("tape"),
format = require("../");
tape("format(specifier)(number) returns a string", function(test) {
test.equal(typeof format.format("d")(0), "string");
test.end();
});
tape("format(specifier).toString() returns the normalized specifier", function(test) {
test.equal(format.format("d") + "", " >-d");
test.end();
});
tape("format(specifier) throws an error for invalid formats", function(test) {
test.throws(function() { format.format("foo"); }, /invalid format: foo/);
test.throws(function() { format.format(".-2s"); }, /invalid format: \.-2s/);
test.throws(function() { format.format(".f"); }, /invalid format: \.f/);
test.end();
});
tape("format(\",.\") unreasonable precision values are clamped to reasonable values", function(test) {
test.equal(format.format(".30f")(0), "0.00000000000000000000");
test.equal(format.format(".0g")(1), "1");
test.end();
});
tape("format(\"s\") handles very small and very large values", function(test) {
test.equal(format.format("s")(Number.MIN_VALUE), "0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005y");
test.equal(format.format("s")(Number.MAX_VALUE), "179769000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000Y");
test.end();
});
d3-format-1.0.2/test/format-type-%-test.js 0000664 0000000 0000000 00000002167 12750212270 0020175 0 ustar 00root root 0000000 0000000 var tape = require("tape"),
format = require("../");
tape("format(\"%\") can output a whole percentage", function(test) {
var f = format.format(".0%");
test.equal(f(0), "0%");
test.equal(f(.042), "4%");
test.equal(f(.42), "42%");
test.equal(f(4.2), "420%");
test.equal(f(-.042), "-4%");
test.equal(f(-.42), "-42%");
test.equal(f(-4.2), "-420%");
test.end();
});
tape("format(\".%\") can output a percentage with precision", function(test) {
var f = format.format(".1%");
test.equal(f(.234), "23.4%");
var f = format.format(".2%");
test.equal(f(.234), "23.40%");
test.end();
});
tape("format(\"%\") fill respects suffix", function(test) {
test.equal(format.format("020.0%")(42), "0000000000000004200%");
test.equal(format.format("20.0%")(42), " 4200%");
test.end();
});
tape("format(\"^%\") align center puts suffix adjacent to number", function(test) {
test.equal(format.format("^21.0%")(.42), " 42% ");
test.equal(format.format("^21,.0%")(422), " 42,200% ");
test.equal(format.format("^21,.0%")(-422), " -42,200% ");
test.end();
});
d3-format-1.0.2/test/format-type-b-test.js 0000664 0000000 0000000 00000000447 12750212270 0020271 0 ustar 00root root 0000000 0000000 var tape = require("tape"),
format = require("../");
tape("format(\"b\") binary", function(test) {
test.equal(format.format("b")(10), "1010");
test.end();
});
tape("format(\"#b\") binary with prefix", function(test) {
test.equal(format.format("#b")(10), "0b1010");
test.end();
});
d3-format-1.0.2/test/format-type-c-test.js 0000664 0000000 0000000 00000001032 12750212270 0020261 0 ustar 00root root 0000000 0000000 var tape = require("tape"),
format = require("../");
tape("format(\"c\") unicode character", function(test) {
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.formatLocale({decimal: "/"}).format("c")("."), ".");
test.end();
});
d3-format-1.0.2/test/format-type-d-test.js 0000664 0000000 0000000 00000021371 12750212270 0020272 0 ustar 00root root 0000000 0000000 var tape = require("tape"),
format = require("../");
tape("format(\"d\") can zero fill", function(test) {
var f = format.format("08d");
test.equal(f(0), "00000000");
test.equal(f(42), "00000042");
test.equal(f(42000000), "42000000");
test.equal(f(420000000), "420000000");
test.equal(f(-4), "-0000004");
test.equal(f(-42), "-0000042");
test.equal(f(-4200000), "-4200000");
test.equal(f(-42000000), "-42000000");
test.end();
});
tape("format(\"d\") can space fill", function(test) {
var f = format.format("8d");
test.equal(f(0), " 0");
test.equal(f(42), " 42");
test.equal(f(42000000), "42000000");
test.equal(f(420000000), "420000000");
test.equal(f(-4), " -4");
test.equal(f(-42), " -42");
test.equal(f(-4200000), "-4200000");
test.equal(f(-42000000), "-42000000");
test.end();
});
tape("format(\"d\") can underscore fill", function(test) {
var f = format.format("_>8d");
test.equal(f(0), "_______0");
test.equal(f(42), "______42");
test.equal(f(42000000), "42000000");
test.equal(f(420000000), "420000000");
test.equal(f(-4), "______-4");
test.equal(f(-42), "_____-42");
test.equal(f(-4200000), "-4200000");
test.equal(f(-42000000), "-42000000");
test.end();
});
tape("format(\"d\") can zero fill with sign and group", function(test) {
var f = format.format("+08,d");
test.equal(f(0), "+0,000,000");
test.equal(f(42), "+0,000,042");
test.equal(f(42000000), "+42,000,000");
test.equal(f(420000000), "+420,000,000");
test.equal(f(-4), "-0,000,004");
test.equal(f(-42), "-0,000,042");
test.equal(f(-4200000), "-4,200,000");
test.equal(f(-42000000), "-42,000,000");
test.end();
});
tape("format(\"d\") always uses zero precision", function(test) {
var f = format.format(".2d");
test.equal(f(0), "0");
test.equal(f(42), "42");
test.equal(f(-4.2), "-4");
test.end();
});
tape("format(\"d\") rounds non-integers", function(test) {
var f = format.format("d");
test.equal(f(4.2), "4");
test.end();
});
tape("format(\",d\") can group thousands", function(test) {
var f = format.format(",d");
test.equal(f(0), "0");
test.equal(f(42), "42");
test.equal(f(42000000), "42,000,000");
test.equal(f(420000000), "420,000,000");
test.equal(f(-4), "-4");
test.equal(f(-42), "-42");
test.equal(f(-4200000), "-4,200,000");
test.equal(f(-42000000), "-42,000,000");
test.equal(f(1e21), "1e+21");
test.end();
});
tape("format(\"0,d\") can group thousands and zero fill", function(test) {
test.equal(format.format("01,d")(0), "0");
test.equal(format.format("01,d")(0), "0");
test.equal(format.format("02,d")(0), "00");
test.equal(format.format("03,d")(0), "000");
test.equal(format.format("04,d")(0), "0,000");
test.equal(format.format("05,d")(0), "0,000");
test.equal(format.format("06,d")(0), "00,000");
test.equal(format.format("08,d")(0), "0,000,000");
test.equal(format.format("013,d")(0), "0,000,000,000");
test.equal(format.format("021,d")(0), "0,000,000,000,000,000");
test.equal(format.format("013,d")(-42000000), "-0,042,000,000");
test.equal(format.format("012,d")(1e21), "0,000,001e+21");
test.equal(format.format("013,d")(1e21), "0,000,001e+21");
test.equal(format.format("014,d")(1e21), "00,000,001e+21");
test.equal(format.format("015,d")(1e21), "000,000,001e+21");
test.end();
});
tape("format(\"0,d\") can group thousands and zero fill with overflow", function(test) {
test.equal(format.format("01,d")(1), "1");
test.equal(format.format("01,d")(1), "1");
test.equal(format.format("02,d")(12), "12");
test.equal(format.format("03,d")(123), "123");
test.equal(format.format("05,d")(12345), "12,345");
test.equal(format.format("08,d")(12345678), "12,345,678");
test.equal(format.format("013,d")(1234567890123), "1,234,567,890,123");
test.end();
});
tape("format(\",d\") can group thousands and space fill", function(test) {
test.equal(format.format("1,d")(0), "0");
test.equal(format.format("1,d")(0), "0");
test.equal(format.format("2,d")(0), " 0");
test.equal(format.format("3,d")(0), " 0");
test.equal(format.format("5,d")(0), " 0");
test.equal(format.format("8,d")(0), " 0");
test.equal(format.format("13,d")(0), " 0");
test.equal(format.format("21,d")(0), " 0");
test.end();
});
tape("format(\",d\") can group thousands and space fill with overflow", function(test) {
test.equal(format.format("1,d")(1), "1");
test.equal(format.format("1,d")(1), "1");
test.equal(format.format("2,d")(12), "12");
test.equal(format.format("3,d")(123), "123");
test.equal(format.format("5,d")(12345), "12,345");
test.equal(format.format("8,d")(12345678), "12,345,678");
test.equal(format.format("13,d")(1234567890123), "1,234,567,890,123");
test.end();
});
tape("format(\"d\") align right", function(test) {
test.equal(format.format(">1,d")(0), "0");
test.equal(format.format(">1,d")(0), "0");
test.equal(format.format(">2,d")(0), " 0");
test.equal(format.format(">3,d")(0), " 0");
test.equal(format.format(">5,d")(0), " 0");
test.equal(format.format(">8,d")(0), " 0");
test.equal(format.format(">13,d")(0), " 0");
test.equal(format.format(">21,d")(0), " 0");
test.equal(format.format(">21,d")(1000), " 1,000");
test.equal(format.format(">21,d")(1e21), " 1e+21");
test.end();
});
tape("format(\"^d\") align center", function(test) {
test.equal(format.format("^1,d")(0), "0");
test.equal(format.format("^1,d")(0), "0");
test.equal(format.format("^2,d")(0), "0 ");
test.equal(format.format("^3,d")(0), " 0 ");
test.equal(format.format("^5,d")(0), " 0 ");
test.equal(format.format("^8,d")(0), " 0 ");
test.equal(format.format("^13,d")(0), " 0 ");
test.equal(format.format("^21,d")(0), " 0 ");
test.equal(format.format("^21,d")(1000), " 1,000 ");
test.equal(format.format("^21,d")(1e21), " 1e+21 ");
test.end();
});
tape("format(\"=+,d\") pad after sign", function(test) {
test.equal(format.format("=+1,d")(0), "+0");
test.equal(format.format("=+1,d")(0), "+0");
test.equal(format.format("=+2,d")(0), "+0");
test.equal(format.format("=+3,d")(0), "+ 0");
test.equal(format.format("=+5,d")(0), "+ 0");
test.equal(format.format("=+8,d")(0), "+ 0");
test.equal(format.format("=+13,d")(0), "+ 0");
test.equal(format.format("=+21,d")(0), "+ 0");
test.equal(format.format("=+21,d")(1e21), "+ 1e+21");
test.end();
});
tape("format(\"=+$,d\") pad after sign with currency", function(test) {
test.equal(format.format("=+$1,d")(0), "+$0");
test.equal(format.format("=+$1,d")(0), "+$0");
test.equal(format.format("=+$2,d")(0), "+$0");
test.equal(format.format("=+$3,d")(0), "+$0");
test.equal(format.format("=+$5,d")(0), "+$ 0");
test.equal(format.format("=+$8,d")(0), "+$ 0");
test.equal(format.format("=+$13,d")(0), "+$ 0");
test.equal(format.format("=+$21,d")(0), "+$ 0");
test.equal(format.format("=+$21,d")(1e21), "+$ 1e+21");
test.end();
});
tape("format(\" ,d\") a space can denote positive numbers", function(test) {
test.equal(format.format(" 1,d")(-1), "-1");
test.equal(format.format(" 1,d")(0), " 0");
test.equal(format.format(" 2,d")(0), " 0");
test.equal(format.format(" 3,d")(0), " 0");
test.equal(format.format(" 5,d")(0), " 0");
test.equal(format.format(" 8,d")(0), " 0");
test.equal(format.format(" 13,d")(0), " 0");
test.equal(format.format(" 21,d")(0), " 0");
test.equal(format.format(" 21,d")(1e21), " 1e+21");
test.end();
});
tape("format(\"-,d\") explicitly only use a sign for negative numbers", function(test) {
test.equal(format.format("-1,d")(-1), "-1");
test.equal(format.format("-1,d")(0), "0");
test.equal(format.format("-2,d")(0), " 0");
test.equal(format.format("-3,d")(0), " 0");
test.equal(format.format("-5,d")(0), " 0");
test.equal(format.format("-8,d")(0), " 0");
test.equal(format.format("-13,d")(0), " 0");
test.equal(format.format("-21,d")(0), " 0");
test.end();
});
tape("format(\"d\") can format negative zero as zero", function(test) {
test.equal(format.format("1d")(-0), "0");
test.equal(format.format("1d")(-1e-12), "0");
test.end();
});
d3-format-1.0.2/test/format-type-e-test.js 0000664 0000000 0000000 00000001647 12750212270 0020277 0 ustar 00root root 0000000 0000000 var tape = require("tape"),
format = require("../");
tape("format(\"e\") can output exponent notation", function(test) {
var f = format.format("e");
test.equal(f(0), "0.000000e+0");
test.equal(f(42), "4.200000e+1");
test.equal(f(42000000), "4.200000e+7");
test.equal(f(420000000), "4.200000e+8");
test.equal(f(-4), "-4.000000e+0");
test.equal(f(-42), "-4.200000e+1");
test.equal(f(-4200000), "-4.200000e+6");
test.equal(f(-42000000), "-4.200000e+7");
test.equal(format.format(".0e")(42), "4e+1")
test.equal(format.format(".3e")(42), "4.200e+1")
test.end();
});
tape("format(\"e\") can format negative zero as zero", function(test) {
test.equal(format.format("1e")(-0), "0.000000e+0");
test.equal(format.format("1e")(-1e-12), "-1.000000e-12");
test.end();
});
tape("format(\",e\") does not group Infinity", function(test) {
test.equal(format.format(",e")(Infinity), "Infinity");
test.end();
});
d3-format-1.0.2/test/format-type-f-test.js 0000664 0000000 0000000 00000003767 12750212270 0020305 0 ustar 00root root 0000000 0000000 var tape = require("tape"),
format = require("../");
tape("format(\"f\") can output fixed-point notation", function(test) {
test.equal(format.format(".1f")(0.49), "0.5");
test.equal(format.format(".2f")(0.449), "0.45");
test.equal(format.format(".3f")(0.4449), "0.445");
test.equal(format.format(".5f")(0.444449), "0.44445");
test.equal(format.format(".1f")(100), "100.0");
test.equal(format.format(".2f")(100), "100.00");
test.equal(format.format(".3f")(100), "100.000");
test.equal(format.format(".5f")(100), "100.00000");
test.end();
});
tape("format(\"+$,f\") can output a currency with comma-grouping and sign", function(test) {
var f = format.format("+$,.2f");
test.equal(f(0), "+$0.00");
test.equal(f(0.429), "+$0.43");
test.equal(f(-0.429), "-$0.43");
test.equal(f(-1), "-$1.00");
test.equal(f(1e4), "+$10,000.00");
test.end();
});
tape("format(\",.f\") can group thousands, space fill, and round to significant digits", function(test) {
test.equal(format.format("10,.1f")(123456.49), " 123,456.5");
test.equal(format.format("10,.2f")(1234567.449), "1,234,567.45");
test.equal(format.format("10,.3f")(12345678.4449), "12,345,678.445");
test.equal(format.format("10,.5f")(123456789.444449), "123,456,789.44445");
test.equal(format.format("10,.1f")(123456), " 123,456.0");
test.equal(format.format("10,.2f")(1234567), "1,234,567.00");
test.equal(format.format("10,.3f")(12345678), "12,345,678.000");
test.equal(format.format("10,.5f")(123456789), "123,456,789.00000");
test.end();
});
tape("format(\"f\") can display integers in fixed-point notation", function(test) {
test.equal(format.format("f")(42), "42.000000");
test.end();
});
tape("format(\"f\") can format negative zero as zero", function(test) {
test.equal(format.format("1f")(-0), "0.000000");
test.equal(format.format("1f")(-1e-12), "0.000000");
test.end();
});
tape("format(\",f\") does not group Infinity", function(test) {
test.equal(format.format(",f")(Infinity), "Infinity");
test.end();
});
d3-format-1.0.2/test/format-type-g-test.js 0000664 0000000 0000000 00000002242 12750212270 0020271 0 ustar 00root root 0000000 0000000 var tape = require("tape"),
format = require("../");
tape("format(\"g\") can output general notation", function(test) {
test.equal(format.format(".1g")(0.049), "0.05");
test.equal(format.format(".1g")(0.49), "0.5");
test.equal(format.format(".2g")(0.449), "0.45");
test.equal(format.format(".3g")(0.4449), "0.445");
test.equal(format.format(".5g")(0.444449), "0.44445");
test.equal(format.format(".1g")(100), "1e+2");
test.equal(format.format(".2g")(100), "1.0e+2");
test.equal(format.format(".3g")(100), "100");
test.equal(format.format(".5g")(100), "100.00");
test.equal(format.format(".5g")(100.2), "100.20");
test.equal(format.format(".2g")(0.002), "0.0020");
test.end();
});
tape("format(\",g\") can group thousands with general notation", function(test) {
var f = format.format(",.12g");
test.equal(f(0), "0.00000000000");
test.equal(f(42), "42.0000000000");
test.equal(f(42000000), "42,000,000.0000");
test.equal(f(420000000), "420,000,000.000");
test.equal(f(-4), "-4.00000000000");
test.equal(f(-42), "-42.0000000000");
test.equal(f(-4200000), "-4,200,000.00000");
test.equal(f(-42000000), "-42,000,000.0000");
test.end();
});
d3-format-1.0.2/test/format-type-n-test.js 0000664 0000000 0000000 00000002204 12750212270 0020276 0 ustar 00root root 0000000 0000000 var tape = require("tape"),
format = require("../");
tape("format(\"n\") is an alias for \",g\"", function(test) {
var f = format.format(".12n");
test.equal(f(0), "0.00000000000");
test.equal(f(42), "42.0000000000");
test.equal(f(42000000), "42,000,000.0000");
test.equal(f(420000000), "420,000,000.000");
test.equal(f(-4), "-4.00000000000");
test.equal(f(-42), "-42.0000000000");
test.equal(f(-4200000), "-4,200,000.00000");
test.equal(f(-42000000), "-42,000,000.0000");
test.equal(f(.0042), "0.00420000000000");
test.equal(f(.42), "0.420000000000");
test.equal(f(1e21), "1.00000000000e+21");
test.end();
});
tape("format(\"n\") uses zero padding", function(test) {
test.equal(format.format("01.0n")(0), "0");
test.equal(format.format("02.0n")(0), "00");
test.equal(format.format("03.0n")(0), "000");
test.equal(format.format("05.0n")(0), "0,000");
test.equal(format.format("08.0n")(0), "0,000,000");
test.equal(format.format("013.0n")(0), "0,000,000,000");
test.equal(format.format("021.0n")(0), "0,000,000,000,000,000");
test.equal(format.format("013.8n")(-42000000), "-0,042,000,000");
test.end();
});
d3-format-1.0.2/test/format-type-none-test.js 0000664 0000000 0000000 00000004310 12750212270 0021000 0 ustar 00root root 0000000 0000000 var tape = require("tape"),
format = require("../");
tape("format(\".[precision]\") uses significant precision and trims insignificant zeros", function(test) {
test.equal(format.format(".1")(4.9), "5");
test.equal(format.format(".1")(0.49), "0.5");
test.equal(format.format(".2")(4.9), "4.9");
test.equal(format.format(".2")(0.49), "0.49");
test.equal(format.format(".2")(0.449), "0.45");
test.equal(format.format(".3")(4.9), "4.9");
test.equal(format.format(".3")(0.49), "0.49");
test.equal(format.format(".3")(0.449), "0.449");
test.equal(format.format(".3")(0.4449), "0.445");
test.equal(format.format(".5")(0.444449), "0.44445");
test.end();
});
tape("format(\".[precision]\") does not trim significant zeros", function(test) {
test.equal(format.format(".5")(10), "10");
test.equal(format.format(".5")(100), "100");
test.equal(format.format(".5")(1000), "1000");
test.equal(format.format(".5")(21010), "21010");
test.equal(format.format(".5")(1.10001), "1.1");
test.equal(format.format(".5")(1.10001e6), "1.1e+6");
test.equal(format.format(".6")(1.10001), "1.10001");
test.equal(format.format(".6")(1.10001e6), "1.10001e+6");
test.end();
});
tape("format(\".[precision]\") also trims the decimal point if there are only insignificant zeros", function(test) {
test.equal(format.format(".5")(1.00001), "1");
test.equal(format.format(".5")(1.00001e6), "1e+6");
test.equal(format.format(".6")(1.00001), "1.00001");
test.equal(format.format(".6")(1.00001e6), "1.00001e+6");
test.end();
});
tape("format(\"$\") can output a currency", function(test) {
var f = format.format("$");
test.equal(f(0), "$0");
test.equal(f(.042), "$0.042");
test.equal(f(.42), "$0.42");
test.equal(f(4.2), "$4.2");
test.equal(f(-.042), "-$0.042");
test.equal(f(-.42), "-$0.42");
test.equal(f(-4.2), "-$4.2");
test.end();
});
tape("format(\"($\") can output a currency with parentheses for negative values", function(test) {
var f = format.format("($");
test.equal(f(0), "$0");
test.equal(f(.042), "$0.042");
test.equal(f(.42), "$0.42");
test.equal(f(4.2), "$4.2");
test.equal(f(-.042), "($0.042)");
test.equal(f(-.42), "($0.42)");
test.equal(f(-4.2), "($4.2)");
test.end();
});
d3-format-1.0.2/test/format-type-o-test.js 0000664 0000000 0000000 00000000441 12750212270 0020300 0 ustar 00root root 0000000 0000000 var tape = require("tape"),
format = require("../");
tape("format(\"o\") octal", function(test) {
test.equal(format.format("o")(10), "12");
test.end();
});
tape("format(\"#o\") octal with prefix", function(test) {
test.equal(format.format("#o")(10), "0o12");
test.end();
});
d3-format-1.0.2/test/format-type-p-test.js 0000664 0000000 0000000 00000001611 12750212270 0020301 0 ustar 00root root 0000000 0000000 var tape = require("tape"),
format = require("../");
tape("format(\"p\") can output a percentage", function(test) {
var f = format.format("p");
test.equal(f(.00123), "0.123000%");
test.equal(f(.0123), "1.23000%");
test.equal(f(.123), "12.3000%");
test.equal(f(.234), "23.4000%");
test.equal(f(1.23), "123.000%");
test.equal(f(-.00123), "-0.123000%");
test.equal(f(-.0123), "-1.23000%");
test.equal(f(-.123), "-12.3000%");
test.equal(f(-1.23), "-123.000%");
test.end();
});
tape("format(\"+p\") can output a percentage with rounding and sign", function(test) {
var f = format.format("+.2p");
test.equal(f(.00123), "+0.12%");
test.equal(f(.0123), "+1.2%");
test.equal(f(.123), "+12%");
test.equal(f(1.23), "+120%");
test.equal(f(-.00123), "-0.12%");
test.equal(f(-.0123), "-1.2%");
test.equal(f(-.123), "-12%");
test.equal(f(-1.23), "-120%");
test.end();
});
d3-format-1.0.2/test/format-type-r-test.js 0000664 0000000 0000000 00000003512 12750212270 0020305 0 ustar 00root root 0000000 0000000 var tape = require("tape"),
format = require("../");
tape("format(\"r\") can round to significant digits", function(test) {
test.equal(format.format(".2r")(0), "0.0");
test.equal(format.format(".1r")(0.049), "0.05");
test.equal(format.format(".1r")(-0.049), "-0.05");
test.equal(format.format(".1r")(0.49), "0.5");
test.equal(format.format(".1r")(-0.49), "-0.5");
test.equal(format.format(".2r")(0.449), "0.45");
test.equal(format.format(".3r")(0.4449), "0.445");
test.equal(format.format(".3r")(1.00), "1.00");
test.equal(format.format(".3r")(0.9995), "1.00");
test.equal(format.format(".5r")(0.444449), "0.44445");
test.equal(format.format("r")(123.45), "123.450");
test.equal(format.format(".1r")(123.45), "100");
test.equal(format.format(".2r")(123.45), "120");
test.equal(format.format(".3r")(123.45), "123");
test.equal(format.format(".4r")(123.45), "123.5");
test.equal(format.format(".5r")(123.45), "123.45");
test.equal(format.format(".6r")(123.45), "123.450");
test.equal(format.format(".1r")(.9), "0.9");
test.equal(format.format(".1r")(.09), "0.09");
test.equal(format.format(".1r")(.949), "0.9");
test.equal(format.format(".1r")(.0949), "0.09");
test.equal(format.format(".1r")(.0000000129), "0.00000001");
test.equal(format.format(".2r")(.0000000129), "0.000000013");
test.equal(format.format(".2r")(.00000000129), "0.0000000013");
test.equal(format.format(".3r")(.00000000129), "0.00000000129");
test.equal(format.format(".4r")(.00000000129), "0.000000001290");
test.equal(format.format(".10r")(.9999999999), "0.9999999999");
test.equal(format.format(".15r")(.999999999999999), "0.999999999999999");
test.end();
});
tape("format(\"r\") can round very small numbers", function(test) {
var f = format.format(".2r");
test.equal(f(1e-22), "0.00000000000000000000010");
test.end();
});
d3-format-1.0.2/test/format-type-s-test.js 0000664 0000000 0000000 00000012607 12750212270 0020313 0 ustar 00root root 0000000 0000000 var tape = require("tape"),
format = require("../");
tape("format(\"s\") outputs SI-prefix notation with default precision 6", function(test) {
var f = format.format("s");
test.equal(f(0), "0.00000");
test.equal(f(1), "1.00000");
test.equal(f(10), "10.0000");
test.equal(f(100), "100.000");
test.equal(f(999.5), "999.500");
test.equal(f(999500), "999.500k");
test.equal(f(1000), "1.00000k");
test.equal(f(100), "100.000");
test.equal(f(1400), "1.40000k");
test.equal(f(1500.5), "1.50050k");
test.equal(f(.00001), "10.0000µ");
test.equal(f(.000001), "1.00000µ");
test.end();
});
tape("format(\"[.precision]s\") outputs SI-prefix notation with precision significant digits", function(test) {
var f = format.format(".3s");
test.equal(f(0), "0.00");
test.equal(f(1), "1.00");
test.equal(f(10), "10.0");
test.equal(f(100), "100");
test.equal(f(999.5), "1.00k");
test.equal(f(999500), "1.00M");
test.equal(f(1000), "1.00k");
test.equal(f(1500.5), "1.50k");
test.equal(f(145500000), "146M");
test.equal(f(145999999.999999347), "146M");
test.equal(f(1e26), "100Y");
test.equal(f(.000001), "1.00µ");
test.equal(f(.009995), "10.0m");
var f = format.format(".4s");
test.equal(f(999.5), "999.5");
test.equal(f(999500), "999.5k");
test.equal(f(.009995), "9.995m");
test.end();
});
tape("format(\"s\") formats numbers smaller than 1e-24 with yocto", function(test) {
var f = format.format(".8s");
test.equal(f(1.29e-30), "0.0000013y"); // Note: rounded!
test.equal(f(1.29e-29), "0.0000129y");
test.equal(f(1.29e-28), "0.0001290y");
test.equal(f(1.29e-27), "0.0012900y");
test.equal(f(1.29e-26), "0.0129000y");
test.equal(f(1.29e-25), "0.1290000y");
test.equal(f(1.29e-24), "1.2900000y");
test.equal(f(1.29e-23), "12.900000y");
test.equal(f(1.29e-22), "129.00000y");
test.equal(f(1.29e-21), "1.2900000z");
test.equal(f(-1.29e-30), "-0.0000013y"); // Note: rounded!
test.equal(f(-1.29e-29), "-0.0000129y");
test.equal(f(-1.29e-28), "-0.0001290y");
test.equal(f(-1.29e-27), "-0.0012900y");
test.equal(f(-1.29e-26), "-0.0129000y");
test.equal(f(-1.29e-25), "-0.1290000y");
test.equal(f(-1.29e-24), "-1.2900000y");
test.equal(f(-1.29e-23), "-12.900000y");
test.equal(f(-1.29e-22), "-129.00000y");
test.equal(f(-1.29e-21), "-1.2900000z");
test.end();
});
tape("format(\"s\") formats numbers larger than 1e24 with yotta", function(test) {
var f = format.format(".8s");
test.equal(f(1.23e+21), "1.2300000Z");
test.equal(f(1.23e+22), "12.300000Z");
test.equal(f(1.23e+23), "123.00000Z");
test.equal(f(1.23e+24), "1.2300000Y");
test.equal(f(1.23e+25), "12.300000Y");
test.equal(f(1.23e+26), "123.00000Y");
test.equal(f(1.23e+27), "1230.0000Y");
test.equal(f(1.23e+28), "12300.000Y");
test.equal(f(1.23e+29), "123000.00Y");
test.equal(f(1.23e+30), "1230000.0Y");
test.equal(f(-1.23e+21), "-1.2300000Z");
test.equal(f(-1.23e+22), "-12.300000Z");
test.equal(f(-1.23e+23), "-123.00000Z");
test.equal(f(-1.23e+24), "-1.2300000Y");
test.equal(f(-1.23e+25), "-12.300000Y");
test.equal(f(-1.23e+26), "-123.00000Y");
test.equal(f(-1.23e+27), "-1230.0000Y");
test.equal(f(-1.23e+28), "-12300.000Y");
test.equal(f(-1.23e+29), "-123000.00Y");
test.equal(f(-1.23e+30), "-1230000.0Y");
test.end();
});
tape("format(\"$s\") outputs SI-prefix notation with a currency symbol", function(test) {
var f = format.format("$.2s");
test.equal(f(0), "$0.0");
test.equal(f(2.5e5), "$250k");
test.equal(f(-2.5e8), "-$250M");
test.equal(f(2.5e11), "$250G");
var f = format.format("$.3s");
test.equal(f(0), "$0.00");
test.equal(f(1), "$1.00");
test.equal(f(10), "$10.0");
test.equal(f(100), "$100");
test.equal(f(999.5), "$1.00k");
test.equal(f(999500), "$1.00M");
test.equal(f(1000), "$1.00k");
test.equal(f(1500.5), "$1.50k");
test.equal(f(145500000), "$146M");
test.equal(f(145999999.999999347), "$146M");
test.equal(f(1e26), "$100Y");
test.equal(f(.000001), "$1.00µ");
test.equal(f(.009995), "$10.0m");
var f = format.format("$.4s");
test.equal(f(999.5), "$999.5");
test.equal(f(999500), "$999.5k");
test.equal(f(.009995), "$9.995m");
test.end();
});
tape("format(\"s\") SI-prefix notation precision is consistent for small and large numbers", function(test) {
var f = format.format(".0s");
test.equal(f(1e-5), "10µ");
test.equal(f(1e-4), "100µ");
test.equal(f(1e-3), "1m");
test.equal(f(1e-2), "10m");
test.equal(f(1e-1), "100m");
test.equal(f(1e+0), "1");
test.equal(f(1e+1), "10");
test.equal(f(1e+2), "100");
test.equal(f(1e+3), "1k");
test.equal(f(1e+4), "10k");
test.equal(f(1e+5), "100k");
var f = format.format(".4s");
test.equal(f(1e-5), "10.00µ");
test.equal(f(1e-4), "100.0µ");
test.equal(f(1e-3), "1.000m");
test.equal(f(1e-2), "10.00m");
test.equal(f(1e-1), "100.0m");
test.equal(f(1e+0), "1.000");
test.equal(f(1e+1), "10.00");
test.equal(f(1e+2), "100.0");
test.equal(f(1e+3), "1.000k");
test.equal(f(1e+4), "10.00k");
test.equal(f(1e+5), "100.0k");
test.end();
});
tape("format(\"0[width],s\") will group thousands due to zero fill", function(test) {
var f = format.format("020,s");
test.equal(f(42), "000,000,000,042.0000");
test.equal(f(42e12), "00,000,000,042.0000T");
test.end();
});
tape("format(\",s\") will group thousands for very large numbers", function(test) {
var f = format.format(",s");
test.equal(f(42e30), "42,000,000Y");
test.end();
});
d3-format-1.0.2/test/format-type-x-test.js 0000664 0000000 0000000 00000005756 12750212270 0020327 0 ustar 00root root 0000000 0000000 var tape = require("tape"),
format = require("../");
tape("format(\"x\") returns the expected hexadecimal (lowercase) string", function(test) {
test.equal(format.format("x")(0xdeadbeef), "deadbeef");
test.end();
});
tape("format(\"#x\") returns the expected hexadecimal (lowercase) string with prefix", function(test) {
test.equal(format.format("#x")(0xdeadbeef), "0xdeadbeef");
test.end();
});
tape("format(\",x\") groups thousands", function(test) {
test.equal(format.format(",x")(0xdeadbeef), "de,adb,eef");
test.end();
});
tape("format(\",x\") groups thousands", function(test) {
test.equal(format.format(",x")(0xdeadbeef), "de,adb,eef");
test.end();
});
tape("format(\"#,x\") does not group the prefix", function(test) {
test.equal(format.format("#,x")(0xadeadbeef), "0xade,adb,eef");
test.end();
});
tape("format(\"+#x\") puts the sign before the prefix", function(test) {
test.equal(format.format("+#x")(0xdeadbeef), "+0xdeadbeef");
test.equal(format.format("+#x")(-0xdeadbeef), "-0xdeadbeef");
test.equal(format.format(" #x")(0xdeadbeef), " 0xdeadbeef");
test.equal(format.format(" #x")(-0xdeadbeef), "-0xdeadbeef");
test.end();
});
tape("format(\"$,x\") formats hexadecimal currency", function(test) {
test.equal(format.format("$,x")(0xdeadbeef), "$de,adb,eef");
test.end();
});
tape("format(\"[.precision]x\") always has precision zero", function(test) {
test.equal(format.format(".2x")(0xdeadbeef), "deadbeef");
test.equal(format.format(".2x")(-4.2), "-4");
test.end();
});
tape("format(\"x\") rounds non-integers", function(test) {
test.equal(format.format("x")(2.4), "2");
test.end();
});
tape("format(\"x\") can format negative zero as zero", function(test) {
test.equal(format.format("x")(-0), "0");
test.equal(format.format("x")(-1e-12), "0");
test.end();
});
tape("format(\"x\") does not consider -0xeee to be positive", function(test) {
test.equal(format.format("x")(-0xeee), "-eee");
test.end();
});
tape("format(\"X\") returns the expected hexadecimal (uppercase) string", function(test) {
test.equal(format.format("X")(0xdeadbeef), "DEADBEEF");
test.end();
});
tape("format(\"#X\") returns the expected hexadecimal (uppercase) string with prefix", function(test) {
test.equal(format.format("#X")(0xdeadbeef), "0xDEADBEEF");
test.end();
});
tape("format(\"X\") can format negative zero as zero", function(test) {
test.equal(format.format("X")(-0), "0");
test.equal(format.format("X")(-1e-12), "0");
test.end();
});
tape("format(\"X\") does not consider -0xeee to be positive", function(test) {
test.equal(format.format("X")(-0xeee), "-EEE");
test.end();
});
tape("format(\"#[width]x\") considers the prefix", function(test) {
test.equal(format.format("20x")(0xdeadbeef), " deadbeef");
test.equal(format.format("#20x")(0xdeadbeef), " 0xdeadbeef");
test.equal(format.format("020x")(0xdeadbeef), "000000000000deadbeef");
test.equal(format.format("#020x")(0xdeadbeef), "0x0000000000deadbeef");
test.end();
});
d3-format-1.0.2/test/formatPrefix-test.js 0000664 0000000 0000000 00000001770 12750212270 0020251 0 ustar 00root root 0000000 0000000 var tape = require("tape"),
format = require("../");
tape("formatPrefix(\"s\", value)(number) formats with the SI prefix appropriate to the specified value", function(test) {
test.equal(format.formatPrefix(",.0s", 1e-6)(.00042), "420µ");
test.equal(format.formatPrefix(",.0s", 1e-6)(.0042), "4,200µ");
test.equal(format.formatPrefix(",.3s", 1e-3)(.00042), "0.420m");
test.end();
});
tape("formatPrefix(\"s\", value)(number) uses yocto for very small reference values", function(test) {
test.equal(format.formatPrefix(",.0s", 1e-27)(1e-24), "1y");
test.end();
});
tape("formatPrefix(\"s\", value)(number) uses yotta for very small reference values", function(test) {
test.equal(format.formatPrefix(",.0s", 1e27)(1e24), "1Y");
test.end();
});
tape("formatPrefix(\"$,s\", value)(number) formats with the specified SI prefix", function(test) {
var f = format.formatPrefix(" $12,.1s", 1e6);
test.equal(f(-42e6), " -$42.0M");
test.equal(f(+4.2e6), " $4.2M");
test.end();
});
d3-format-1.0.2/test/formatSpecifier-test.js 0000664 0000000 0000000 00000004624 12750212270 0020726 0 ustar 00root root 0000000 0000000 var tape = require("tape"),
format = require("../");
tape("formatSpecifier(specifier) throws an error for invalid formats", function(test) {
test.throws(function() { format.formatSpecifier("foo"); }, /invalid format: foo/);
test.throws(function() { format.formatSpecifier(".-2s"); }, /invalid format: \.-2s/);
test.throws(function() { format.formatSpecifier(".f"); }, /invalid format: \.f/);
test.end();
});
tape("formatSpecifier(\"\") has the expected defaults", function(test) {
var s = format.formatSpecifier("");
test.equal(s.fill, " ");
test.equal(s.align, ">");
test.equal(s.sign, "-");
test.equal(s.symbol, "");
test.equal(s.zero, false);
test.equal(s.width, undefined);
test.equal(s.comma, false);
test.equal(s.precision, undefined);
test.equal(s.type, "");
test.end();
});
tape("formatSpecifier(specifier) uses the none type for unknown types", function(test) {
test.equal(format.formatSpecifier("q").type, "");
test.equal(format.formatSpecifier("S").type, "");
test.end();
});
tape("formatSpecifier(\"n\") is an alias for \",g\"", function(test) {
var s = format.formatSpecifier("n")
test.equal(s.comma, true);
test.equal(s.type, "g");
test.end();
});
tape("formatSpecifier(\"0\") is an alias for \"0=\"", function(test) {
var s = format.formatSpecifier("0")
test.equal(s.zero, true);
test.equal(s.fill, "0");
test.equal(s.align, "=");
test.end();
});
tape("formatSpecifier(specifier).toString() reflects current field values", function(test) {
var s = format.formatSpecifier("");
test.equal((s.fill = "_", s) + "", "_>-");
test.equal((s.align = "^", s) + "", "_^-");
test.equal((s.sign = "+", s) + "", "_^+");
test.equal((s.symbol = "$", s) + "", "_^+$");
test.equal((s.zero = true, s) + "", "_^+$0");
test.equal((s.width = 12, s) + "", "_^+$012");
test.equal((s.comma = true, s) + "", "_^+$012,");
test.equal((s.precision = 2, s) + "", "_^+$012,.2");
test.equal((s.type = "f", s) + "", "_^+$012,.2f");
test.equal(format.format(s)(42), "+$0,000,042.00");
test.end();
});
tape("formatSpecifier(specifier).toString() clamps precision to zero", function(test) {
var s = format.formatSpecifier("");
test.equal((s.precision = -1, s) + "", " >-.0");
test.end();
});
tape("formatSpecifier(specifier).toString() clamps width to one", function(test) {
var s = format.formatSpecifier("");
test.equal((s.width = -1, s) + "", " >-1");
test.end();
});
d3-format-1.0.2/test/inDelta.js 0000664 0000000 0000000 00000000417 12750212270 0016203 0 ustar 00root root 0000000 0000000 var tape = require("tape");
tape.Test.prototype.inDelta = function(actual, expected) {
this._assert(expected - 1e-6 < actual && actual < expected + 1e-6, {
message: "should be in delta",
operator: "inDelta",
actual: actual,
expected: expected
});
};
d3-format-1.0.2/test/locale-test.js 0000664 0000000 0000000 00000005141 12750212270 0017036 0 ustar 00root root 0000000 0000000 var fs = require("fs"),
path = require("path"),
tape = require("tape"),
queue = require("d3-queue"),
d3 = require("../");
tape("formatLocale({decimal: decimal}) observes the specified decimal point", function(test) {
test.equal(d3.formatLocale({decimal: "|"}).format("06.2f")(2), "002|00");
test.equal(d3.formatLocale({decimal: "/"}).format("06.2f")(2), "002/00");
test.end();
});
tape("formatLocale({currency: [prefix, suffix]}) observes the specified currency prefix and suffix", function(test) {
test.equal(d3.formatLocale({decimal: ".", currency: ["฿", ""]}).format("$06.2f")(2), "฿02.00");
test.equal(d3.formatLocale({decimal: ".", currency: ["", "฿"]}).format("$06.2f")(2), "02.00฿");
test.end();
});
tape("formatLocale({grouping: null}) does not perform any grouping", function(test) {
test.equal(d3.formatLocale({decimal: ".", grouping: null}).format("012,.2f")(2), "000000002.00");
test.end();
});
tape("formatLocale({grouping: [sizes…]}) observes the specified group sizes", function(test) {
test.equal(d3.formatLocale({decimal: ".", grouping: [3], thousands: ","}).format("012,.2f")(2), "0,000,002.00");
test.equal(d3.formatLocale({decimal: ".", grouping: [2], thousands: ","}).format("012,.2f")(2), "0,00,00,02.00");
test.equal(d3.formatLocale({decimal: ".", grouping: [2, 3], thousands: ","}).format("012,.2f")(2), "00,000,02.00");
test.equal(d3.formatLocale({decimal: ".", grouping: [3, 2, 2, 2, 2, 2, 2], thousands: ","}).format(",d")(1e12), "10,00,00,00,00,000");
test.end();
});
tape("formatLocale({thousands: separator}) observes the specified group separator", function(test) {
test.equal(d3.formatLocale({decimal: ".", grouping: [3], thousands: " "}).format("012,.2f")(2), "0 000 002.00");
test.equal(d3.formatLocale({decimal: ".", grouping: [3], thousands: "/"}).format("012,.2f")(2), "0/000/002.00");
test.end();
});
tape("locale data is valid", function(test) {
fs.readdir("locale", function(error, locales) {
if (error) throw error;
var q = queue.queue(1);
locales.forEach(function(locale) {
if (!/\.json$/i.test(locale)) return;
q.defer(testLocale, path.join("locale", locale));
});
q.awaitAll(function(error) {
if (error) throw error;
test.end();
});
});
function testLocale(locale, callback) {
fs.readFile(locale, "utf8", function(error, locale) {
if (error) return void callback(error);
locale = JSON.parse(locale);
test.deepEqual(Object.keys(locale).sort(), ["currency", "decimal", "grouping", "thousands"]);
locale = d3.formatLocale(locale);
callback(null);
});
}
});
d3-format-1.0.2/test/precisionFixed-test.js 0000664 0000000 0000000 00000000653 12750212270 0020555 0 ustar 00root root 0000000 0000000 var tape = require("tape"),
format = require("../");
tape("precisionFixed(number) returns the expected value", function(test) {
test.equal(format.precisionFixed(8.9), 0);
test.equal(format.precisionFixed(1.1), 0);
test.equal(format.precisionFixed(0.89), 1);
test.equal(format.precisionFixed(0.11), 1);
test.equal(format.precisionFixed(0.089), 2);
test.equal(format.precisionFixed(0.011), 2);
test.end();
});
d3-format-1.0.2/test/precisionPrefix-test.js 0000664 0000000 0000000 00000003727 12750212270 0020760 0 ustar 00root root 0000000 0000000 var tape = require("tape"),
format = require("../");
// A generalization from µ to all prefixes:
// test.equal(format.precisionPrefix(1e-6, 1e-6), 0); // 1µ
// test.equal(format.precisionPrefix(1e-6, 1e-7), 0); // 10µ
// test.equal(format.precisionPrefix(1e-6, 1e-8), 0); // 100µ
tape("precisionPrefix(step, value) returns zero if step has the same units as value", function(test) {
for (var i = -24; i <= 24; i += 3) {
for (var j = i; j < i + 3; ++j) {
test.equal(format.precisionPrefix(+("1e" + i), +("1e" + j)), 0);
}
}
test.end();
});
// A generalization from µ to all prefixes:
// test.equal(format.precisionPrefix(1e-9, 1e-6), 3); // 0.001µ
// test.equal(format.precisionPrefix(1e-8, 1e-6), 2); // 0.01µ
// test.equal(format.precisionPrefix(1e-7, 1e-6), 1); // 0.1µ
tape("precisionPrefix(step, value) returns greater than zero if fractional digits are needed", function(test) {
for (var i = -24; i <= 24; i += 3) {
for (var j = i - 4; j < i; ++j) {
test.equal(format.precisionPrefix(+("1e" + j), +("1e" + i)), i - j);
}
}
test.end();
});
tape("precisionPrefix(step, value) returns the expected precision when value is less than one yocto", function(test) {
test.equal(format.precisionPrefix(1e-24, 1e-24), 0); // 1y
test.equal(format.precisionPrefix(1e-25, 1e-25), 1); // 0.1y
test.equal(format.precisionPrefix(1e-26, 1e-26), 2); // 0.01y
test.equal(format.precisionPrefix(1e-27, 1e-27), 3); // 0.001y
test.equal(format.precisionPrefix(1e-28, 1e-28), 4); // 0.0001y
test.end();
});
tape("precisionPrefix(step, value) returns the expected precision when value is greater than than one yotta", function(test) {
test.equal(format.precisionPrefix(1e24, 1e24), 0); // 1Y
test.equal(format.precisionPrefix(1e24, 1e25), 0); // 10Y
test.equal(format.precisionPrefix(1e24, 1e26), 0); // 100Y
test.equal(format.precisionPrefix(1e24, 1e27), 0); // 1000Y
test.equal(format.precisionPrefix(1e23, 1e27), 1); // 1000.0Y
test.end();
});
d3-format-1.0.2/test/precisionRound-test.js 0000664 0000000 0000000 00000000655 12750212270 0020607 0 ustar 00root root 0000000 0000000 var tape = require("tape"),
format = require("../");
tape("precisionRound(step, max) returns the expected value", function(test) {
test.equal(format.precisionRound(0.1, 1.1), 2); // "1.0", "1.1"
test.equal(format.precisionRound(0.01, 0.99), 2); // "0.98", "0.99"
test.equal(format.precisionRound(0.01, 1.00), 2); // "0.99", "1.0"
test.equal(format.precisionRound(0.01, 1.01), 3); // "1.00", "1.01"
test.end();
});