@shopify/dates
Advanced tools
Comparing version 2.0.3 to 2.1.0
@@ -6,3 +6,2 @@ 'use strict'; | ||
exports.TimeUnit = void 0; | ||
(function (TimeUnit) { | ||
@@ -9,0 +8,0 @@ TimeUnit[TimeUnit["Second"] = 1000] = "Second"; |
@@ -6,2 +6,3 @@ 'use strict'; | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
const deprecatedTimezones = { | ||
@@ -8,0 +9,0 @@ 'America/Indiana': 'America/Indiana/Indianapolis', |
@@ -169,3 +169,2 @@ 'use strict'; | ||
var _Intl$DateTimeFormat$; | ||
const { | ||
@@ -180,3 +179,2 @@ type, | ||
}).formatToParts(date).find(part => part.type === type)) === null || _Intl$DateTimeFormat$ === void 0 ? void 0 : _Intl$DateTimeFormat$.value; | ||
if (timeUnit !== undefined && extraTransform) { | ||
@@ -183,0 +181,0 @@ return extraTransform(timeUnit); |
@@ -21,9 +21,6 @@ 'use strict'; | ||
} | ||
function dateTimeCacheKey(unit) { | ||
return (date, timeZone) => `${unit}-${date.toString()}-${timeZone}`; | ||
} | ||
var Weekday; | ||
(function (Weekday) { | ||
@@ -38,3 +35,2 @@ Weekday["Monday"] = "Monday"; | ||
})(Weekday || (Weekday = {})); | ||
const weekdays = { | ||
@@ -49,11 +45,8 @@ Monday: 0, | ||
}; | ||
function isWeekday(weekday) { | ||
return Object.keys(weekdays).some(key => key === weekday); | ||
} | ||
function assertNever(message) { | ||
throw new Error(message); | ||
} | ||
function getWeekdayValue(weekday) { | ||
@@ -63,9 +56,7 @@ if (!isWeekday(weekday)) { | ||
} | ||
return weekdays[weekday]; | ||
} // eslint-disable-next-line @typescript-eslint/no-extraneous-class | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-extraneous-class | ||
class DateTimeParts {} | ||
DateTimeParts.getYear = functionEnhancers.memoize((date, timeZone) => { | ||
@@ -75,3 +66,2 @@ if (isNaN(date.valueOf())) { | ||
} | ||
const yearString = formatDate.formatDate(date, 'en', { | ||
@@ -83,7 +73,5 @@ timeZone, | ||
const year = parseInt(sanitisedYearString, 10); | ||
if (isNaN(year)) { | ||
throw new Error(`Unable to parse year: '${yearString}'`); | ||
} | ||
return year; | ||
@@ -98,7 +86,5 @@ }, dateTimeCacheKey('year')); | ||
const month = parseInt(sanitisedMonthString, 10); | ||
if (isNaN(month)) { | ||
throw new Error(`Unable to parse month: '${monthString}'`); | ||
} | ||
return month; | ||
@@ -113,7 +99,5 @@ }, dateTimeCacheKey('month')); | ||
const day = parseInt(sanitisedDayString, 10); | ||
if (isNaN(day)) { | ||
throw new Error(`Unable to parse day: '${dayString}'`); | ||
} | ||
return day; | ||
@@ -136,7 +120,5 @@ }, dateTimeCacheKey('day')); | ||
let hour = parseInt(hourString, 10); | ||
if (isNaN(hour)) { | ||
hour = DateTimeParts.getTimePartsFallback(date, timeZone).hour; | ||
} | ||
return hour; | ||
@@ -150,7 +132,5 @@ }, dateTimeCacheKey('hour')); | ||
let minute = parseInt(minuteString, 10); | ||
if (isNaN(minute)) { | ||
minute = DateTimeParts.getTimePartsFallback(date, timeZone).minute; | ||
} | ||
return minute; | ||
@@ -164,7 +144,5 @@ }, dateTimeCacheKey('minute')); | ||
let second = parseInt(secondString, 10); | ||
if (isNaN(second)) { | ||
second = DateTimeParts.getTimePartsFallback(date, timeZone).second; | ||
} | ||
return second; | ||
@@ -179,4 +157,5 @@ }, dateTimeCacheKey('second')); | ||
second: '2-digit' | ||
}); // In Microsoft Edge, Intl.DateTimeFormat returns invisible characters around the individual numbers | ||
}); | ||
// In Microsoft Edge, Intl.DateTimeFormat returns invisible characters around the individual numbers | ||
const [dirtyHour, dirtyMinute, dirtySecond] = timeString.split(':'); | ||
@@ -186,3 +165,2 @@ const rawHour = new RegExp(TWO_DIGIT_REGEX).exec(dirtyHour); | ||
const rawSecond = new RegExp(TWO_DIGIT_REGEX).exec(dirtySecond); | ||
if (rawHour != null && rawMinute != null && rawSecond != null) { | ||
@@ -198,3 +176,2 @@ const hour = parseInt(rawHour[1], 10); | ||
} | ||
throw new Error(`Unable to parse timeString: '${timeString}'`); | ||
@@ -201,0 +178,0 @@ }, dateTimeCacheKey('timePartsFallback')); |
@@ -12,3 +12,2 @@ 'use strict'; | ||
} | ||
function constructZonedDateFromParts(date, timeZone) { | ||
@@ -15,0 +14,0 @@ const { |
@@ -16,8 +16,7 @@ 'use strict'; | ||
const dateTimeParts = new RegExp(DATE_TIME_PARTS_REGEX).exec(dateString); | ||
if (dateTimeParts == null) { | ||
return null; | ||
} // slice the first regex part (the full match) off | ||
} | ||
// slice the first regex part (the full match) off | ||
const [year, month, day, hour, minute, second, millisecond, timeZoneOffset, sign, timeZoneHour, timeZoneMinute] = Array.from(dateTimeParts).slice(1); | ||
@@ -24,0 +23,0 @@ return { |
@@ -10,7 +10,5 @@ 'use strict'; | ||
const dateTimeParts = parseDateStringParts.parseDateStringParts(dateString); | ||
if (dateTimeParts == null) { | ||
return null; | ||
} | ||
const { | ||
@@ -39,11 +37,8 @@ year: rawYear, | ||
const utcDate = new Date(Date.UTC(year, month - 1, day, hour, minute, second, millisecond)); | ||
if (timeZoneOffset === 'Z') { | ||
return utcDate; | ||
} | ||
if (sign == null) { | ||
return applyTimeZoneOffset.applyTimeZoneOffset(utcDate, timeZone, 'UTC'); | ||
} | ||
switch (sign) { | ||
@@ -54,3 +49,2 @@ case '+': | ||
return utcDate; | ||
case '-': | ||
@@ -60,3 +54,2 @@ utcDate.setHours(utcDate.getHours() + timeZoneHour); | ||
return utcDate; | ||
default: | ||
@@ -63,0 +56,0 @@ return null; |
@@ -8,7 +8,5 @@ 'use strict'; | ||
const key = dateTimeFormatCacheKey(locales, options); | ||
if (intl.has(key)) { | ||
return intl.get(key); | ||
} | ||
const i = new Intl.DateTimeFormat(locales, options); | ||
@@ -20,23 +18,27 @@ intl.set(key, i); | ||
hour: 'numeric' | ||
}); // hourCycle to Intl.DateTimeFormatOptions was added in TS 4.2, so we could | ||
}); | ||
// hourCycle to Intl.DateTimeFormatOptions was added in TS 4.2, so we could | ||
// remove this, but that would require consumers to update to at least TS 4.2 | ||
// and be including the libs for es2020 | ||
// hourcycle is not yet added to Intl.ResolvedDateTimeFormatOptions. This seems | ||
// to be an omission in TS, see https://github.com/microsoft/TypeScript/issues/45420 | ||
const resolvedOptions = typeof browserFeatureDetectionDate.resolvedOptions === 'undefined' ? undefined : browserFeatureDetectionDate.resolvedOptions(); | ||
function formatDate(date, locales, options = {}) { | ||
const hourCycleRequired = resolvedOptions != null && options.hour12 === false && resolvedOptions.hourCycle != null; | ||
if (hourCycleRequired) { | ||
options.hour12 = undefined; | ||
options.hourCycle = 'h23'; | ||
} // Etc/GMT+12 is not supported in most browsers and there is no equivalent fallback | ||
} | ||
// Etc/GMT+12 is not supported in most browsers and there is no equivalent fallback | ||
if (options.timeZone != null && options.timeZone === 'Etc/GMT+12') { | ||
const adjustedDate = new Date(date.valueOf() - 12 * 60 * 60 * 1000); | ||
return memoizedGetDateTimeFormat(locales, { ...options, | ||
return memoizedGetDateTimeFormat(locales, { | ||
...options, | ||
timeZone: 'UTC' | ||
}).format(adjustedDate); | ||
} | ||
return memoizedGetDateTimeFormat(locales, options).format(date); | ||
@@ -43,0 +45,0 @@ } |
{ | ||
"name": "@shopify/dates", | ||
"version": "2.0.3", | ||
"version": "2.1.0", | ||
"license": "MIT", | ||
@@ -24,9 +24,9 @@ "description": "Lightweight date operations library", | ||
"engines": { | ||
"node": "^14.17.0 || >=16.0.0" | ||
"node": ">=18.12.0" | ||
}, | ||
"dependencies": { | ||
"@shopify/function-enhancers": "^3.0.1" | ||
"@shopify/function-enhancers": "^3.1.0" | ||
}, | ||
"devDependencies": { | ||
"@shopify/jest-dom-mocks": "^4.1.1" | ||
"@shopify/jest-dom-mocks": "^5.1.0" | ||
}, | ||
@@ -33,0 +33,0 @@ "files": [ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
106494
1677