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.
- package/LICENSE +20 -0
- package/README.md +300 -2
- package/lib/module/calendar/Appear.js +75 -0
- package/lib/module/calendar/Appear.js.map +1 -0
- package/lib/module/calendar/Chevron.js +60 -0
- package/lib/module/calendar/Chevron.js.map +1 -0
- package/lib/module/calendar/DayCell.js +51 -0
- package/lib/module/calendar/DayCell.js.map +1 -0
- package/lib/module/calendar/HilalCalendar.js +565 -0
- package/lib/module/calendar/HilalCalendar.js.map +1 -0
- package/lib/module/calendar/PickerGrid.js +66 -0
- package/lib/module/calendar/PickerGrid.js.map +1 -0
- package/lib/module/calendar/index.js +5 -0
- package/lib/module/calendar/index.js.map +1 -0
- package/lib/module/calendar/month.js +291 -0
- package/lib/module/calendar/month.js.map +1 -0
- package/lib/module/calendar/styles.js +338 -0
- package/lib/module/calendar/styles.js.map +1 -0
- package/lib/module/calendar/theme.js +49 -0
- package/lib/module/calendar/theme.js.map +1 -0
- package/lib/module/calendar/types.js +4 -0
- package/lib/module/calendar/types.js.map +1 -0
- package/lib/module/core/convert.js +191 -0
- package/lib/module/core/convert.js.map +1 -0
- package/lib/module/core/days.js +61 -0
- package/lib/module/core/days.js.map +1 -0
- package/lib/module/core/format.js +37 -0
- package/lib/module/core/format.js.map +1 -0
- package/lib/module/core/index.js +6 -0
- package/lib/module/core/index.js.map +1 -0
- package/lib/module/core/locale.js +62 -0
- package/lib/module/core/locale.js.map +1 -0
- package/lib/module/core/types.js +2 -0
- package/lib/module/core/types.js.map +1 -0
- package/lib/module/core/umalqura-data.js +24 -0
- package/lib/module/core/umalqura-data.js.map +1 -0
- package/lib/module/index.js +5 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/package.json +1 -0
- package/lib/typescript/package.json +1 -0
- 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/Chevron.d.ts +13 -0
- package/lib/typescript/src/calendar/Chevron.d.ts.map +1 -0
- package/lib/typescript/src/calendar/DayCell.d.ts +24 -0
- package/lib/typescript/src/calendar/DayCell.d.ts.map +1 -0
- package/lib/typescript/src/calendar/HilalCalendar.d.ts +7 -0
- package/lib/typescript/src/calendar/HilalCalendar.d.ts.map +1 -0
- 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 +4 -0
- package/lib/typescript/src/calendar/index.d.ts.map +1 -0
- package/lib/typescript/src/calendar/month.d.ts +86 -0
- package/lib/typescript/src/calendar/month.d.ts.map +1 -0
- package/lib/typescript/src/calendar/styles.d.ts +206 -0
- package/lib/typescript/src/calendar/styles.d.ts.map +1 -0
- package/lib/typescript/src/calendar/theme.d.ts +51 -0
- package/lib/typescript/src/calendar/theme.d.ts.map +1 -0
- package/lib/typescript/src/calendar/types.d.ts +162 -0
- package/lib/typescript/src/calendar/types.d.ts.map +1 -0
- package/lib/typescript/src/core/convert.d.ts +77 -0
- package/lib/typescript/src/core/convert.d.ts.map +1 -0
- package/lib/typescript/src/core/days.d.ts +24 -0
- package/lib/typescript/src/core/days.d.ts.map +1 -0
- package/lib/typescript/src/core/format.d.ts +23 -0
- package/lib/typescript/src/core/format.d.ts.map +1 -0
- package/lib/typescript/src/core/index.d.ts +5 -0
- package/lib/typescript/src/core/index.d.ts.map +1 -0
- package/lib/typescript/src/core/locale.d.ts +21 -0
- package/lib/typescript/src/core/locale.d.ts.map +1 -0
- package/lib/typescript/src/core/types.d.ts +7 -0
- package/lib/typescript/src/core/types.d.ts.map +1 -0
- package/lib/typescript/src/core/umalqura-data.d.ts +15 -0
- package/lib/typescript/src/core/umalqura-data.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +3 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/package.json +180 -4
- package/src/calendar/Appear.tsx +94 -0
- package/src/calendar/Chevron.tsx +55 -0
- package/src/calendar/DayCell.tsx +89 -0
- package/src/calendar/HilalCalendar.tsx +728 -0
- package/src/calendar/PickerGrid.tsx +99 -0
- package/src/calendar/index.ts +13 -0
- package/src/calendar/month.ts +364 -0
- package/src/calendar/styles.ts +357 -0
- package/src/calendar/theme.ts +89 -0
- package/src/calendar/types.ts +168 -0
- package/src/core/convert.ts +256 -0
- package/src/core/days.ts +64 -0
- package/src/core/format.ts +65 -0
- package/src/core/index.ts +19 -0
- package/src/core/locale.ts +141 -0
- package/src/core/types.ts +6 -0
- package/src/core/umalqura-data.ts +34 -0
- package/src/index.tsx +2 -0
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,364 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The calendar's model: which days a month shows, how they are laid out
|
|
3
|
+
* in weeks, how the header reads, and how far navigation may go. Pure
|
|
4
|
+
* functions on epoch days, no React.
|
|
5
|
+
*/
|
|
6
|
+
import {
|
|
7
|
+
epochDayOfHijri,
|
|
8
|
+
FIRST_DAY,
|
|
9
|
+
getHijriMonthLength,
|
|
10
|
+
hijriOfEpochDay,
|
|
11
|
+
LAST_DAY,
|
|
12
|
+
supportedEpochDays,
|
|
13
|
+
} from '../core/convert';
|
|
14
|
+
import { civilFromDays, daysFromCivil, weekdayOfEpochDay } from '../core/days';
|
|
15
|
+
import {
|
|
16
|
+
formatNumber,
|
|
17
|
+
getMonthNames,
|
|
18
|
+
type CalendarType,
|
|
19
|
+
type Digits,
|
|
20
|
+
type Locale,
|
|
21
|
+
} from '../core/locale';
|
|
22
|
+
|
|
23
|
+
/** A month of either calendar. `month` is 1-12. */
|
|
24
|
+
export type CalendarMonth = {
|
|
25
|
+
calendar: CalendarType;
|
|
26
|
+
year: number;
|
|
27
|
+
month: number;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export type DayCell = {
|
|
31
|
+
epochDay: number;
|
|
32
|
+
/** Day of the month in the primary calendar. */
|
|
33
|
+
day: number;
|
|
34
|
+
/**
|
|
35
|
+
* Day of the month in the other calendar, or null when the day is
|
|
36
|
+
* outside the supported Hijri range (possible in the first and last
|
|
37
|
+
* Gregorian months).
|
|
38
|
+
*/
|
|
39
|
+
secondaryDay: number | null;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/** A month as weeks of 7 slots; null slots pad the first and last week. */
|
|
43
|
+
export type MonthGrid = Array<Array<DayCell | null>>;
|
|
44
|
+
|
|
45
|
+
/** Inclusive range of epoch days. */
|
|
46
|
+
export type DayRange = { min: number; max: number };
|
|
47
|
+
|
|
48
|
+
/** The days the calendar can ever show: the supported Hijri range. */
|
|
49
|
+
export const SUPPORTED_DAYS: DayRange = { min: FIRST_DAY, max: LAST_DAY };
|
|
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
|
+
|
|
57
|
+
export function otherCalendar(calendar: CalendarType): CalendarType {
|
|
58
|
+
return calendar === 'hijri' ? 'gregorian' : 'hijri';
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Epoch day of the first day of a month. */
|
|
62
|
+
export function monthStart(
|
|
63
|
+
{ calendar, year, month }: CalendarMonth,
|
|
64
|
+
adjustment = 0
|
|
65
|
+
) {
|
|
66
|
+
return calendar === 'hijri'
|
|
67
|
+
? epochDayOfHijri({ year, month, day: 1 }, 'monthStart', adjustment)
|
|
68
|
+
: daysFromCivil(year, month, 1);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Number of days in a month. */
|
|
72
|
+
export function monthLength(m: CalendarMonth) {
|
|
73
|
+
if (m.calendar === 'hijri') {
|
|
74
|
+
return getHijriMonthLength(m.year, m.month);
|
|
75
|
+
}
|
|
76
|
+
const next = addMonths(m, 1);
|
|
77
|
+
return daysFromCivil(next.year, next.month, 1) - monthStart(m);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Year, month and day of an epoch day in either calendar. */
|
|
81
|
+
export function dateInCalendar(
|
|
82
|
+
epochDay: number,
|
|
83
|
+
calendar: CalendarType,
|
|
84
|
+
adjustment = 0
|
|
85
|
+
) {
|
|
86
|
+
return calendar === 'hijri'
|
|
87
|
+
? hijriOfEpochDay(epochDay, 'dateInCalendar', adjustment)
|
|
88
|
+
: civilFromDays(epochDay);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* The month of `calendar` that contains `epochDay`. Days outside the
|
|
93
|
+
* supported range are clamped to it first.
|
|
94
|
+
*/
|
|
95
|
+
export function monthContaining(
|
|
96
|
+
epochDay: number,
|
|
97
|
+
calendar: CalendarType,
|
|
98
|
+
adjustment = 0
|
|
99
|
+
): CalendarMonth {
|
|
100
|
+
const { year, month } = dateInCalendar(
|
|
101
|
+
clampDay(epochDay, supportedEpochDays(adjustment)),
|
|
102
|
+
calendar,
|
|
103
|
+
adjustment
|
|
104
|
+
);
|
|
105
|
+
return { calendar, year, month };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export function addMonths(m: CalendarMonth, delta: number): CalendarMonth {
|
|
109
|
+
const index = m.year * 12 + (m.month - 1) + delta;
|
|
110
|
+
return {
|
|
111
|
+
calendar: m.calendar,
|
|
112
|
+
year: Math.floor(index / 12),
|
|
113
|
+
month: (index % 12) + 1,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export function clampDay(epochDay: number, range: DayRange) {
|
|
118
|
+
return Math.min(Math.max(epochDay, range.min), range.max);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* The days that may be selected: `minDay`/`maxDay` (when given)
|
|
123
|
+
* intersected with the supported range.
|
|
124
|
+
*/
|
|
125
|
+
export function selectableRange(
|
|
126
|
+
minDay: number | undefined,
|
|
127
|
+
maxDay: number | undefined,
|
|
128
|
+
adjustment = 0
|
|
129
|
+
): DayRange {
|
|
130
|
+
const supported = supportedEpochDays(adjustment);
|
|
131
|
+
return {
|
|
132
|
+
min: Math.max(minDay ?? supported.min, supported.min),
|
|
133
|
+
max: Math.min(maxDay ?? supported.max, supported.max),
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** Whether any day of month `m` falls inside `range`. */
|
|
138
|
+
export function monthOverlaps(
|
|
139
|
+
m: CalendarMonth,
|
|
140
|
+
range: DayRange,
|
|
141
|
+
adjustment = 0
|
|
142
|
+
) {
|
|
143
|
+
if (m.calendar === 'hijri') {
|
|
144
|
+
// Hijri months outside the table have no start day at all.
|
|
145
|
+
const { min, max } = monthRangeOfSupportedHijri();
|
|
146
|
+
const index = m.year * 12 + m.month;
|
|
147
|
+
if (index < min || index > max) {
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
const start = monthStart(m, adjustment);
|
|
152
|
+
const end = start + monthLength(m) - 1;
|
|
153
|
+
return start <= range.max && end >= range.min;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function monthRangeOfSupportedHijri() {
|
|
157
|
+
const first = hijriOfEpochDay(FIRST_DAY);
|
|
158
|
+
const last = hijriOfEpochDay(LAST_DAY);
|
|
159
|
+
return {
|
|
160
|
+
min: first.year * 12 + first.month,
|
|
161
|
+
max: last.year * 12 + last.month,
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Lays out month `m` in weeks starting on `firstDayOfWeek` (0 = Sunday,
|
|
167
|
+
* 1 = Monday, 6 = Saturday).
|
|
168
|
+
*/
|
|
169
|
+
export function buildMonthGrid(
|
|
170
|
+
m: CalendarMonth,
|
|
171
|
+
firstDayOfWeek: number,
|
|
172
|
+
adjustment = 0
|
|
173
|
+
): MonthGrid {
|
|
174
|
+
const start = monthStart(m, adjustment);
|
|
175
|
+
const supported = supportedEpochDays(adjustment);
|
|
176
|
+
const length = monthLength(m);
|
|
177
|
+
const secondary = otherCalendar(m.calendar);
|
|
178
|
+
const leading = (weekdayOfEpochDay(start) - firstDayOfWeek + 7) % 7;
|
|
179
|
+
|
|
180
|
+
const slots: Array<DayCell | null> = Array(leading).fill(null);
|
|
181
|
+
for (let day = 1; day <= length; day++) {
|
|
182
|
+
const epochDay = start + day - 1;
|
|
183
|
+
const inRange = epochDay >= supported.min && epochDay <= supported.max;
|
|
184
|
+
slots.push({
|
|
185
|
+
epochDay,
|
|
186
|
+
day,
|
|
187
|
+
secondaryDay: inRange
|
|
188
|
+
? dateInCalendar(epochDay, secondary, adjustment).day
|
|
189
|
+
: null,
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
while (slots.length % 7 !== 0) {
|
|
193
|
+
slots.push(null);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const weeks: MonthGrid = [];
|
|
197
|
+
for (let i = 0; i < slots.length; i += 7) {
|
|
198
|
+
weeks.push(slots.slice(i, i + 7));
|
|
199
|
+
}
|
|
200
|
+
return weeks;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** "رمضان ١٤٤٦" / "Ramadan 1446" / "March 2025". */
|
|
204
|
+
export function monthTitle(m: CalendarMonth, locale: Locale, digits: Digits) {
|
|
205
|
+
const name = getMonthNames(m.calendar, locale)[m.month - 1];
|
|
206
|
+
return `${name} ${formatNumber(m.year, digits)}`;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* The months of the other calendar that month `m` overlaps, for example
|
|
211
|
+
* "مارس – أبريل ٢٠٢٤" for Ramadan 1445, "ديسمبر ٢٠٢٤ – يناير ٢٠٢٥" across
|
|
212
|
+
* a year boundary, or a single month. Only days in the supported range
|
|
213
|
+
* count.
|
|
214
|
+
*/
|
|
215
|
+
export function secondaryTitle(
|
|
216
|
+
m: CalendarMonth,
|
|
217
|
+
locale: Locale,
|
|
218
|
+
digits: Digits,
|
|
219
|
+
adjustment = 0
|
|
220
|
+
) {
|
|
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);
|
|
225
|
+
const calendar = otherCalendar(m.calendar);
|
|
226
|
+
const from = dateInCalendar(first, calendar, adjustment);
|
|
227
|
+
const to = dateInCalendar(last, calendar, adjustment);
|
|
228
|
+
const names = getMonthNames(calendar, locale);
|
|
229
|
+
const fromName = names[from.month - 1];
|
|
230
|
+
const toName = names[to.month - 1];
|
|
231
|
+
const year = (y: number) => formatNumber(y, digits);
|
|
232
|
+
|
|
233
|
+
if (from.year === to.year && from.month === to.month) {
|
|
234
|
+
return `${fromName} ${year(from.year)}`;
|
|
235
|
+
}
|
|
236
|
+
if (from.year === to.year) {
|
|
237
|
+
return `${fromName} – ${toName} ${year(to.year)}`;
|
|
238
|
+
}
|
|
239
|
+
return `${fromName} ${year(from.year)} – ${toName} ${year(to.year)}`;
|
|
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
|
+
}
|