@amimpact/willy-utils 4.8.0 → 4.8.2

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 (2) hide show
  1. package/package.json +1 -1
  2. package/src/date.ts +27 -7
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amimpact/willy-utils",
3
- "version": "4.8.0",
3
+ "version": "4.8.2",
4
4
  "description": "Javascript utils",
5
5
  "scripts": {
6
6
  "publish:prerelease": "npm version prerelease --preid beta && npm publish --tag beta",
package/src/date.ts CHANGED
@@ -19,6 +19,22 @@ const availableLocales = {
19
19
  nl: nl,
20
20
  };
21
21
 
22
+ /**
23
+ * Helper om de locale opties mee te geven aan date-fns
24
+ * Returns een object met de locale of een leeg object
25
+ * Dit moet omdat als EN de locale is er geen locale meegegeven hoeft te worden
26
+ */
27
+ const getLocaleOptions = (localeString: string) => {
28
+ // Alleen locales met daadwerkelijk een locale meegeven
29
+ if (availableLocales?.[localeString]?.code?.length) {
30
+ return {
31
+ locale: availableLocales[localeString],
32
+ };
33
+ }
34
+
35
+ return {};
36
+ };
37
+
22
38
  /**
23
39
  * Dates uit Craft mooi formatteren
24
40
  *
@@ -179,9 +195,11 @@ export const formatCraftDate = (dateObject: CraftDate, options: FormatCraftDateO
179
195
  : dateObject.date.replace(/-/g, '/').split('.')[0];
180
196
 
181
197
  const dateRaw = new Date(dateToFormat);
182
- const dateFormatted = format(new Date(dateToFormat), dateFormat, {
183
- locale: availableLocales[localeString],
184
- });
198
+ const dateFormatted = format(
199
+ new Date(dateToFormat),
200
+ dateFormat,
201
+ getLocaleOptions(localeString),
202
+ );
185
203
 
186
204
  // Check of dateRaw een valide datum is, zo ja, dan geven we de geformatteerde datum terug, anders de originele dateToFormat
187
205
  return isNaN(dateRaw.getTime()) ? dateToFormat : dateFormatted;
@@ -229,14 +247,16 @@ export const formatDateFromNow = (dateObject: CraftDate, options: FormatDateFrom
229
247
  ) {
230
248
  const dateString = isString ? dateObject : dateObject.date;
231
249
  const dateToFormat = dateString.replace(' ', 'T');
232
- return format(new Date(dateToFormat), opts.showDateForOldFormat, {
233
- locale: availableLocales[opts.locale],
234
- });
250
+ return format(
251
+ new Date(dateToFormat),
252
+ opts.showDateForOldFormat,
253
+ getLocaleOptions(opts.locale),
254
+ );
235
255
  } else {
236
256
  const formatDistanceOptions = {
237
257
  includeSeconds: opts.includeSeconds,
238
258
  addSuffix: opts.addSuffix,
239
- locale: availableLocales[opts.locale],
259
+ ...getLocaleOptions(opts.locale),
240
260
  };
241
261
 
242
262
  /**