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.
- package/README.md +165 -49
- package/lib/module/calendar/Appear.js +75 -0
- package/lib/module/calendar/Appear.js.map +1 -0
- package/lib/module/calendar/DayCell.js +32 -5
- package/lib/module/calendar/DayCell.js.map +1 -1
- package/lib/module/calendar/HilalCalendar.js +434 -76
- package/lib/module/calendar/HilalCalendar.js.map +1 -1
- package/lib/module/calendar/PickerGrid.js +66 -0
- package/lib/module/calendar/PickerGrid.js.map +1 -0
- package/lib/module/calendar/month.js +135 -22
- package/lib/module/calendar/month.js.map +1 -1
- package/lib/module/calendar/range.js +103 -0
- package/lib/module/calendar/range.js.map +1 -0
- package/lib/module/calendar/styles.js +233 -7
- package/lib/module/calendar/styles.js.map +1 -1
- package/lib/module/calendar/theme.js +3 -3
- package/lib/module/calendar/theme.js.map +1 -1
- package/lib/module/core/convert.js +52 -21
- package/lib/module/core/convert.js.map +1 -1
- package/lib/module/core/index.js.map +1 -1
- package/lib/module/core/locale.js +5 -2
- package/lib/module/core/locale.js.map +1 -1
- package/lib/typescript/src/calendar/Appear.d.ts +22 -0
- package/lib/typescript/src/calendar/Appear.d.ts.map +1 -0
- package/lib/typescript/src/calendar/DayCell.d.ts +8 -1
- package/lib/typescript/src/calendar/DayCell.d.ts.map +1 -1
- package/lib/typescript/src/calendar/HilalCalendar.d.ts +1 -1
- package/lib/typescript/src/calendar/HilalCalendar.d.ts.map +1 -1
- package/lib/typescript/src/calendar/PickerGrid.d.ts +28 -0
- package/lib/typescript/src/calendar/PickerGrid.d.ts.map +1 -0
- package/lib/typescript/src/calendar/index.d.ts +1 -1
- package/lib/typescript/src/calendar/index.d.ts.map +1 -1
- package/lib/typescript/src/calendar/month.d.ts +30 -7
- package/lib/typescript/src/calendar/month.d.ts.map +1 -1
- package/lib/typescript/src/calendar/range.d.ts +39 -0
- package/lib/typescript/src/calendar/range.d.ts.map +1 -0
- package/lib/typescript/src/calendar/styles.d.ts +207 -113
- package/lib/typescript/src/calendar/styles.d.ts.map +1 -1
- package/lib/typescript/src/calendar/theme.d.ts +21 -1
- package/lib/typescript/src/calendar/theme.d.ts.map +1 -1
- package/lib/typescript/src/calendar/types.d.ts +153 -2
- package/lib/typescript/src/calendar/types.d.ts.map +1 -1
- package/lib/typescript/src/core/convert.d.ts +38 -15
- package/lib/typescript/src/core/convert.d.ts.map +1 -1
- package/lib/typescript/src/core/index.d.ts +1 -1
- package/lib/typescript/src/core/index.d.ts.map +1 -1
- package/lib/typescript/src/core/locale.d.ts +1 -1
- package/lib/typescript/src/core/locale.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/calendar/Appear.tsx +94 -0
- package/src/calendar/DayCell.tsx +41 -1
- package/src/calendar/HilalCalendar.tsx +541 -85
- package/src/calendar/PickerGrid.tsx +99 -0
- package/src/calendar/index.ts +8 -1
- package/src/calendar/month.ts +174 -23
- package/src/calendar/range.ts +114 -0
- package/src/calendar/styles.ts +249 -6
- package/src/calendar/theme.ts +23 -3
- package/src/calendar/types.ts +156 -2
- package/src/core/convert.ts +87 -23
- package/src/core/index.ts +1 -0
- package/src/core/locale.ts +4 -1
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { Pressable, Text, View } from 'react-native';
|
|
2
|
+
|
|
3
|
+
import type { CalendarStyles } from './styles';
|
|
4
|
+
|
|
5
|
+
export type PickerGridItem = {
|
|
6
|
+
key: number;
|
|
7
|
+
label: string;
|
|
8
|
+
accessibilityLabel: string;
|
|
9
|
+
selected: boolean;
|
|
10
|
+
/** The current month or year (today's), drawn with a ring. */
|
|
11
|
+
current: boolean;
|
|
12
|
+
disabled: boolean;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
type PickerGridProps = {
|
|
16
|
+
items: PickerGridItem[];
|
|
17
|
+
styles: CalendarStyles;
|
|
18
|
+
/** 'row' or 'row-reverse', the same as the day grid. */
|
|
19
|
+
row: 'row' | 'row-reverse';
|
|
20
|
+
/** Grow the rows to fill the parent's height. */
|
|
21
|
+
fill: boolean;
|
|
22
|
+
/** maxFontSizeMultiplier of the texts. */
|
|
23
|
+
maxFontScale: number;
|
|
24
|
+
onSelect: (key: number) => void;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const COLUMNS = 3;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Lets names wrap only at spaces: "Rabi' al-Awwal" breaks into "Rabi'" and
|
|
31
|
+
* "al‑Awwal", never after the hyphen. U+2011 looks like a hyphen.
|
|
32
|
+
*/
|
|
33
|
+
function noBreakAtHyphens(label: string) {
|
|
34
|
+
return label.replace(/-/g, '\u2011');
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The month or year picker: items in rows of three, each a button. Only
|
|
39
|
+
* rendered while the picker is open.
|
|
40
|
+
*/
|
|
41
|
+
export function PickerGrid({
|
|
42
|
+
items,
|
|
43
|
+
styles,
|
|
44
|
+
row,
|
|
45
|
+
fill,
|
|
46
|
+
maxFontScale,
|
|
47
|
+
onSelect,
|
|
48
|
+
}: PickerGridProps) {
|
|
49
|
+
const rows: PickerGridItem[][] = [];
|
|
50
|
+
for (let i = 0; i < items.length; i += COLUMNS) {
|
|
51
|
+
rows.push(items.slice(i, i + COLUMNS));
|
|
52
|
+
}
|
|
53
|
+
return (
|
|
54
|
+
<View style={[styles.picker, fill && styles.grow]}>
|
|
55
|
+
{rows.map((rowItems, r) => (
|
|
56
|
+
<View key={r} style={[{ flexDirection: row }, fill && styles.grow]}>
|
|
57
|
+
{rowItems.map((item) => (
|
|
58
|
+
<View key={item.key} style={styles.pickerSlot}>
|
|
59
|
+
<Pressable
|
|
60
|
+
accessibilityRole="button"
|
|
61
|
+
accessibilityLabel={item.accessibilityLabel}
|
|
62
|
+
accessibilityState={{
|
|
63
|
+
selected: item.selected,
|
|
64
|
+
disabled: item.disabled,
|
|
65
|
+
}}
|
|
66
|
+
disabled={item.disabled}
|
|
67
|
+
onPress={() => onSelect(item.key)}
|
|
68
|
+
style={({ pressed }) => [
|
|
69
|
+
styles.pickerItem,
|
|
70
|
+
fill && styles.grow,
|
|
71
|
+
item.current && styles.today,
|
|
72
|
+
item.selected && styles.selected,
|
|
73
|
+
pressed && !item.selected && styles.pressed,
|
|
74
|
+
]}
|
|
75
|
+
>
|
|
76
|
+
{!item.selected && <View style={styles.pickerItemTint} />}
|
|
77
|
+
{/* Long names (جمادى الآخرة, Jumada al-Akhirah) wrap to a
|
|
78
|
+
second line on narrow phones instead of being cut. */}
|
|
79
|
+
<Text
|
|
80
|
+
numberOfLines={2}
|
|
81
|
+
adjustsFontSizeToFit
|
|
82
|
+
minimumFontScale={0.8}
|
|
83
|
+
maxFontSizeMultiplier={maxFontScale}
|
|
84
|
+
style={[
|
|
85
|
+
styles.pickerText,
|
|
86
|
+
item.disabled && styles.disabledText,
|
|
87
|
+
item.selected && styles.selectedText,
|
|
88
|
+
]}
|
|
89
|
+
>
|
|
90
|
+
{noBreakAtHyphens(item.label)}
|
|
91
|
+
</Text>
|
|
92
|
+
</Pressable>
|
|
93
|
+
</View>
|
|
94
|
+
))}
|
|
95
|
+
</View>
|
|
96
|
+
))}
|
|
97
|
+
</View>
|
|
98
|
+
);
|
|
99
|
+
}
|
package/src/calendar/index.ts
CHANGED
|
@@ -5,4 +5,11 @@ export {
|
|
|
5
5
|
type HilalTheme,
|
|
6
6
|
type ThemeProp,
|
|
7
7
|
} from './theme';
|
|
8
|
-
export type {
|
|
8
|
+
export type {
|
|
9
|
+
DateRange,
|
|
10
|
+
HilalCalendarProps,
|
|
11
|
+
HilalLabels,
|
|
12
|
+
HilalStyles,
|
|
13
|
+
SelectedDate,
|
|
14
|
+
SelectedRange,
|
|
15
|
+
} from './types';
|
package/src/calendar/month.ts
CHANGED
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
getHijriMonthLength,
|
|
10
10
|
hijriOfEpochDay,
|
|
11
11
|
LAST_DAY,
|
|
12
|
+
supportedEpochDays,
|
|
12
13
|
} from '../core/convert';
|
|
13
14
|
import { civilFromDays, daysFromCivil, weekdayOfEpochDay } from '../core/days';
|
|
14
15
|
import {
|
|
@@ -47,14 +48,23 @@ export type DayRange = { min: number; max: number };
|
|
|
47
48
|
/** The days the calendar can ever show: the supported Hijri range. */
|
|
48
49
|
export const SUPPORTED_DAYS: DayRange = { min: FIRST_DAY, max: LAST_DAY };
|
|
49
50
|
|
|
51
|
+
/*
|
|
52
|
+
* `adjustment` below is the Hijri adjustment in days (see
|
|
53
|
+
* HijriConversionOptions). It moves which Gregorian days carry which
|
|
54
|
+
* Hijri date, and so the supported days; Gregorian dates never change.
|
|
55
|
+
*/
|
|
56
|
+
|
|
50
57
|
export function otherCalendar(calendar: CalendarType): CalendarType {
|
|
51
58
|
return calendar === 'hijri' ? 'gregorian' : 'hijri';
|
|
52
59
|
}
|
|
53
60
|
|
|
54
61
|
/** Epoch day of the first day of a month. */
|
|
55
|
-
export function monthStart(
|
|
62
|
+
export function monthStart(
|
|
63
|
+
{ calendar, year, month }: CalendarMonth,
|
|
64
|
+
adjustment = 0
|
|
65
|
+
) {
|
|
56
66
|
return calendar === 'hijri'
|
|
57
|
-
? epochDayOfHijri({ year, month, day: 1 }, 'monthStart')
|
|
67
|
+
? epochDayOfHijri({ year, month, day: 1 }, 'monthStart', adjustment)
|
|
58
68
|
: daysFromCivil(year, month, 1);
|
|
59
69
|
}
|
|
60
70
|
|
|
@@ -68,9 +78,13 @@ export function monthLength(m: CalendarMonth) {
|
|
|
68
78
|
}
|
|
69
79
|
|
|
70
80
|
/** Year, month and day of an epoch day in either calendar. */
|
|
71
|
-
export function dateInCalendar(
|
|
81
|
+
export function dateInCalendar(
|
|
82
|
+
epochDay: number,
|
|
83
|
+
calendar: CalendarType,
|
|
84
|
+
adjustment = 0
|
|
85
|
+
) {
|
|
72
86
|
return calendar === 'hijri'
|
|
73
|
-
? hijriOfEpochDay(epochDay)
|
|
87
|
+
? hijriOfEpochDay(epochDay, 'dateInCalendar', adjustment)
|
|
74
88
|
: civilFromDays(epochDay);
|
|
75
89
|
}
|
|
76
90
|
|
|
@@ -80,11 +94,13 @@ export function dateInCalendar(epochDay: number, calendar: CalendarType) {
|
|
|
80
94
|
*/
|
|
81
95
|
export function monthContaining(
|
|
82
96
|
epochDay: number,
|
|
83
|
-
calendar: CalendarType
|
|
97
|
+
calendar: CalendarType,
|
|
98
|
+
adjustment = 0
|
|
84
99
|
): CalendarMonth {
|
|
85
100
|
const { year, month } = dateInCalendar(
|
|
86
|
-
clampDay(epochDay,
|
|
87
|
-
calendar
|
|
101
|
+
clampDay(epochDay, supportedEpochDays(adjustment)),
|
|
102
|
+
calendar,
|
|
103
|
+
adjustment
|
|
88
104
|
);
|
|
89
105
|
return { calendar, year, month };
|
|
90
106
|
}
|
|
@@ -108,16 +124,22 @@ export function clampDay(epochDay: number, range: DayRange) {
|
|
|
108
124
|
*/
|
|
109
125
|
export function selectableRange(
|
|
110
126
|
minDay: number | undefined,
|
|
111
|
-
maxDay: number | undefined
|
|
127
|
+
maxDay: number | undefined,
|
|
128
|
+
adjustment = 0
|
|
112
129
|
): DayRange {
|
|
130
|
+
const supported = supportedEpochDays(adjustment);
|
|
113
131
|
return {
|
|
114
|
-
min: Math.max(minDay ??
|
|
115
|
-
max: Math.min(maxDay ??
|
|
132
|
+
min: Math.max(minDay ?? supported.min, supported.min),
|
|
133
|
+
max: Math.min(maxDay ?? supported.max, supported.max),
|
|
116
134
|
};
|
|
117
135
|
}
|
|
118
136
|
|
|
119
137
|
/** Whether any day of month `m` falls inside `range`. */
|
|
120
|
-
export function monthOverlaps(
|
|
138
|
+
export function monthOverlaps(
|
|
139
|
+
m: CalendarMonth,
|
|
140
|
+
range: DayRange,
|
|
141
|
+
adjustment = 0
|
|
142
|
+
) {
|
|
121
143
|
if (m.calendar === 'hijri') {
|
|
122
144
|
// Hijri months outside the table have no start day at all.
|
|
123
145
|
const { min, max } = monthRangeOfSupportedHijri();
|
|
@@ -126,7 +148,7 @@ export function monthOverlaps(m: CalendarMonth, range: DayRange) {
|
|
|
126
148
|
return false;
|
|
127
149
|
}
|
|
128
150
|
}
|
|
129
|
-
const start = monthStart(m);
|
|
151
|
+
const start = monthStart(m, adjustment);
|
|
130
152
|
const end = start + monthLength(m) - 1;
|
|
131
153
|
return start <= range.max && end >= range.min;
|
|
132
154
|
}
|
|
@@ -146,9 +168,11 @@ function monthRangeOfSupportedHijri() {
|
|
|
146
168
|
*/
|
|
147
169
|
export function buildMonthGrid(
|
|
148
170
|
m: CalendarMonth,
|
|
149
|
-
firstDayOfWeek: number
|
|
171
|
+
firstDayOfWeek: number,
|
|
172
|
+
adjustment = 0
|
|
150
173
|
): MonthGrid {
|
|
151
|
-
const start = monthStart(m);
|
|
174
|
+
const start = monthStart(m, adjustment);
|
|
175
|
+
const supported = supportedEpochDays(adjustment);
|
|
152
176
|
const length = monthLength(m);
|
|
153
177
|
const secondary = otherCalendar(m.calendar);
|
|
154
178
|
const leading = (weekdayOfEpochDay(start) - firstDayOfWeek + 7) % 7;
|
|
@@ -156,12 +180,13 @@ export function buildMonthGrid(
|
|
|
156
180
|
const slots: Array<DayCell | null> = Array(leading).fill(null);
|
|
157
181
|
for (let day = 1; day <= length; day++) {
|
|
158
182
|
const epochDay = start + day - 1;
|
|
159
|
-
const inRange =
|
|
160
|
-
epochDay >= SUPPORTED_DAYS.min && epochDay <= SUPPORTED_DAYS.max;
|
|
183
|
+
const inRange = epochDay >= supported.min && epochDay <= supported.max;
|
|
161
184
|
slots.push({
|
|
162
185
|
epochDay,
|
|
163
186
|
day,
|
|
164
|
-
secondaryDay: inRange
|
|
187
|
+
secondaryDay: inRange
|
|
188
|
+
? dateInCalendar(epochDay, secondary, adjustment).day
|
|
189
|
+
: null,
|
|
165
190
|
});
|
|
166
191
|
}
|
|
167
192
|
while (slots.length % 7 !== 0) {
|
|
@@ -190,14 +215,16 @@ export function monthTitle(m: CalendarMonth, locale: Locale, digits: Digits) {
|
|
|
190
215
|
export function secondaryTitle(
|
|
191
216
|
m: CalendarMonth,
|
|
192
217
|
locale: Locale,
|
|
193
|
-
digits: Digits
|
|
218
|
+
digits: Digits,
|
|
219
|
+
adjustment = 0
|
|
194
220
|
) {
|
|
195
|
-
const start = monthStart(m);
|
|
196
|
-
const
|
|
197
|
-
const
|
|
221
|
+
const start = monthStart(m, adjustment);
|
|
222
|
+
const supported = supportedEpochDays(adjustment);
|
|
223
|
+
const first = clampDay(start, supported);
|
|
224
|
+
const last = clampDay(start + monthLength(m) - 1, supported);
|
|
198
225
|
const calendar = otherCalendar(m.calendar);
|
|
199
|
-
const from = dateInCalendar(first, calendar);
|
|
200
|
-
const to = dateInCalendar(last, calendar);
|
|
226
|
+
const from = dateInCalendar(first, calendar, adjustment);
|
|
227
|
+
const to = dateInCalendar(last, calendar, adjustment);
|
|
201
228
|
const names = getMonthNames(calendar, locale);
|
|
202
229
|
const fromName = names[from.month - 1];
|
|
203
230
|
const toName = names[to.month - 1];
|
|
@@ -211,3 +238,127 @@ export function secondaryTitle(
|
|
|
211
238
|
}
|
|
212
239
|
return `${fromName} ${year(from.year)} – ${toName} ${year(to.year)}`;
|
|
213
240
|
}
|
|
241
|
+
|
|
242
|
+
/*
|
|
243
|
+
* Month and year picker. Opened from the header: a grid of the 12 months
|
|
244
|
+
* of a year, and a grid of years, YEARS_PER_PAGE at a time.
|
|
245
|
+
*/
|
|
246
|
+
|
|
247
|
+
/** Years shown on one page of the year picker. */
|
|
248
|
+
export const YEARS_PER_PAGE = 12;
|
|
249
|
+
|
|
250
|
+
/** A month (1-12) or a year in the picker, and whether it can be picked. */
|
|
251
|
+
export type PickerItem = { value: number; disabled: boolean };
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* First and last epoch day of years `fromYear` to `toYear` of a calendar,
|
|
255
|
+
* limited to the supported range, or null when none of it is supported.
|
|
256
|
+
*/
|
|
257
|
+
function yearsSpan(
|
|
258
|
+
calendar: CalendarType,
|
|
259
|
+
fromYear: number,
|
|
260
|
+
toYear: number,
|
|
261
|
+
adjustment: number
|
|
262
|
+
): DayRange | null {
|
|
263
|
+
const supported = supportedEpochDays(adjustment);
|
|
264
|
+
let start: number;
|
|
265
|
+
let end: number;
|
|
266
|
+
if (calendar === 'hijri') {
|
|
267
|
+
const first = Math.max(fromYear, hijriOfEpochDay(FIRST_DAY).year);
|
|
268
|
+
const last = Math.min(toYear, hijriOfEpochDay(LAST_DAY).year);
|
|
269
|
+
if (first > last) {
|
|
270
|
+
return null;
|
|
271
|
+
}
|
|
272
|
+
const lastMonth: CalendarMonth = { calendar, year: last, month: 12 };
|
|
273
|
+
start = monthStart({ calendar, year: first, month: 1 }, adjustment);
|
|
274
|
+
end = monthStart(lastMonth, adjustment) + monthLength(lastMonth) - 1;
|
|
275
|
+
} else {
|
|
276
|
+
start = daysFromCivil(fromYear, 1, 1);
|
|
277
|
+
end = daysFromCivil(toYear + 1, 1, 1) - 1;
|
|
278
|
+
}
|
|
279
|
+
const min = Math.max(start, supported.min);
|
|
280
|
+
const max = Math.min(end, supported.max);
|
|
281
|
+
return min <= max ? { min, max } : null;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/** Whether any day of `year` falls inside `range`. */
|
|
285
|
+
export function yearOverlaps(
|
|
286
|
+
calendar: CalendarType,
|
|
287
|
+
year: number,
|
|
288
|
+
range: DayRange,
|
|
289
|
+
adjustment = 0
|
|
290
|
+
) {
|
|
291
|
+
const span = yearsSpan(calendar, year, year, adjustment);
|
|
292
|
+
return span !== null && span.min <= range.max && span.max >= range.min;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/** The 12 months of `year`, disabled when none of their days is in `range`. */
|
|
296
|
+
export function monthsOfYear(
|
|
297
|
+
calendar: CalendarType,
|
|
298
|
+
year: number,
|
|
299
|
+
range: DayRange,
|
|
300
|
+
adjustment = 0
|
|
301
|
+
): PickerItem[] {
|
|
302
|
+
return Array.from({ length: 12 }, (_, i) => ({
|
|
303
|
+
value: i + 1,
|
|
304
|
+
disabled: !monthOverlaps(
|
|
305
|
+
{ calendar, year, month: i + 1 },
|
|
306
|
+
range,
|
|
307
|
+
adjustment
|
|
308
|
+
),
|
|
309
|
+
}));
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/** First year of the year-picker page that contains `year`. */
|
|
313
|
+
export function yearPageStart(year: number) {
|
|
314
|
+
return Math.floor(year / YEARS_PER_PAGE) * YEARS_PER_PAGE;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/** The years of the page starting at `pageStart`. */
|
|
318
|
+
export function yearsOnPage(
|
|
319
|
+
calendar: CalendarType,
|
|
320
|
+
pageStart: number,
|
|
321
|
+
range: DayRange,
|
|
322
|
+
adjustment = 0
|
|
323
|
+
): PickerItem[] {
|
|
324
|
+
return Array.from({ length: YEARS_PER_PAGE }, (_, i) => ({
|
|
325
|
+
value: pageStart + i,
|
|
326
|
+
disabled: !yearOverlaps(calendar, pageStart + i, range, adjustment),
|
|
327
|
+
}));
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/** Whether the page starting at `pageStart` has any year that can be picked. */
|
|
331
|
+
export function pageOverlaps(
|
|
332
|
+
calendar: CalendarType,
|
|
333
|
+
pageStart: number,
|
|
334
|
+
range: DayRange,
|
|
335
|
+
adjustment = 0
|
|
336
|
+
) {
|
|
337
|
+
return yearsOnPage(calendar, pageStart, range, adjustment).some(
|
|
338
|
+
(year) => !year.disabled
|
|
339
|
+
);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* The years of the other calendar that years `fromYear` to `toYear`
|
|
344
|
+
* overlap, for example "٢٠٢٣ – ٢٠٢٤" for 1445 AH, or a single year. Only
|
|
345
|
+
* days in the supported range count.
|
|
346
|
+
*/
|
|
347
|
+
export function yearsSecondaryTitle(
|
|
348
|
+
calendar: CalendarType,
|
|
349
|
+
fromYear: number,
|
|
350
|
+
toYear: number,
|
|
351
|
+
digits: Digits,
|
|
352
|
+
adjustment = 0
|
|
353
|
+
) {
|
|
354
|
+
const span = yearsSpan(calendar, fromYear, toYear, adjustment);
|
|
355
|
+
if (span === null) {
|
|
356
|
+
return '';
|
|
357
|
+
}
|
|
358
|
+
const other = otherCalendar(calendar);
|
|
359
|
+
const from = dateInCalendar(span.min, other, adjustment).year;
|
|
360
|
+
const to = dateInCalendar(span.max, other, adjustment).year;
|
|
361
|
+
return from === to
|
|
362
|
+
? formatNumber(from, digits)
|
|
363
|
+
: `${formatNumber(from, digits)} – ${formatNumber(to, digits)}`;
|
|
364
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Range selection: which days a range covers and how pressing a day
|
|
3
|
+
* changes it. Pure functions on epoch days, no React.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/** A picked range of days. `end` is undefined while only the start is picked. */
|
|
7
|
+
export type DayRangeSelection = { start: number; end?: number };
|
|
8
|
+
|
|
9
|
+
/** The fewest and most days a range may have, both ends included. */
|
|
10
|
+
export type RangeLimits = { min: number; max: number };
|
|
11
|
+
|
|
12
|
+
export const NO_RANGE_LIMITS: RangeLimits = { min: 1, max: Infinity };
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Checks minRangeDays and maxRangeDays: whole numbers of at least 1, the
|
|
16
|
+
* minimum no larger than the maximum. Throws a RangeError otherwise.
|
|
17
|
+
*/
|
|
18
|
+
export function checkRangeLimits(
|
|
19
|
+
min: number | undefined,
|
|
20
|
+
max: number | undefined
|
|
21
|
+
): RangeLimits {
|
|
22
|
+
const limits = { min: min ?? 1, max: max ?? Infinity };
|
|
23
|
+
for (const [name, value] of [
|
|
24
|
+
['minRangeDays', limits.min],
|
|
25
|
+
['maxRangeDays', limits.max],
|
|
26
|
+
] as const) {
|
|
27
|
+
if (!(value >= 1 && (Number.isInteger(value) || value === Infinity))) {
|
|
28
|
+
throw new RangeError(
|
|
29
|
+
`HilalCalendar: ${name} must be a whole number of at least 1, got ${value}`
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
if (limits.min > limits.max) {
|
|
34
|
+
throw new RangeError(
|
|
35
|
+
`HilalCalendar: minRangeDays (${limits.min}) is larger than maxRangeDays (${limits.max})`
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
return limits;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Whether `day` can't end the range started at `range.start` because the
|
|
43
|
+
* range would be shorter or longer than `limits`. Only days after the
|
|
44
|
+
* start of a range without an end; any other day can be pressed.
|
|
45
|
+
*/
|
|
46
|
+
export function outsideRangeLimits(
|
|
47
|
+
range: DayRangeSelection | undefined,
|
|
48
|
+
day: number,
|
|
49
|
+
limits: RangeLimits
|
|
50
|
+
) {
|
|
51
|
+
if (range === undefined || range.end !== undefined || day <= range.start) {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
const length = day - range.start + 1;
|
|
55
|
+
return length < limits.min || length > limits.max;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* The range after pressing `day`. The first press picks the start, the
|
|
60
|
+
* second the end. A press before the start, a press after a complete
|
|
61
|
+
* range, a press across a blocked day, or a press that would make the
|
|
62
|
+
* range shorter or longer than `limits` starts a new range instead.
|
|
63
|
+
*/
|
|
64
|
+
export function nextRange(
|
|
65
|
+
current: DayRangeSelection | undefined,
|
|
66
|
+
day: number,
|
|
67
|
+
isBlocked?: (day: number) => boolean,
|
|
68
|
+
limits: RangeLimits = NO_RANGE_LIMITS
|
|
69
|
+
): DayRangeSelection {
|
|
70
|
+
if (
|
|
71
|
+
current === undefined ||
|
|
72
|
+
current.end !== undefined ||
|
|
73
|
+
day < current.start
|
|
74
|
+
) {
|
|
75
|
+
return { start: day };
|
|
76
|
+
}
|
|
77
|
+
const length = day - current.start + 1;
|
|
78
|
+
if (length < limits.min || length > limits.max) {
|
|
79
|
+
return { start: day };
|
|
80
|
+
}
|
|
81
|
+
if (isBlocked) {
|
|
82
|
+
for (let between = current.start + 1; between < day; between++) {
|
|
83
|
+
if (isBlocked(between)) {
|
|
84
|
+
return { start: day };
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return { start: current.start, end: day };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Where a day sits in a range: its ends, inside it, or alone in it. */
|
|
92
|
+
export type RangePosition = 'start' | 'middle' | 'end' | 'single';
|
|
93
|
+
|
|
94
|
+
export function rangePosition(
|
|
95
|
+
range: DayRangeSelection | undefined,
|
|
96
|
+
day: number
|
|
97
|
+
): RangePosition | null {
|
|
98
|
+
if (range === undefined) {
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
const end = range.end ?? range.start;
|
|
102
|
+
if (day === range.start) {
|
|
103
|
+
return end === range.start ? 'single' : 'start';
|
|
104
|
+
}
|
|
105
|
+
if (day === end) {
|
|
106
|
+
return 'end';
|
|
107
|
+
}
|
|
108
|
+
return day > range.start && day < end ? 'middle' : null;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** Days in a complete range, both ends included. */
|
|
112
|
+
export function rangeLength(range: DayRangeSelection) {
|
|
113
|
+
return range.end === undefined ? null : range.end - range.start + 1;
|
|
114
|
+
}
|