@astryxdesign/charts 0.7.9-canary.de1be1f → 0.7.9-canary.e657e2e

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,5 +1,6 @@
1
1
  /**
2
2
  * @file formatters.ts
3
+ * @input Values, a provider locale, and for dates an optional calendar
3
4
  * @output Built-in locale-aware tick, value, and list format utilities
4
5
  * @position Utility; consumed by Chart and ChartAxis
5
6
  *
@@ -9,7 +10,7 @@
9
10
  * to cached `Intl` formatters so output is locale-aware and rounding is correct
10
11
  * across the full magnitude range (K/M/B/T and beyond).
11
12
  */
12
- import type { Locale } from '@astryxdesign/core/i18n';
13
+ import type { CalendarSystem, Locale } from '@astryxdesign/core/i18n';
13
14
  /** Format labels as a conjunction list using the provider locale. */
14
15
  export declare function formatList(values: readonly string[], locale: Locale): string;
15
16
  /**
@@ -40,8 +41,16 @@ export declare function currency(symbol?: string, locale?: Locale): (value: unkn
40
41
  * render without a decimal, and up to one fractional digit is shown otherwise.
41
42
  */
42
43
  export declare function percent(value: unknown, locale: Locale): string;
43
- /** Date formatter — short date (e.g. "Jan 5", "Mar 12"). */
44
- export declare function shortDate(value: unknown, locale: Locale): string;
45
- /** Date formatter — month/year (e.g. "Jan 2024", "Mar 2025"). */
46
- export declare function monthYear(value: unknown, locale: Locale): string;
44
+ /**
45
+ * Date formatter — short date (e.g. "Jan 5", "Mar 12"), in `calendar`. The
46
+ * calendar defaults to Gregorian; pass the application's calendar to match
47
+ * the date components.
48
+ */
49
+ export declare function shortDate(value: unknown, locale: Locale, calendar?: CalendarSystem): string;
50
+ /**
51
+ * Date formatter — month/year (e.g. "Jan 2024", "Mar 2025"), in `calendar`.
52
+ * The calendar defaults to Gregorian; pass the application's calendar to
53
+ * match the date components.
54
+ */
55
+ export declare function monthYear(value: unknown, locale: Locale, calendar?: CalendarSystem): string;
47
56
  //# sourceMappingURL=formatters.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"formatters.d.ts","sourceRoot":"","sources":["../src/formatters.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;GAUG;AAEH,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,yBAAyB,CAAC;AAuDpD,qEAAqE;AACrE,wBAAgB,UAAU,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAE5E;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAMpE;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,QAAQ,CACtB,MAAM,SAAM,EACZ,MAAM,CAAC,EAAE,MAAM,GACd,CAAC,KAAK,EAAE,OAAO,KAAK,MAAM,CAc5B;AAED;;;;;GAKG;AACH,wBAAgB,OAAO,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAM9D;AAiDD,4DAA4D;AAC5D,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAMhE;AAED,iEAAiE;AACjE,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAMhE"}
