react-native-hilal 0.0.0-stage → 0.2.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.
Files changed (95) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +300 -2
  3. package/lib/module/calendar/Appear.js +75 -0
  4. package/lib/module/calendar/Appear.js.map +1 -0
  5. package/lib/module/calendar/Chevron.js +60 -0
  6. package/lib/module/calendar/Chevron.js.map +1 -0
  7. package/lib/module/calendar/DayCell.js +51 -0
  8. package/lib/module/calendar/DayCell.js.map +1 -0
  9. package/lib/module/calendar/HilalCalendar.js +565 -0
  10. package/lib/module/calendar/HilalCalendar.js.map +1 -0
  11. package/lib/module/calendar/PickerGrid.js +66 -0
  12. package/lib/module/calendar/PickerGrid.js.map +1 -0
  13. package/lib/module/calendar/index.js +5 -0
  14. package/lib/module/calendar/index.js.map +1 -0
  15. package/lib/module/calendar/month.js +291 -0
  16. package/lib/module/calendar/month.js.map +1 -0
  17. package/lib/module/calendar/styles.js +338 -0
  18. package/lib/module/calendar/styles.js.map +1 -0
  19. package/lib/module/calendar/theme.js +49 -0
  20. package/lib/module/calendar/theme.js.map +1 -0
  21. package/lib/module/calendar/types.js +4 -0
  22. package/lib/module/calendar/types.js.map +1 -0
  23. package/lib/module/core/convert.js +191 -0
  24. package/lib/module/core/convert.js.map +1 -0
  25. package/lib/module/core/days.js +61 -0
  26. package/lib/module/core/days.js.map +1 -0
  27. package/lib/module/core/format.js +37 -0
  28. package/lib/module/core/format.js.map +1 -0
  29. package/lib/module/core/index.js +6 -0
  30. package/lib/module/core/index.js.map +1 -0
  31. package/lib/module/core/locale.js +62 -0
  32. package/lib/module/core/locale.js.map +1 -0
  33. package/lib/module/core/types.js +2 -0
  34. package/lib/module/core/types.js.map +1 -0
  35. package/lib/module/core/umalqura-data.js +24 -0
  36. package/lib/module/core/umalqura-data.js.map +1 -0
  37. package/lib/module/index.js +5 -0
  38. package/lib/module/index.js.map +1 -0
  39. package/lib/module/package.json +1 -0
  40. package/lib/typescript/package.json +1 -0
  41. package/lib/typescript/src/calendar/Appear.d.ts +22 -0
  42. package/lib/typescript/src/calendar/Appear.d.ts.map +1 -0
  43. package/lib/typescript/src/calendar/Chevron.d.ts +13 -0
  44. package/lib/typescript/src/calendar/Chevron.d.ts.map +1 -0
  45. package/lib/typescript/src/calendar/DayCell.d.ts +24 -0
  46. package/lib/typescript/src/calendar/DayCell.d.ts.map +1 -0
  47. package/lib/typescript/src/calendar/HilalCalendar.d.ts +7 -0
  48. package/lib/typescript/src/calendar/HilalCalendar.d.ts.map +1 -0
  49. package/lib/typescript/src/calendar/PickerGrid.d.ts +28 -0
  50. package/lib/typescript/src/calendar/PickerGrid.d.ts.map +1 -0
  51. package/lib/typescript/src/calendar/index.d.ts +4 -0
  52. package/lib/typescript/src/calendar/index.d.ts.map +1 -0
  53. package/lib/typescript/src/calendar/month.d.ts +86 -0
  54. package/lib/typescript/src/calendar/month.d.ts.map +1 -0
  55. package/lib/typescript/src/calendar/styles.d.ts +206 -0
  56. package/lib/typescript/src/calendar/styles.d.ts.map +1 -0
  57. package/lib/typescript/src/calendar/theme.d.ts +51 -0
  58. package/lib/typescript/src/calendar/theme.d.ts.map +1 -0
  59. package/lib/typescript/src/calendar/types.d.ts +162 -0
  60. package/lib/typescript/src/calendar/types.d.ts.map +1 -0
  61. package/lib/typescript/src/core/convert.d.ts +77 -0
  62. package/lib/typescript/src/core/convert.d.ts.map +1 -0
  63. package/lib/typescript/src/core/days.d.ts +24 -0
  64. package/lib/typescript/src/core/days.d.ts.map +1 -0
  65. package/lib/typescript/src/core/format.d.ts +23 -0
  66. package/lib/typescript/src/core/format.d.ts.map +1 -0
  67. package/lib/typescript/src/core/index.d.ts +5 -0
  68. package/lib/typescript/src/core/index.d.ts.map +1 -0
  69. package/lib/typescript/src/core/locale.d.ts +21 -0
  70. package/lib/typescript/src/core/locale.d.ts.map +1 -0
  71. package/lib/typescript/src/core/types.d.ts +7 -0
  72. package/lib/typescript/src/core/types.d.ts.map +1 -0
  73. package/lib/typescript/src/core/umalqura-data.d.ts +15 -0
  74. package/lib/typescript/src/core/umalqura-data.d.ts.map +1 -0
  75. package/lib/typescript/src/index.d.ts +3 -0
  76. package/lib/typescript/src/index.d.ts.map +1 -0
  77. package/package.json +180 -4
  78. package/src/calendar/Appear.tsx +94 -0
  79. package/src/calendar/Chevron.tsx +55 -0
  80. package/src/calendar/DayCell.tsx +89 -0
  81. package/src/calendar/HilalCalendar.tsx +728 -0
  82. package/src/calendar/PickerGrid.tsx +99 -0
  83. package/src/calendar/index.ts +13 -0
  84. package/src/calendar/month.ts +364 -0
  85. package/src/calendar/styles.ts +357 -0
  86. package/src/calendar/theme.ts +89 -0
  87. package/src/calendar/types.ts +168 -0
  88. package/src/core/convert.ts +256 -0
  89. package/src/core/days.ts +64 -0
  90. package/src/core/format.ts +65 -0
  91. package/src/core/index.ts +19 -0
  92. package/src/core/locale.ts +141 -0
  93. package/src/core/types.ts +6 -0
  94. package/src/core/umalqura-data.ts +34 -0
  95. package/src/index.tsx +2 -0
