react-native-hilal 0.1.0 → 0.3.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 (62) hide show
  1. package/README.md +165 -49
  2. package/lib/module/calendar/Appear.js +75 -0
  3. package/lib/module/calendar/Appear.js.map +1 -0
  4. package/lib/module/calendar/DayCell.js +32 -5
  5. package/lib/module/calendar/DayCell.js.map +1 -1
  6. package/lib/module/calendar/HilalCalendar.js +434 -76
  7. package/lib/module/calendar/HilalCalendar.js.map +1 -1
  8. package/lib/module/calendar/PickerGrid.js +66 -0
  9. package/lib/module/calendar/PickerGrid.js.map +1 -0
  10. package/lib/module/calendar/month.js +135 -22
  11. package/lib/module/calendar/month.js.map +1 -1
  12. package/lib/module/calendar/range.js +103 -0
  13. package/lib/module/calendar/range.js.map +1 -0
  14. package/lib/module/calendar/styles.js +233 -7
  15. package/lib/module/calendar/styles.js.map +1 -1
  16. package/lib/module/calendar/theme.js +3 -3
  17. package/lib/module/calendar/theme.js.map +1 -1
  18. package/lib/module/core/convert.js +52 -21
  19. package/lib/module/core/convert.js.map +1 -1
  20. package/lib/module/core/index.js.map +1 -1
  21. package/lib/module/core/locale.js +5 -2
  22. package/lib/module/core/locale.js.map +1 -1
  23. package/lib/typescript/src/calendar/Appear.d.ts +22 -0
  24. package/lib/typescript/src/calendar/Appear.d.ts.map +1 -0
  25. package/lib/typescript/src/calendar/DayCell.d.ts +8 -1
  26. package/lib/typescript/src/calendar/DayCell.d.ts.map +1 -1
  27. package/lib/typescript/src/calendar/HilalCalendar.d.ts +1 -1
  28. package/lib/typescript/src/calendar/HilalCalendar.d.ts.map +1 -1
  29. package/lib/typescript/src/calendar/PickerGrid.d.ts +28 -0
  30. package/lib/typescript/src/calendar/PickerGrid.d.ts.map +1 -0
  31. package/lib/typescript/src/calendar/index.d.ts +1 -1
  32. package/lib/typescript/src/calendar/index.d.ts.map +1 -1
  33. package/lib/typescript/src/calendar/month.d.ts +30 -7
  34. package/lib/typescript/src/calendar/month.d.ts.map +1 -1
  35. package/lib/typescript/src/calendar/range.d.ts +39 -0
  36. package/lib/typescript/src/calendar/range.d.ts.map +1 -0
  37. package/lib/typescript/src/calendar/styles.d.ts +207 -113
  38. package/lib/typescript/src/calendar/styles.d.ts.map +1 -1
  39. package/lib/typescript/src/calendar/theme.d.ts +21 -1
  40. package/lib/typescript/src/calendar/theme.d.ts.map +1 -1
  41. package/lib/typescript/src/calendar/types.d.ts +153 -2
  42. package/lib/typescript/src/calendar/types.d.ts.map +1 -1
  43. package/lib/typescript/src/core/convert.d.ts +38 -15
  44. package/lib/typescript/src/core/convert.d.ts.map +1 -1
  45. package/lib/typescript/src/core/index.d.ts +1 -1
  46. package/lib/typescript/src/core/index.d.ts.map +1 -1
  47. package/lib/typescript/src/core/locale.d.ts +1 -1
  48. package/lib/typescript/src/core/locale.d.ts.map +1 -1
  49. package/package.json +1 -1
  50. package/src/calendar/Appear.tsx +94 -0
  51. package/src/calendar/DayCell.tsx +41 -1
  52. package/src/calendar/HilalCalendar.tsx +541 -85
  53. package/src/calendar/PickerGrid.tsx +99 -0
  54. package/src/calendar/index.ts +8 -1
  55. package/src/calendar/month.ts +174 -23
  56. package/src/calendar/range.ts +114 -0
  57. package/src/calendar/styles.ts +249 -6
  58. package/src/calendar/theme.ts +23 -3
  59. package/src/calendar/types.ts +156 -2
  60. package/src/core/convert.ts +87 -23
  61. package/src/core/index.ts +1 -0
  62. package/src/core/locale.ts +4 -1
@@ -29,10 +29,50 @@ const MONTH_STARTS: readonly number[] = (() => {
29
29
  return starts;
30
30
  })();
