@astryxdesign/lab 0.7.9-canary.de1be1f → 0.7.9-canary.edf7128
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.
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @file formatters.ts
|
|
3
|
-
* @input Tick values
|
|
3
|
+
* @input Tick values, optional BCP 47 locale tags, and for dates an optional
|
|
4
|
+
* calendar
|
|
4
5
|
* @output Built-in tick format utilities for common data types
|
|
5
6
|
* @position Utility; consumed by ChartAxis via tickFormat prop
|
|
6
7
|
*/
|
|
7
|
-
import type { Locale } from '@astryxdesign/core/i18n';
|
|
8
|
+
import type { CalendarSystem, Locale } from '@astryxdesign/core/i18n';
|
|
8
9
|
/**
|
|
9
10
|
* Compact number formatter (e.g. 1200 → "1.2K", 1500000 → "1.5M")
|
|
10
11
|
*/
|
|
@@ -25,11 +26,13 @@ export declare function currency(symbol?: string, locale?: Locale): (value: unkn
|
|
|
25
26
|
*/
|
|
26
27
|
export declare function percent(value: unknown): string;
|
|
27
28
|
/**
|
|
28
|
-
* Date formatter — short date (e.g. "Jan 5", "Mar 12")
|
|
29
|
+
* Date formatter — short date (e.g. "Jan 5", "Mar 12"), in `calendar`
|
|
30
|
+
* (Gregorian by default)
|
|
29
31
|
*/
|
|
30
|
-
export declare function shortDate(value: unknown, locale?: Locale): string;
|
|
32
|
+
export declare function shortDate(value: unknown, locale?: Locale, calendar?: CalendarSystem): string;
|
|
31
33
|
/**
|
|
32
|
-
* Date formatter — month/year (e.g. "Jan 2024", "Mar 2025")
|
|
34
|
+
* Date formatter — month/year (e.g. "Jan 2024", "Mar 2025"), in `calendar`
|
|
35
|
+
* (Gregorian by default)
|
|
33
36
|
*/
|
|
34
|
-
export declare function monthYear(value: unknown, locale?: Locale): string;
|
|
37
|
+
export declare function monthYear(value: unknown, locale?: Locale, calendar?: CalendarSystem): string;
|
|
35
38
|
//# sourceMappingURL=formatters.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"formatters.d.ts","sourceRoot":"","sources":["../../src/Chart/formatters.ts"],"names":[],"mappings":"AAEA
|
|
1
|
+
{"version":3,"file":"formatters.d.ts","sourceRoot":"","sources":["../../src/Chart/formatters.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAC,cAAc,EAAE,MAAM,EAAC,MAAM,yBAAyB,CAAC;AAGpE;;GAEG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAepD;AAED;;;;;;;;;GASG;AACH,wBAAgB,QAAQ,CACtB,MAAM,SAAM,EACZ,MAAM,GAAE,MAAa,GACpB,CAAC,KAAK,EAAE,OAAO,KAAK,MAAM,CAiB5B;AAQD;;GAEG;AACH,wBAAgB,OAAO,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAM9C;AAED;;;GAGG;AACH,wBAAgB,SAAS,CACvB,KAAK,EAAE,OAAO,EACd,MAAM,GAAE,MAAa,EACrB,QAAQ,GAAE,cAA0B,GACnC,MAAM,CAUR;AAED;;;GAGG;AACH,wBAAgB,SAAS,CACvB,KAAK,EAAE,OAAO,EACd,MAAM,GAAE,MAAa,EACrB,QAAQ,GAAE,cAA0B,GACnC,MAAM,CAUR"}
|
package/dist/Chart/formatters.js
CHANGED
|
@@ -2,11 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* @file formatters.ts
|
|
5
|
-
* @input Tick values
|
|
5
|
+
* @input Tick values, optional BCP 47 locale tags, and for dates an optional
|
|
6
|
+
* calendar
|
|
6
7
|
* @output Built-in tick format utilities for common data types
|
|
7
8
|
* @position Utility; consumed by ChartAxis via tickFormat prop
|
|
8
9
|
*/
|
|
9
10
|
|
|
11
|
+
import { plainDateFormat, plainDateFromDate } from '@astryxdesign/core/utils';
|
|
12
|
+
|
|
10
13
|
/**
|
|
11
14
|
* Compact number formatter (e.g. 1200 → "1.2K", 1500000 → "1.5M")
|
|
12
15
|
*/
|
|
@@ -74,31 +77,33 @@ export function percent(value) {
|
|
|
74
77
|
}
|
|
75
78
|
|
|
76
79
|
/**
|
|
77
|
-
* Date formatter — short date (e.g. "Jan 5", "Mar 12")
|
|
80
|
+
* Date formatter — short date (e.g. "Jan 5", "Mar 12"), in `calendar`
|
|
81
|
+
* (Gregorian by default)
|
|
78
82
|
*/
|
|
79
|
-
export function shortDate(value, locale = 'en') {
|
|
83
|
+
export function shortDate(value, locale = 'en', calendar = 'gregory') {
|
|
80
84
|
const d = value instanceof Date ? value : new Date(Number(value));
|
|
81
85
|
if (isNaN(d.getTime())) {
|
|
82
86
|
return String(value);
|
|
83
87
|
}
|
|
84
|
-
return
|
|
88
|
+
return plainDateFormat(plainDateFromDate(d), {
|
|
85
89
|
month: 'short',
|
|
86
90
|
day: 'numeric',
|
|
87
|
-
calendar
|
|
88
|
-
})
|
|
91
|
+
calendar
|
|
92
|
+
}, locale);
|
|
89
93
|
}
|
|
90
94
|
|
|
91
95
|
/**
|
|
92
|
-
* Date formatter — month/year (e.g. "Jan 2024", "Mar 2025")
|
|
96
|
+
* Date formatter — month/year (e.g. "Jan 2024", "Mar 2025"), in `calendar`
|
|
97
|
+
* (Gregorian by default)
|
|
93
98
|
*/
|
|
94
|
-
export function monthYear(value, locale = 'en') {
|
|
99
|
+
export function monthYear(value, locale = 'en', calendar = 'gregory') {
|
|
95
100
|
const d = value instanceof Date ? value : new Date(Number(value));
|
|
96
101
|
if (isNaN(d.getTime())) {
|
|
97
102
|
return String(value);
|
|
98
103
|
}
|
|
99
|
-
return
|
|
104
|
+
return plainDateFormat(plainDateFromDate(d), {
|
|
100
105
|
month: 'short',
|
|
101
106
|
year: 'numeric',
|
|
102
|
-
calendar
|
|
103
|
-
})
|
|
107
|
+
calendar
|
|
108
|
+
}, locale);
|
|
104
109
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@astryxdesign/lab",
|
|
3
|
-
"version": "0.7.9-canary.
|
|
3
|
+
"version": "0.7.9-canary.edf7128",
|
|
4
4
|
"description": "Experimental Astryx components — published to npm only under the @canary dist-tag for early testing; never released as a stable (latest) version.",
|
|
5
5
|
"author": "Meta Open Source",
|
|
6
6
|
"license": "MIT",
|
|
@@ -49,7 +49,7 @@
|
|
|
49
49
|
},
|
|
50
50
|
"peerDependencies": {
|
|
51
51
|
"@stylexjs/stylex": ">=0.10.0",
|
|
52
|
-
"@astryxdesign/core": "0.7.9-canary.
|
|
52
|
+
"@astryxdesign/core": "0.7.9-canary.edf7128",
|
|
53
53
|
"react": ">=19.0.0",
|
|
54
54
|
"react-dom": ">=19.0.0"
|
|
55
55
|
},
|
|
@@ -59,9 +59,9 @@
|
|
|
59
59
|
"d3-shape": "^3.2.0"
|
|
60
60
|
},
|
|
61
61
|
"devDependencies": {
|
|
62
|
-
"@astryxdesign/a11y-spec": "0.7.9-canary.
|
|
63
|
-
"@astryxdesign/cli": "0.7.9-canary.
|
|
64
|
-
"@astryxdesign/interaction-harness": "0.7.9-canary.
|
|
62
|
+
"@astryxdesign/a11y-spec": "0.7.9-canary.edf7128",
|
|
63
|
+
"@astryxdesign/cli": "0.7.9-canary.edf7128",
|
|
64
|
+
"@astryxdesign/interaction-harness": "0.7.9-canary.edf7128",
|
|
65
65
|
"@babel/cli": "^7.29.7",
|
|
66
66
|
"@babel/core": "^7.29.7",
|
|
67
67
|
"@babel/preset-react": "^7.29.7",
|
|
@@ -23,6 +23,42 @@ describe('Chart date formatters', () => {
|
|
|
23
23
|
);
|
|
24
24
|
});
|
|
25
25
|
|
|
26
|
+
it.each([
|
|
27
|
+
{calendar: 'persian', locale: 'en', year: '1405'},
|
|
28
|
+
{calendar: 'persian', locale: 'fa', year: '۱۴۰۵'},
|
|
29
|
+
{calendar: 'hebrew', locale: 'en', year: '5787'},
|
|
30
|
+
{calendar: 'hebrew', locale: 'he', year: '5787'},
|
|
31
|
+
{calendar: 'buddhist', locale: 'en', year: '2569'},
|
|
32
|
+
{calendar: 'buddhist', locale: 'th', year: '2569'},
|
|
33
|
+
] as const)(
|
|
34
|
+
'formats dates in an explicit $calendar calendar under $locale',
|
|
35
|
+
({calendar, locale, year}) => {
|
|
36
|
+
const day = new Date(2026, 9, 30, 12);
|
|
37
|
+
expect(shortDate(day, locale, calendar)).toBe(
|
|
38
|
+
new Intl.DateTimeFormat(locale, {
|
|
39
|
+
month: 'short',
|
|
40
|
+
day: 'numeric',
|
|
41
|
+
calendar,
|
|
42
|
+
}).format(day),
|
|
43
|
+
);
|
|
44
|
+
const label = monthYear(day, locale, calendar);
|
|
45
|
+
expect(label).toBe(
|
|
46
|
+
new Intl.DateTimeFormat(locale, {
|
|
47
|
+
month: 'short',
|
|
48
|
+
year: 'numeric',
|
|
49
|
+
calendar,
|
|
50
|
+
}).format(day),
|
|
51
|
+
);
|
|
52
|
+
expect(label).toContain(year);
|
|
53
|
+
},
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
it('writes `iso8601` dates as Gregorian ones', () => {
|
|
57
|
+
const day = new Date(2026, 9, 30, 12);
|
|
58
|
+
expect(shortDate(day, 'en', 'iso8601')).toBe(shortDate(day, 'en'));
|
|
59
|
+
expect(monthYear(day, 'en', 'iso8601')).toBe('Oct 2026');
|
|
60
|
+
});
|
|
61
|
+
|
|
26
62
|
it('uses an explicit locale for unabridged currency values', () => {
|
|
27
63
|
expect(currency('€', 'de-DE')(12.5)).toBe(
|
|
28
64
|
`€${(12.5).toLocaleString('de-DE')}`,
|
package/src/Chart/formatters.ts
CHANGED
|
@@ -2,12 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* @file formatters.ts
|
|
5
|
-
* @input Tick values
|
|
5
|
+
* @input Tick values, optional BCP 47 locale tags, and for dates an optional
|
|
6
|
+
* calendar
|
|
6
7
|
* @output Built-in tick format utilities for common data types
|
|
7
8
|
* @position Utility; consumed by ChartAxis via tickFormat prop
|
|
8
9
|
*/
|
|
9
10
|
|
|
10
|
-
import type {Locale} from '@astryxdesign/core/i18n';
|
|
11
|
+
import type {CalendarSystem, Locale} from '@astryxdesign/core/i18n';
|
|
12
|
+
import {plainDateFormat, plainDateFromDate} from '@astryxdesign/core/utils';
|
|
11
13
|
|
|
12
14
|
/**
|
|
13
15
|
* Compact number formatter (e.g. 1200 → "1.2K", 1500000 → "1.5M")
|
|
@@ -79,31 +81,41 @@ export function percent(value: unknown): string {
|
|
|
79
81
|
}
|
|
80
82
|
|
|
81
83
|
/**
|
|
82
|
-
* Date formatter — short date (e.g. "Jan 5", "Mar 12")
|
|
84
|
+
* Date formatter — short date (e.g. "Jan 5", "Mar 12"), in `calendar`
|
|
85
|
+
* (Gregorian by default)
|
|
83
86
|
*/
|
|
84
|
-
export function shortDate(
|
|
87
|
+
export function shortDate(
|
|
88
|
+
value: unknown,
|
|
89
|
+
locale: Locale = 'en',
|
|
90
|
+
calendar: CalendarSystem = 'gregory',
|
|
91
|
+
): string {
|
|
85
92
|
const d = value instanceof Date ? value : new Date(Number(value));
|
|
86
93
|
if (isNaN(d.getTime())) {
|
|
87
94
|
return String(value);
|
|
88
95
|
}
|
|
89
|
-
return
|
|
90
|
-
|
|
91
|
-
day: 'numeric',
|
|
92
|
-
|
|
93
|
-
|
|
96
|
+
return plainDateFormat(
|
|
97
|
+
plainDateFromDate(d),
|
|
98
|
+
{month: 'short', day: 'numeric', calendar},
|
|
99
|
+
locale,
|
|
100
|
+
);
|
|
94
101
|
}
|
|
95
102
|
|
|
96
103
|
/**
|
|
97
|
-
* Date formatter — month/year (e.g. "Jan 2024", "Mar 2025")
|
|
104
|
+
* Date formatter — month/year (e.g. "Jan 2024", "Mar 2025"), in `calendar`
|
|
105
|
+
* (Gregorian by default)
|
|
98
106
|
*/
|
|
99
|
-
export function monthYear(
|
|
107
|
+
export function monthYear(
|
|
108
|
+
value: unknown,
|
|
109
|
+
locale: Locale = 'en',
|
|
110
|
+
calendar: CalendarSystem = 'gregory',
|
|
111
|
+
): string {
|
|
100
112
|
const d = value instanceof Date ? value : new Date(Number(value));
|
|
101
113
|
if (isNaN(d.getTime())) {
|
|
102
114
|
return String(value);
|
|
103
115
|
}
|
|
104
|
-
return
|
|
105
|
-
|
|
106
|
-
year: 'numeric',
|
|
107
|
-
|
|
108
|
-
|
|
116
|
+
return plainDateFormat(
|
|
117
|
+
plainDateFromDate(d),
|
|
118
|
+
{month: 'short', year: 'numeric', calendar},
|
|
119
|
+
locale,
|
|
120
|
+
);
|
|
109
121
|
}
|