kui-utils 0.0.24 → 0.0.26

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/cjs/index.js CHANGED
@@ -6,6 +6,7 @@ var React = require('react');
6
6
  var _ = require('lodash');
7
7
  var reactRouterDom = require('react-router-dom');
8
8
  var mobx = require('mobx');
9
+ var luxon = require('luxon');
9
10
 
10
11
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
11
12
 
@@ -2021,13 +2022,14 @@ var toNumber = function (value) {
2021
2022
  : null;
2022
2023
  };
2023
2024
 
2024
- var updateQueryParams = function (newLink) {
2025
+ var updateQueryParams = function (newLink, withBase) {
2025
2026
  var searchQuery = new URLSearchParams(window.location.search);
2026
2027
  var hrefLinkParams = newLink.split(/\?|&/);
2027
2028
  var hrefParams = Object.fromEntries(new URLSearchParams(hrefLinkParams.slice(1).join("&")).entries());
2028
2029
  var searchParams = Object.fromEntries(searchQuery.entries());
2030
+ var route =
2029
2031
  // @ts-ignore
2030
- var route = searchQuery.size ? hrefLinkParams[0] : window.location.pathname;
2032
+ searchQuery.size && !withBase ? hrefLinkParams[0] : window.location.pathname;
2031
2033
  Object.keys(searchParams).forEach(function (key, index) {
2032
2034
  if (key in hrefParams) {
2033
2035
  searchParams[key] = hrefParams[key];
@@ -2044,6 +2046,110 @@ var updateQueryParams = function (newLink) {
2044
2046
  window.history.pushState({ route: route }, "", route);
2045
2047
  };
2046
2048
 
2049
+ function cleanObjectForIndexDB(value) {
2050
+ if (typeof value === "undefined")
2051
+ return null;
2052
+ if (value instanceof File)
2053
+ return value;
2054
+ if (value instanceof luxon.DateTime)
2055
+ return value.toISO();
2056
+ if (mobx.isObservable(value)) {
2057
+ return cleanObjectForIndexDB(mobx.toJS(value));
2058
+ }
2059
+ if (Array.isArray(value)) {
2060
+ return value.map(function (item) { return cleanObjectForIndexDB(item); });
2061
+ }
2062
+ if (value !== null && typeof value === "object") {
2063
+ var plainObject = {};
2064
+ // eslint-disable-next-line no-restricted-syntax
2065
+ for (var key in value) {
2066
+ // eslint-disable-next-line no-prototype-builtins
2067
+ if (value.hasOwnProperty(key)) {
2068
+ var objectValue = value[key];
2069
+ if (typeof objectValue !== "function" && objectValue !== undefined) {
2070
+ plainObject[key] = cleanObjectForIndexDB(objectValue);
2071
+ }
2072
+ }
2073
+ }
2074
+ return plainObject;
2075
+ }
2076
+ return value;
2077
+ }
2078
+ var transactionQueue = Promise.resolve();
2079
+ function addToIndexDBWithQueue(db, storeName, data, loader) {
2080
+ transactionQueue = transactionQueue
2081
+ .then(function () {
2082
+ return new Promise(function (resolve, reject) {
2083
+ if (db && db.objectStoreNames.contains(storeName) && data) {
2084
+ var transaction = db.transaction(storeName, "readwrite");
2085
+ var store = transaction.objectStore(storeName);
2086
+ // eslint-disable-next-line no-param-reassign
2087
+ data.id = 1;
2088
+ var formattedData = cleanObjectForIndexDB(data);
2089
+ var request_1 = store.put(formattedData);
2090
+ request_1.onsuccess = function () {
2091
+ resolve(request_1);
2092
+ };
2093
+ request_1.onerror = function () {
2094
+ loader.setError("Не удалось сохранить данные в локальное хранилище");
2095
+ reject();
2096
+ };
2097
+ }
2098
+ });
2099
+ })
2100
+ .catch(function (error) {
2101
+ console.error("Ошибка в очереди транзакций:", error);
2102
+ });
2103
+ }
2104
+ var readFromIndexDB = function (db, name, loader) { return __awaiter(void 0, void 0, void 0, function () {
2105
+ var transaction, request_2;
2106
+ return __generator(this, function (_a) {
2107
+ if (db.objectStoreNames.contains(name)) {
2108
+ transaction = db.transaction(name, "readonly").objectStore(name);
2109
+ request_2 = transaction.getAll();
2110
+ return [2 /*return*/, new Promise(function (resolve, reject) {
2111
+ request_2.onerror = function () {
2112
+ reject();
2113
+ loader.setError("Не удалось сохранить данные в локальное хранилище");
2114
+ };
2115
+ request_2.onsuccess = function () {
2116
+ var _a;
2117
+ resolve((_a = request_2.result) === null || _a === void 0 ? void 0 : _a[0]);
2118
+ };
2119
+ })];
2120
+ }
2121
+ return [2 /*return*/, null];
2122
+ });
2123
+ }); };
2124
+ var initIndexDB = function (dbName, onupgradeneeded, loader, setIndexDB) {
2125
+ var dbRequest = indexedDB.open(dbName, 1);
2126
+ dbRequest.onerror = function () {
2127
+ loader.setError("Ошибка при открытии хранилища на устройстве. Данные не будут сохраняться локально");
2128
+ };
2129
+ dbRequest.onsuccess = function () {
2130
+ var db = dbRequest.result;
2131
+ setIndexDB(db);
2132
+ };
2133
+ dbRequest.onupgradeneeded = function () { return onupgradeneeded(dbRequest.result); };
2134
+ };
2135
+ var addIndexDBStore = function (db, name) {
2136
+ if (db && !db.objectStoreNames.contains(name)) {
2137
+ db.createObjectStore(name, { keyPath: "id" });
2138
+ }
2139
+ };
2140
+ var clearIndexStore = function (db, name) {
2141
+ if (db) {
2142
+ var transaction = db.transaction(name, "readwrite");
2143
+ var objectStore = transaction.objectStore(name);
2144
+ objectStore.clear();
2145
+ }
2146
+ };
2147
+ var clearIndexStores = function (db, stores) {
2148
+ Array.from(stores).forEach(function (name) {
2149
+ clearIndexStore(db, name);
2150
+ });
2151
+ };
2152
+
2047
2153
  function setCookie(name, value, options) {
2048
2154
  if (options === void 0) { options = {}; }
2049
2155
  var cookieOptions = __assign({ path: "/" }, options);
@@ -2368,14 +2474,18 @@ exports.MultistepForm = MultistepForm;
2368
2474
  exports.Paginator = Paginator;
2369
2475
  exports.SortingFilter = SortingFilter;
2370
2476
  exports.accountRegExp = accountRegExp;
2477
+ exports.addIndexDBStore = addIndexDBStore;
2371
2478
  exports.addLeadZero = addLeadZero;
2372
2479
  exports.addToArrayByCondition = addToArrayByCondition;
2480
+ exports.addToIndexDBWithQueue = addToIndexDBWithQueue;
2373
2481
  exports.applyMask = applyMask;
2374
2482
  exports.bicRegExp = bicRegExp;
2375
2483
  exports.callPromises = callPromises;
2376
2484
  exports.carNumberRegExp = carNumberRegExp;
2377
2485
  exports.checkIsIOS = checkIsIOS;
2378
2486
  exports.cleanEmptyFields = cleanEmptyFields;
2487
+ exports.clearIndexStore = clearIndexStore;
2488
+ exports.clearIndexStores = clearIndexStores;
2379
2489
  exports.clearNotValidFields = clearNotValidFields;
2380
2490
  exports.copyInfo = copyInfo;
2381
2491
  exports.corrAccountRegExp = corrAccountRegExp;
@@ -2394,12 +2504,14 @@ exports.getFileNameFromUrl = getFileNameFromUrl;
2394
2504
  exports.getNestedData = getNestedData;
2395
2505
  exports.getPhoneNumberFromPhoneParams = getPhoneNumberFromPhoneParams;
2396
2506
  exports.getPhoneParamsFromString = getPhoneParamsFromString;
2507
+ exports.initIndexDB = initIndexDB;
2397
2508
  exports.innRegExp = innRegExp;
2398
2509
  exports.isValidWithMaskExp = isValidWithMaskExp;
2399
2510
  exports.mediumPasswordRegExp = mediumPasswordRegExp;
2400
2511
  exports.monthYearRegExp = monthYearRegExp;
2401
2512
  exports.phoneRegExp = phoneRegExp;
2402
2513
  exports.promisesWithCallback = promisesWithCallback;
2514
+ exports.readFromIndexDB = readFromIndexDB;
2403
2515
  exports.resHandler = resHandler;
2404
2516
  exports.setCookie = setCookie;
2405
2517
  exports.simplePasswordRegExp = simplePasswordRegExp;