@opengis/fastify-table 1.3.21 → 1.3.23

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 (34) hide show
  1. package/index.js +17 -6
  2. package/package.json +6 -3
  3. package/server/helpers/format/formatAuto.js +13 -0
  4. package/server/helpers/format/formatDate.js +258 -0
  5. package/server/helpers/format/formatDigit.js +21 -0
  6. package/server/helpers/format/formatNum.js +361 -0
  7. package/server/helpers/format/formatNumber.js +54 -0
  8. package/server/helpers/format/formatRelative.js +106 -0
  9. package/server/helpers/format/formatUnit.js +38 -0
  10. package/server/helpers/format/num_format.js +42 -0
  11. package/server/helpers/format/set.js +26 -0
  12. package/server/helpers/funcs/_math.js +50 -0
  13. package/server/helpers/funcs/contentList.js +58 -0
  14. package/server/helpers/funcs/empty.js +21 -0
  15. package/server/helpers/funcs/ifCond.js +106 -0
  16. package/server/helpers/funcs/ifCondAnd.js +96 -0
  17. package/server/helpers/funcs/ifCondOr.js +98 -0
  18. package/server/helpers/funcs/inc.js +21 -0
  19. package/server/helpers/funcs/json.js +3 -0
  20. package/server/helpers/funcs/qrcode.js +66 -0
  21. package/server/helpers/funcs/round.js +28 -0
  22. package/server/helpers/funcs/select.js +50 -0
  23. package/server/helpers/index.js +70 -3
  24. package/server/helpers/string/coalesce.js +31 -0
  25. package/server/helpers/string/concat.js +28 -0
  26. package/server/helpers/string/split.js +20 -0
  27. package/server/helpers/string/str_replace.js +61 -0
  28. package/server/helpers/string/substr.js +32 -0
  29. package/server/helpers/string/translit.js +23 -0
  30. package/server/helpers/string/utils/alphabet.js +76 -0
  31. package/server/plugins/crud/funcs/utils/logChanges.js +1 -1
  32. package/server/plugins/pg/funcs/getMeta.js +1 -1
  33. package/server/plugins/table/funcs/metaFormat/index.js +1 -1
  34. package/utils.js +2 -2
package/index.js CHANGED
@@ -4,10 +4,16 @@ import { fileURLToPath } from 'node:url';
4
4
 
5
5
  import config from './config.js';
6
6
 
7
+ // hb
8
+ import helper from './server/helpers/index.js';
9
+ import handlebarsSync from 'handlebars';
10
+ import promisedHandlebars from 'promised-handlebars';
11
+ const handlebars = promisedHandlebars(handlebarsSync);
12
+
7
13
  // plugins
8
14
  import cronPlugin from './server/plugins/cron/index.js';
9
15
  import crudPlugin from './server/plugins/crud/index.js';
10
- // hook, logger, migration - utils only
16
+
11
17
  import pgPlugin from './server/plugins/pg/index.js';
12
18
  import policyPlugin from './server/plugins/policy/index.js';
13
19
  import metricPlugin from './server/plugins/metric/index.js';
@@ -24,9 +30,9 @@ import propertiesRoutes from './server/routes/properties/index.js';
24
30
  import tableRoutes from './server/routes/table/index.js';
25
31
  import utilRoutes from './server/routes/util/index.js';
26
32
 
27
- import {
28
- addTemplateDir, execMigrations,
29
- } from './utils.js';
33
+ // utils
34
+ import addTemplateDir from './server/plugins/table/funcs/addTemplateDir.js';
35
+ import execMigrations from './server/plugins/migration/exec.migrations.js';
30
36
 
31
37
  // core templates && cls
32
38
  const filename = fileURLToPath(import.meta.url);
