@karta.io/utils 1.0.0-beta.38 → 1.0.0-beta.4

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/index.cjs CHANGED
@@ -1,7 +1,6 @@
1
1
  'use strict';
2
2
 
3
3
  const camelCase = require('lodash/camelCase.js');
4
- const flatMap = require('lodash/flatMap.js');
5
4
  const commonData = require('@karta.io/common-data');
6
5
  const libphonenumberJs = require('libphonenumber-js');
7
6
  const snakeCase = require('lodash/snakeCase.js');
@@ -25,7 +24,6 @@ const qs = require('qs');
25
24
  function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
26
25
 
27
26
  const camelCase__default = /*#__PURE__*/_interopDefaultCompat(camelCase);
28
- const flatMap__default = /*#__PURE__*/_interopDefaultCompat(flatMap);
29
27
  const snakeCase__default = /*#__PURE__*/_interopDefaultCompat(snakeCase);
30
28
  const get__default = /*#__PURE__*/_interopDefaultCompat(get);
31
29
  const has__default = /*#__PURE__*/_interopDefaultCompat(has);
@@ -44,16 +42,14 @@ function AppComponentsResolver(options = {}) {
44
42
  return (name) => {
45
43
  const componentName = prefix && name.startsWith(prefix) ? name.substring(prefix.length) : name;
46
44
  const isAppComponent = componentName.startsWith("App");
47
- if (isAppComponent) {
45
+ if (isAppComponent)
48
46
  return { name: componentName, from: "@karta.io/app-components" };
49
- }
50
47
  };
51
48
  }
52
49
 
53
50
  function camelizeKeys(object) {
54
- if (Array.isArray(object)) {
51
+ if (Array.isArray(object))
55
52
  return object.map((v) => camelizeKeys(v));
56
- }
57
53
  if (object !== null && object.constructor === Object) {
58
54
  return Object.keys(object).reduce(
59
55
  (result, key) => ({
@@ -80,109 +76,6 @@ function convertEnumToOptions(obj, options) {
80
76
  }));
81
77
  }
82
78
 
83
- class EnrichError extends Error {
84
- constructor(data) {
85
- super(data.message);
86
- Object.assign(this, data);
87
- }
88
- }
89
-
90
- function isHtml(data) {
91
- try {
92
- if (typeof DOMParser !== "undefined") {
93
- const parser = new DOMParser();
94
- const doc = parser.parseFromString(data, "text/html");
95
- return Array.from(doc.body.childNodes).some(
96
- (node) => node.nodeType === Node.ELEMENT_NODE
97
- );
98
- }
99
- const htmlPattern = /<\/?[a-z][\s\S]*>/i;
100
- return htmlPattern.test(data);
101
- } catch (_) {
102
- return false;
103
- }
104
- }
105
-
106
- function getStatusMessage(status) {
107
- let message = "";
108
- switch (status) {
109
- case 200:
110
- message = "All done. Request successfully executed";
111
- break;
112
- case 201:
113
- message = "Data successfully created";
114
- break;
115
- case 400:
116
- message = "Bad request";
117
- break;
118
- case 401:
119
- message = "Need auth";
120
- break;
121
- case 404:
122
- message = "Not found";
123
- break;
124
- case 500:
125
- message = "Something wrong. Internal server error";
126
- break;
127
- case 501:
128
- message = "501: Not implemented";
129
- break;
130
- case 502:
131
- message = "502: Bad gateway";
132
- break;
133
- case 503:
134
- message = "Service unavailable. Try again later";
135
- break;
136
- default:
137
- message = "Something wrong. Client default error message";
138
- break;
139
- }
140
- return message;
141
- }
142
- function formatErrorDataMessage(data) {
143
- if (!data)
144
- return "";
145
- return Object.values(data).reduce((accum, current) => {
146
- if (current && typeof current === "string")
147
- return accum ? `${accum} | ${current}` : current;
148
- if (Array.isArray(current) && current.length) {
149
- const value = `${flatMap__default(current).join(" | ")}`;
150
- return accum ? `${accum} | ${value}` : value;
151
- }
152
- return accum;
153
- }, "");
154
- }
155
- function formatErrorForNotify(error) {
156
- if (!error)
157
- return;
158
- if (error.status >= 400 && error.status < 500) {
159
- let message = "";
160
- if (!error.data || typeof error.data === "string" && isHtml(error.data)) {
161
- message = error.statusMessage || error.message || getStatusMessage(error.status);
162
- } else if (typeof error.data === "string" && !isHtml(error.data)) {
163
- message = error.data;
164
- } else {
165
- message = formatErrorDataMessage(error.data);
166
- }
167
- return {
168
- title: "Request error",
169
- message
170
- };
171
- }
172
- if (error.status >= 500) {
173
- return {
174
- title: "Internal server error",
175
- message: error.statusMessage || error.message || getStatusMessage(error.status)
176
- };
177
- }
178
- if (error instanceof Error) {
179
- return {
180
- title: "Internal client error",
181
- message: error.message
182
- };
183
- }
184
- }
185
-
186
79
  function formatMoney(value, currency = commonData.CURRENCY_DEFAULT, options = {}) {
187
80
  const { locale = "en-US", defaultValue = 0, ...otherOptions } = options;
188
81
  try {
@@ -200,7 +93,7 @@ function formatMoney(value, currency = commonData.CURRENCY_DEFAULT, options = {}
200
93
  ...otherOptions
201
94
  });
202
95
  return formatter.format(convertedValue);
203
- } catch (_error) {
96
+ } catch (error) {
204
97
  return defaultValue;
205
98
  }
206
99
  }
@@ -230,7 +123,7 @@ function genUUID() {
230
123
  r = (d2 + r) % 16 | 0;
231
124
  d2 = Math.floor(d2 / 16);
232
125
  }
233
- return (c === "x" ? r : r & 7 | 8).toString(16);
126
+ return (c == "x" ? r : r & 7 | 8).toString(16);
234
127
  });
