@talend/utils
Advanced tools
Comparing version
# @talend/utils | ||
## 2.9.0 | ||
### Minor Changes | ||
- 1bc49cd: fix(): Reduce package size by fixing date-fns import | ||
- 424544a: chore: upgrade date-fns to 2.x and fix | ||
## 2.8.0 | ||
@@ -4,0 +11,0 @@ |
@@ -7,4 +7,4 @@ "use strict"; | ||
exports.buildWeeks = buildWeeks; | ||
var _start_of_week = _interopRequireDefault(require("date-fns/start_of_week")); | ||
var _add_days = _interopRequireDefault(require("date-fns/add_days")); | ||
var _addDays = _interopRequireDefault(require("date-fns/addDays")); | ||
var _startOfWeek = _interopRequireDefault(require("date-fns/startOfWeek")); | ||
var _lodash = require("lodash"); | ||
@@ -18,8 +18,8 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
const firstDateOfMonth = new Date(year, monthIndex); | ||
const firstDateOfCalendar = (0, _start_of_week.default)(firstDateOfMonth, { | ||
const firstDateOfCalendar = (0, _startOfWeek.default)(firstDateOfMonth, { | ||
weekStartsOn: firstDayOfWeek | ||
}); | ||
const dates = new Array(7 * 6).fill(0).map((_, i) => (0, _add_days.default)(firstDateOfCalendar, i)); | ||
const dates = new Array(7 * 6).fill(0).map((_, i) => (0, _addDays.default)(firstDateOfCalendar, i)); | ||
return (0, _lodash.chunk)(dates, 7); | ||
} | ||
//# sourceMappingURL=generator.js.map |
@@ -45,2 +45,3 @@ import * as generator from './generator'; | ||
export declare function convertToTimeZone(date: DateFnsFormatInput, options: ConversionOptions): Date; | ||
export declare function formatToUnicode(formatString: string): string; | ||
/** | ||
@@ -47,0 +48,0 @@ * Returns the formatted date string in the given format, after converting it to the given time zone. |
@@ -14,6 +14,7 @@ "use strict"; | ||
exports.formatToTimeZone = formatToTimeZone; | ||
exports.formatToUnicode = formatToUnicode; | ||
exports.getUTCOffset = getUTCOffset; | ||
exports.timeZoneExists = timeZoneExists; | ||
var _format = _interopRequireDefault(require("date-fns/format")); | ||
var _parse = _interopRequireDefault(require("date-fns/parse")); | ||
var _parseISO = _interopRequireDefault(require("date-fns/parseISO")); | ||
var generator = _interopRequireWildcard(require("./generator")); | ||
@@ -111,3 +112,3 @@ function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); } | ||
function convertToLocalTime(date, options) { | ||
const parsedDate = (0, _parse.default)(date); | ||
const parsedDate = (0, _parseISO.default)(new Date(date).toISOString()); | ||
const offset = getUTCOffset(options.timeZone) + parsedDate.getTimezoneOffset(); | ||
@@ -131,3 +132,3 @@ return new Date(parsedDate.getTime() - offset * 60 * 1000); | ||
} = options; | ||
const parsedDate = (0, _parse.default)(date); | ||
const parsedDate = (0, _parseISO.default)(new Date(date).toISOString()); | ||
let offset = getUTCOffset(timeZone, parsedDate) + parsedDate.getTimezoneOffset(); | ||
@@ -140,2 +141,15 @@ if (sourceTimeZone) { | ||
} | ||
function formatToUnicode(formatString) { | ||
// See: https://date-fns.org/v2.16.1/docs/format | ||
return formatString | ||
// Characters are now escaped using single quote symbols (') instead of square brackets. | ||
.replace(/\[T\]/g, () => "'T'") | ||
// See: https://date-fns.org/docs/Unicode-Tokens | ||
// Replace years YYYY to yyyy | ||
.replace(/YYYY/g, () => 'yyyy') | ||
// Replace days of the month DD to dd | ||
.replace(/DD/g, () => 'dd') | ||
// Replace days of the week ddd to EEE | ||
.replace(/ddd/g, () => 'EEE'); | ||
} | ||
@@ -153,5 +167,8 @@ /** | ||
const dateConvertedToTimezone = convertToTimeZone(date, options); | ||
const unicodeFormatedString = formatToUnicode(formatString); | ||
// Replace timezone token(s) in the string format with timezone values, since format() will use local timezone | ||
const dateFnsFormatWithTimeZoneValue = formatTimeZoneTokens(formatString, options.timeZone); | ||
const dateFnsFormatWithTimeZoneValue = formatTimeZoneTokens(unicodeFormatedString, options.timeZone) | ||
// Avoid error: "Format string contains an unescaped latin alphabet character `Z`" | ||
.replace(/\[Z\]/g, () => "'Z'"); | ||
return (0, _format.default)(dateConvertedToTimezone, dateFnsFormatWithTimeZoneValue, options); | ||
@@ -236,3 +253,4 @@ } | ||
function format(date, dateOption, lang) { | ||
return new Intl.DateTimeFormat(lang, options[dateOption]).format((0, _parse.default)(date)); | ||
const parsedDate = (0, _parseISO.default)(new Date(date).toISOString()); | ||
return new Intl.DateTimeFormat(lang, options[dateOption]).format(parsedDate); | ||
} | ||
@@ -239,0 +257,0 @@ const buildWeeks = exports.buildWeeks = generator.buildWeeks; |
{ | ||
"name": "@talend/utils", | ||
"version": "2.8.0", | ||
"version": "2.9.0", | ||
"description": "Various utilities", | ||
@@ -23,14 +23,14 @@ "main": "lib/index.js", | ||
"devDependencies": { | ||
"@talend/eslint-config": "^13.0.0", | ||
"@talend/eslint-plugin": "^1.0.1", | ||
"@talend/scripts-core": "^16.1.1", | ||
"@talend/scripts-config-jest": "^13.0.0", | ||
"@talend/scripts-config-stylelint": "^4.0.0", | ||
"@talend/scripts-config-typescript": "^11.1.0", | ||
"@types/date-fns": "^0.0.2", | ||
"@types/lodash": "^4.14.201", | ||
"@talend/eslint-config": "^13.0.1", | ||
"@talend/eslint-plugin": "^1.1.0", | ||
"@talend/scripts-core": "^16.3.1", | ||
"@talend/scripts-config-jest": "^14.0.0", | ||
"@talend/scripts-config-stylelint": "^4.1.0", | ||
"@talend/scripts-config-typescript": "^11.2.0", | ||
"@types/date-fns": "^2.6.0", | ||
"@types/lodash": "^4.14.202", | ||
"cross-env": "^7.0.3" | ||
}, | ||
"dependencies": { | ||
"date-fns": "^1.30.1", | ||
"date-fns": "^2.30.0", | ||
"lodash": "^4.17.21" | ||
@@ -37,0 +37,0 @@ }, |
import dateFnsFormat from 'date-fns/format'; | ||
import enGB from 'date-fns/locale/en-GB'; | ||
import { | ||
@@ -140,3 +142,2 @@ convertToLocalTime, | ||
// given | ||
const mockLocal = { format: () => {} }; | ||
const dateObj = new Date('2020-12-20, 20:00'); | ||
@@ -146,3 +147,3 @@ const formatString = 'ddd YYYY-MM-DD HH:mm:ss'; | ||
timeZone: timeZones['UTC+5'], | ||
locale: mockLocal, | ||
locale: enGB, | ||
}; | ||
@@ -158,3 +159,3 @@ | ||
expect.objectContaining({ | ||
locale: mockLocal, | ||
locale: enGB, | ||
}), | ||
@@ -161,0 +162,0 @@ ); |
@@ -1,3 +0,3 @@ | ||
import startOfWeek from 'date-fns/start_of_week'; | ||
import addDays from 'date-fns/add_days'; | ||
import addDays from 'date-fns/addDays'; | ||
import startOfWeek from 'date-fns/startOfWeek'; | ||
import chunk from 'lodash/chunk'; | ||
@@ -4,0 +4,0 @@ |
import dateFnsFormat from 'date-fns/format'; | ||
import parse from 'date-fns/parse'; | ||
import parseISO from 'date-fns/parseISO'; | ||
import * as generator from './generator'; | ||
@@ -103,3 +104,3 @@ | ||
export function convertToLocalTime(date: DateFnsFormatInput, options: ConversionOptions): Date { | ||
const parsedDate = parse(date); | ||
const parsedDate = parseISO(new Date(date).toISOString()); | ||
const offset = getUTCOffset(options.timeZone) + parsedDate.getTimezoneOffset(); | ||
@@ -122,3 +123,3 @@ | ||
const parsedDate = parse(date); | ||
const parsedDate = parseISO(new Date(date).toISOString()); | ||
@@ -135,2 +136,18 @@ let offset = getUTCOffset(timeZone, parsedDate) + parsedDate.getTimezoneOffset(); | ||
export function formatToUnicode(formatString: string): string { | ||
// See: https://date-fns.org/v2.16.1/docs/format | ||
return ( | ||
formatString | ||
// Characters are now escaped using single quote symbols (') instead of square brackets. | ||
.replace(/\[T\]/g, () => "'T'") | ||
// See: https://date-fns.org/docs/Unicode-Tokens | ||
// Replace years YYYY to yyyy | ||
.replace(/YYYY/g, () => 'yyyy') | ||
// Replace days of the month DD to dd | ||
.replace(/DD/g, () => 'dd') | ||
// Replace days of the week ddd to EEE | ||
.replace(/ddd/g, () => 'EEE') | ||
); | ||
} | ||
/** | ||
@@ -152,4 +169,11 @@ * Returns the formatted date string in the given format, after converting it to the given time zone. | ||
const unicodeFormatedString = formatToUnicode(formatString); | ||
// Replace timezone token(s) in the string format with timezone values, since format() will use local timezone | ||
const dateFnsFormatWithTimeZoneValue = formatTimeZoneTokens(formatString, options.timeZone); | ||
const dateFnsFormatWithTimeZoneValue = formatTimeZoneTokens( | ||
unicodeFormatedString, | ||
options.timeZone, | ||
) | ||
// Avoid error: "Format string contains an unescaped latin alphabet character `Z`" | ||
.replace(/\[Z\]/g, () => "'Z'"); | ||
@@ -226,3 +250,4 @@ return dateFnsFormat(dateConvertedToTimezone, dateFnsFormatWithTimeZoneValue, options); | ||
export function format(date: DateFnsFormatInput, dateOption: string, lang: string): string { | ||
return new Intl.DateTimeFormat(lang, options[dateOption]).format(parse(date)); | ||
const parsedDate = parseISO(new Date(date).toISOString()); | ||
return new Intl.DateTimeFormat(lang, options[dateOption]).format(parsedDate); | ||
} | ||
@@ -229,0 +254,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
85168
4.18%1377
3.07%+ Added
+ Added
- Removed
Updated