ts-time-utils 3.0.4 → 4.0.1

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 (68) hide show
  1. package/README.md +91 -6
  2. package/dist/calculate.d.ts +25 -0
  3. package/dist/calculate.d.ts.map +1 -1
  4. package/dist/calculate.js +125 -0
  5. package/dist/calendar.d.ts +45 -0
  6. package/dist/calendar.d.ts.map +1 -1
  7. package/dist/calendar.js +68 -0
  8. package/dist/calendars.d.ts +156 -0
  9. package/dist/calendars.d.ts.map +1 -0
  10. package/dist/calendars.js +348 -0
  11. package/dist/compare.d.ts +27 -0
  12. package/dist/compare.d.ts.map +1 -1
  13. package/dist/compare.js +46 -0
  14. package/dist/esm/calculate.d.ts +25 -0
  15. package/dist/esm/calculate.d.ts.map +1 -1
  16. package/dist/esm/calculate.js +125 -0
  17. package/dist/esm/calendar.d.ts +45 -0
  18. package/dist/esm/calendar.d.ts.map +1 -1
  19. package/dist/esm/calendar.js +68 -0
  20. package/dist/esm/calendars.d.ts +156 -0
  21. package/dist/esm/calendars.d.ts.map +1 -0
  22. package/dist/esm/calendars.js +348 -0
  23. package/dist/esm/compare.d.ts +27 -0
  24. package/dist/esm/compare.d.ts.map +1 -1
  25. package/dist/esm/compare.js +46 -0
  26. package/dist/esm/holidays.d.ts +11 -1
  27. package/dist/esm/holidays.d.ts.map +1 -1
  28. package/dist/esm/holidays.js +220 -1
  29. package/dist/esm/index.d.ts +13 -7
  30. package/dist/esm/index.d.ts.map +1 -1
  31. package/dist/esm/index.js +17 -9
  32. package/dist/esm/iterate.d.ts +55 -0
  33. package/dist/esm/iterate.d.ts.map +1 -1
  34. package/dist/esm/iterate.js +86 -0
  35. package/dist/esm/locale.d.ts +53 -0
  36. package/dist/esm/locale.d.ts.map +1 -1
  37. package/dist/esm/locale.js +141 -0
  38. package/dist/esm/precision.d.ts +225 -0
  39. package/dist/esm/precision.d.ts.map +1 -0
  40. package/dist/esm/precision.js +491 -0
  41. package/dist/esm/temporal.d.ts +237 -0
  42. package/dist/esm/temporal.d.ts.map +1 -0
  43. package/dist/esm/temporal.js +660 -0
  44. package/dist/esm/validate.d.ts +30 -0
  45. package/dist/esm/validate.d.ts.map +1 -1
  46. package/dist/esm/validate.js +48 -0
  47. package/dist/holidays.d.ts +11 -1
  48. package/dist/holidays.d.ts.map +1 -1
  49. package/dist/holidays.js +220 -1
  50. package/dist/index.d.ts +13 -7
  51. package/dist/index.d.ts.map +1 -1
  52. package/dist/index.js +17 -9
  53. package/dist/iterate.d.ts +55 -0
  54. package/dist/iterate.d.ts.map +1 -1
  55. package/dist/iterate.js +86 -0
  56. package/dist/locale.d.ts +53 -0
  57. package/dist/locale.d.ts.map +1 -1
  58. package/dist/locale.js +141 -0
  59. package/dist/precision.d.ts +225 -0
  60. package/dist/precision.d.ts.map +1 -0
  61. package/dist/precision.js +491 -0
  62. package/dist/temporal.d.ts +237 -0
  63. package/dist/temporal.d.ts.map +1 -0
  64. package/dist/temporal.js +660 -0
  65. package/dist/validate.d.ts +30 -0
  66. package/dist/validate.d.ts.map +1 -1
  67. package/dist/validate.js +48 -0
  68. package/package.json +16 -1
@@ -91,4 +91,57 @@ export declare function compareLocaleFormats(locale1: SupportedLocale, locale2:
91
91
  locale2: number;
92
92
  };
93
93
  };