1
+ {"version":3,"file":"formatters.d.ts","sourceRoot":"","sources":["../src/formatters.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;GAWG;AAEH,OAAO,KAAK,EAAC,cAAc,EAAE,MAAM,EAAC,MAAM,yBAAyB,CAAC;AAwDpE,qEAAqE;AACrE,wBAAgB,UAAU,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAE5E;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAMpE;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,QAAQ,CACtB,MAAM,SAAM,EACZ,MAAM,CAAC,EAAE,MAAM,GACd,CAAC,KAAK,EAAE,OAAO,KAAK,MAAM,CAc5B;AAED;;;;;GAKG;AACH,wBAAgB,OAAO,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAM9D;AAiDD;;;;GAIG;AACH,wBAAgB,SAAS,CACvB,KAAK,EAAE,OAAO,EACd,MAAM,EAAE,MAAM,EACd,QAAQ,GAAE,cAA0B,GACnC,MAAM,CAYR;AAED;;;;GAIG;AACH,wBAAgB,SAAS,CACvB,KAAK,EAAE,OAAO,EACd,MAAM,EAAE,MAAM,EACd,QAAQ,GAAE,cAA0B,GACnC,MAAM,CAYR"}
@@ -2,6 +2,7 @@
2
2
 
3
3
  /**
4
4
  * @file formatters.ts
5
+ * @input Values, a provider locale, and for dates an optional calendar
5
6
  * @output Built-in locale-aware tick, value, and list format utilities
6
7
  * @position Utility; consumed by Chart and ChartAxis
7
8
  *
@@ -12,6 +13,8 @@
12
13
  * across the full magnitude range (K/M/B/T and beyond).
13
14
  */
14
15
 
16
+ import { plainDateFormat, plainDateFromDate } from '@astryxdesign/core/utils';
17
+
15
18
  /** Lazily cache one formatter per locale, including the legacy host default. */
16
19
  function byLocale(create) {
17
20
  const cache = new Map();
@@ -149,20 +152,36 @@ function toDate(value) {
149
152
  return new Date(NaN);
150
153
  }
151
154
 
152
- /** Date formatter — short date (e.g. "Jan 5", "Mar 12"). */
153
- export function shortDate(value, locale) {
155
+ /**
156
+ * Date formatter — short date (e.g. "Jan 5", "Mar 12"), in `calendar`. The
157
+ * calendar defaults to Gregorian; pass the application's calendar to match
158
+ * the date components.
159
+ */
160
+ export function shortDate(value, locale, calendar = 'gregory') {
154
161
  const d = toDate(value);
155
162
  if (Number.isNaN(d.getTime())) {
156
163
  return String(value);
157
164
  }
158
- return getShortDateFormat(locale).format(d);
165
+ return calendar === 'gregory' ? getShortDateFormat(locale).format(d) : plainDateFormat(plainDateFromDate(d), {
166
+ month: 'short',
167
+ day: 'numeric',
168
+ calendar
169
+ }, locale);
159
170
  }
160
171
 
161
- /** Date formatter — month/year (e.g. "Jan 2024", "Mar 2025"). */
162
- export function monthYear(value, locale) {
172
+ /**
173
+ * Date formatter — month/year (e.g. "Jan 2024", "Mar 2025"), in `calendar`.
174
+ * The calendar defaults to Gregorian; pass the application's calendar to
175
+ * match the date components.
176
+ */
177
+ export function monthYear(value, locale, calendar = 'gregory') {
163
178
  const d = toDate(value);
164
179
  if (Number.isNaN(d.getTime())) {
165
180
  return String(value);
166
181
  }
167
- return getMonthYearFormat(locale).format(d);
182
+ return calendar === 'gregory' ? getMonthYearFormat(locale).format(d) : plainDateFormat(plainDateFromDate(d), {
183
+ month: 'short',
184
+ year: 'numeric',
185
+ calendar
186
+ }, locale);
168
187
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astryxdesign/charts",
3
- "version": "0.7.9-canary.de1be1f",
3
+ "version": "0.7.9-canary.e657e2e",
4
4
  "description": "Astryx charts — a config-model data visualization library (d3-based). 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.de1be1f",
52
+ "@astryxdesign/core": "0.7.9-canary.e657e2e",
53
53
  "react": ">=19.0.0",
54
54
  "react-dom": ">=19.0.0"
55
55
  },
@@ -59,7 +59,7 @@
59
59
  "d3-shape": "^3.2.0"
60
60
  },
61
61
  "devDependencies": {
62
- "@astryxdesign/cli": "0.7.9-canary.de1be1f",
62
+ "@astryxdesign/cli": "0.7.9-canary.e657e2e",
63
63
  "@babel/cli": "^7.29.7",
64
64
  "@babel/core": "^7.29.7",
65
65
  "@babel/preset-react": "^7.29.7",
@@ -86,6 +86,45 @@ describe('chart formatters', () => {
86
86
  );
87
87
  });
88
88
 
89
+ it.each([
90
+ {calendar: 'persian', locale: 'en', year: '1405'},
91
+ {calendar: 'persian', locale: 'fa', year: '۱۴۰۵'},
92
+ {calendar: 'hebrew', locale: 'en', year: '5787'},
93
+ {calendar: 'hebrew', locale: 'he', year: '5787'},
94
+ {calendar: 'buddhist', locale: 'en', year: '2569'},
95
+ {calendar: 'buddhist', locale: 'th', year: '2569'},
96
+ ] as const)(
97
+ 'formats dates in an explicit $calendar calendar under $locale',
98
+ ({calendar, locale, year}) => {
99
+ const value = '2026-10-30';
100
+ const date = new Date(2026, 9, 30);
101
+ expect(shortDate(value, locale, calendar)).toBe(
102
+ new Intl.DateTimeFormat(locale, {
103
+ month: 'short',
104
+ day: 'numeric',
105
+ calendar,
106
+ }).format(date),
107
+ );
108
+ const label = monthYear(value, locale, calendar);
109
+ expect(label).toBe(
110
+ new Intl.DateTimeFormat(locale, {
111
+ month: 'short',
112
+ year: 'numeric',
113
+ calendar,
114
+ }).format(date),
115
+ );
116
+ expect(label).toContain(year);
117
+ expect(label).not.toContain('2026');
118
+ },
119
+ );
120
+
121
+ it('defaults to Gregorian and writes `iso8601` dates as Gregorian ones', () => {
122
+ const value = '2026-10-30';
123
+ expect(monthYear(value, 'en')).toBe('Oct 2026');
124
+ expect(monthYear(value, 'en', 'iso8601')).toBe('Oct 2026');
125
+ expect(shortDate(value, 'en', 'iso8601')).toBe(shortDate(value, 'en'));
126
+ });
127
+
89
128
  it('passes through non-finite and unparseable values', () => {
90
129
  expect(compactNumber(Infinity, 'en-US')).toBe('Infinity');
91
130
  expect(percent('not-a-number', 'en-US')).toBe('not-a-number');
package/src/formatters.ts CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  /**
4
4
  * @file formatters.ts
5
+ * @input Values, a provider locale, and for dates an optional calendar
5
6
  * @output Built-in locale-aware tick, value, and list format utilities
6
7
  * @position Utility; consumed by Chart and ChartAxis
7
8
  *
@@ -12,7 +13,8 @@
12
13
  * across the full magnitude range (K/M/B/T and beyond).
13
14
  */
14
15
 
15
- import type {Locale} from '@astryxdesign/core/i18n';
16
+ import type {CalendarSystem, Locale} from '@astryxdesign/core/i18n';
17
+ import {plainDateFormat, plainDateFromDate} from '@astryxdesign/core/utils';
16
18
 
17
19
  /** Lazily cache one formatter per locale, including the legacy host default. */
18
20
  function byLocale<T>(
@@ -179,20 +181,48 @@ function toDate(value: unknown): Date {
179
181
  return new Date(NaN);
180
182
  }
181
183
 
182
- /** Date formatter — short date (e.g. "Jan 5", "Mar 12"). */
183
- export function shortDate(value: unknown, locale: Locale): string {
184
+ /**
185
+ * Date formatter — short date (e.g. "Jan 5", "Mar 12"), in `calendar`. The
186
+ * calendar defaults to Gregorian; pass the application's calendar to match
187
+ * the date components.
188
+ */
189
+ export function shortDate(
190
+ value: unknown,
191
+ locale: Locale,
192
+ calendar: CalendarSystem = 'gregory',
193
+ ): string {
184
194
  const d = toDate(value);
185
195
  if (Number.isNaN(d.getTime())) {
186
196
  return String(value);
187
197
  }
188
- return getShortDateFormat(locale).format(d);
198
+ return calendar === 'gregory'
199
+ ? getShortDateFormat(locale).format(d)
200
+ : plainDateFormat(
201
+ plainDateFromDate(d),
202
+ {month: 'short', day: 'numeric', calendar},
203
+ locale,
204
+ );
189
205
  }
190
206
 
191
- /** Date formatter — month/year (e.g. "Jan 2024", "Mar 2025"). */
192
- export function monthYear(value: unknown, locale: Locale): string {
207
+ /**
208
+ * Date formatter — month/year (e.g. "Jan 2024", "Mar 2025"), in `calendar`.
209
+ * The calendar defaults to Gregorian; pass the application's calendar to
210
+ * match the date components.
211
+ */
212
+ export function monthYear(
213
+ value: unknown,
214
+ locale: Locale,
215
+ calendar: CalendarSystem = 'gregory',
216
+ ): string {
193
217
  const d = toDate(value);
194
218
  if (Number.isNaN(d.getTime())) {
195
219
  return String(value);
196
220
  }
197
- return getMonthYearFormat(locale).format(d);
221
+ return calendar === 'gregory'
222
+ ? getMonthYearFormat(locale).format(d)
223
+ : plainDateFormat(
224
+ plainDateFromDate(d),
225
+ {month: 'short', year: 'numeric', calendar},
226
+ locale,
227
+ );
198
228
  }