@d3plus/format 3.0.15 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +75 -27
- package/es/src/format.js +2 -7
- package/es/src/formatAbbreviate.js +13 -14
- package/es/src/formatDate.js +16 -18
- package/es/src/formatDefaultLocale.js +3 -5
- package/package.json +19 -11
- package/types/index.d.ts +4 -0
- package/types/src/format.d.ts +9 -0
- package/types/src/formatAbbreviate.d.ts +8 -0
- package/types/src/formatDate.d.ts +9 -0
- package/types/src/formatDefaultLocale.d.ts +6 -0
- package/umd/d3plus-format.full.js +89 -203
- package/umd/d3plus-format.full.js.map +1 -1
- package/umd/d3plus-format.full.min.js +69 -67
- package/umd/d3plus-format.js +33 -149
- package/umd/d3plus-format.js.map +1 -1
- package/umd/d3plus-format.min.js +26 -35
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 D3plus
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
# @d3plus/format
|
|
2
|
-
|
|
2
|
+
|
|
3
3
|
JavaScript formatters for localized numbers and dates.
|
|
4
4
|
|
|
5
5
|
## Installing
|
|
@@ -7,10 +7,10 @@ JavaScript formatters for localized numbers and dates.
|
|
|
7
7
|
If using npm, `npm install @d3plus/format`. Otherwise, you can download the [latest release from GitHub](https://github.com/d3plus/d3plus/releases/latest) or load from a [CDN](https://cdn.jsdelivr.net/npm/@d3plus/format).
|
|
8
8
|
|
|
9
9
|
```js
|
|
10
|
-
import
|
|
10
|
+
import {*} from "@d3plus/format";
|
|
11
11
|
```
|
|
12
12
|
|
|
13
|
-
In vanilla
|
|
13
|
+
In a vanilla environment, a `d3plus` global is exported from the pre-bundled version:
|
|
14
14
|
|
|
15
15
|
```html
|
|
16
16
|
<script src="https://cdn.jsdelivr.net/npm/@d3plus/format"></script>
|
|
@@ -21,55 +21,103 @@ In vanilla JavaScript, a `d3plus` global is exported from the pre-bundled versio
|
|
|
21
21
|
|
|
22
22
|
## Examples
|
|
23
23
|
|
|
24
|
-
Live examples can be found on [d3plus.org](https://d3plus.org/), which includes a collection of example visualizations using @d3plus/react.
|
|
24
|
+
Live examples can be found on [d3plus.org](https://d3plus.org/), which includes a collection of example visualizations using [@d3plus/react](https://github.com/d3plus/d3plus/tree/main/packages/react).
|
|
25
25
|
|
|
26
26
|
## API Reference
|
|
27
27
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
28
|
+
| Functions | Description |
|
|
29
|
+
| --- | --- |
|
|
30
|
+
| [`format`](#format) | |
|
|
31
|
+
| [`formatAbbreviate`](#formatabbreviate) | Formats a number to an appropriate number of decimal places and rounding, adding suffixes if applicable (ie. `1200000` t |
|
|
32
|
+
| [`formatDate`](#formatdate) | A default set of date formatters, which takes into account both the interval in between in each data point but also the |
|
|
33
|
+
| [`formatDefaultLocale`](#formatdefaultlocale) | An extension to d3's [formatDefaultLocale](https://github.com/d3/d3-format#api-reference) function that allows setting t |
|
|
34
|
+
|
|
35
|
+
## Functions
|
|
36
|
+
|
|
37
|
+
<a id="format"></a>
|
|
38
|
+
|
|
39
|
+
### format()
|
|
40
|
+
|
|
41
|
+
> **format**(`specifier`: `string`): `Formatter`
|
|
33
42
|
|
|
34
|
-
|
|
43
|
+
Defined in: [format.ts:10](https://github.com/d3plus/d3plus/blob/e9db3c74352143cd7b6bdc8d0786477ea971eb6d/packages/format/src/format.ts#L10)
|
|
35
44
|
|
|
36
|
-
|
|
37
|
-
#### d3plus.**format**(specifier) [<>](https://github.com/d3plus/d3plus/blob/main/packages/format/src/format.js#L4)
|
|
45
|
+
#### Parameters
|
|
38
46
|
|
|
39
|
-
|
|
47
|
+
| Parameter | Type |
|
|
48
|
+
| ------ | ------ |
|
|
49
|
+
| `specifier` | `string` |
|
|
40
50
|
|
|
51
|
+
#### Returns
|
|
41
52
|
|
|
42
|
-
|
|
53
|
+
`Formatter`
|
|
43
54
|
|
|
44
|
-
|
|
55
|
+
***
|
|
45
56
|
|
|
46
|
-
<a
|
|
47
|
-
|
|
57
|
+
<a id="formatabbreviate"></a>
|
|
58
|
+
|
|
59
|
+
### formatAbbreviate()
|
|
60
|
+
|
|
61
|
+
> **formatAbbreviate**(`n`: `string` \| `number`, `locale?`: `string` \| `FormatLocaleDefinition`, `precision?`: `string`): `string`
|
|
62
|
+
|
|
63
|
+
Defined in: [formatAbbreviate.ts:55](https://github.com/d3plus/d3plus/blob/e9db3c74352143cd7b6bdc8d0786477ea971eb6d/packages/format/src/formatAbbreviate.ts#L55)
|
|
48
64
|
|
|
49
65
|
Formats a number to an appropriate number of decimal places and rounding, adding suffixes if applicable (ie. `1200000` to `"1.2M"`).
|
|
50
66
|
|
|
67
|
+
#### Parameters
|
|
68
|
+
|
|
69
|
+
| Parameter | Type | Default | Description |
|
|
70
|
+
| ------ | ------ | ------ | ------ |
|
|
71
|
+
| `n` | `string` \| `number` | *required* | The number to be formatted. |
|
|
72
|
+
| `locale` | `string` \| `FormatLocaleDefinition` | `"en-US"` | The locale config to be used. If an object is provided, the function will format the numbers according to the object. The object must include `suffixes`, `delimiter` and `currency` properties. |
|
|
73
|
+
| `precision?` | `string` | *required* | Number of significant digits to display. |
|
|
51
74
|
|
|
52
|
-
|
|
75
|
+
#### Returns
|
|
53
76
|
|
|
54
|
-
|
|
77
|
+
`string`
|
|
55
78
|
|
|
56
|
-
|
|
57
|
-
|
|
79
|
+
***
|
|
80
|
+
|
|
81
|
+
<a id="formatdate"></a>
|
|
82
|
+
|
|
83
|
+
### formatDate()
|
|
84
|
+
|
|
85
|
+
> **formatDate**(`d`: `Date`, `dataArray`: `Date`[], `formatter?`: `DateFormatter`): `string`
|
|
86
|
+
|
|
87
|
+
Defined in: [formatDate.ts:12](https://github.com/d3plus/d3plus/blob/e9db3c74352143cd7b6bdc8d0786477ea971eb6d/packages/format/src/formatDate.ts#L12)
|
|
58
88
|
|
|
59
89
|
A default set of date formatters, which takes into account both the interval in between in each data point but also the start/end data points.
|
|
60
90
|
|
|
91
|
+
#### Parameters
|
|
92
|
+
|
|
93
|
+
| Parameter | Type | Default | Description |
|
|
94
|
+
| ------ | ------ | ------ | ------ |
|
|
95
|
+
| `d` | `Date` | *required* | The date to format. |
|
|
96
|
+
| `dataArray` | `Date`[] | *required* | The full array of ordered Date Objects. |
|
|
97
|
+
| `formatter` | `DateFormatter` | `timeFormat` | Optional custom format string or function. |
|
|
98
|
+
|
|
99
|
+
#### Returns
|
|
100
|
+
|
|
101
|
+
`string`
|
|
102
|
+
|
|
103
|
+
***
|
|
104
|
+
|
|
105
|
+
<a id="formatdefaultlocale"></a>
|
|
61
106
|
|
|
62
|
-
|
|
107
|
+
### formatDefaultLocale()
|
|
63
108
|
|
|
64
|
-
|
|
109
|
+
> **formatDefaultLocale**(`definition`: `FormatLocaleDefinition`): `Record`\<`string`, `unknown`\>
|
|
65
110
|
|
|
66
|
-
|
|
67
|
-
#### d3plus.**formatDefaultLocale**(definition) [<>](https://github.com/d3plus/d3plus/blob/main/packages/format/src/formatDefaultLocale.js#L4)
|
|
111
|
+
Defined in: [formatDefaultLocale.ts:8](https://github.com/d3plus/d3plus/blob/e9db3c74352143cd7b6bdc8d0786477ea971eb6d/packages/format/src/formatDefaultLocale.ts#L8)
|
|
68
112
|
|
|
69
113
|
An extension to d3's [formatDefaultLocale](https://github.com/d3/d3-format#api-reference) function that allows setting the locale globally for formatters.
|
|
70
114
|
|
|
115
|
+
#### Parameters
|
|
71
116
|
|
|
72
|
-
|
|
117
|
+
| Parameter | Type | Description |
|
|
118
|
+
| ------ | ------ | ------ |
|
|
119
|
+
| `definition` | `FormatLocaleDefinition` | The localization definition. |
|
|
73
120
|
|
|
74
|
-
|
|
121
|
+
#### Returns
|
|
75
122
|
|
|
123
|
+
`Record`\<`string`, `unknown`\>
|
package/es/src/format.js
CHANGED
|
@@ -1,11 +1,6 @@
|
|
|
1
1
|
import abbreviate from "./formatAbbreviate.js";
|
|
2
2
|
import { format } from "d3-format";
|
|
3
|
-
|
|
4
|
-
@function format
|
|
5
|
-
@desc An extension to d3's [format](https://github.com/d3/d3-format#api-reference) function that adds more string formatting types and localizations.
|
|
6
|
-
@param {String} specifier The string specifier used by the format function.
|
|
7
|
-
@returns {Function}
|
|
8
|
-
*/ export default function(specifier) {
|
|
3
|
+
export default function(specifier) {
|
|
9
4
|
if (specifier === ".3~a") return abbreviate;
|
|
10
5
|
return format(specifier);
|
|
11
|
-
}
|
|
6
|
+
}
|
|
@@ -4,14 +4,14 @@ function _type_of(obj) {
|
|
|
4
4
|
}
|
|
5
5
|
import { formatLocale } from "d3-format";
|
|
6
6
|
import { formatLocale as defaultLocale } from "@d3plus/locales";
|
|
7
|
-
var round = function(x, n) {
|
|
8
|
-
return parseFloat(Math.round(x * Math.pow(10, n)) / Math.pow(10, n)).toFixed(n);
|
|
7
|
+
var round = function round(x, n) {
|
|
8
|
+
return parseFloat((Math.round(x * Math.pow(10, n)) / Math.pow(10, n)).toString()).toFixed(n);
|
|
9
9
|
};
|
|
10
10
|
/**
|
|
11
11
|
* @private
|
|
12
|
-
*/ function formatSuffix(str, precision, suffixes) {
|
|
12
|
+
*/ function formatSuffix(str, precision, suffixes) {
|
|
13
13
|
var i = 0;
|
|
14
|
-
var value = parseFloat(str.replace("
|
|
14
|
+
var value = parseFloat(str.replace("\u2212", "-"));
|
|
15
15
|
if (value) {
|
|
16
16
|
if (value < 0) value *= -1;
|
|
17
17
|
i = 1 + Math.floor(1e-12 + Math.log(value) / Math.LN10);
|
|
@@ -25,7 +25,7 @@ var round = function(x, n) {
|
|
|
25
25
|
}
|
|
26
26
|
/**
|
|
27
27
|
* @private
|
|
28
|
-
*/ function parseSuffixes(d, i) {
|
|
28
|
+
*/ function parseSuffixes(d, i) {
|
|
29
29
|
var k = Math.pow(10, Math.abs(8 - i) * 3);
|
|
30
30
|
return {
|
|
31
31
|
scale: i > 8 ? function(d) {
|
|
@@ -37,14 +37,13 @@ var round = function(x, n) {
|
|
|
37
37
|
};
|
|
38
38
|
}
|
|
39
39
|
/**
|
|
40
|
-
|
|
41
|
-
@
|
|
42
|
-
@param
|
|
43
|
-
@param
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
if (isFinite(n)) n *= 1;
|
|
40
|
+
Formats a number to an appropriate number of decimal places and rounding, adding suffixes if applicable (ie. `1200000` to `"1.2M"`).
|
|
41
|
+
@param n The number to be formatted.
|
|
42
|
+
@param locale The locale config to be used. If an object is provided, the function will format the numbers according to the object. The object must include `suffixes`, `delimiter` and `currency` properties.
|
|
43
|
+
@param precision Number of significant digits to display.
|
|
44
|
+
*/ export default function formatAbbreviate(n) {
|
|
45
|
+
var locale = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : "en-US", precision = arguments.length > 2 ? arguments[2] : void 0;
|
|
46
|
+
if (isFinite(n)) n = n * 1;
|
|
48
47
|
else return "N/A";
|
|
49
48
|
var negative = n < 0;
|
|
50
49
|
var length = n.toString().split(".")[0].replace("-", "").length, localeConfig = (typeof locale === "undefined" ? "undefined" : _type_of(locale)) === "object" ? locale : defaultLocale[locale] || defaultLocale["en-US"], suffixes = localeConfig.suffixes.map(parseSuffixes);
|
|
@@ -71,7 +70,7 @@ var round = function(x, n) {
|
|
|
71
70
|
} else if (length === 3) val = d3plusFormatLocale.format(",f")(n);
|
|
72
71
|
else if (n < 1 && n > -1) val = d3plusFormatLocale.format(".2g")(n);
|
|
73
72
|
else val = d3plusFormatLocale.format(".3g")(n);
|
|
74
|
-
return "".concat(negative && val.charAt(0) !== "
|
|
73
|
+
return "".concat(negative && val.charAt(0) !== "\u2212" ? "\u2212" : "").concat(val).replace(/\u2212/g, "-") // replace new d3 default minus sign (\u2212) to hyphen-minus (-)
|
|
75
74
|
.replace(/(\.[0]*[1-9]*)[0]*$/g, "$1") // removes any trailing zeros
|
|
76
75
|
.replace(/\.[0]*$/g, ""); // removes any trailing decimal point
|
|
77
76
|
}
|
package/es/src/formatDate.js
CHANGED
|
@@ -47,13 +47,11 @@ function _unsupported_iterable_to_array(o, minLen) {
|
|
|
47
47
|
import { timeYear, timeMonth, timeHour, timeMinute, timeSecond } from "d3-time";
|
|
48
48
|
import { timeFormat } from "d3-time-format";
|
|
49
49
|
/**
|
|
50
|
-
|
|
51
|
-
@
|
|
52
|
-
@param
|
|
53
|
-
@param
|
|
54
|
-
|
|
55
|
-
@returns {String}
|
|
56
|
-
*/ export default function(d, dataArray) {
|
|
50
|
+
A default set of date formatters, which takes into account both the interval in between in each data point but also the start/end data points.
|
|
51
|
+
@param d The date to format.
|
|
52
|
+
@param dataArray The full array of ordered Date Objects.
|
|
53
|
+
@param formatter Optional custom format string or function.
|
|
54
|
+
*/ export default function formatDate(d, dataArray) {
|
|
57
55
|
var formatter = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : timeFormat;
|
|
58
56
|
var formatHour = formatter("%I %p"), formatMillisecond = formatter(".%L"), formatMinute = formatter("%I:%M"), formatMonth = formatter("%b"), formatMonthDay = formatter("%b %-d"), formatMonthDayYear = formatter("%b %-d, %Y"), formatMonthYear = formatter("%b %Y"), formatQuarter = formatter("Q%q"), formatQuarterYear = formatter("Q%q %Y"), formatSecond = formatter(":%S"), formatYear = formatter("%Y");
|
|
59
57
|
var labelIndex = dataArray.findIndex(function(a) {
|
|
@@ -65,8 +63,8 @@ import { timeFormat } from "d3-time-format";
|
|
|
65
63
|
if (i) {
|
|
66
64
|
arr[0].push(d.getFullYear() - dataArray[i - 1].getFullYear());
|
|
67
65
|
arr[1].push(monthDiff(dataArray[i - 1], d));
|
|
68
|
-
arr[2].push(Math.round((d - dataArray[i - 1]) / (1000 * 60 * 60 * 24)));
|
|
69
|
-
arr[3].push(Math.round((d - dataArray[i - 1]) / (1000 * 60 * 60)));
|
|
66
|
+
arr[2].push(Math.round((+d - +dataArray[i - 1]) / (1000 * 60 * 60 * 24)));
|
|
67
|
+
arr[3].push(Math.round((+d - +dataArray[i - 1]) / (1000 * 60 * 60)));
|
|
70
68
|
}
|
|
71
69
|
return arr;
|
|
72
70
|
}, [
|
|
@@ -77,26 +75,26 @@ import { timeFormat } from "d3-time-format";
|
|
|
77
75
|
]), 4), yearlySteps = _dataArray_reduce[0], monthlySteps = _dataArray_reduce[1], dailySteps = _dataArray_reduce[2], hourlySteps = _dataArray_reduce[3];
|
|
78
76
|
return (yearlySteps.every(function(s) {
|
|
79
77
|
return s >= 1 && !(s % 1);
|
|
80
|
-
}) // Yearly Data
|
|
78
|
+
}) // Yearly Data
|
|
81
79
|
? formatYear : monthlySteps.every(function(s) {
|
|
82
80
|
return s >= 3 && !(s % 3);
|
|
83
81
|
}) // Quarterly Data
|
|
84
|
-
? +timeYear(d) === d || firstOrLast || smallArray ? formatQuarterYear : formatQuarter : monthlySteps.every(function(s) {
|
|
82
|
+
? +timeYear(d) === +d || firstOrLast || smallArray ? formatQuarterYear : formatQuarter : monthlySteps.every(function(s) {
|
|
85
83
|
return s >= 1 && !(s % 1);
|
|
86
84
|
}) // Monthly Data
|
|
87
|
-
? +timeYear(d) === d || firstOrLast || smallArray ? formatMonthYear : formatMonth : dailySteps.every(function(s) {
|
|
85
|
+
? +timeYear(d) === +d || firstOrLast || smallArray ? formatMonthYear : formatMonth : dailySteps.every(function(s) {
|
|
88
86
|
return s >= 1 && !(s % 1);
|
|
89
87
|
}) // Daily Data
|
|
90
|
-
? +timeYear(d) === d || firstOrLast || smallArray ? formatMonthDayYear : formatMonthDay : hourlySteps.every(function(s) {
|
|
88
|
+
? +timeYear(d) === +d || firstOrLast || smallArray ? formatMonthDayYear : formatMonthDay : hourlySteps.every(function(s) {
|
|
91
89
|
return s >= 1 && !(s % 1);
|
|
92
90
|
}) // Hourly Data
|
|
93
|
-
? firstOrLast || smallArray ? formatMonthDayYear : +timeMonth(d) === d ? formatMonthDay : formatHour : timeSecond(d) < d ? formatMillisecond : timeMinute(d) < d ? formatSecond : timeHour(d) < d ? formatMinute :
|
|
91
|
+
? firstOrLast || smallArray ? formatMonthDayYear : +timeMonth(d) === +d ? formatMonthDay : formatHour : timeSecond(d) < d ? formatMillisecond : timeMinute(d) < d ? formatSecond : timeHour(d) < d ? formatMinute : function(_d) {
|
|
92
|
+
return _d.toString();
|
|
93
|
+
})(d);
|
|
94
94
|
}
|
|
95
95
|
/**
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
@param {*} d1
|
|
99
|
-
@param {*} d2
|
|
96
|
+
Returns the number of months between two Date objects
|
|
97
|
+
|
|
100
98
|
@returns {Number} the number of months between the two Date objects
|
|
101
99
|
@private
|
|
102
100
|
*/ function monthDiff(d1, d2) {
|
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
import format from "./format.js";
|
|
2
2
|
import { formatDefaultLocale } from "d3-format";
|
|
3
3
|
/**
|
|
4
|
-
|
|
5
|
-
@
|
|
6
|
-
@param {Object} definition The localization definition.
|
|
7
|
-
@returns {Object}
|
|
4
|
+
An extension to d3's [formatDefaultLocale](https://github.com/d3/d3-format#api-reference) function that allows setting the locale globally for formatters.
|
|
5
|
+
@param definition The localization definition.
|
|
8
6
|
*/ export default function(definition) {
|
|
9
7
|
var locale = formatDefaultLocale(definition);
|
|
10
8
|
locale.format = format;
|
|
11
9
|
return locale;
|
|
12
|
-
}
|
|
10
|
+
}
|
package/package.json
CHANGED
|
@@ -1,18 +1,25 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@d3plus/format",
|
|
3
|
-
"version": "3.0
|
|
3
|
+
"version": "3.1.0",
|
|
4
4
|
"description": "JavaScript formatters for localized numbers and dates.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
7
|
-
"
|
|
7
|
+
"types": "./types/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./types/index.d.ts",
|
|
11
|
+
"default": "./es/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
8
14
|
"browser": "./umd/d3plus-format.full.js",
|
|
9
15
|
"engines": {
|
|
10
|
-
"node": ">=
|
|
16
|
+
"node": ">=20"
|
|
11
17
|
},
|
|
12
18
|
"sideEffects": false,
|
|
13
19
|
"files": [
|
|
14
20
|
"umd",
|
|
15
|
-
"es"
|
|
21
|
+
"es",
|
|
22
|
+
"types"
|
|
16
23
|
],
|
|
17
24
|
"homepage": "https://d3plus.org",
|
|
18
25
|
"repository": {
|
|
@@ -27,16 +34,17 @@
|
|
|
27
34
|
"data",
|
|
28
35
|
"visualization"
|
|
29
36
|
],
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"d3-format": "^3.1.0",
|
|
39
|
+
"d3-time": "^3.1.0",
|
|
40
|
+
"d3-time-format": "^4.1.0",
|
|
41
|
+
"@d3plus/locales": "3.1.0"
|
|
42
|
+
},
|
|
30
43
|
"scripts": {
|
|
31
44
|
"build:esm": "node ../../scripts/build-esm.js",
|
|
45
|
+
"build:types": "tsc",
|
|
32
46
|
"build:umd": "node ../../scripts/build-umd.js",
|
|
33
47
|
"dev": "node ../../scripts/dev.js",
|
|
34
|
-
"test": "eslint index.
|
|
35
|
-
},
|
|
36
|
-
"dependencies": {
|
|
37
|
-
"@d3plus/locales": "3.0.15",
|
|
38
|
-
"d3-format": "^3.1.0",
|
|
39
|
-
"d3-time": "^3.1.0",
|
|
40
|
-
"d3-time-format": "^4.1.0"
|
|
48
|
+
"test": "eslint index.ts src/**/*.ts && eslint --global=it test && mocha 'test/**/*-test.js'"
|
|
41
49
|
}
|
|
42
50
|
}
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
An extension to d3's [format](https://github.com/d3/d3-format#api-reference) function that adds more string formatting types and localizations.
|
|
3
|
+
@param specifier The string specifier used by the format function.
|
|
4
|
+
*/
|
|
5
|
+
type Formatter = (n: number | string | {
|
|
6
|
+
valueOf(): number;
|
|
7
|
+
}) => string;
|
|
8
|
+
export default function (specifier: string): Formatter;
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { FormatLocaleDefinition } from "@d3plus/locales";
|
|
2
|
+
/**
|
|
3
|
+
Formats a number to an appropriate number of decimal places and rounding, adding suffixes if applicable (ie. `1200000` to `"1.2M"`).
|
|
4
|
+
@param n The number to be formatted.
|
|
5
|
+
@param locale The locale config to be used. If an object is provided, the function will format the numbers according to the object. The object must include `suffixes`, `delimiter` and `currency` properties.
|
|
6
|
+
@param precision Number of significant digits to display.
|
|
7
|
+
*/
|
|
8
|
+
export default function formatAbbreviate(n: number | string, locale?: string | FormatLocaleDefinition, precision?: string): string;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
type DateFormatter = (specifier: string) => (date: Date) => string;
|
|
2
|
+
/**
|
|
3
|
+
A default set of date formatters, which takes into account both the interval in between in each data point but also the start/end data points.
|
|
4
|
+
@param d The date to format.
|
|
5
|
+
@param dataArray The full array of ordered Date Objects.
|
|
6
|
+
@param formatter Optional custom format string or function.
|
|
7
|
+
*/
|
|
8
|
+
export default function formatDate(d: Date, dataArray: Date[], formatter?: DateFormatter): string;
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { formatDefaultLocale } from "d3-format";
|
|
2
|
+
/**
|
|
3
|
+
An extension to d3's [formatDefaultLocale](https://github.com/d3/d3-format#api-reference) function that allows setting the locale globally for formatters.
|
|
4
|
+
@param definition The localization definition.
|
|
5
|
+
*/
|
|
6
|
+
export default function (definition: Parameters<typeof formatDefaultLocale>[0]): Record<string, unknown>;
|