94
+ /**
95
+ * Get week information for a locale
96
+ * @param locale - locale code
97
+ * @returns Object with firstDay, weekend days, and minimalDays
98
+ */
99
+ export declare function getWeekInfo(locale: SupportedLocale): {
100
+ firstDay: number;
101
+ weekend: number[];
102
+ minimalDays: number;
103
+ };
104
+ /**
105
+ * Get the day the week starts on for a locale
106
+ * @param locale - locale code
107
+ * @returns 0-6 (0 = Sunday, 1 = Monday, etc.)
108
+ */
109
+ export declare function getLocaleWeekStartsOn(locale: SupportedLocale): number;
110
+ /**
111
+ * Get the weekend days for a locale
112
+ * @param locale - locale code
113
+ * @returns Array of day numbers (0 = Sunday, 6 = Saturday)
114
+ */
115
+ export declare function getLocaleWeekendDays(locale: SupportedLocale): number[];
116
+ /**
117
+ * Thin wrapper around Intl.DateTimeFormat
118
+ * @param date - date to format
119
+ * @param options - Intl.DateTimeFormatOptions
120
+ * @param locale - locale string (defaults to system locale)
121
+ */
122
+ export declare function intlFormat(date: Date, options?: Intl.DateTimeFormatOptions, locale?: string): string;
123
+ /**
124
+ * Format only the date part in ISO format (YYYY-MM-DD)
125
+ * @param date - date to format
126
+ */
127
+ export declare function formatISODate(date: Date): string;
128
+ /**
129
+ * Format only the time part in ISO format (HH:mm:ss)
130
+ * @param date - date to format
131
+ * @param includeMs - include milliseconds
132
+ */
133
+ export declare function formatISOTime(date: Date, includeMs?: boolean): string;
134
+ /**
135
+ * Format distance between two dates without approximation words like "about" or "almost"
136
+ * Returns exact values like "5 days" instead of "about 5 days"
137
+ * @param date - the date
138
+ * @param baseDate - the base date to compare against (defaults to now)
139
+ * @param options - formatting options
140
+ */
141
+ export declare function formatDistanceStrict(date: Date, baseDate?: Date, options?: {
142
+ locale?: SupportedLocale;
143
+ unit?: 'second' | 'minute' | 'hour' | 'day' | 'week' | 'month' | 'year';
144
+ roundingMethod?: 'floor' | 'ceil' | 'round';
145
+ addSuffix?: boolean;
146
+ }): string;
94
147
  //# sourceMappingURL=locale.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"locale.d.ts","sourceRoot":"","sources":["../../src/locale.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,eAAe,EAAE,YAAY,EAAE,mBAAmB,EAAoB,MAAM,YAAY,CAAC;AA6alH;;GAEG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,YAAY,GAAG,IAAI,CAQzD;AAED;;GAEG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,eAAe,GAAG,YAAY,CAcrE;AAED;;GAEG;AACH,wBAAgB,mBAAmB,IAAI,eAAe,EAAE,CAEvD;AAED;;GAEG;AACH,wBAAgB,kBAAkB,CAChC,IAAI,EAAE,SAAS,EACf,O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AAC;IACxE,cAAc,CAAC,EAAE,OAAO,GAAG,MAAM,GAAG,OAAO,CAAC;IAC5C,SAAS,CAAC,EAAE,OAAO,CAAC;CAChB,GACL,MAAM,CAmER"}
@@ -1085,3 +1085,144 @@ function tryParseWithPattern(dateString, pattern, config) {
1085
1085
  const isoDate = new Date(dateString);
1086
1086
  return isNaN(isoDate.getTime()) ? null : isoDate;
1087
1087
  }
