@inzombieland/nuxt-common 1.18.36 → 1.18.38

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/dist/module.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inzombieland/nuxt-common",
3
- "version": "1.18.36",
3
+ "version": "1.18.38",
4
4
  "configKey": "nuxt-common",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "0.6.0",
package/dist/module.mjs CHANGED
@@ -2,7 +2,7 @@ import { fileURLToPath } from 'node:url';
2
2
  import { defineNuxtModule, createResolver, addServerHandler, addImportsDir, addPlugin, addComponent } from '@nuxt/kit';
3
3
 
4
4
  const name = "@inzombieland/nuxt-common";
5
- const version = "1.18.36";
5
+ const version = "1.18.38";
6
6
 
7
7
  const module = defineNuxtModule({
8
8
  meta: {
@@ -1,5 +1,4 @@
1
- import { dateHelper } from "./date-helper.mjs";
2
- const isoExp = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)((-(\d{2}):(\d{2})|Z)?)$/gm;
1
+ const isoExp = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)((-(\d{2}):(\d{2})|Z)?)$/;
3
2
  const isISODate = (value) => {
4
3
  return typeof value === "string" && isoExp.test(value);
5
4
  };
@@ -40,7 +39,7 @@ export function convertToClient(data) {
40
39
  } else if (isObject(value)) {
41
40
  res[newKey] = convertToClient(value);
42
41
  } else if (isISODate(value)) {
43
- res[newKey] = dateHelper.stringToDate(value);
42
+ res[newKey] = stringToDate(value);
44
43
  } else if (value === null) {
45
44
  delete res[newKey];
46
45
  } else {
@@ -82,7 +81,7 @@ export function convertToServer(data) {
82
81
  } else if (isObject(value)) {
83
82
  res[newKey] = convertToServer(value);
84
83
  } else if (isDate(value)) {
85
- res[newKey] = dateHelper.dateFormat(value, "isoDateTime");
84
+ res[newKey] = dateToString(value);
86
85
  } else {
87
86
  res[newKey] = value;
88
87
  }
@@ -96,6 +95,31 @@ function parseData(data) {
96
95
  return data;
97
96
  }
98
97
  }
98
+ function stringToDate(dateString) {
99
+ if (typeof dateString !== "string" || dateString === "0001-01-01T00:00:00Z") {
100
+ return void 0;
101
+ }
102
+ const parts = isoExp.exec(dateString);
103
+ if (!parts) {
104
+ return new Date(Number.NaN);
105
+ }
106
+ const year = Number(parts[1]);
107
+ const month = Number(parts[2]) - 1;
108
+ const day = Number(parts[3]);
109
+ const hours = Number(parts[4]);
110
+ const minutes = Number(parts[5]);
111
+ const seconds = Number(parts[6]);
112
+ return parts.at(-1) === "Z" ? new Date(Date.UTC(year, month, day, hours, minutes, seconds)) : new Date(year, month, day, hours, minutes, seconds);
113
+ }
114
+ function dateToString(date) {
115
+ const year = date.getFullYear();
116
+ const month = String(date.getMonth() + 1).padStart(2, "0");
117
+ const day = String(date.getDate()).padStart(2, "0");
118
+ const hours = String(date.getHours()).padStart(2, "0");
119
+ const minutes = String(date.getMinutes()).padStart(2, "0");
120
+ const seconds = String(date.getSeconds()).padStart(2, "0");
121
+ return `${year}-${month}-${day}T${hours}:${minutes}:${seconds}`;
122
+ }
99
123
  export const apiHelper = { convertToClient, convertToServer };
100
124
  export const useApiHelper = () => {
101
125
  return apiHelper;
@@ -1,5 +1,4 @@
1
1
  export { useApiHelper } from "./api-helper";
2
- export { useDateHelper } from "./date-helper";
3
2
  export { useDevice } from "./device-helper";
4
3
  export { usePhoneHelper } from "./phone-helper";
5
4
  export { useStringHelper } from "./string-helper";
@@ -1,5 +1,4 @@
1
1
  export { useApiHelper } from "./api-helper.mjs";
2
- export { useDateHelper } from "./date-helper.mjs";
3
2
  export { useDevice } from "./device-helper.mjs";
4
3
  export { usePhoneHelper } from "./phone-helper.mjs";
5
4
  export { useStringHelper } from "./string-helper.mjs";
@@ -1,5 +1,4 @@
1
1
  export { apiHelper } from "./api-helper";
2
- export { dateHelper } from "./date-helper";
3
2
  export { deviceHelper, type Device } from "./device-helper";
4
3
  export { phoneHelper } from "./phone-helper";
5
4
  export { stringHelper } from "./string-helper";
@@ -1,5 +1,4 @@
1
1
  export { apiHelper } from "./api-helper.mjs";
2
- export { dateHelper } from "./date-helper.mjs";
3
2
  export { deviceHelper } from "./device-helper.mjs";
4
3
  export { phoneHelper } from "./phone-helper.mjs";
5
4
  export { stringHelper } from "./string-helper.mjs";
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inzombieland/core",
3
- "version": "1.18.36",
3
+ "version": "1.18.38",
4
4
  "type": "module",
5
5
  "license": "ISC",
6
6
  "main": "./index.mjs",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inzombieland/nuxt-common",
3
- "version": "1.18.36",
3
+ "version": "1.18.38",
4
4
  "license": "ISC",
5
5
  "type": "module",
6
6
  "exports": {
@@ -1,19 +0,0 @@
1
- export declare function stringToDate(dateString: unknown): Date | undefined;
2
- /**
3
- * Преобразование даты.
4
- * Например из "d mmmm:R yyyy" в "5 февраля 2020"
5
- * @param date - дата
6
- * @param format - формат даты
7
- * @param locale
8
- * @param dateTimeNow
9
- */
10
- export declare function dateFormat(// @SONAR_STOP@
11
- date?: Date, format?: string, locale?: string, dateTimeNow?: Date): string;
12
- export declare const dateHelper: {
13
- stringToDate: typeof stringToDate;
14
- dateFormat: typeof dateFormat;
15
- };
16
- export declare const useDateHelper: () => {
17
- stringToDate: typeof stringToDate;
18
- dateFormat: typeof dateFormat;
19
- };
@@ -1,233 +0,0 @@
1
- const isoExp = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)((-(\d{2}):(\d{2})|Z)?)$/;
2
- export function stringToDate(dateString) {
3
- if (typeof dateString === "string") {
4
- if (dateString === "0001-01-01T00:00:00Z") {
5
- return;
6
- }
7
- let date = new Date(Number.NaN);
8
- const parts = isoExp.exec(dateString);
9
- if (parts) {
10
- date = parts.at(-1) === "Z" ? new Date(
11
- Date.UTC(
12
- Number(parts[1]),
13
- Number(parts[2]) - 1,
14
- Number(parts[3]),
15
- Number(parts[4]),
16
- Number(parts[5]),
17
- Number(parts[6])
18
- )
19
- ) : new Date(
20
- Number(parts[1]),
21
- Number(parts[2]) - 1,
22
- Number(parts[3]),
23
- Number(parts[4]),
24
- Number(parts[5]),
25
- Number(parts[6])
26
- );
27
- }
28
- return date;
29
- }
30
- return void 0;
31
- }
32
- const masks = {
33
- default: "dd mmm yyyy",
34
- "default:R": "dd mmmm:R yyyy",
35
- "default:RC": "dd mmmm:R yyyy:C",
36
- defaultshortday: "d mmm yyyy",
37
- "defaultshortday:C": "d mmm yyyy:C",
38
- "defaultshortday:R": "d mmmm:R yyyy",
39
- "defaultshortday:RC": "d mmmm:R yyyy:C",
40
- shortDate: "m/d/yy",
41
- mediumDate: "mmm d, yyyy",
42
- longDate: "mmmm d, yyyy",
43
- fullDate: "dddd, mmmm d, yyyy",
44
- shortTime: "h:MM TT",
45
- mediumTime: "h:MM:ss TT",
46
- longTime: "h:MM:ss TT Z",
47
- isoDate: "yyyy-mm-dd",
48
- isoTime: "HH:MM:ss",
49
- isoDateTime: "yyyy-mm-dd'T'HH:MM:ss",
50
- isoUtcDateTime: "UTC:yyyy-mm-dd'T'HH:MM:ss'Z'",
51
- time: "HH:MM",
52
- dateTime: "dd.mm.yyyy HH:MM",
53
- clever: "clever"
54
- };
55
- const i18n = {
56
- en: {
57
- dayNames: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
58
- dayNamesShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
59
- monthName: [
60
- "January",
61
- "February",
62
- "March",
63
- "April",
64
- "May",
65
- "June",
66
- "July",
67
- "August",
68
- "September",
69
- "October",
70
- "November",
71
- "December"
72
- ],
73
- yesterday: "'Yesterday'",
74
- today: "'Today'"
75
- },
76
- ru: {
77
- dayNames: ["\u0432\u043E\u0441\u043A\u0440\u0435\u0441\u0435\u043D\u044C\u0435", "\u043F\u043E\u043D\u0435\u0434\u0435\u043B\u044C\u043D\u0438\u043A", "\u0432\u0442\u043E\u0440\u043D\u0438\u043A", "\u0441\u0440\u0435\u0434\u0430", "\u0447\u0435\u0442\u0432\u0435\u0440\u0433", "\u043F\u044F\u0442\u043D\u0438\u0446\u0430", "\u0441\u0443\u0431\u0431\u043E\u0442\u0430"],
78
- dayNamesShort: ["\u0412\u0441", "\u041F\u043D", "\u0412\u0442", "\u0421\u0440", "\u0427\u0442", "\u041F\u0442", "\u0421\u0431"],
79
- monthName: [
80
- "\u044F\u043D\u0432\u0430\u0440\u044C",
81
- "\u0444\u0435\u0432\u0440\u0430\u043B\u044C",
82
- "\u043C\u0430\u0440\u0442",
83
- "\u0430\u043F\u0440\u0435\u043B\u044C",
84
- "\u043C\u0430\u0439",
85
- "\u0438\u044E\u043D\u044C",
86
- "\u0438\u044E\u043B\u044C",
87
- "\u0430\u0432\u0433\u0443\u0441\u0442",
88
- "\u0441\u0435\u043D\u0442\u044F\u0431\u0440\u044C",
89
- "\u043E\u043A\u0442\u044F\u0431\u0440\u044C",
90
- "\u043D\u043E\u044F\u0431\u0440\u044C",
91
- "\u0434\u0435\u043A\u0430\u0431\u0440\u044C"
92
- ],
93
- yesterday: "'\u0412\u0447\u0435\u0440\u0430'",
94
- today: "'\u0421\u0435\u0433\u043E\u0434\u043D\u044F'"
95
- }
96
- };
97
- const token = /d{1,4}:?C?|m{1,4}:?[RC]?|yy(?:yy|y)?:?[RC]?|([HhMsTt])\1?|[LloZ]|"[^"]*"|'[^']*'/g;
98
- const timezone = /\b(?:[PMCEA][SDP]T|(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time|(?:GMT|UTC)(?:[-+]\d{4})?)\b/g;
99
- const timezoneClip = /[^-+\dA-Z]/g;
100
- const pad = (value, len = 2) => {
101
- let val = String(value);
102
- while (val.length < len) {
103
- val = `0${val}`;
104
- }
105
- return val;
106
- };
107
- const createOnlyDate = (date) => {
108
- const d = new Date(date);
109
- d.setHours(0, 0, 0, 0);
110
- return d;
111
- };
112
- const countCalendarDayBetween = (date1, date2) => {
113
- const day1 = createOnlyDate(date1);
114
- const day2 = createOnlyDate(date2);
115
- const dayCount = (day1.getTime() - day2.getTime()) / (24 * 60 * 60 * 1e3);
116
- return Math.abs(dayCount);
117
- };
118
- const checkIsYesterday = (now, date) => {
119
- return now > date && countCalendarDayBetween(now, date) === 1;
120
- };
121
- const getDayName = (day, locale, short = false) => {
122
- return short ? i18n[locale].dayNamesShort[day] : i18n[locale].dayNames[day];
123
- };
124
- const monthDeclination = (month, locale) => {
125
- return month === 3 || month === 8 ? `${i18n[locale].monthName[month - 1]}\u0430` : `${i18n[locale].monthName[month - 1].slice(0, -1)}\u044F`;
126
- };
127
- const getMonthName = (month, locale, countChar = 0, declination = false) => {
128
- const monthName = locale === "ru" && declination ? monthDeclination(month, locale) : i18n[locale].monthName[month - 1];
129
- if (countChar && countChar > 0) {
130
- return monthName.slice(0, 3);
131
- }
132
- return monthName;
133
- };
134
- const getMask = (format, locale, isCurrentDay, isYesterday, isCurrentYear) => {
135
- const defaultFormat = {
136
- en: "mmmm d, yyyy",
137
- ru: "d mmmm:R yyyy"
138
- };
139
- const formats = {
140
- default: defaultFormat,
141
- get clever() {
142
- if (isCurrentDay) {
143
- return {
144
- en: `${i18n.en.today} 'at' H:MM TT`,
145
- ru: `${i18n.ru.today}, HH:MM`
146
- };
147
- } else if (isYesterday) {
148
- return {
149
- en: `${i18n.en.yesterday} 'at' H:MM TT`,
150
- ru: `${i18n.ru.yesterday}, HH:MM`
151
- };
152
- } else if (isCurrentYear) {
153
- return {
154
- en: "mmmm d",
155
- ru: "d mmmm:R"
156
- };
157
- } else {
158
- return defaultFormat;
159
- }
160
- }
161
- };
162
- let mask = String(masks[format] ?? (format || masks.default));
163
- if (mask.startsWith("UTC:")) {
164
- mask = mask.slice(4);
165
- }
166
- if (formats[format]?.[locale]) {
167
- return formats[format]?.[locale] ?? "";
168
- }
169
- return mask;
170
- };
171
- export function dateFormat(date = /* @__PURE__ */ new Date(), format = "default", locale = "en", dateTimeNow) {
172
- date = date ? new Date(date) : /* @__PURE__ */ new Date();
173
- if (Number.isNaN(date)) {
174
- throw new SyntaxError("invalid date");
175
- }
176
- const utc = (masks[format] ?? format).startsWith("UTC:");
177
- const now = dateTimeNow ?? /* @__PURE__ */ new Date();
178
- const currentYear = utc ? now.getUTCFullYear() : now.getFullYear();
179
- const currentMonth = utc ? now.getUTCMonth() : now.getMonth();
180
- const currentDate = utc ? now.getUTCDate() : now.getDate();
181
- const isYesterday = checkIsYesterday(now, date);
182
- const d = utc ? date.getUTCDate() : date.getDate();
183
- const D = utc ? date.getUTCDay() : date.getDay();
184
- const m = utc ? date.getUTCMonth() : date.getMonth();
185
- const y = utc ? date.getUTCFullYear() : date.getFullYear();
186
- const isCurrentYear = y === currentYear;
187
- const isCurrentDay = y === currentYear && m === currentMonth && d === currentDate;
188
- const H = utc ? date.getUTCHours() : date.getHours();
189
- const M = utc ? date.getUTCMinutes() : date.getMinutes();
190
- const s = utc ? date.getUTCSeconds() : date.getSeconds();
191
- const L = utc ? date.getUTCMilliseconds() : date.getMilliseconds();
192
- const o = utc ? 0 : date.getTimezoneOffset();
193
- const flags = {
194
- d,
195
- dd: pad(d),
196
- ddd: getDayName(D, locale, true),
197
- dddd: getDayName(D, locale),
198
- m: m + 1,
199
- mm: pad(m + 1),
200
- mmm: getMonthName(m + 1, locale, 3),
201
- mmmm: getMonthName(m + 1, locale),
202
- "mmmm:R": getMonthName(m + 1, locale, 0, true),
203
- "mm:C": isYesterday || isCurrentDay ? "" : pad(m + 1),
204
- "yy:C": isCurrentYear ? "" : String(y).slice(2),
205
- yy: String(y).slice(2),
206
- "yyyy:C": isCurrentYear ? "" : y,
207
- yyyy: y,
208
- yyy: y,
209
- h: H % 12 || 12,
210
- hh: pad(H % 12 || 12),
211
- H,
212
- HH: pad(H),
213
- M,
214
- MM: pad(M),
215
- s,
216
- ss: pad(s),
217
- l: pad(L, 3),
218
- L: pad(L > 99 ? Math.round(L / 10) : L),
219
- tt: H < 12 ? "am" : "pm",
220
- TT: H < 12 ? "AM" : "PM",
221
- Z: utc ? "UTC" : (String(date).match(timezone) || [""]).pop()?.replace(timezoneClip, ""),
222
- o: (o > 0 ? "-" : "+") + pad(Math.floor(Math.abs(o) / 60) * 100 + Math.abs(o) % 60, 4)
223
- };
224
- const mask = getMask(format, locale, isCurrentDay, isYesterday, isCurrentYear);
225
- const parsedDate = mask.replaceAll(token, ($0) => {
226
- return $0 in flags ? String(flags[$0]) : $0.slice(1, -1);
227
- });
228
- return parsedDate.trim();
229
- }
230
- export const dateHelper = { stringToDate, dateFormat };
231
- export const useDateHelper = () => {
232
- return dateHelper;
233
- };