31
31
 
32
- /** First and last epoch day of the supported range (both inclusive). */
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
+ */
33
37
  export const FIRST_DAY = MONTH_STARTS[0]!;
34
38
  export const LAST_DAY = MONTH_STARTS[MONTH_STARTS.length - 1]! - 1;
35
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
+
36
76
  function monthIndex(year: number, month: number) {
37
77
  return (year - UMALQURA_MIN_YEAR) * 12 + (month - 1);
38
78
  }
@@ -42,12 +82,13 @@ function formatIso({ year, month, day }: HijriDate) {
42
82
  return `${year}-${pad(month)}-${pad(day)}`;
43
83
  }
44
84
 
45
- function supportedRangeText() {
85
+ function supportedRangeText(adjustment: number) {
46
86
  const { min, max } = getSupportedRange();
87
+ const days = supportedEpochDays(adjustment);
47
88
  return (
48
89
  `${formatIso(min)} to ${formatIso(max)} AH ` +
49
- `(${formatIso(civilFromDays(FIRST_DAY))} to ` +
50
- `${formatIso(civilFromDays(LAST_DAY))})`
90
+ `(${formatIso(civilFromDays(days.min))} to ` +
91
+ `${formatIso(civilFromDays(days.max))})`
51
92
  );
52
93
  }
53
94
 
@@ -109,20 +150,25 @@ export function isValidHijri(hijri: HijriDate): boolean {
109
150
  }
110
151
 
111
152
  /**
112
- * Hijri date of an epoch day. Internal: the public entry point is
113
- * `toHijri`.
153
+ * Hijri date of an epoch day, shifted by `adjustment` days. Internal: the
154
+ * public entry point is `toHijri`.
114
155
  *
115
156
  * @throws {RangeError} if the day is outside the supported range.
116
157
  */
117
- export function hijriOfEpochDay(epochDay: number, caller = 'toHijri') {
158
+ export function hijriOfEpochDay(
159
+ epochDay: number,
160
+ caller = 'toHijri',
161
+ adjustment = 0
162
+ ): HijriDate {
163
+ const tableDay = epochDay + adjustment;
118
164
  if (
119
165
  !Number.isInteger(epochDay) ||
120
- epochDay < FIRST_DAY ||
121
- epochDay > LAST_DAY
166
+ tableDay < FIRST_DAY ||
167
+ tableDay > LAST_DAY
122
168
  ) {
123
169
  throw new RangeError(
124
170
  `${caller}: ${formatIso(civilFromDays(epochDay))} is outside the ` +
125
- `supported range ${supportedRangeText()}`
171
+ `supported range ${supportedRangeText(adjustment)}`
126
172
  );
127
173
  }
128
174
 
@@ -131,7 +177,7 @@ export function hijriOfEpochDay(epochDay: number, caller = 'toHijri') {
131
177
  let high = MONTH_STARTS.length - 2;
132
178
  while (low < high) {
133
179
  const mid = (low + high + 1) >> 1; // eslint-disable-line no-bitwise
134
- if (MONTH_STARTS[mid]! <= epochDay) {
180
+ if (MONTH_STARTS[mid]! <= tableDay) {
135
181
  low = mid;
136
182
  } else {
137
183
  high = mid - 1;
@@ -141,18 +187,22 @@ export function hijriOfEpochDay(epochDay: number, caller = 'toHijri') {
141
187
  return {
142
188
  year: UMALQURA_MIN_YEAR + Math.floor(low / 12),
143
189
  month: (low % 12) + 1,
144
- day: epochDay - MONTH_STARTS[low]! + 1,
190
+ day: tableDay - MONTH_STARTS[low]! + 1,
145
191
  };
146
192
  }
147
193
 
148
194
  /**
149
- * Epoch day of a Hijri date. Internal: the public entry point is
150
- * `toGregorian`.
195
+ * Epoch day of a Hijri date, shifted by `adjustment` days. Internal: the
196
+ * public entry point is `toGregorian`.
151
197
  *
152
198
  * @throws {RangeError} if `hijri` is not a valid date in the supported
153
199
  * range.
154
200
  */
155
- export function epochDayOfHijri(hijri: HijriDate, caller = 'toGregorian') {
201
+ export function epochDayOfHijri(
202
+ hijri: HijriDate,
203
+ caller = 'toGregorian',
204
+ adjustment = 0
205
+ ) {
156
206
  if (!isValidHijri(hijri)) {
157
207
  const text =
158
208
  typeof hijri === 'object' && hijri !== null
@@ -160,10 +210,15 @@ export function epochDayOfHijri(hijri: HijriDate, caller = 'toGregorian') {
160
210
  : String(hijri);
161
211
  throw new RangeError(
162
212
  `${caller}: ${text} is not a valid Hijri date in the supported ` +
163
- `range ${supportedRangeText()}`
213
+ `range ${supportedRangeText(adjustment)}`
164
214
  );
165
215
  }
166
- return MONTH_STARTS[monthIndex(hijri.year, hijri.month)]! + hijri.day - 1;
216
+ return (
217
+ MONTH_STARTS[monthIndex(hijri.year, hijri.month)]! +
218
+ hijri.day -
219
+ 1 -
220
+ adjustment
221
+ );
167
222
  }
168
223
 
169
224
  /**
@@ -171,13 +226,18 @@ export function epochDayOfHijri(hijri: HijriDate, caller = 'toGregorian') {
171
226
  * day and the time zone offset are ignored: 23:59 and 00:00 on the same
172
227
  * local day give the same result.
173
228
  *
174
- * @throws {RangeError} if `date` is invalid or outside the supported range.
229
+ * @throws {RangeError} if `date` is invalid or outside the supported
230
+ * range, or the adjustment is not an integer from -2 to 2.
175
231
  */
176
- export function toHijri(date: Date): HijriDate {
232
+ export function toHijri(
233
+ date: Date,
234
+ options: HijriConversionOptions = {}
235
+ ): HijriDate {
236
+ const adjustment = checkAdjustment(options.adjustment ?? 0, 'toHijri');
177
237
  if (!(date instanceof Date) || Number.isNaN(date.getTime())) {
178
238
  throw new RangeError('toHijri: expected a valid Date');
179
239
  }
180
- return hijriOfEpochDay(epochDayOfLocalDate(date));
240
+ return hijriOfEpochDay(epochDayOfLocalDate(date), 'toHijri', adjustment);
181
241
  }
182
242
 
183
243
  /**
@@ -185,8 +245,12 @@ export function toHijri(date: Date): HijriDate {
185
245
  * Gregorian day.
186
246
  *
187
247
  * @throws {RangeError} if `hijri` is not a valid date in the supported
188
- * range.
248
+ * range, or the adjustment is not an integer from -2 to 2.
189
249
  */
190
- export function toGregorian(hijri: HijriDate): Date {
191
- return localDateOfEpochDay(epochDayOfHijri(hijri));
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));
192
256
  }
package/src/core/index.ts CHANGED
@@ -4,6 +4,7 @@ export {
4
4
  isValidHijri,
5
5
  toGregorian,
6
6
  toHijri,
7
+ type HijriConversionOptions,
7
8
  } from './convert';
8
9
  export { formatHijri, type FormatHijriOptions } from './format';
9
10
  export {
@@ -1,7 +1,7 @@
1
1
  export type Locale = 'ar' | 'en';
2
2
  export type Digits = 'arabic' | 'latin';
3
3
  export type CalendarType = 'hijri' | 'gregorian';
4
- export type WeekdayFormat = 'short' | 'long';
4
+ export type WeekdayFormat = 'short' | 'long' | 'narrow';
5
5
 
6
6
  const HIJRI_MONTHS: Record<Locale, readonly string[]> = {
7
7
  ar: [
@@ -80,6 +80,8 @@ const WEEKDAYS: Record<Locale, Record<WeekdayFormat, readonly string[]>> = {
80
80
  'السبت',
81
81
  ],
82
82
  short: ['أحد', 'اثنين', 'ثلاثاء', 'أربعاء', 'خميس', 'جمعة', 'سبت'],
83
+ // The same letters as ICU's narrow format.
84
+ narrow: ['ح', 'ن', 'ث', 'ر', 'خ', 'ج', 'س'],
83
85
  },
84
86
  en: {
85
87
  long: [
@@ -92,6 +94,7 @@ const WEEKDAYS: Record<Locale, Record<WeekdayFormat, readonly string[]>> = {
92
94
  'Saturday',
93
95
  ],
94
96
  short: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
97
+ narrow: ['S', 'M', 'T', 'W', 'T', 'F', 'S'],
95
98
  },
96
99
  };
97
100