@amimpact/willy-utils 4.5.0 → 4.6.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.
- package/package.json +1 -1
- package/src/date.ts +34 -21
- package/src/misc.ts +4 -0
package/package.json
CHANGED
package/src/date.ts
CHANGED
|
@@ -25,11 +25,8 @@ const availableLocales = {
|
|
|
25
25
|
* @param {Object} dateObject
|
|
26
26
|
* @param {Object} options
|
|
27
27
|
*/
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
timezone: string;
|
|
31
|
-
timezone_type: number;
|
|
32
|
-
}
|
|
28
|
+
|
|
29
|
+
export type CraftDate = { date: string; timezone: string; timezone_type: number } | string;
|
|
33
30
|
|
|
34
31
|
export interface FormatCraftDateOptions {
|
|
35
32
|
/**
|
|
@@ -165,7 +162,7 @@ export const formatCraftDate = (dateObject: CraftDate, options: FormatCraftDateO
|
|
|
165
162
|
localeString = options.locale;
|
|
166
163
|
}
|
|
167
164
|
|
|
168
|
-
if (!dateObject
|
|
165
|
+
if (!dateObject) {
|
|
169
166
|
return '';
|
|
170
167
|
}
|
|
171
168
|
|
|
@@ -173,18 +170,27 @@ export const formatCraftDate = (dateObject: CraftDate, options: FormatCraftDateO
|
|
|
173
170
|
* De date uit Craft heeft het volgende format: "2019-11-14 08:00:00.000000"
|
|
174
171
|
* Safari begrijpt dit niet en geeft dan een NaN met new Date(date)
|
|
175
172
|
* Safari begrijpt het wel als alle - vervangen worden door /, en het laatste gedeelte achter de punt weg is
|
|
173
|
+
*
|
|
174
|
+
* Als het een string is, dan is het al een datum in het juiste format
|
|
176
175
|
*/
|
|
177
|
-
const dateToFormat =
|
|
176
|
+
const dateToFormat =
|
|
177
|
+
typeof dateObject === 'string'
|
|
178
|
+
? dateObject
|
|
179
|
+
: dateObject.date.replace(/-/g, '/').split('.')[0];
|
|
178
180
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
+
const dateRaw = new Date(dateToFormat);
|
|
182
|
+
const dateFormatted = format(new Date(dateToFormat), dateFormat, {
|
|
181
183
|
locale: availableLocales[localeString],
|
|
182
184
|
});
|
|
185
|
+
|
|
186
|
+
// Check of dateRaw een valide datum is, zo ja, dan geven we de geformatteerde datum terug, anders de originele dateToFormat
|
|
187
|
+
return isNaN(dateRaw.getTime()) ? dateToFormat : dateFormatted;
|
|
183
188
|
};
|
|
184
189
|
|
|
185
190
|
/**
|
|
186
191
|
* Datum formatteren naar "x minuten etc. geleden"
|
|
187
|
-
* @param {
|
|
192
|
+
* @param {CraftDate} dateObject
|
|
193
|
+
* @param {FormatDateFromNowOptions} options
|
|
188
194
|
*/
|
|
189
195
|
export const formatDateFromNow = (dateObject: CraftDate, options: FormatDateFromNowOptions) => {
|
|
190
196
|
const defaultOptions: FormatDateFromNowOptions = {
|
|
@@ -202,11 +208,17 @@ export const formatDateFromNow = (dateObject: CraftDate, options: FormatDateFrom
|
|
|
202
208
|
opts = (<any>Object).assign({}, defaultOptions, options);
|
|
203
209
|
}
|
|
204
210
|
|
|
205
|
-
if (!dateObject
|
|
211
|
+
if (!dateObject) {
|
|
206
212
|
return '';
|
|
207
213
|
}
|
|
208
214
|
|
|
209
|
-
|
|
215
|
+
// Het dateObject kan een string zijn of een object
|
|
216
|
+
// Als het een object is gaan we er vanuit dat het object een date en timezone key heeft
|
|
217
|
+
const isString = typeof dateObject === 'string';
|
|
218
|
+
|
|
219
|
+
const parsed = isString
|
|
220
|
+
? new Date(dateObject).toString()
|
|
221
|
+
: fromZonedTime(dateObject.date, dateObject.timezone);
|
|
210
222
|
|
|
211
223
|
/**
|
|
212
224
|
* Datum tonen voor ouder dan x tijd
|
|
@@ -215,7 +227,9 @@ export const formatDateFromNow = (dateObject: CraftDate, options: FormatDateFrom
|
|
|
215
227
|
opts.showDateForOld &&
|
|
216
228
|
isBefore(parsed, subDays(new Date(), opts.numberOfDaysShowDateForOld))
|
|
217
229
|
) {
|
|
218
|
-
|
|
230
|
+
const dateString = isString ? dateObject : dateObject.date;
|
|
231
|
+
const dateToFormat = dateString.replace(' ', 'T');
|
|
232
|
+
return format(new Date(dateToFormat), opts.showDateForOldFormat, {
|
|
219
233
|
locale: availableLocales[opts.locale],
|
|
220
234
|
});
|
|
221
235
|
} else {
|
|
@@ -242,18 +256,17 @@ export const formatDateFromNow = (dateObject: CraftDate, options: FormatDateFrom
|
|
|
242
256
|
* @param {date, timezone, timezone_type} dateObject
|
|
243
257
|
*/
|
|
244
258
|
export const formatNewDate = (dateObject: CraftDate) => {
|
|
245
|
-
if (!dateObject
|
|
259
|
+
if (!dateObject) {
|
|
246
260
|
return '';
|
|
247
261
|
}
|
|
248
262
|
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
* Safari begrijpt het wel als alle - vervangen worden door /, en het laatste gedeelte achter de punt weg is
|
|
253
|
-
*/
|
|
254
|
-
// const dateString = dateObject.date.replace(/-/g, '/').split('.')[0];
|
|
263
|
+
// Het dateObject kan een string zijn of een object
|
|
264
|
+
// Als het een object is gaan we er vanuit dat het object een date en timezone key heeft
|
|
265
|
+
const isString = typeof dateObject === 'string';
|
|
255
266
|
|
|
256
|
-
const parsed =
|
|
267
|
+
const parsed = isString
|
|
268
|
+
? new Date(dateObject).toString()
|
|
269
|
+
: fromZonedTime(dateObject.date, dateObject.timezone);
|
|
257
270
|
|
|
258
271
|
return new Date(parsed);
|
|
259
272
|
};
|