@@ -40,7 +46,7 @@ async function plugin(fastify, opt) {
40
46
 
41
47
  // independent npm start / unit test
42
48
  if (!fastify.config) {
43
- fastify.decorate('config', config);
49
+ // fastify.decorate('config', config);
44
50
  }
45
51
 
46
52
  fastify.register(import('@fastify/sensible'), {
@@ -51,7 +57,7 @@ async function plugin(fastify, opt) {
51
57
  errorHandler: false,
52
58
  });
53
59
 
54
- fastify.register(import('@opengis/fastify-hb'));
60
+ helper(handlebars); // fastify-hb
55
61
 
56
62
  // core migrations (second argument for core only)
57
63
  execMigrations(path.join(cwd, 'server/migrations'), true).catch(err => console.log(err));
@@ -84,3 +90,8 @@ async function plugin(fastify, opt) {
84
90
  fastify.get('/api/test-proxy', {}, (req) => req.headers);
85
91
  }
86
92
  export default fp(plugin);
93
+
94
+ export {
95
+ handlebars,
96
+ handlebarsSync,
97
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengis/fastify-table",
3
- "version": "1.3.21",
3
+ "version": "1.3.23",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "keywords": [
@@ -22,6 +22,7 @@
22
22
  "scripts": {
23
23
  "lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix --ignore-path .gitignore",
24
24
  "test": "node --test",
25
+ "test:helpers": "node --test .\\test\\helpers",
25
26
  "docs:dev": "vitepress dev docs",
26
27
  "docs:build": "vitepress build docs",
27
28
  "docs:preview": "vitepress preview docs",
@@ -31,7 +32,6 @@
31
32
  "@fastify/http-proxy": "9.5.0",
32
33
  "@fastify/sensible": "^5.0.0",
33
34
  "@fastify/url-data": "5.4.0",
34
- "@opengis/fastify-hb": "^1.7.3",
35
35
  "fastify": "^4.26.1",
36
36
  "fastify-plugin": "^4.0.0",
37
37
  "ioredis": "5.3.2",
@@ -39,7 +39,10 @@
39
39
  "pg": "8.11.3",
40
40
  "pino": "9.5.0",
41
41
  "pino-abstract-transport": "2.0.0",
42
- "uglify-js": "3.19.3"
42
+ "uglify-js": "3.19.3",
43
+ "handlebars": "^4.7.8",
44
+ "promised-handlebars": "^2.0.1",
45
+ "qrcode": "^1.5.4"
43
46
  },
44
47
  "devDependencies": {
45
48
  "@panzoom/panzoom": "^4.5.1",
@@ -0,0 +1,13 @@
1
+ import formatDate from './formatDate.js'
2
+
3
+ export default function formatAuto(data, options = {}) {
4
+ const format = options.hash?.format || 'text';
5
+ const auto = { true: 'так', false: 'ні' };
6
+ if (auto[data]) return auto[data];
7
+ if (['date'].includes(format)) {
8
+ return formatDate(data, options);
9
+ }
10
+
11
+ if (typeof data === 'object') { return null; }
12
+ return data;
13
+ };
@@ -0,0 +1,258 @@
1
+ /* eslint-disable no-param-reassign */
2
+ const monthUa = {
3
+ 0: 'січня',
4
+ 1: 'лютого',
5
+ 2: 'березня',
6
+ 3: 'квітня',
7
+ 4: 'травня',
8
+ 5: 'червня',
9
+ 6: 'липня',
10
+ 7: 'серпня',
11
+ 8: 'вересня',
12
+ 9: 'жовтня',
13
+ 10: 'листопада',
14
+ 11: 'грудня',
15
+ };
16
+ const monthRu = {
17
+ 0: 'января',
18
+ 1: 'февраля',
19
+ 2: 'марта',
20
+ 3: 'апреля',
21
+ 4: 'мая',
22
+ 5: 'июня',
23
+ 6: 'июля',
24
+ 7: 'авгувта',
25
+ 8: 'сентября',
26
+ 9: 'октября',
27
+ 10: 'ноября',
28
+ 11: 'декабря',
29
+ };
30
+ const monthEn = {
31
+ 0: 'january',
32
+ 1: 'february',
33
+ 2: 'march',
34
+ 3: 'april',
35
+ 4: 'may',
36
+ 5: 'june',
37
+ 6: 'july',
38
+ 7: 'august',
39
+ 8: 'september',
40
+ 9: 'october',
41
+ 10: 'november',
42
+ 11: 'december',
43
+ };
44
+ const weekDayUa = {
45
+ 1: 'Понеділок',
46
+ 2: 'Вівторок',
47
+ 3: 'Середа',
48
+ 4: 'Четвер',
49
+ 5: 'Пёятниця',
50
+ 6: 'Субота',
51
+ 0: 'Неділя',
52
+ };
53
+ const mthShort = {
54
+ ua: {
55
+ 0: 'Січ',
56
+ 1: 'Лют',
57
+ 2: 'Бер',
58
+ 3: 'Кві',
59
+ 4: 'Тра',
60
+ 5: 'Чер',
61
+ 6: 'Лип',
62
+ 7: 'Сер',
63
+ 8: 'Вер',
64
+ 9: 'Жов',
65
+ 10: 'Лис',
66
+ 11: 'Гру',
67
+ },
68
+ ru: {
69
+ 0: 'Янв',
70
+ 1: 'Фев',
71
+ 2: 'Мар',
72
+ 3: 'Апр',
73
+ 4: 'Май',
74
+ 5: 'Июн',
75
+ 6: 'Июл',
76
+ 7: 'Авг',
77
+ 8: 'Сен',
78
+ 9: 'Окт',
79
+ 10: 'Ноя',
80
+ 11: 'Дек',
81
+ },
82
+ };
83
+ const mth = {
84
+ ua: {
85
+ 0: 'Січень',
86
+ 1: 'Лютий',
87
+ 2: 'Березень',
88
+ 3: 'Квітень',
89
+ 4: 'Травень',
90
+ 5: 'Червень',
91
+ 6: 'Липень',
92
+ 7: 'Серпень',
93
+ 8: 'Вересень',
94
+ 9: 'Жовтень',
95
+ 10: 'Листопад',
96
+ 11: 'Грудень',
97
+ },
98
+ ru: {
99
+ 0: 'Январь',
100
+ 1: 'Февраль',
101
+ 2: 'Март',
102
+ 3: 'Апрель',
103
+ 4: 'Май',
104
+ 5: 'Июнь',
105
+ 6: 'Июль',
106
+ 7: 'Август',
107
+ 8: 'Сентябрь',
108
+ 9: 'Октябрь',
109
+ 10: 'Ноябрь',
110
+ 11: 'Декабрь',
111
+ },
112
+ };
113
+
114
+ /**
115
+ * Форматування дати. Є можливість обмеження точності. Є можливість зміщення дат і часу. Є можливість виведення дати на момент запиту.
116
+ *
117
+ * @summary Форматування дати за умовою і можливе зміщення дат і часу. Є можливість виведенння поточної дати через значення аргументу "0"
118
+ * @priority 3
119
+ * @type helper
120
+ * @tag format
121
+ * @alias formatDate
122
+ * @example
123
+ * {{formatDate "2021-09-08T12:22:27.983" shift_year=1 format='mm - yy mw_ru'}}
124
+ * @descr Виведення дати формату 09 - 2021 з назвою місяця російською мовою
125
+ * @example
126
+ * {{formatDate "2021-09-08T12:22:27.983" shift=1478 format='dd.MM.YY hh:mi:sec'}}
127
+ * @descr Виведення дати із зсувом відносно "2021-09-08T12:22:27.983" на 5 діб (13.09.21 12:22:27)
128
+ * @example
129
+ * {{formatDate "2021-09-08T12:22:27.983" trunc='year' format='dd month yy hh:mi:sec'}}
130
+ * @example
131
+ * {{formatDate "2021-09-08T12:22:27.983" trunc='quarter' format='dd.MM.yy hh:mi:sec'}}
132
+ * @example
133
+ * {{formatDate "2021-09-08T12:22:27.983" format='dd.mm.yy hh:mi'}}
134
+ * @descr Виведення дати формату 08.09.2021 12:22
135
+ * @example
136
+ * {{formatDate 0}}
137
+ * @descr Виведення поточної дати
138
+ * @param {String} format Формат для вигляду при поверненні.Можливі значення: dd, mm, MM, month, mon, yy, YY, hh, mi, sec, mw_ua, mw_en, mw_ru, wd_ua
139
+ * @param {Any} time Для вибору чи робити формат виводу години і хвилини, чи ні
140
+ * @param {String} trunc Для вибору чи онуляти рік або квартал
141
+ * @param {Number} shift_year Здвинути рік
142
+ * @param {Number} shift_quarter Здвинути квартал
143
+ * @param {Number} shift_month Здвинути місяць
144
+ * @param {Number} shift_week Здвинути тиждень
145
+ * @param {Number} shift Здвинути час
146
+ * @param {Date|String} data Набір вхідних даних
147
+ * @returns {String} Returns HTML
148
+ */
149
+ export default function formatDate(data, options = {}) {
150
+
151
+ if (typeof data === 'object' && data === null) return '';
152
+ if (typeof data === 'undefined') return '';
153
+
154
+ if (!data.toLocaleDateString && data && data.includes('T')) {
155
+ data = new Date(data);
156
+ }
157
+
158
+ if (
159
+ (!options.hash.format || options.hash.format === 'dd.mm')
160
+ && data
161
+ && data.toLocaleDateString
162
+ ) {
163
+ const time = options.hash.time
164
+ ? `${data.getHours()}:${data.getMinutes()}`
165
+ : '';
166
+ let m = data.getMonth() + 1;
167
+ m = m < 10 ? `0${m}` : m;
168
+ const dt = options.hash.format === 'dd.mm'
169
+ ? `${data.getDate()}.${m}`
170
+ : `${data.getDate()}.${m}.${data.getFullYear()}`;
171
+ return dt + (options.hash.time && time !== '00:00' ? ` ${time}` : '');
172
+ }
173
+
174
+ let val = data;
175
+
176
+ options.hash = options.hash || {};
177
+
178
+ if (
179
+ typeof val === 'string'
180
+ && (val.match(/\./g) || []).length === 2
181
+ && val.length < 11
182
+ ) {
183
+ val = val.replace(/(\d{2}).(\d{2}).(\d{4})/, '$2/$1/$3');
184
+ }
185
+
186
+ try {
187
+ val = val === 0 || val === '0' ? new Date() : new Date(val);
188
+ } catch (e) {
189
+ return val;
190
+ }
191
+ if (options.hash.trunc) {
192
+ val = options.hash.trunc === 'year'
193
+ ? new Date(new Date(val.setMonth(0)).setDate(1))
194
+ : val;
195
+ val = options.hash.trunc === 'quarter'
196
+ ? new Date(
197
+ new Date(
198
+ val.setMonth(val.getMonth() - (val.getMonth() % 3)),
199
+ ).setDate(1),
200
+ )
201
+ : val;
202
+ }
203
+ if (options.hash.shift_year) {
204
+ val = new Date(
205
+ val.setYear(val.getFullYear() + (options.hash.shift_year - 0)),
206
+ );
207
+ }
208
+ if (options.hash.shift_quarter) {
209
+ val = new Date(
210
+ val.setMonth(val.getMonth() + (options.hash.shift_quarter - 0) * 3),
211
+ );
212
+ }
213
+
214
+ if (options.hash.shift_month) {
215
+ val = new Date(
216
+ val.setMonth(val.getMonth() + (options.hash.shift_month - 0)),
217
+ );
218
+ }
219
+ if (options.hash.shift_week) {
220
+ val = new Date(
221
+ val.getTime() + (options.hash.shift_week - 0) * 86400 * 7 * 1000,
222
+ );
223
+ }
224
+ if (options.hash.shift) {
225
+ val = new Date(val.getTime() + 86400000 * (options.hash.shift - 0));
226
+ }
227
+ if (options.hash.shift_time) {
228
+ val = new Date(val.getTime() + (options.hash.shift_time - 0));
229
+ }
230
+
231
+ if (options.hash.format === 'iso') return val.toISOString();
232
+
233
+ const opt1 = {
234
+ dd: (val.getDate() < 10 ? '0' : '') + val.getDate(),
235
+ mm: (val.getMonth() < 9 ? '0' : '') + (val.getMonth() + 1),
236
+ MM: (val.getMonth() < 9 ? '0' : '') + (val.getMonth() + 1),
237
+ month: (mth[
238
+ options.data && options.data.root && options.data.root.lang
239
+ ? options.data.root.lang
240
+ : 'ua'
241
+ ] || {})[val.getMonth()],
242
+ mon: (mthShort[
243
+ options.data && options.data.root && options.data.root.lang
244
+ ? options.data.root.lang
245
+ : 'ua'
246
+ ] || {})[val.getMonth()],
247
+ yy: val.getFullYear(),
248
+ YY: (`${val.getFullYear()}`).substr(-2),
249
+ hh: (val.getHours() < 10 ? '0' : '') + val.getHours(),
250
+ mi: (val.getMinutes() < 10 ? '0' : '') + val.getMinutes(),
251
+ sec: (val.getSeconds() < 10 ? '0' : '') + val.getSeconds(),
252
+ mw_ua: monthUa[val.getMonth()],
253
+ mw_en: monthEn[val.getMonth()],
254
+ mw_ru: monthRu[val.getMonth()],
255
+ wd_ua: weekDayUa[val.getDay()],
256
+ };
257
+ return Object.keys(opt1).reduce((prev, el) => prev.replace(el, opt1[el]), options.hash.format || 'dd.mm.yy');
258
+ };
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Приведення чисел до стандартизованого виду
3
+ *
4
+ * @summary Форматування чисел. Приводить до встановленої довжини шляхом доповнення нулями на початку
5
+ * @priority 4
6
+ * @type helper
7
+ * @tag format
8
+ * @alias formatDigit
9
+ * @example
10
+ * {{{formatDigit '11' rank=4}}}
11
+ * @param {String|Number} data Число для форматування
12
+ * @param {String|Number} opt.rank Фіксована довжина
13
+ * @returns {String} Returns String
14
+ */
15
+
16
+ export default function formatDigit(data, options) {
17
+ const dataType = typeof data;
18
+ const { rank = 4 } = options.hash;
19
+ if (!data || (dataType !== 'number' && dataType !== 'string')) return '';
20
+ return String(data).padStart(+rank, '0');
21
+ };