@opendata-ai/openchart-core 7.9.3 → 7.11.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/dist/index.d.ts +3 -1
- package/dist/index.js +17 -3
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/locale/__tests__/format.test.ts +48 -0
- package/src/locale/format.ts +25 -2
package/dist/index.d.ts
CHANGED
|
@@ -4520,8 +4520,10 @@ type DateGranularity = 'year' | 'quarter' | 'month' | 'week' | 'day' | 'hour' |
|
|
|
4520
4520
|
* Pass `false` when formatting ticks from a local-time scale (d3 scaleTime),
|
|
4521
4521
|
* so that e.g. midnight local isn't misread as an intra-day UTC time.
|
|
4522
4522
|
* Defaults to `true` for backward compatibility.
|
|
4523
|
+
* @param compact - Use the compact format table (axis ticks whose full-format
|
|
4524
|
+
* labels would overlap). Defaults to `false`.
|
|
4523
4525
|
*/
|
|
4524
|
-
declare function formatDate(value: Date | string | number, _locale?: string, granularity?: DateGranularity, useUtc?: boolean): string;
|
|
4526
|
+
declare function formatDate(value: Date | string | number, _locale?: string, granularity?: DateGranularity, useUtc?: boolean, compact?: boolean): string;
|
|
4525
4527
|
/**
|
|
4526
4528
|
* Build a formatter for temporal values using a d3-time-format string (e.g. "%Y", "%b %Y").
|
|
4527
4529
|
* Returns a function that accepts a Date, string, or number and returns the formatted string.
|
package/dist/index.js
CHANGED
|
@@ -3198,15 +3198,29 @@ var GRANULARITY_FORMATS = {
|
|
|
3198
3198
|
hour: "%b %d %H:%M",
|
|
3199
3199
|
minute: "%H:%M"
|
|
3200
3200
|
};
|
|
3201
|
-
|
|
3201
|
+
var GRANULARITY_FORMATS_COMPACT = {
|
|
3202
|
+
year: "%Y",
|
|
3203
|
+
quarter: "",
|
|
3204
|
+
// special-cased below, like GRANULARITY_FORMATS
|
|
3205
|
+
month: "%b",
|
|
3206
|
+
week: "%b %d",
|
|
3207
|
+
day: "%b %d",
|
|
3208
|
+
hour: "%H:%M",
|
|
3209
|
+
minute: "%H:%M"
|
|
3210
|
+
};
|
|
3211
|
+
function formatDate(value, _locale, granularity, useUtc = true, compact = false) {
|
|
3202
3212
|
const date = value instanceof Date ? value : new Date(value);
|
|
3203
3213
|
if (Number.isNaN(date.getTime())) return String(value);
|
|
3204
3214
|
const gran = granularity ?? inferGranularity(date, useUtc);
|
|
3205
3215
|
if (gran === "quarter") {
|
|
3206
3216
|
const q = useUtc ? Math.ceil((date.getUTCMonth() + 1) / 3) : Math.ceil((date.getMonth() + 1) / 3);
|
|
3207
|
-
|
|
3217
|
+
const year = useUtc ? date.getUTCFullYear() : date.getFullYear();
|
|
3218
|
+
if (compact) {
|
|
3219
|
+
return `Q${q} '${String(year).slice(-2)}`;
|
|
3220
|
+
}
|
|
3221
|
+
return `Q${q} ${year}`;
|
|
3208
3222
|
}
|
|
3209
|
-
const formatStr = GRANULARITY_FORMATS[gran];
|
|
3223
|
+
const formatStr = (compact ? GRANULARITY_FORMATS_COMPACT : GRANULARITY_FORMATS)[gran];
|
|
3210
3224
|
if (useUtc) {
|
|
3211
3225
|
return utcFormat(formatStr)(date);
|
|
3212
3226
|
}
|