@shopify/dates
Advanced tools
Comparing version 2.0.1 to 2.0.2
@@ -5,2 +5,3 @@ 'use strict'; | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
const deprecatedTimezones = { | ||
@@ -7,0 +8,0 @@ 'America/Indiana': 'America/Indiana/Indianapolis', |
@@ -5,8 +5,6 @@ 'use strict'; | ||
var _rollupPluginBabelHelpers = require('./_virtual/_rollupPluginBabelHelpers.js'); | ||
var decorators = require('@shopify/decorators'); | ||
var functionEnhancers = require('@shopify/function-enhancers'); | ||
var sanitiseDateString = require('./sanitise-date-string.js'); | ||
var formatDate = require('./utilities/formatDate.js'); | ||
var _dec, _dec2, _dec3, _dec4, _dec5, _dec6, _dec7, _dec8, _class; | ||
const TWO_DIGIT_REGEX = /(\d{2})/; | ||
@@ -68,134 +66,126 @@ function getDateTimeParts(date, timeZone) { | ||
let DateTimeParts = (_dec = decorators.memoize(dateTimeCacheKey('year')), _dec2 = decorators.memoize(dateTimeCacheKey('month')), _dec3 = decorators.memoize(dateTimeCacheKey('day')), _dec4 = decorators.memoize(dateTimeCacheKey('weekday')), _dec5 = decorators.memoize(dateTimeCacheKey('hour')), _dec6 = decorators.memoize(dateTimeCacheKey('minute')), _dec7 = decorators.memoize(dateTimeCacheKey('second')), _dec8 = decorators.memoize(dateTimeCacheKey('timePartsFallback')), (_class = class DateTimeParts { | ||
static getYear(date, timeZone) { | ||
if (isNaN(date.valueOf())) { | ||
throw new Error(`Unable to parse date: ${date} for timezone: ${timeZone}`); | ||
} | ||
class DateTimeParts {} | ||
const yearString = formatDate.formatDate(date, 'en', { | ||
timeZone, | ||
year: 'numeric' | ||
}); | ||
const sanitisedYearString = sanitiseDateString.sanitiseDateString(yearString); | ||
const year = parseInt(sanitisedYearString, 10); | ||
DateTimeParts.getYear = functionEnhancers.memoize((date, timeZone) => { | ||
if (isNaN(date.valueOf())) { | ||
throw new Error(`Unable to parse date: ${date} for timezone: ${timeZone}`); | ||
} | ||
if (isNaN(year)) { | ||
throw new Error(`Unable to parse year: '${yearString}'`); | ||
} | ||
const yearString = formatDate.formatDate(date, 'en', { | ||
timeZone, | ||
year: 'numeric' | ||
}); | ||
const sanitisedYearString = sanitiseDateString.sanitiseDateString(yearString); | ||
const year = parseInt(sanitisedYearString, 10); | ||
return year; | ||
if (isNaN(year)) { | ||
throw new Error(`Unable to parse year: '${yearString}'`); | ||
} | ||
static getMonth(date, timeZone) { | ||
const monthString = formatDate.formatDate(date, 'en', { | ||
timeZone, | ||
month: 'numeric' | ||
}); | ||
const sanitisedMonthString = sanitiseDateString.sanitiseDateString(monthString); | ||
const month = parseInt(sanitisedMonthString, 10); | ||
return year; | ||
}, dateTimeCacheKey('year')); | ||
DateTimeParts.getMonth = functionEnhancers.memoize((date, timeZone) => { | ||
const monthString = formatDate.formatDate(date, 'en', { | ||
timeZone, | ||
month: 'numeric' | ||
}); | ||
const sanitisedMonthString = sanitiseDateString.sanitiseDateString(monthString); | ||
const month = parseInt(sanitisedMonthString, 10); | ||
if (isNaN(month)) { | ||
throw new Error(`Unable to parse month: '${monthString}'`); | ||
} | ||
return month; | ||
if (isNaN(month)) { | ||
throw new Error(`Unable to parse month: '${monthString}'`); | ||
} | ||
static getDay(date, timeZone) { | ||
const dayString = formatDate.formatDate(date, 'en', { | ||
timeZone, | ||
day: 'numeric' | ||
}); | ||
const sanitisedDayString = sanitiseDateString.sanitiseDateString(dayString); | ||
const day = parseInt(sanitisedDayString, 10); | ||
return month; | ||
}, dateTimeCacheKey('month')); | ||
DateTimeParts.getDay = functionEnhancers.memoize((date, timeZone) => { | ||
const dayString = formatDate.formatDate(date, 'en', { | ||
timeZone, | ||
day: 'numeric' | ||
}); | ||
const sanitisedDayString = sanitiseDateString.sanitiseDateString(dayString); | ||
const day = parseInt(sanitisedDayString, 10); | ||
if (isNaN(day)) { | ||
throw new Error(`Unable to parse day: '${dayString}'`); | ||
} | ||
return day; | ||
if (isNaN(day)) { | ||
throw new Error(`Unable to parse day: '${dayString}'`); | ||
} | ||
static getWeekday(date, timeZone) { | ||
const weekdayString = formatDate.formatDate(date, 'en', { | ||
timeZone, | ||
weekday: 'long' | ||
}); | ||
const sanitisedWeekdayString = sanitiseDateString.sanitiseDateString(weekdayString); | ||
return getWeekdayValue(sanitisedWeekdayString); | ||
} | ||
return day; | ||
}, dateTimeCacheKey('day')); | ||
DateTimeParts.getWeekday = functionEnhancers.memoize((date, timeZone) => { | ||
const weekdayString = formatDate.formatDate(date, 'en', { | ||
timeZone, | ||
weekday: 'long' | ||
}); | ||
const sanitisedWeekdayString = sanitiseDateString.sanitiseDateString(weekdayString); | ||
return getWeekdayValue(sanitisedWeekdayString); | ||
}, dateTimeCacheKey('weekday')); | ||
DateTimeParts.getHour = functionEnhancers.memoize((date, timeZone) => { | ||
const hourString = formatDate.formatDate(date, 'en', { | ||
timeZone, | ||
hour12: false, | ||
hour: 'numeric' | ||
}); | ||
let hour = parseInt(hourString, 10); | ||
static getHour(date, timeZone) { | ||
const hourString = formatDate.formatDate(date, 'en', { | ||
timeZone, | ||
hour12: false, | ||
hour: 'numeric' | ||
}); | ||
let hour = parseInt(hourString, 10); | ||
if (isNaN(hour)) { | ||
hour = DateTimeParts.getTimePartsFallback(date, timeZone).hour; | ||
} | ||
return hour; | ||
if (isNaN(hour)) { | ||
hour = DateTimeParts.getTimePartsFallback(date, timeZone).hour; | ||
} | ||
static getMinute(date, timeZone) { | ||
const minuteString = formatDate.formatDate(date, 'en', { | ||
timeZone, | ||
minute: 'numeric' | ||
}); | ||
let minute = parseInt(minuteString, 10); | ||
return hour; | ||
}, dateTimeCacheKey('hour')); | ||
DateTimeParts.getMinute = functionEnhancers.memoize((date, timeZone) => { | ||
const minuteString = formatDate.formatDate(date, 'en', { | ||
timeZone, | ||
minute: 'numeric' | ||
}); | ||
let minute = parseInt(minuteString, 10); | ||
if (isNaN(minute)) { | ||
minute = DateTimeParts.getTimePartsFallback(date, timeZone).minute; | ||
} | ||
return minute; | ||
if (isNaN(minute)) { | ||
minute = DateTimeParts.getTimePartsFallback(date, timeZone).minute; | ||
} | ||
static getSecond(date, timeZone) { | ||
const secondString = formatDate.formatDate(date, 'en', { | ||
timeZone, | ||
second: 'numeric' | ||
}); | ||
let second = parseInt(secondString, 10); | ||
return minute; | ||
}, dateTimeCacheKey('minute')); | ||
DateTimeParts.getSecond = functionEnhancers.memoize((date, timeZone) => { | ||
const secondString = formatDate.formatDate(date, 'en', { | ||
timeZone, | ||
second: 'numeric' | ||
}); | ||
let second = parseInt(secondString, 10); | ||
if (isNaN(second)) { | ||
second = DateTimeParts.getTimePartsFallback(date, timeZone).second; | ||
} | ||
return second; | ||
if (isNaN(second)) { | ||
second = DateTimeParts.getTimePartsFallback(date, timeZone).second; | ||
} | ||
static getTimePartsFallback(date, timeZone) { | ||
const timeString = formatDate.formatDate(date, 'en', { | ||
timeZone, | ||
hour12: false, | ||
hour: '2-digit', | ||
minute: '2-digit', | ||
second: '2-digit' | ||
}); // In Microsoft Edge, Intl.DateTimeFormat returns invisible characters around the individual numbers | ||
return second; | ||
}, dateTimeCacheKey('second')); | ||
DateTimeParts.getTimePartsFallback = functionEnhancers.memoize((date, timeZone) => { | ||
const timeString = formatDate.formatDate(date, 'en', { | ||
timeZone, | ||
hour12: false, | ||
hour: '2-digit', | ||
minute: '2-digit', | ||
second: '2-digit' | ||
}); // In Microsoft Edge, Intl.DateTimeFormat returns invisible characters around the individual numbers | ||
const [dirtyHour, dirtyMinute, dirtySecond] = timeString.split(':'); | ||
const rawHour = new RegExp(TWO_DIGIT_REGEX).exec(dirtyHour); | ||
const rawMinute = new RegExp(TWO_DIGIT_REGEX).exec(dirtyMinute); | ||
const rawSecond = new RegExp(TWO_DIGIT_REGEX).exec(dirtySecond); | ||
const [dirtyHour, dirtyMinute, dirtySecond] = timeString.split(':'); | ||
const rawHour = new RegExp(TWO_DIGIT_REGEX).exec(dirtyHour); | ||
const rawMinute = new RegExp(TWO_DIGIT_REGEX).exec(dirtyMinute); | ||
const rawSecond = new RegExp(TWO_DIGIT_REGEX).exec(dirtySecond); | ||
if (rawHour != null && rawMinute != null && rawSecond != null) { | ||
const hour = parseInt(rawHour[1], 10); | ||
const minute = parseInt(rawMinute[1], 10); | ||
const second = parseInt(rawSecond[1], 10); | ||
return { | ||
hour, | ||
minute, | ||
second | ||
}; | ||
} | ||
throw new Error(`Unable to parse timeString: '${timeString}'`); | ||
if (rawHour != null && rawMinute != null && rawSecond != null) { | ||
const hour = parseInt(rawHour[1], 10); | ||
const minute = parseInt(rawMinute[1], 10); | ||
const second = parseInt(rawSecond[1], 10); | ||
return { | ||
hour, | ||
minute, | ||
second | ||
}; | ||
} | ||
}, (_rollupPluginBabelHelpers.applyDecoratedDescriptor(_class, "getYear", [_dec], Object.getOwnPropertyDescriptor(_class, "getYear"), _class), _rollupPluginBabelHelpers.applyDecoratedDescriptor(_class, "getMonth", [_dec2], Object.getOwnPropertyDescriptor(_class, "getMonth"), _class), _rollupPluginBabelHelpers.applyDecoratedDescriptor(_class, "getDay", [_dec3], Object.getOwnPropertyDescriptor(_class, "getDay"), _class), _rollupPluginBabelHelpers.applyDecoratedDescriptor(_class, "getWeekday", [_dec4], Object.getOwnPropertyDescriptor(_class, "getWeekday"), _class), _rollupPluginBabelHelpers.applyDecoratedDescriptor(_class, "getHour", [_dec5], Object.getOwnPropertyDescriptor(_class, "getHour"), _class), _rollupPluginBabelHelpers.applyDecoratedDescriptor(_class, "getMinute", [_dec6], Object.getOwnPropertyDescriptor(_class, "getMinute"), _class), _rollupPluginBabelHelpers.applyDecoratedDescriptor(_class, "getSecond", [_dec7], Object.getOwnPropertyDescriptor(_class, "getSecond"), _class), _rollupPluginBabelHelpers.applyDecoratedDescriptor(_class, "getTimePartsFallback", [_dec8], Object.getOwnPropertyDescriptor(_class, "getTimePartsFallback"), _class)), _class)); | ||
throw new Error(`Unable to parse timeString: '${timeString}'`); | ||
}, dateTimeCacheKey('timePartsFallback')); | ||
exports.getDateTimeParts = getDateTimeParts; |
@@ -1,1 +0,1 @@ | ||
module.exports = require("./build/cjs/index.js"); | ||
module.exports = require("./build/cjs/index.js"); |
{ | ||
"name": "@shopify/dates", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"license": "MIT", | ||
@@ -27,6 +27,6 @@ "description": "Lightweight date operations library", | ||
"dependencies": { | ||
"@shopify/decorators": "^3.0.1" | ||
"@shopify/function-enhancers": "^3.0.1" | ||
}, | ||
"devDependencies": { | ||
"@shopify/jest-dom-mocks": "^4.0.1" | ||
"@shopify/jest-dom-mocks": "^4.1.0" | ||
}, | ||
@@ -33,0 +33,0 @@ "files": [ |
@@ -12,3 +12,3 @@ # `@shopify/dates` | ||
```bash | ||
$ yarn add @shopify/dates | ||
yarn add @shopify/dates | ||
``` | ||
@@ -15,0 +15,0 @@ |
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
106084
157
1661
- Removed@shopify/decorators@^3.0.1
- Removed@shopify/decorators@3.0.1(transitive)