235
128
  }
236
129
 
@@ -310,9 +203,7 @@ function getPaginationParams({
310
203
  offset: validatedPage * validatedLimit - validatedLimit
311
204
  };
312
205
  }
313
- function getPaginationLimit({
314
- limit
315
- }) {
206
+ function getPaginationLimit({ limit }) {
316
207
  return validateNumber(limit, LIMIT_DEFAULT);
317
208
  }
318
209
  function getPaginationOffset({
@@ -334,17 +225,15 @@ function parsePhone(phone) {
334
225
  if (!phone || typeof phone !== "string")
335
226
  return "";
336
227
  const replacedPhone = phone.replace(/[a-zA-Z]/g, "");
337
- if (replacedPhone.length === 1 && replacedPhone !== "+") {
228
+ if (replacedPhone.length === 1 && replacedPhone !== "+")
338
229
  return `+${replacedPhone}`;
339
- }
340
230
  const phoneNumber = libphonenumberJs.parsePhoneNumberFromString(replacedPhone);
341
231
  return phoneNumber ? phoneNumber.formatInternational() : replacedPhone;
342
232
  }
343
233
 
344
234
  function snakelizeKeys(prop) {
345
- if (Array.isArray(prop)) {
235
+ if (Array.isArray(prop))
346
236
  return prop.map((item) => snakelizeKeys(item));
347
- }
348
237
  if (prop !== null && prop !== void 0 && prop.constructor === Object) {
349
238
  return Object.keys(prop).reduce(
350
239
  (accum, current) => ({
@@ -384,9 +273,8 @@ function unformatNumber(value) {
384
273
  if (commasCount === 1 && !hasSpace && !hasDot) {
385
274
  locale = string.length - 1 - commaIndex === 3 ? LOCALE_EN : LOCALE_RU;
386
275
  }
387
- if (locale === LOCALE_RU) {
276
+ if (locale === LOCALE_RU)
388
277
  return Number(string.replaceAll(" ", "").replace(",", "."));
389
- }
390
278
  if (locale === LOCALE_EN)
391
279
  return Number(string.replaceAll(",", ""));
392
280
  return value;
@@ -397,9 +285,8 @@ function UiComponentsResolver(options = {}) {
397
285
  return (name) => {
398
286
  const componentName = prefix && name.startsWith(prefix) ? name.substring(prefix.length) : name;
399
287
  const isUiComponent = componentName.startsWith("Ui");
400
- if (isUiComponent) {
288
+ if (isUiComponent)
401
289
  return { name: componentName, from: "@karta.io/ui-components" };
402
- }
403
290
  };
404
291
  }
405
292
 
@@ -447,9 +334,7 @@ function useAsyncItems({
447
334
  queryKey: getQueryKey(),
448
335
  queryFn: () => call(core.toValue(params), config)
449
336
  });
450
- const items = vue.computed(
451
- () => data.value?.results || []
452
- );
337
+ const items = vue.computed(() => data.value?.results || []);
453
338
  const count = vue.ref(0);
454
339
  vue.watch(
455
340
  data,
@@ -526,12 +411,7 @@ function useAsyncMultiselect({
526
411
  color: "#b54708",
527
412
  debug: true
528
413
  });
529
- const paramsForLogger = {
530
- queryKey,
531
- dataItemsKey,
532
- dataItemIdKey,
533
- dataCountKey
534
- };
414
+ const paramsForLogger = { queryKey, dataItemsKey, dataItemIdKey, dataCountKey };
535
415
  const reactiveData = vue.reactive({
536
416
  search: "",
537
417
  page: 1,
@@ -577,9 +457,8 @@ function useAsyncMultiselect({
577
457
  );
578
458
  return;
579
459
  }
580
- if (!get__default(newValue, dataItemsKey) || get__default(newValue, dataCountKey) === void 0) {
460
+ if (!get__default(newValue, dataItemsKey) || get__default(newValue, dataCountKey) === void 0)
581
461
  return;
582
- }
583
462
  count.value = get__default(newValue, dataCountKey, 0);
584
463
  const newItems = get__default(newValue, dataItemsKey);
585
464
  itemsByPages.value = {
@@ -610,9 +489,8 @@ function useAsyncMultiselect({
610
489
  const items = vue.computed(() => {
611
490
  if (!itemsByPages.value)
612
491
  return selectedItemsCached.value;
613
- if (!Object.keys(itemsByPages.value).length) {
492
+ if (!Object.keys(itemsByPages.value).length)
614
493
  return selectedItemsCached.value;
615
- }
616
494
  const itemsByPagesAsArray = Object.values(itemsByPages.value).reduce(
617
495
  (acc, item) => [...acc, ...item],
618
496
  []
@@ -628,7 +506,7 @@ function useAsyncMultiselect({
628
506
  if (!items.value.length)
629
507
  return [];
630
508
  if (!mapOptionCallback)
631
- return items.value;
509
+ return this.items.value;
632
510
  return items.value.map(mapOptionCallback);
633
511
  });
634
512
  const hasNextPage = vue.computed(() => count.value > items.value.length);
@@ -647,28 +525,6 @@ function useAsyncMultiselect({
647
525
  search.value = value;
648
526
  updateSearchWithDebounce(value);
649
527
  };
650
- const queryClient = vueQuery.useQueryClient();
651
- const refetch = () => {
652
- if (reactiveData.page !== 1) {
653
- queryClient.invalidateQueries({
654
- queryKey: [queryKey],
655
- refetchType: "none"
656
- });
657
- loadPage(1);
658
- } else {
659
- queryClient.invalidateQueries({
660
- queryKey: [queryKey]
661
- });
662
- }
663
- };
664
- const reload = () => {
665
- if (reactiveData.page !== 1) {
666
- queryClient.removeQueries({ queryKey: [queryKey] });
667
- loadPage(1);
668
- } else {
669
- queryClient.resetQueries({ queryKey: [queryKey] });
670
- }
671
- };
672
528
  if (vue.isRef(selectedValues)) {
673
529
  vue.watch(selectedValues, async (newSelectedValues) => {
674
530
  if (!newSelectedValues.length) {
@@ -709,9 +565,7 @@ function useAsyncMultiselect({
709
565
  isFetching,
710
566
  isLoading,
711
567
  loadPage,
712
- updateSearch,
713
- refetch,
714
- reload
568
+ updateSearch
715
569
  };
716
570
  }
717
571
 
@@ -743,9 +597,7 @@ function useAsyncSelect({
743
597
  const loadWAC = () => call({
744
598
  page: reactiveData.page,
745
599
  search: reactiveData.search,
746
- ...reactiveData.selectedValue && {
747
- includedIds: [reactiveData.selectedValue]
748
- },
600
+ ...reactiveData.selectedValue && { includedIds: [reactiveData.selectedValue] },
749
601
  ...reactiveData.params && { ...JSON.parse(reactiveData.params) }
750
602
  });
751
603
  const { data, isFetching, isLoading } = vueQuery.useQuery({
@@ -773,9 +625,8 @@ function useAsyncSelect({
773
625
  );
774
626
  return;
775
627
  }
776
- if (!get__default(newValue, dataItemsKey) || get__default(newValue, dataCountKey) === void 0) {
628
+ if (!get__default(newValue, dataItemsKey) || get__default(newValue, dataCountKey) === void 0)
777
629
  return;
778
- }
779
630
  count.value = get__default(newValue, dataCountKey, 0);
780
631
  const newItems = get__default(newValue, dataItemsKey);
781
632
  itemsByPages.value = {
@@ -805,7 +656,7 @@ function useAsyncSelect({
805
656
  if (!items.value.length)
806
657
  return [];
807
658
  if (!mapOptionCallback)
808
- return items.value;
659
+ return this.items.value;
809
660
  return items.value.map(mapOptionCallback);
810
661
  });
811
662
  const hasNextPage = vue.computed(() => count.value > items.value.length);
@@ -820,28 +671,6 @@ function useAsyncSelect({
820
671
  search.value = value;
821
672
  updateSearchWithDebounce(value);
822
673
  };
823
- const queryClient = vueQuery.useQueryClient();
824
- const refetch = () => {
825
- if (reactiveData.page !== 1) {
826
- queryClient.invalidateQueries({
827
- queryKey: [queryKey],
828
- refetchType: "none"
829
- });
830
- loadPage(1);
831
- } else {
832
- queryClient.invalidateQueries({
833
- queryKey: [queryKey]
834
- });
835
- }
836
- };
837
- const reload = () => {
838
- if (reactiveData.page !== 1) {
839
- queryClient.removeQueries({ queryKey: [queryKey] });
840
- loadPage(1);
841
- } else {
842
- queryClient.resetQueries({ queryKey: [queryKey] });
843
- }
844
- };
845
674
  if (vue.isRef(selectedValue)) {
846
675
  vue.watch(selectedValue, (newSelectedValue) => {
847
676
  reactiveData.page = 1;
@@ -865,22 +694,20 @@ function useAsyncSelect({
865
694
  isFetching,
866
695
  isLoading,
867
696
  loadPage,
868
- updateSearch,
869
- refetch,
870
- reload
697
+ updateSearch
871
698
  };
872
699
  }
873
700
 
874
701
  function useBreakpoint() {
875
702
  const breakpoints = core.useBreakpoints(commonData.BREAKPOINTS);
876
703
  const isMobile = breakpoints.smaller("sm");
877
- const isDesktop = breakpoints.greaterOrEqual("md");
878
- const isGreaterXxs = breakpoints.greaterOrEqual("xxs");
879
- const isGreaterXs = breakpoints.greaterOrEqual("xs");
880
- const isGreaterSm = breakpoints.greaterOrEqual("sm");
881
- const isGreaterMd = breakpoints.greaterOrEqual("md");
882
- const isGreaterLg = breakpoints.greaterOrEqual("lg");
883
- const isGreaterXl = breakpoints.greaterOrEqual("xl");
704
+ const isDesktop = breakpoints.greater("md");
705
+ const isGreaterXxs = breakpoints.greater("xxs");
706
+ const isGreaterXs = breakpoints.greater("xs");
707
+ const isGreaterSm = breakpoints.greater("sm");
708
+ const isGreaterMd = breakpoints.greater("md");
709
+ const isGreaterLg = breakpoints.greater("lg");
710
+ const isGreaterXl = breakpoints.greater("xl");
884
711
  return {
885
712
  isMobile,
886
713
  isDesktop,
@@ -947,9 +774,7 @@ function useDayjs(timezone) {
947
774
 
948
775
  function parseVersion(version) {
949
776
  if (typeof version !== "string" && typeof version !== "number") {
950
- throw new TypeError(
951
- "Browser version must be type of string, number or negative boolean"
952
- );
777
+ throw new TypeError("Browser version must be type of string, number or negative boolean");
953
778
  }
954
779
  const [major, minor = 0, patch = 0, build = 0] = typeof version === "string" ? version.split(".").map(Number) : [version];
955
780
  return {
@@ -1029,7 +854,7 @@ function useDialog() {
1029
854
  let dataFromHash;
1030
855
  try {
1031
856
  dataFromHash = JSON.parse(window.atob(hash.slice(HASH_PREFIX.length)));
1032
- } catch (_error) {
857
+ } catch (error) {
1033
858
  logger.warn("*parseHash* cant parse hash");
1034
859
  }
1035
860
  if (typeof dataFromHash !== "object")
@@ -1166,11 +991,6 @@ function useDialog() {
1166
991
  const getInstance = (uuid) => {
1167
992
  return dialogs.find((item) => item.uuid === uuid)?.vnode;
1168
993
  };
1169
- const getLayer = (uuid) => {
1170
- if (!dialogs.length)
1171
- return 0;
1172
- return dialogs.find((dialog) => dialog.uuid === uuid)?.layer || 0;
1173
- };
1174
994
  const isOnTop = (uuid) => {
1175
995
  if (!dialogs.length)
1176
996
  return false;
@@ -1238,7 +1058,6 @@ function useDialog() {
1238
1058
  return {
1239
1059
  open,
1240
1060
  getInstance,
1241
- getLayer,
1242
1061
  close,
1243
1062
  closeAll,
1244
1063
  showConfirm,
@@ -1255,7 +1074,10 @@ const QS_STRINGIFY_PARAMS = {
1255
1074
  addQueryPrefix: true
1256
1075
  };
1257
1076
  const isOpen = vue.ref(false);
1258
- function useFilters({ config, initialFilters }) {
1077
+ function useFilters({
1078
+ config,
1079
+ initialFilters
1080
+ }) {
1259
1081
  const router = vueRouter.useRouter();
1260
1082
  const route = vueRouter.useRoute();
1261
1083
  const parseRouteQuery = (query) => qs__default.parse(query ?? route.query);
@@ -1263,43 +1085,35 @@ function useFilters({ config, initialFilters }) {
1263
1085
  const parsedRouteQuery = parseRouteQuery();
1264
1086
  if (!parsedRouteQuery.filters)
1265
1087
  return void 0;
1266
- return Object.entries(config).reduce(
1267
- (accum, [currentKey, currentValue]) => {
1268
- if (!parsedRouteQuery.filters[currentKey])
1088
+ return Object.entries(config).reduce((accum, [currentKey, currentValue]) => {
1089
+ if (!parsedRouteQuery.filters[currentKey])
1090
+ return accum;
1091
+ if (currentValue.type === "ARRAY" /* Array */) {
1092
+ const splitValues = parsedRouteQuery.filters[currentKey].split(",");
1093
+ const checkedValue = currentValue?.possibleValues ? splitValues.filter((k) => currentValue?.possibleValues?.includes(k)) : splitValues;
1094
+ if (!checkedValue?.length)
1269
1095
  return accum;
1270
- if (currentValue.type === "ARRAY" /* Array */) {
1271
- const splitValues = parsedRouteQuery.filters[currentKey].split(",");
1272
- const checkedValue = currentValue?.possibleValues ? splitValues.filter(
1273
- (k) => currentValue?.possibleValues?.includes(k)
1274
- ) : splitValues;
1275
- if (!checkedValue?.length)
1276
- return accum;
1277
- return {
1278
- ...accum,
1279
- [currentKey]: checkedValue
1280
- };
1281
- }
1282
1096
  return {
1283
1097
  ...accum,
1284
- [currentKey]: parsedRouteQuery.filters[currentKey]
1098
+ [currentKey]: checkedValue
1285
1099
  };
1286
- },
1287
- {}
1288
- );
1100
+ }
1101
+ return {
1102
+ ...accum,
1103
+ [currentKey]: parsedRouteQuery.filters[currentKey]
1104
+ };
1105
+ }, {});
1289
1106
  });
1290
1107
  const filtersCount = vue.computed(
1291
1108
  () => filtersFromQuery.value ? Object.keys(filtersFromQuery.value).length : 0
1292
1109
  );
1293
1110
  const setQueryFromFilters = (filters) => {
1294
1111
  const parsedRouteQuery = parseRouteQuery();
1295
- const filteredValue = Object.entries(filters).reduce(
1296
- (accum, [currentKey, currentValue]) => {
1297
- if (!currentValue?.length)
1298
- return accum;
1299
- return { ...accum, [currentKey]: currentValue };
1300
- },
1301
- {}
1302
- );
1112
+ const filteredValue = Object.entries(filters).reduce((accum, [currentKey, currentValue]) => {
1113
+ if (!currentValue?.length)
1114
+ return accum;
1115
+ return { ...accum, [currentKey]: currentValue };
1116
+ }, {});
1303
1117
  const query = qs__default.stringify(
1304
1118
  {
1305
1119
  ...parsedRouteQuery,
@@ -1308,7 +1122,7 @@ function useFilters({ config, initialFilters }) {
1308
1122
  },
1309
1123
  QS_STRINGIFY_PARAMS
1310
1124
  );
1311
- router.push(query.replaceAll("#", ""));
1125
+ router.push(query);
1312
1126
  };
1313
1127
  const getValidatedParams = (query) => {
1314
1128
  const parsedRouteQuery = parseRouteQuery(query);
@@ -1336,15 +1150,12 @@ function useFilters({ config, initialFilters }) {
1336
1150
  });
1337
1151
  };
1338
1152
  const updateMany = (filters) => {
1339
- const newFilters = Object.entries(filters).reduce(
1340
- (accum, [currentKey, currentValue]) => {
1341
- return {
1342
- ...accum,
1343
- [currentKey]: currentValue ? String(currentValue) : void 0
1344
- };
1345
- },
1346
- {}
1347
- );
1153
+ const newFilters = Object.entries(filters).reduce((accum, [currentKey, currentValue]) => {
1154
+ return {
1155
+ ...accum,
1156
+ [currentKey]: currentValue ? String(currentValue) : void 0
1157
+ };
1158
+ }, {});
1348
1159
  setQueryFromFilters({
1349
1160
  ...filtersModel.value,
1350
1161
  ...newFilters
@@ -1520,7 +1331,7 @@ function usePanel() {
1520
1331
  let dataFromHash;
1521
1332
  try {
1522
1333
  dataFromHash = JSON.parse(window.atob(newHash.slice(1)));
1523
- } catch (_error) {
1334
+ } catch (error) {
1524
1335
  logger.error("*watchHash* cant parse hash");
1525
1336
  }
1526
1337
  if (typeof dataFromHash !== "object")
@@ -1624,12 +1435,9 @@ function isStrictLength(value = 3, message) {
1624
1435
  }
1625
1436
 
1626
1437
  exports.AppComponentsResolver = AppComponentsResolver;
1627
- exports.EnrichError = EnrichError;
1628
1438
  exports.UiComponentsResolver = UiComponentsResolver;
1629
1439
  exports.camelizeKeys = camelizeKeys;
1630
1440
  exports.convertEnumToOptions = convertEnumToOptions;
1631
- exports.formatErrorDataMessage = formatErrorDataMessage;
1632
- exports.formatErrorForNotify = formatErrorForNotify;
1633
1441
  exports.formatMoney = formatMoney;
1634
1442
  exports.genHash = genHash;
1635
1443
  exports.genRandomNumber = genRandomNumber;
@@ -1640,10 +1448,8 @@ exports.getPaginationLimit = getPaginationLimit;
1640
1448
  exports.getPaginationOffset = getPaginationOffset;
1641
1449
  exports.getPaginationPages = getPaginationPages;
1642
1450
  exports.getPaginationParams = getPaginationParams;
1643
- exports.getStatusMessage = getStatusMessage;
1644
1451
  exports.isElementIgnored = isElementIgnored;
1645
1452
  exports.isEmptyObject = isEmptyObject;
1646
- exports.isHtml = isHtml;
1647
1453
  exports.isMax = isMax;
1648
1454
  exports.isMin = isMin;
1649
1455
  exports.isRequired = isRequired;