1088
+ // ========================================
1089
+ // WEEK INFO UTILITIES
1090
+ // ========================================
1091
+ /**
1092
+ * Get week information for a locale
1093
+ * @param locale - locale code
1094
+ * @returns Object with firstDay, weekend days, and minimalDays
1095
+ */
1096
+ export function getWeekInfo(locale) {
1097
+ const config = getLocaleConfig(locale);
1098
+ const firstDay = config.calendar?.weekStartsOn ?? 0;
1099
+ // Most locales use Saturday-Sunday weekend
1100
+ // Some Middle Eastern locales use Friday-Saturday
1101
+ const middleEasternLocales = ['ar', 'fa', 'he', 'ur'];
1102
+ const baseLang = locale.split('-')[0];
1103
+ const weekend = middleEasternLocales.includes(baseLang) ? [5, 6] : [6, 0];
1104
+ // Minimal days in first week (ISO = 4, US = 1)
1105
+ const minimalDays = firstDay === 1 ? 4 : 1;
1106
+ return { firstDay, weekend, minimalDays };
1107
+ }
1108
+ /**
1109
+ * Get the day the week starts on for a locale
1110
+ * @param locale - locale code
1111
+ * @returns 0-6 (0 = Sunday, 1 = Monday, etc.)
1112
+ */
1113
+ export function getLocaleWeekStartsOn(locale) {
1114
+ const config = getLocaleConfig(locale);
1115
+ return config.calendar?.weekStartsOn ?? 0;
1116
+ }
1117
+ /**
1118
+ * Get the weekend days for a locale
1119
+ * @param locale - locale code
1120
+ * @returns Array of day numbers (0 = Sunday, 6 = Saturday)
1121
+ */
1122
+ export function getLocaleWeekendDays(locale) {
1123
+ const middleEasternLocales = ['ar', 'fa', 'he', 'ur'];
1124
+ const baseLang = locale.split('-')[0];
1125
+ return middleEasternLocales.includes(baseLang) ? [5, 6] : [6, 0];
1126
+ }
1127
+ /**
1128
+ * Thin wrapper around Intl.DateTimeFormat
1129
+ * @param date - date to format
1130
+ * @param options - Intl.DateTimeFormatOptions
1131
+ * @param locale - locale string (defaults to system locale)
1132
+ */
1133
+ export function intlFormat(date, options, locale) {
1134
+ return new Intl.DateTimeFormat(locale, options).format(date);
1135
+ }
1136
+ /**
1137
+ * Format only the date part in ISO format (YYYY-MM-DD)
1138
+ * @param date - date to format
1139
+ */
1140
+ export function formatISODate(date) {
1141
+ const year = date.getFullYear();
1142
+ const month = String(date.getMonth() + 1).padStart(2, '0');
1143
+ const day = String(date.getDate()).padStart(2, '0');
1144
+ return `${year}-${month}-${day}`;
1145
+ }
1146
+ /**
1147
+ * Format only the time part in ISO format (HH:mm:ss)
1148
+ * @param date - date to format
1149
+ * @param includeMs - include milliseconds
1150
+ */
1151
+ export function formatISOTime(date, includeMs = false) {
1152
+ const hours = String(date.getHours()).padStart(2, '0');
1153
+ const minutes = String(date.getMinutes()).padStart(2, '0');
1154
+ const seconds = String(date.getSeconds()).padStart(2, '0');
1155
+ if (includeMs) {
1156
+ const ms = String(date.getMilliseconds()).padStart(3, '0');
1157
+ return `${hours}:${minutes}:${seconds}.${ms}`;
1158
+ }
1159
+ return `${hours}:${minutes}:${seconds}`;
1160
+ }
1161
+ /**
1162
+ * Format distance between two dates without approximation words like "about" or "almost"
1163
+ * Returns exact values like "5 days" instead of "about 5 days"
1164
+ * @param date - the date
1165
+ * @param baseDate - the base date to compare against (defaults to now)
1166
+ * @param options - formatting options
1167
+ */
1168
+ export function formatDistanceStrict(date, baseDate = new Date(), options = {}) {
1169
+ const { locale = 'en', unit, roundingMethod = 'round', addSuffix = false } = options;
1170
+ const config = getLocaleConfig(locale);
1171
+ const diffMs = date.getTime() - baseDate.getTime();
1172
+ const absDiffMs = Math.abs(diffMs);
1173
+ const isPast = diffMs < 0;
1174
+ const roundFn = roundingMethod === 'floor' ? Math.floor
1175
+ : roundingMethod === 'ceil' ? Math.ceil
1176
+ : Math.round;
1177
+ // Unit definitions in ms
1178
+ const units = {
1179
+ second: 1000,
1180
+ minute: 60 * 1000,
1181
+ hour: 60 * 60 * 1000,
1182
+ day: 24 * 60 * 60 * 1000,
1183
+ week: 7 * 24 * 60 * 60 * 1000,
1184
+ month: 30.44 * 24 * 60 * 60 * 1000,
1185
+ year: 365.25 * 24 * 60 * 60 * 1000
1186
+ };
1187
+ let selectedUnit = unit;
1188
+ let value;
1189
+ if (selectedUnit) {
1190
+ value = roundFn(absDiffMs / units[selectedUnit]);
1191
+ }
1192
+ else {
1193
+ // Auto-select unit based on duration
1194
+ if (absDiffMs < units.minute) {
1195
+ selectedUnit = 'second';
1196
+ }
1197
+ else if (absDiffMs < units.hour) {
1198
+ selectedUnit = 'minute';
1199
+ }
1200
+ else if (absDiffMs < units.day) {
1201
+ selectedUnit = 'hour';
1202
+ }
1203
+ else if (absDiffMs < units.week) {
1204
+ selectedUnit = 'day';
1205
+ }
1206
+ else if (absDiffMs < units.month) {
1207
+ selectedUnit = 'week';
1208
+ }
1209
+ else if (absDiffMs < units.year) {
1210
+ selectedUnit = 'month';
1211
+ }
1212
+ else {
1213
+ selectedUnit = 'year';
1214
+ }
1215
+ value = roundFn(absDiffMs / units[selectedUnit]);
1216
+ }
1217
+ // Get unit text
1218
+ const unitKey = (value === 1 ? selectedUnit : selectedUnit + 's');
1219
+ const unitText = config.relativeTime?.units?.[unitKey] || unitKey;
1220
+ const result = `${value} ${unitText}`;
1221
+ if (addSuffix) {
1222
+ const template = isPast
1223
+ ? (config.relativeTime?.past || '{0} ago')
1224
+ : (config.relativeTime?.future || 'in {0}');
1225
+ return template.replace('{0}', result);
1226
+ }
1227
+ return result;
1228
+ }
@@ -0,0 +1,225 @@
1
+ /**
2
+ * @fileoverview High-precision time utilities
3
+ * Handles nanoseconds, BigInt timestamps, sub-millisecond operations
4
+ */
5
+ /**
6
+ * Nanosecond precision timestamp
7
+ * Uses BigInt for precision beyond milliseconds
8
+ */
9
+ export interface NanosecondTimestamp {
10
+ readonly milliseconds: number;
11
+ readonly nanoseconds: number;
12
+ readonly totalNanoseconds: bigint;
13
+ }
14
+ /**
15
+ * High-resolution duration with nanosecond precision
16
+ */
17
+ export interface HighResDuration {
18
+ readonly seconds: number;
19
+ readonly nanoseconds: number;
20
+ }
21
+ /**
22
+ * Create a nanosecond timestamp from components
23
+ */
24
+ export declare function createNanosecondTimestamp(milliseconds: number, nanoseconds?: number): NanosecondTimestamp;
25
+ /**
26
+ * Create a nanosecond timestamp from BigInt
27
+ */
28
+ export declare function fromNanoseconds(totalNs: bigint): NanosecondTimestamp;
29
+ /**
30
+ * Create a nanosecond timestamp from a Date
31
+ */
32
+ export declare function dateToNanoseconds(date: Date): NanosecondTimestamp;
33
+ /**
34
+ * Convert nanosecond timestamp to Date (loses sub-millisecond precision)
35
+ */
36
+ export declare function nanosecondsToDate(timestamp: NanosecondTimestamp): Date;
37
+ /**
38
+ * Add two nanosecond timestamps
39
+ */
40
+ export declare function addNanoseconds(a: NanosecondTimestamp, b: NanosecondTimestamp): NanosecondTimestamp;
41
+ /**
42
+ * Subtract nanosecond timestamps
43
+ */
44
+ export declare function subtractNanoseconds(a: NanosecondTimestamp, b: NanosecondTimestamp): NanosecondTimestamp;
45
+ /**
46
+ * Compare nanosecond timestamps
47
+ * @returns -1 if a < b, 0 if equal, 1 if a > b
48
+ */
49
+ export declare function compareNanoseconds(a: NanosecondTimestamp, b: NanosecondTimestamp): -1 | 0 | 1;
50
+ /**
51
+ * Get current time with nanosecond precision (if available)
52
+ * Falls back to millisecond precision if performance.now() not available
53
+ */
54
+ export declare function nowNanoseconds(): NanosecondTimestamp;
55
+ /**
56
+ * Format nanosecond timestamp to ISO string with sub-millisecond precision
57
+ */
58
+ export declare function formatNanoseconds(timestamp: NanosecondTimestamp): string;
59
+ /**
60
+ * Parse ISO string with nanosecond precision
61
+ */
62
+ export declare function parseNanoseconds(isoString: string): NanosecondTimestamp | null;
63
+ /**
64
+ * Create a high-resolution duration
65
+ */
66
+ export declare function createHighResDuration(seconds: number, nanoseconds?: number): HighResDuration;
67
+ /**
68
+ * Add two high-resolution durations
69
+ */
70
+ export declare function addHighResDuration(a: HighResDuration, b: HighResDuration): HighResDuration;
71
+ /**
72
+ * Subtract high-resolution durations
73
+ */
74
+ export declare function subtractHighResDuration(a: HighResDuration, b: HighResDuration): HighResDuration;
75
+ /**
76
+ * Convert high-resolution duration to milliseconds
77
+ */
78
+ export declare function highResDurationToMs(duration: HighResDuration): number;
79
+ /**
80
+ * Convert milliseconds to high-resolution duration
81
+ */
82
+ export declare function msToHighResDuration(ms: number): HighResDuration;
83
+ /**
84
+ * Convert Unix epoch milliseconds to BigInt
85
+ */
86
+ export declare function toBigIntMs(date: Date): bigint;
87
+ /**
88
+ * Convert BigInt milliseconds to Date
89
+ */
90
+ export declare function fromBigIntMs(ms: bigint): Date;
91
+ /**
92
+ * Convert Unix epoch seconds to BigInt
93
+ */
94
+ export declare function toBigIntSeconds(date: Date): bigint;
95
+ /**
96
+ * Convert BigInt seconds to Date
97
+ */
98
+ export declare function fromBigIntSeconds(seconds: bigint): Date;
99
+ /**
100
+ * Add milliseconds (as BigInt) to a date
101
+ */
102
+ export declare function addBigIntMs(date: Date, ms: bigint): Date;
103
+ /**
104
+ * Subtract milliseconds (as BigInt) from a date
105
+ */
106
+ export declare function subtractBigIntMs(date: Date, ms: bigint): Date;
107
+ /**
108
+ * Calculate difference between dates in BigInt milliseconds
109
+ */
110
+ export declare function diffBigIntMs(a: Date, b: Date): bigint;
111
+ /**
112
+ * DST transition info
113
+ */
114
+ export interface DSTTransition {
115
+ /** Transition date/time (UTC) */
116
+ readonly utc: Date;
117
+ /** Transition date/time (local) */
118
+ readonly local: Date;
119
+ /** Whether transitioning to DST */
120
+ readonly toDST: boolean;
121
+ /** Offset before transition (minutes) */
122
+ readonly offsetBefore: number;
123
+ /** Offset after transition (minutes) */
124
+ readonly offsetAfter: number;
125
+ /** Gap or overlap duration (minutes) */
126
+ readonly adjustmentMinutes: number;
127
+ }
128
+ /**
129
+ * Check if a date falls in a DST transition gap (skipped time)
130
+ * @param date - Date to check
131
+ * @param timeZone - IANA timezone (optional, uses local if not provided)
132
+ */
133
+ export declare function isInDSTGap(date: Date, timeZone?: string): boolean;
134
+ /**
135
+ * Check if a date falls in a DST overlap (ambiguous time)
136
+ * @param date - Date to check
137
+ * @param timeZone - IANA timezone (optional, uses local if not provided)
138
+ */
139
+ export declare function isInDSTOverlap(date: Date, timeZone?: string): boolean;
140
+ /**
141
+ * Find DST transitions in a year for local timezone
142
+ */
143
+ export declare function getDSTTransitionsInYear(year: number): DSTTransition[];
144
+ /**
145
+ * Resolve ambiguous time in DST overlap
146
+ * @param date - Potentially ambiguous date
147
+ * @param prefer - Prefer 'earlier' or 'later' interpretation
148
+ */
149
+ export declare function resolveAmbiguousTime(date: Date, prefer?: 'earlier' | 'later'): Date;
150
+ /**
151
+ * Validated Date wrapper that guarantees a valid date
152
+ */
153
+ export declare class ValidDate {
154
+ private readonly _date;
155
+ private constructor();
156
+ /**
157
+ * Create a ValidDate, throws if invalid
158
+ */
159
+ static from(date: Date): ValidDate;
160
+ /**
161
+ * Create a ValidDate, returns null if invalid
162
+ */
163
+ static tryFrom(date: Date): ValidDate | null;
164
+ /**
165
+ * Create from timestamp, throws if invalid
166
+ */
167
+ static fromTimestamp(ms: number): ValidDate;
168
+ /**
169
+ * Create from ISO string, throws if invalid
170
+ */
171
+ static fromISO(isoString: string): ValidDate;
172
+ /**
173
+ * Get the underlying Date object
174
+ */
175
+ get value(): Date;
176
+ /**
177
+ * Get timestamp
178
+ */
179
+ get time(): number;
180
+ /**
181
+ * Format to ISO string
182
+ */
183
+ toISOString(): string;
184
+ /**
185
+ * Format to locale string
186
+ */
187
+ toLocaleString(locale?: string, options?: Intl.DateTimeFormatOptions): string;
188
+ }
189
+ /**
190
+ * Ensure a date is valid, with fallback
191
+ */
192
+ export declare function ensureValidDate(date: Date, fallback?: Date): Date;
193
+ /**
194
+ * Parse date with validation
195
+ */
196
+ export declare function parseValidDate(input: string | number | Date): Date | null;
197
+ /**
198
+ * Assert date is valid, throws if not
199
+ */
200
+ export declare function assertValidDate(date: Date, message?: string): asserts date is Date & {
201
+ getTime(): number;
202
+ };
203
+ /**
204
+ * Known leap seconds (added at end of these dates, 23:59:60 UTC)
205
+ * List from https://www.ietf.org/timezones/data/leap-seconds.list
206
+ */
207
+ export declare const LEAP_SECONDS: readonly Date[];
208
+ /**
209
+ * Get number of leap seconds between two dates
210
+ */
211
+ export declare function leapSecondsBetween(start: Date, end: Date): number;
212
+ /**
213
+ * Check if a date is near a leap second (within 1 second)
214
+ */
215
+ export declare function isNearLeapSecond(date: Date): boolean;
216
+ /**
217
+ * Convert TAI (International Atomic Time) to UTC
218
+ * TAI = UTC + accumulated leap seconds + 10 (initial offset)
219
+ */
220
+ export declare function taiToUtc(taiMs: number): Date;
221
+ /**
222
+ * Convert UTC to TAI
223
+ */
224
+ export declare function utcToTai(date: Date): number;
225
+ //# sourceMappingURL=precision.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"precision.d.ts","sourceRoot":"","sources":["../../src/precision.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;GAGG;AACH,MAAM,WAAW,mBAAmB;IAClC,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,gBAAgB,EAAE,MAAM,CAAC;CACnC;AAED;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;CAC9B;AAED;;GAEG;AACH,wBAAgB,yBAAyB,CACvC,YAAY,EAAE,MAAM,EACpB,WAAW,GAAE,MAAU,GACtB,mBAAmB,CAarB;AAED;;GAEG;AACH,wBAAgB,eAAe,CAAC,OAAO,EAAE,MAAM,GAAG,mBAAmB,CASpE;AAED;;GAEG;AACH,wBAAgB,iB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