@@ -0,0 +1,256 @@
1
+ import {
2
+ civilFromDays,
3
+ epochDayOfLocalDate,
4
+ localDateOfEpochDay,
5
+ } from './days';
6
+ import type { HijriDate } from './types';
7
+ import {
8
+ UMALQURA_MAX_YEAR,
9
+ UMALQURA_MIN_YEAR,
10
+ UMALQURA_MONTH_LENGTH_MASKS,
11
+ UMALQURA_START_EPOCH_DAY,
12
+ } from './umalqura-data';
13
+
14
+ /**
15
+ * Epoch day of the first day of every month in the table, plus one extra
16
+ * entry for the day after the last month. Index `(year - MIN) * 12 +
17
+ * (month - 1)`.
18
+ */
19
+ const MONTH_STARTS: readonly number[] = (() => {
20
+ const starts = [UMALQURA_START_EPOCH_DAY];
21
+ let epochDay = UMALQURA_START_EPOCH_DAY;
22
+ for (const mask of UMALQURA_MONTH_LENGTH_MASKS) {
23
+ for (let month = 0; month < 12; month++) {
24
+ // eslint-disable-next-line no-bitwise -- the table is a bit mask
25
+ epochDay += (mask >> month) & 1 ? 30 : 29;
26
+ starts.push(epochDay);
27
+ }
28
+ }
29
+ return starts;
30
+ })();
31
+
32
+ /**
33
+ * First and last epoch day of the Umm al-Qura table (both inclusive). With
34
+ * a Hijri adjustment of `n` days, the supported Gregorian days move by
35
+ * `-n`: see `supportedEpochDays`.
36
+ */
37
+ export const FIRST_DAY = MONTH_STARTS[0]!;
38
+ export const LAST_DAY = MONTH_STARTS[MONTH_STARTS.length - 1]! - 1;
39
+
40
+ /** The largest Hijri adjustment accepted, in days, in either direction. */
41
+ export const MAX_HIJRI_ADJUSTMENT = 2;
42
+
43
+ export type HijriConversionOptions = {
44
+ /**
45
+ * Days to shift the Hijri calendar by, from -2 to 2. `1` makes every
46
+ * Hijri date one day later than Umm al-Qura (Umm al-Qura's 29 Sha'ban
47
+ * becomes 30 Sha'ban), for places where the month started a day
48
+ * earlier. Default 0.
49
+ */
50
+ adjustment?: number;
51
+ };
52
+
53
+ /**
54
+ * Checks a Hijri adjustment and returns it.
55
+ *
56
+ * @throws {RangeError} unless it is an integer from -2 to 2.
57
+ */
58
+ export function checkAdjustment(adjustment: number, caller: string) {
59
+ if (
60
+ !Number.isInteger(adjustment) ||
61
+ Math.abs(adjustment) > MAX_HIJRI_ADJUSTMENT
62
+ ) {
63
+ throw new RangeError(
64
+ `${caller}: the Hijri adjustment must be an integer from ` +
65
+ `-${MAX_HIJRI_ADJUSTMENT} to ${MAX_HIJRI_ADJUSTMENT}, got ${adjustment}`
66
+ );
67
+ }
68
+ return adjustment;
69
+ }
70
+
71
+ /** First and last epoch day that have a Hijri date, for an adjustment. */
72
+ export function supportedEpochDays(adjustment = 0) {
73
+ return { min: FIRST_DAY - adjustment, max: LAST_DAY - adjustment };
74
+ }
75
+
76
+ function monthIndex(year: number, month: number) {
77
+ return (year - UMALQURA_MIN_YEAR) * 12 + (month - 1);
78
+ }
79
+
80
+ function formatIso({ year, month, day }: HijriDate) {
81
+ const pad = (n: number) => String(n).padStart(2, '0');
82
+ return `${year}-${pad(month)}-${pad(day)}`;
83
+ }
84
+
85
+ function supportedRangeText(adjustment: number) {
86
+ const { min, max } = getSupportedRange();
87
+ const days = supportedEpochDays(adjustment);
88
+ return (
89
+ `${formatIso(min)} to ${formatIso(max)} AH ` +
90
+ `(${formatIso(civilFromDays(days.min))} to ` +
91
+ `${formatIso(civilFromDays(days.max))})`
92
+ );
93
+ }
94
+
95
+ /** First and last Hijri dates the table covers. */
96
+ export function getSupportedRange(): { min: HijriDate; max: HijriDate } {
97
+ return {
98
+ min: { year: UMALQURA_MIN_YEAR, month: 1, day: 1 },
99
+ max: {
100
+ year: UMALQURA_MAX_YEAR,
101
+ month: 12,
102
+ day: getHijriMonthLength(UMALQURA_MAX_YEAR, 12),
103
+ },
104
+ };
105
+ }
106
+
107
+ /**
108
+ * Number of days in a Hijri month.
109
+ *
110
+ * @throws {RangeError} if the year is outside the supported range or the
111
+ * month is not an integer from 1 to 12.
112
+ */
113
+ export function getHijriMonthLength(year: number, month: number): 29 | 30 {
114
+ if (
115
+ !Number.isInteger(year) ||
116
+ !Number.isInteger(month) ||
117
+ year < UMALQURA_MIN_YEAR ||
118
+ year > UMALQURA_MAX_YEAR ||
119
+ month < 1 ||
120
+ month > 12
121
+ ) {
122
+ throw new RangeError(
123
+ `getHijriMonthLength: ${year}-${month} is not a month in the ` +
124
+ `supported range (${UMALQURA_MIN_YEAR}-${UMALQURA_MAX_YEAR} AH)`
125
+ );
126
+ }
127
+ const index = monthIndex(year, month);
128
+ return MONTH_STARTS[index + 1]! - MONTH_STARTS[index]! === 30 ? 30 : 29;
129
+ }
130
+
131
+ /** Whether `hijri` is a real day within the supported range. */
132
+ export function isValidHijri(hijri: HijriDate): boolean {
133
+ if (typeof hijri !== 'object' || hijri === null) {
134
+ return false;
135
+ }
136
+ const { year, month, day } = hijri;
137
+ if (
138
+ !Number.isInteger(year) ||
139
+ !Number.isInteger(month) ||
140
+ !Number.isInteger(day) ||
141
+ year < UMALQURA_MIN_YEAR ||
142
+ year > UMALQURA_MAX_YEAR ||
143
+ month < 1 ||
144
+ month > 12 ||
145
+ day < 1
146
+ ) {
147
+ return false;
148
+ }
149
+ return day <= getHijriMonthLength(year, month);
150
+ }
151
+
152
+ /**
153
+ * Hijri date of an epoch day, shifted by `adjustment` days. Internal: the
154
+ * public entry point is `toHijri`.
155
+ *
156
+ * @throws {RangeError} if the day is outside the supported range.
157
+ */
158
+ export function hijriOfEpochDay(
159
+ epochDay: number,
160
+ caller = 'toHijri',
161
+ adjustment = 0
162
+ ): HijriDate {
163
+ const tableDay = epochDay + adjustment;
164
+ if (
165
+ !Number.isInteger(epochDay) ||
166
+ tableDay < FIRST_DAY ||
167
+ tableDay > LAST_DAY
168
+ ) {
169
+ throw new RangeError(
170
+ `${caller}: ${formatIso(civilFromDays(epochDay))} is outside the ` +
171
+ `supported range ${supportedRangeText(adjustment)}`
172
+ );
173
+ }
174
+
175
+ // Last month that starts on or before `epochDay`.
176
+ let low = 0;
177
+ let high = MONTH_STARTS.length - 2;
178
+ while (low < high) {
179
+ const mid = (low + high + 1) >> 1; // eslint-disable-line no-bitwise
180
+ if (MONTH_STARTS[mid]! <= tableDay) {
181
+ low = mid;
182
+ } else {
183
+ high = mid - 1;
184
+ }
185
+ }
186
+
187
+ return {
188
+ year: UMALQURA_MIN_YEAR + Math.floor(low / 12),
189
+ month: (low % 12) + 1,
190
+ day: tableDay - MONTH_STARTS[low]! + 1,
191
+ };
192
+ }
193
+
194
+ /**
195
+ * Epoch day of a Hijri date, shifted by `adjustment` days. Internal: the
196
+ * public entry point is `toGregorian`.
197
+ *
198
+ * @throws {RangeError} if `hijri` is not a valid date in the supported
199
+ * range.
200
+ */
201
+ export function epochDayOfHijri(
202
+ hijri: HijriDate,
203
+ caller = 'toGregorian',
204
+ adjustment = 0
205
+ ) {
206
+ if (!isValidHijri(hijri)) {
207
+ const text =
208
+ typeof hijri === 'object' && hijri !== null
209
+ ? `${hijri.year}-${hijri.month}-${hijri.day}`
210
+ : String(hijri);
211
+ throw new RangeError(
212
+ `${caller}: ${text} is not a valid Hijri date in the supported ` +
213
+ `range ${supportedRangeText(adjustment)}`
214
+ );
215
+ }
216
+ return (
217
+ MONTH_STARTS[monthIndex(hijri.year, hijri.month)]! +
218
+ hijri.day -
219
+ 1 -
220
+ adjustment
221
+ );
222
+ }
223
+
224
+ /**
225
+ * Converts the local calendar day of `date` to a Hijri date. The time of
226
+ * day and the time zone offset are ignored: 23:59 and 00:00 on the same
227
+ * local day give the same result.
228
+ *
229
+ * @throws {RangeError} if `date` is invalid or outside the supported
230
+ * range, or the adjustment is not an integer from -2 to 2.
231
+ */
232
+ export function toHijri(
233
+ date: Date,
234
+ options: HijriConversionOptions = {}
235
+ ): HijriDate {
236
+ const adjustment = checkAdjustment(options.adjustment ?? 0, 'toHijri');
237
+ if (!(date instanceof Date) || Number.isNaN(date.getTime())) {
238
+ throw new RangeError('toHijri: expected a valid Date');
239
+ }
240
+ return hijriOfEpochDay(epochDayOfLocalDate(date), 'toHijri', adjustment);
241
+ }
242
+
243
+ /**
244
+ * Converts a Hijri date to a `Date` at local midnight of the matching
245
+ * Gregorian day.
246
+ *
247
+ * @throws {RangeError} if `hijri` is not a valid date in the supported
248
+ * range, or the adjustment is not an integer from -2 to 2.
249
+ */
250
+ export function toGregorian(
251
+ hijri: HijriDate,
252
+ options: HijriConversionOptions = {}
253
+ ): Date {
254
+ const adjustment = checkAdjustment(options.adjustment ?? 0, 'toGregorian');
255
+ return localDateOfEpochDay(epochDayOfHijri(hijri, 'toGregorian', adjustment));
256
+ }
@@ -0,0 +1,64 @@
1
+ /**
2
+ * Calendar-day arithmetic on plain year/month/day numbers.
3
+ *
4
+ * Days are counted from 1970-01-01 in the proleptic Gregorian calendar
5
+ * ("epoch days"). The algorithms are Howard Hinnant's `days_from_civil`
6
+ * and `civil_from_days`, which use integer math only and never touch a
7
+ * time zone.
8
+ */
9
+
10
+ export type CivilDate = { year: number; month: number; day: number };
11
+
12
+ /** Epoch day of a Gregorian year/month/day (month is 1-12). */
13
+ export function daysFromCivil(year: number, month: number, day: number) {
14
+ const y = month <= 2 ? year - 1 : year;
15
+ const era = Math.floor(y / 400);
16
+ const yearOfEra = y - era * 400;
17
+ const shiftedMonth = (month + 9) % 12; // March = 0
18
+ const dayOfYear = Math.floor((153 * shiftedMonth + 2) / 5) + day - 1;
19
+ const dayOfEra =
20
+ yearOfEra * 365 +
21
+ Math.floor(yearOfEra / 4) -
22
+ Math.floor(yearOfEra / 100) +
23
+ dayOfYear;
24
+ return era * 146097 + dayOfEra - 719468;
25
+ }
26
+
27
+ /** Gregorian year/month/day (month is 1-12) of an epoch day. */
28
+ export function civilFromDays(epochDay: number): CivilDate {
29
+ const z = epochDay + 719468;
30
+ const era = Math.floor(z / 146097);
31
+ const dayOfEra = z - era * 146097;
32
+ const yearOfEra = Math.floor(
33
+ (dayOfEra -
34
+ Math.floor(dayOfEra / 1460) +
35
+ Math.floor(dayOfEra / 36524) -
36
+ Math.floor(dayOfEra / 146096)) /
37
+ 365
38
+ );
39
+ const dayOfYear =
40
+ dayOfEra -
41
+ (365 * yearOfEra + Math.floor(yearOfEra / 4) - Math.floor(yearOfEra / 100));
42
+ const shiftedMonth = Math.floor((5 * dayOfYear + 2) / 153);
43
+ const day = dayOfYear - Math.floor((153 * shiftedMonth + 2) / 5) + 1;
44
+ const month = shiftedMonth < 10 ? shiftedMonth + 3 : shiftedMonth - 9;
45
+ const year = yearOfEra + era * 400 + (month <= 2 ? 1 : 0);
46
+ return { year, month, day };
47
+ }
48
+
49
+ /** Epoch day of the local calendar day `date` falls on. */
50
+ export function epochDayOfLocalDate(date: Date) {
51
+ return daysFromCivil(date.getFullYear(), date.getMonth() + 1, date.getDate());
52
+ }
53
+
54
+ /** A `Date` at local midnight (start of day) of an epoch day. */
55
+ export function localDateOfEpochDay(epochDay: number) {
56
+ const { year, month, day } = civilFromDays(epochDay);
57
+ return new Date(year, month - 1, day);
58
+ }
59
+
60
+ /** Day of the week of an epoch day, 0 = Sunday (like `Date#getDay()`). */
61
+ export function weekdayOfEpochDay(epochDay: number) {
62
+ // 1970-01-01 was a Thursday.
63
+ return (((epochDay + 4) % 7) + 7) % 7;
64
+ }
@@ -0,0 +1,65 @@
1
+ import { isValidHijri } from './convert';
2
+ import {
3
+ defaultDigits,
4
+ formatNumber,
5
+ getMonthNames,
6
+ type Digits,
7
+ type Locale,
8
+ } from './locale';
9
+ import type { CivilDate } from './days';
10
+ import type { HijriDate } from './types';
11
+
12
+ export type FormatHijriOptions = {
13
+ /** Default: 'ar'. */
14
+ locale?: Locale;
15
+ /** Default: 'arabic' when locale is 'ar', 'latin' otherwise. */
16
+ digits?: Digits;
17
+ };
18
+
19
+ const ERA: Record<Locale, string> = { ar: 'هـ', en: 'AH' };
20
+
21
+ /**
22
+ * Formats a Hijri date as "day month year era", for example
23
+ * "١ رمضان ١٤٤٦ هـ" or "1 Ramadan 1446 AH".
24
+ *
25
+ * @throws {RangeError} if `hijri` is not a valid date in the supported
26
+ * range.
27
+ */
28
+ export function formatHijri(
29
+ hijri: HijriDate,
30
+ options: FormatHijriOptions = {}
31
+ ): string {
32
+ if (!isValidHijri(hijri)) {
33
+ throw new RangeError(
34
+ `formatHijri: ${hijri.year}-${hijri.month}-${hijri.day} is not a ` +
35
+ 'valid Hijri date in the supported range'
36
+ );
37
+ }
38
+ const locale = options.locale ?? 'ar';
39
+ const digits = options.digits ?? defaultDigits(locale);
40
+ const month = getMonthNames('hijri', locale)[hijri.month - 1];
41
+ return [
42
+ formatNumber(hijri.day, digits),
43
+ month,
44
+ formatNumber(hijri.year, digits),
45
+ ERA[locale],
46
+ ].join(' ');
47
+ }
48
+
49
+ /**
50
+ * Formats a Gregorian date as "day month year", for example
51
+ * "١١ مارس ٢٠٢٤" or "11 March 2024". Internal: used by the calendar.
52
+ */
53
+ export function formatGregorian(
54
+ date: CivilDate,
55
+ options: FormatHijriOptions = {}
56
+ ): string {
57
+ const locale = options.locale ?? 'ar';
58
+ const digits = options.digits ?? defaultDigits(locale);
59
+ const month = getMonthNames('gregorian', locale)[date.month - 1];
60
+ return [
61
+ formatNumber(date.day, digits),
62
+ month,
63
+ formatNumber(date.year, digits),
64
+ ].join(' ');
65
+ }
@@ -0,0 +1,19 @@
1
+ export {
2
+ getHijriMonthLength,
3
+ getSupportedRange,
4
+ isValidHijri,
5
+ toGregorian,
6
+ toHijri,
7
+ type HijriConversionOptions,
8
+ } from './convert';
9
+ export { formatHijri, type FormatHijriOptions } from './format';
10
+ export {
11
+ getMonthNames,
12
+ getWeekdayNames,
13
+ toArabicDigits,
14
+ type CalendarType,
15
+ type Digits,
16
+ type Locale,
17
+ type WeekdayFormat,
18
+ } from './locale';
19
+ export type { HijriDate } from './types';
@@ -0,0 +1,141 @@
1
+ export type Locale = 'ar' | 'en';
2
+ export type Digits = 'arabic' | 'latin';
3
+ export type CalendarType = 'hijri' | 'gregorian';
4
+ export type WeekdayFormat = 'short' | 'long' | 'narrow';
5
+
6
+ const HIJRI_MONTHS: Record<Locale, readonly string[]> = {
7
+ ar: [
8
+ 'محرم',
9
+ 'صفر',
10
+ 'ربيع الأول',
11
+ 'ربيع الآخر',
12
+ 'جمادى الأولى',
13
+ 'جمادى الآخرة',
14
+ 'رجب',
15
+ 'شعبان',
16
+ 'رمضان',
17
+ 'شوال',
18
+ 'ذو القعدة',
19
+ 'ذو الحجة',
20
+ ],
21
+ en: [
22
+ 'Muharram',
23
+ 'Safar',
24
+ "Rabi' al-Awwal",
25
+ "Rabi' al-Thani",
26
+ 'Jumada al-Ula',
27
+ 'Jumada al-Akhirah',
28
+ 'Rajab',
29
+ "Sha'ban",
30
+ 'Ramadan',
31
+ 'Shawwal',
32
+ "Dhu al-Qi'dah",
33
+ 'Dhu al-Hijjah',
34
+ ],
35
+ };
36
+
37
+ // Saudi/Gulf style month names (يناير، فبراير ...), not the Levantine
38
+ // names (كانون الثاني، شباط ...).
39
+ const GREGORIAN_MONTHS: Record<Locale, readonly string[]> = {
40
+ ar: [
41
+ 'يناير',
42
+ 'فبراير',
43
+ 'مارس',
44
+ 'أبريل',
45
+ 'مايو',
46
+ 'يونيو',
47
+ 'يوليو',
48
+ 'أغسطس',
49
+ 'سبتمبر',
50
+ 'أكتوبر',
51
+ 'نوفمبر',
52
+ 'ديسمبر',
53
+ ],
54
+ en: [
55
+ 'January',
56
+ 'February',
57
+ 'March',
58
+ 'April',
59
+ 'May',
60
+ 'June',
61
+ 'July',
62
+ 'August',
63
+ 'September',
64
+ 'October',
65
+ 'November',
66
+ 'December',
67
+ ],
68
+ };
69
+
70
+ // Sunday first, matching Date#getDay().
71
+ const WEEKDAYS: Record<Locale, Record<WeekdayFormat, readonly string[]>> = {
72
+ ar: {
73
+ long: [
74
+ 'الأحد',
75
+ 'الاثنين',
76
+ 'الثلاثاء',
77
+ 'الأربعاء',
78
+ 'الخميس',
79
+ 'الجمعة',
80
+ 'السبت',
81
+ ],
82
+ short: ['أحد', 'اثنين', 'ثلاثاء', 'أربعاء', 'خميس', 'جمعة', 'سبت'],
83
+ // The same letters as ICU's narrow format.
84
+ narrow: ['ح', 'ن', 'ث', 'ر', 'خ', 'ج', 'س'],
85
+ },
86
+ en: {
87
+ long: [
88
+ 'Sunday',
89
+ 'Monday',
90
+ 'Tuesday',
91
+ 'Wednesday',
92
+ 'Thursday',
93
+ 'Friday',
94
+ 'Saturday',
95
+ ],
96
+ short: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
97
+ narrow: ['S', 'M', 'T', 'W', 'T', 'F', 'S'],
98
+ },
99
+ };
100
+
101
+ /**
102
+ * The 12 month names of a calendar, January / Muharram first. Returns a
103
+ * new array on every call.
104
+ */
105
+ export function getMonthNames(
106
+ calendar: CalendarType,
107
+ locale: Locale
108
+ ): string[] {
109
+ const names = calendar === 'hijri' ? HIJRI_MONTHS : GREGORIAN_MONTHS;
110
+ return [...names[locale]];
111
+ }
112
+
113
+ /**
114
+ * The 7 weekday names, Sunday first (index matches `Date#getDay()`).
115
+ * Returns a new array on every call.
116
+ */
117
+ export function getWeekdayNames(
118
+ locale: Locale,
119
+ format: WeekdayFormat = 'long'
120
+ ): string[] {
121
+ return [...WEEKDAYS[locale][format]];
122
+ }
123
+
124
+ const ARABIC_INDIC_ZERO = 0x0660; // ٠
125
+
126
+ /** Replaces the Latin digits 0-9 with Arabic-Indic digits (٠-٩). */
127
+ export function toArabicDigits(input: string | number): string {
128
+ return String(input).replace(/[0-9]/g, (digit) =>
129
+ String.fromCharCode(ARABIC_INDIC_ZERO + Number(digit))
130
+ );
131
+ }
132
+
133
+ /** The default digits for a locale: Arabic-Indic for 'ar', Latin otherwise. */
134
+ export function defaultDigits(locale: Locale): Digits {
135
+ return locale === 'ar' ? 'arabic' : 'latin';
136
+ }
137
+
138
+ /** Formats a number with the requested digits. */
139
+ export function formatNumber(value: number, digits: Digits): string {
140
+ return digits === 'arabic' ? toArabicDigits(value) : String(value);
141
+ }
@@ -0,0 +1,6 @@
1
+ /** A date in the Umm al-Qura Hijri calendar. `month` is 1-12. */
2
+ export type HijriDate = {
3
+ year: number;
4
+ month: number;
5
+ day: number;
6
+ };
@@ -0,0 +1,34 @@
1
+ // GENERATED FILE - DO NOT EDIT BY HAND.
2
+ // Generated by scripts/generate-umalqura-table.ts from Node's built-in ICU
3
+ // (v22.22.0, ICU 77.1). Run `yarn generate:umalqura` to rebuild.
4
+
5
+ /** First Hijri year in the table. */
6
+ export const UMALQURA_MIN_YEAR = 1356;
7
+
8
+ /** Last Hijri year in the table. */
9
+ export const UMALQURA_MAX_YEAR = 1500;
10
+
11
+ /**
12
+ * Days since 1970-01-01 (in the proleptic Gregorian calendar) of
13
+ * 1 Muharram 1356.
14
+ */
15
+ export const UMALQURA_START_EPOCH_DAY = -11981;
16
+
17
+ /**
18
+ * One entry per Hijri year from 1356 to 1500. Bit (month - 1) is
19
+ * set when that month has 30 days and clear when it has 29.
20
+ */
21
+ export const UMALQURA_MONTH_LENGTH_MASKS: readonly number[] = [
22
+ 3754, 3732, 3370, 3158, 1198, 2669, 1386, 3413, 3402, 2707, 1323, 2651, 1338,
23
+ 1717, 3753, 3410, 3369, 2645, 1197, 1389, 2794, 1764, 3793, 3490, 2730, 2394,
24
+ 730, 1465, 2994, 1892, 1737, 1365, 683, 1243, 2746, 1460, 3497, 3410, 2725,
25
+ 2349, 621, 2285, 730, 2773, 2725, 2635, 1175, 2359, 694, 2421, 3433, 3410,
26
+ 3221, 2347, 603, 1243, 2517, 1490, 3493, 3402, 2709, 1357, 2733, 938, 3026,
27
+ 3012, 2953, 2709, 1325, 1453, 2922, 1748, 3529, 3474, 2726, 2390, 686, 1389,
28
+ 874, 2901, 2730, 2381, 1181, 2397, 698, 1461, 1450, 3413, 2714, 2350, 622,
29
+ 1373, 2778, 1748, 1701, 2855, 2637, 1197, 1389, 2906, 1876, 3913, 3730, 3366,
30
+ 2646, 854, 1717, 2986, 2962, 2853, 1675, 2715, 1370, 2778, 1460, 3497, 2898,
31
+ 2714, 1334, 630, 1397, 2802, 1748, 1705, 1365, 685, 1213, 2490, 1396, 2921,
32
+ 2898, 2709, 1325, 2653, 1242, 2777, 1714, 3733, 3626, 3222, 2350, 2733, 1386,
33
+ 3429, 3402,
34
+ ];
package/src/index.tsx ADDED
@@ -0,0 +1,2 @@
1
+ export * from './core';
2
+ export * from './calendar';