react-native-month-day-picker 0.1.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 (59) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +269 -0
  3. package/lib/commonjs/BirthdayPicker.js +177 -0
  4. package/lib/commonjs/BirthdayPicker.js.map +1 -0
  5. package/lib/commonjs/BirthdayPickerModal.js +176 -0
  6. package/lib/commonjs/BirthdayPickerModal.js.map +1 -0
  7. package/lib/commonjs/constants.js +80 -0
  8. package/lib/commonjs/constants.js.map +1 -0
  9. package/lib/commonjs/hooks/useBirthdayPicker.js +90 -0
  10. package/lib/commonjs/hooks/useBirthdayPicker.js.map +1 -0
  11. package/lib/commonjs/index.js +89 -0
  12. package/lib/commonjs/index.js.map +1 -0
  13. package/lib/commonjs/types.js +6 -0
  14. package/lib/commonjs/types.js.map +1 -0
  15. package/lib/commonjs/utils/dateUtils.js +103 -0
  16. package/lib/commonjs/utils/dateUtils.js.map +1 -0
  17. package/lib/commonjs/utils/localeUtils.js +90 -0
  18. package/lib/commonjs/utils/localeUtils.js.map +1 -0
  19. package/lib/module/BirthdayPicker.js +169 -0
  20. package/lib/module/BirthdayPicker.js.map +1 -0
  21. package/lib/module/BirthdayPickerModal.js +169 -0
  22. package/lib/module/BirthdayPickerModal.js.map +1 -0
  23. package/lib/module/constants.js +74 -0
  24. package/lib/module/constants.js.map +1 -0
  25. package/lib/module/hooks/useBirthdayPicker.js +84 -0
  26. package/lib/module/hooks/useBirthdayPicker.js.map +1 -0
  27. package/lib/module/index.js +16 -0
  28. package/lib/module/index.js.map +1 -0
  29. package/lib/module/types.js +2 -0
  30. package/lib/module/types.js.map +1 -0
  31. package/lib/module/utils/dateUtils.js +92 -0
  32. package/lib/module/utils/dateUtils.js.map +1 -0
  33. package/lib/module/utils/localeUtils.js +81 -0
  34. package/lib/module/utils/localeUtils.js.map +1 -0
  35. package/lib/typescript/BirthdayPicker.d.ts +25 -0
  36. package/lib/typescript/BirthdayPicker.d.ts.map +1 -0
  37. package/lib/typescript/BirthdayPickerModal.d.ts +24 -0
  38. package/lib/typescript/BirthdayPickerModal.d.ts.map +1 -0
  39. package/lib/typescript/constants.d.ts +39 -0
  40. package/lib/typescript/constants.d.ts.map +1 -0
  41. package/lib/typescript/hooks/useBirthdayPicker.d.ts +17 -0
  42. package/lib/typescript/hooks/useBirthdayPicker.d.ts.map +1 -0
  43. package/lib/typescript/index.d.ts +8 -0
  44. package/lib/typescript/index.d.ts.map +1 -0
  45. package/lib/typescript/types.d.ts +160 -0
  46. package/lib/typescript/types.d.ts.map +1 -0
  47. package/lib/typescript/utils/dateUtils.d.ts +43 -0
  48. package/lib/typescript/utils/dateUtils.d.ts.map +1 -0
  49. package/lib/typescript/utils/localeUtils.d.ts +28 -0
  50. package/lib/typescript/utils/localeUtils.d.ts.map +1 -0
  51. package/package.json +137 -0
  52. package/src/BirthdayPicker.tsx +210 -0
  53. package/src/BirthdayPickerModal.tsx +192 -0
  54. package/src/constants.ts +64 -0
  55. package/src/hooks/useBirthdayPicker.ts +106 -0
  56. package/src/index.ts +31 -0
  57. package/src/types.ts +189 -0
  58. package/src/utils/dateUtils.ts +101 -0
  59. package/src/utils/localeUtils.ts +99 -0
@@ -0,0 +1,99 @@
1
+ import {
2
+ MONTHS_IN_YEAR,
3
+ DEFAULT_LOCALE,
4
+ DEFAULT_MONTH_FORMAT,
5
+ } from '../constants';
6
+ import type { MonthFormat } from '../types';
7
+
8
+ /**
9
+ * Cache for month names to avoid repeated Intl.DateTimeFormat calls
10
+ */
11
+ const monthNamesCache = new Map<string, string[]>();
12
+
13
+ /**
14
+ * Generate a cache key for month names
15
+ */
16
+ function getCacheKey(locale: string, format: MonthFormat): string {
17
+ return `${locale}-${format}`;
18
+ }
19
+
20
+ /**
21
+ * Get month names for a given locale and format
22
+ * @param locale - BCP 47 locale string (e.g., 'en-US', 'ja-JP')
23
+ * @param format - Format for month names ('long', 'short', 'numeric')
24
+ * @returns Array of month names/numbers (1-indexed, index 0 is empty string)
25
+ */
26
+ export function getMonthNames(
27
+ locale: string = DEFAULT_LOCALE,
28
+ format: MonthFormat = DEFAULT_MONTH_FORMAT
29
+ ): string[] {
30
+ const cacheKey = getCacheKey(locale, format);
31
+
32
+ // Return cached result if available
33
+ if (monthNamesCache.has(cacheKey)) {
34
+ return monthNamesCache.get(cacheKey)!;
35
+ }
36
+
37
+ const names: string[] = [''];
38
+
39
+ if (format === 'numeric') {
40
+ // For numeric format, just use month numbers
41
+ for (let i = 1; i <= MONTHS_IN_YEAR; i++) {
42
+ names.push(String(i));
43
+ }
44
+ } else {
45
+ // Use Intl.DateTimeFormat for localized month names
46
+ const formatter = new Intl.DateTimeFormat(locale, { month: format });
47
+
48
+ for (let i = 0; i < MONTHS_IN_YEAR; i++) {
49
+ // Create a date in month i (0-indexed for Date constructor)
50
+ const date = new Date(2024, i, 1);
51
+ names.push(formatter.format(date));
52
+ }
53
+ }
54
+
55
+ // Cache the result
56
+ monthNamesCache.set(cacheKey, names);
57
+
58
+ return names;
59
+ }
60
+
61
+ /**
62
+ * Format a single month number to a localized string
63
+ * @param month - Month number (1-12)
64
+ * @param locale - BCP 47 locale string
65
+ * @param format - Format for month name
66
+ * @returns Formatted month name
67
+ */
68
+ export function formatMonth(
69
+ month: number,
70
+ locale: string = DEFAULT_LOCALE,
71
+ format: MonthFormat = DEFAULT_MONTH_FORMAT
72
+ ): string {
73
+ if (month < 1 || month > MONTHS_IN_YEAR) {
74
+ throw new Error(`Invalid month: ${month}. Must be between 1 and 12.`);
75
+ }
76
+
77
+ const names = getMonthNames(locale, format);
78
+ return names[month];
79
+ }
80
+
81
+ /**
82
+ * Format a day number with optional locale-specific formatting
83
+ * @param day - Day number (1-31)
84
+ * @param locale - BCP 47 locale string (currently unused, for future i18n)
85
+ * @returns Formatted day string
86
+ */
87
+ export function formatDay(
88
+ day: number,
89
+ locale: string = DEFAULT_LOCALE
90
+ ): string {
91
+ return new Intl.NumberFormat(locale).format(day);
92
+ }
93
+
94
+ /**
95
+ * Clear the month names cache (useful for testing)
96
+ */
97
+ export function clearMonthNamesCache(): void {
98
+ monthNamesCache.clear();
99
+ }