@karta.io/utils 0.23.5 → 0.24.0
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/README.md +6 -0
- package/dist/index.cjs +209 -4
- package/dist/index.d.ts +60 -4
- package/dist/index.mjs +206 -5
- package/package.json +11 -5
package/README.md
CHANGED
|
@@ -21,7 +21,13 @@
|
|
|
21
21
|
|
|
22
22
|
## Хуки:
|
|
23
23
|
|
|
24
|
+
- [useAbortController](src/hooks/USE_ABORT_CONTROLLER.md)
|
|
25
|
+
- [useAsyncMultiselect](src/hooks/USE_ASYNC_MULTISELECT.md)
|
|
26
|
+
- [useAsyncSelect](src/hooks/USE_ASYNC_SELECT.md)
|
|
24
27
|
- useClassName
|
|
28
|
+
- useDayjs
|
|
29
|
+
- [useDetectBrowser](src/hooks/USE_DETECT_BROWSER.md)
|
|
30
|
+
- [useLogger](src/hooks/USE_LOGGER.md)
|
|
25
31
|
|
|
26
32
|
## Кастомные правила для валидатора
|
|
27
33
|
|
package/dist/index.cjs
CHANGED
|
@@ -2,12 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
const libphonenumberJs = require('libphonenumber-js');
|
|
4
4
|
const lodashEs = require('lodash-es');
|
|
5
|
+
const vue = require('vue');
|
|
6
|
+
const core = require('@vueuse/core');
|
|
5
7
|
const dayjs = require('dayjs');
|
|
6
|
-
const utc = require('dayjs/plugin/utc');
|
|
7
|
-
const tz = require('dayjs/plugin/timezone');
|
|
8
|
-
const updateLocale = require('dayjs/plugin/updateLocale');
|
|
9
|
-
const isToday = require('dayjs/plugin/isToday');
|
|
8
|
+
const utc = require('dayjs/plugin/utc.js');
|
|
9
|
+
const tz = require('dayjs/plugin/timezone.js');
|
|
10
|
+
const updateLocale = require('dayjs/plugin/updateLocale.js');
|
|
11
|
+
const isToday = require('dayjs/plugin/isToday.js');
|
|
10
12
|
const detectBrowser = require('detect-browser');
|
|
13
|
+
const logdown = require('logdown');
|
|
11
14
|
|
|
12
15
|
function viewExample(value) {
|
|
13
16
|
return value;
|
|
@@ -207,6 +210,175 @@ function isEmptyObject(object) {
|
|
|
207
210
|
return !object || !Object.keys(object).length;
|
|
208
211
|
}
|
|
209
212
|
|
|
213
|
+
function useAbortController(callback) {
|
|
214
|
+
let controller = null;
|
|
215
|
+
return {
|
|
216
|
+
async call(...args) {
|
|
217
|
+
if (controller) {
|
|
218
|
+
controller.abort();
|
|
219
|
+
}
|
|
220
|
+
controller = new AbortController();
|
|
221
|
+
let response;
|
|
222
|
+
if (args.length > 0) {
|
|
223
|
+
response = await callback(...args, {
|
|
224
|
+
signal: controller.signal
|
|
225
|
+
});
|
|
226
|
+
} else {
|
|
227
|
+
response = await callback({
|
|
228
|
+
signal: controller.signal
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
controller = null;
|
|
232
|
+
return response;
|
|
233
|
+
},
|
|
234
|
+
abort() {
|
|
235
|
+
if (!controller)
|
|
236
|
+
return;
|
|
237
|
+
controller.abort();
|
|
238
|
+
controller = null;
|
|
239
|
+
},
|
|
240
|
+
isAborted(error) {
|
|
241
|
+
return error.status === 418;
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function useAsyncMultiselect({
|
|
247
|
+
apiMethod,
|
|
248
|
+
mapResultCallback,
|
|
249
|
+
selectedValues,
|
|
250
|
+
params = void 0
|
|
251
|
+
}) {
|
|
252
|
+
const items = vue.ref(/* @__PURE__ */ new Map());
|
|
253
|
+
const loading = vue.ref(false);
|
|
254
|
+
const search = vue.ref("");
|
|
255
|
+
const count = vue.ref(0);
|
|
256
|
+
const page = vue.ref(1);
|
|
257
|
+
const options = vue.computed(() => [...items.value.values()]);
|
|
258
|
+
const hasNextPage = vue.computed(() => count.value > items.value.size);
|
|
259
|
+
const {
|
|
260
|
+
call: apiMethodWAC,
|
|
261
|
+
abort,
|
|
262
|
+
isAborted
|
|
263
|
+
} = useAbortController(apiMethod);
|
|
264
|
+
const handleSearch = async (value) => {
|
|
265
|
+
if (search.value === value)
|
|
266
|
+
return;
|
|
267
|
+
search.value = value;
|
|
268
|
+
await loadItemsWithDebounce();
|
|
269
|
+
};
|
|
270
|
+
const loadItems = async (isSearch) => {
|
|
271
|
+
loading.value = true;
|
|
272
|
+
try {
|
|
273
|
+
const { results, count: itemsCount } = await apiMethodWAC({
|
|
274
|
+
page: page.value,
|
|
275
|
+
search: search.value,
|
|
276
|
+
...selectedValues.value?.length && !search.value && { includedIds: selectedValues.value },
|
|
277
|
+
...params
|
|
278
|
+
});
|
|
279
|
+
count.value = itemsCount;
|
|
280
|
+
if (isSearch) {
|
|
281
|
+
const cashedMapItems = selectedValues.value.map(
|
|
282
|
+
(selectedValue) => [
|
|
283
|
+
selectedValue,
|
|
284
|
+
items.value.get(selectedValue)
|
|
285
|
+
]
|
|
286
|
+
);
|
|
287
|
+
items.value = new Map([...cashedMapItems]);
|
|
288
|
+
}
|
|
289
|
+
const resultsForMap = results.map(mapResultCallback);
|
|
290
|
+
items.value = new Map([...items.value, ...resultsForMap]);
|
|
291
|
+
page.value += 1;
|
|
292
|
+
loading.value = false;
|
|
293
|
+
} catch (error) {
|
|
294
|
+
if (isAborted(error))
|
|
295
|
+
return;
|
|
296
|
+
loading.value = false;
|
|
297
|
+
}
|
|
298
|
+
};
|
|
299
|
+
const loadItemsWithDebounce = core.useDebounceFn(async () => {
|
|
300
|
+
page.value = 1;
|
|
301
|
+
await loadItems(true);
|
|
302
|
+
}, 300);
|
|
303
|
+
vue.onBeforeUnmount(() => {
|
|
304
|
+
abort?.();
|
|
305
|
+
});
|
|
306
|
+
return {
|
|
307
|
+
search,
|
|
308
|
+
options,
|
|
309
|
+
loading,
|
|
310
|
+
hasNextPage,
|
|
311
|
+
items,
|
|
312
|
+
loadItems,
|
|
313
|
+
handleSearch
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
function useAsyncSelect({
|
|
318
|
+
apiMethod,
|
|
319
|
+
mapResultCallback,
|
|
320
|
+
includedIds = void 0,
|
|
321
|
+
params = void 0
|
|
322
|
+
}) {
|
|
323
|
+
const items = vue.ref(/* @__PURE__ */ new Map());
|
|
324
|
+
const loading = vue.ref(false);
|
|
325
|
+
const search = vue.ref("");
|
|
326
|
+
const count = vue.ref(0);
|
|
327
|
+
const page = vue.ref(1);
|
|
328
|
+
const options = vue.computed(() => [...items.value.values()]);
|
|
329
|
+
const hasNextPage = vue.computed(() => count.value > items.value.size);
|
|
330
|
+
const {
|
|
331
|
+
call: apiMethodWAC,
|
|
332
|
+
abort,
|
|
333
|
+
isAborted
|
|
334
|
+
} = useAbortController(apiMethod);
|
|
335
|
+
const handleSearch = async (value) => {
|
|
336
|
+
if (search.value === value)
|
|
337
|
+
return;
|
|
338
|
+
search.value = value;
|
|
339
|
+
await loadItemsWithDebounce();
|
|
340
|
+
};
|
|
341
|
+
const loadItems = async (isSearch) => {
|
|
342
|
+
loading.value = true;
|
|
343
|
+
try {
|
|
344
|
+
const { results, count: itemsCount } = await apiMethodWAC({
|
|
345
|
+
page: page.value,
|
|
346
|
+
search: search.value,
|
|
347
|
+
...includedIds?.length && includedIds,
|
|
348
|
+
...params
|
|
349
|
+
});
|
|
350
|
+
count.value = itemsCount;
|
|
351
|
+
if (isSearch) {
|
|
352
|
+
items.value = /* @__PURE__ */ new Map();
|
|
353
|
+
}
|
|
354
|
+
const resultsForMap = results.map(mapResultCallback);
|
|
355
|
+
items.value = new Map([...items.value, ...resultsForMap]);
|
|
356
|
+
page.value += 1;
|
|
357
|
+
loading.value = false;
|
|
358
|
+
} catch (error) {
|
|
359
|
+
if (isAborted(error))
|
|
360
|
+
return;
|
|
361
|
+
loading.value = false;
|
|
362
|
+
}
|
|
363
|
+
};
|
|
364
|
+
const loadItemsWithDebounce = core.useDebounceFn(async () => {
|
|
365
|
+
page.value = 1;
|
|
366
|
+
await loadItems(true);
|
|
367
|
+
}, 300);
|
|
368
|
+
vue.onBeforeUnmount(() => {
|
|
369
|
+
abort?.();
|
|
370
|
+
});
|
|
371
|
+
return {
|
|
372
|
+
search,
|
|
373
|
+
options,
|
|
374
|
+
loading,
|
|
375
|
+
hasNextPage,
|
|
376
|
+
items,
|
|
377
|
+
loadItems,
|
|
378
|
+
handleSearch
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
|
|
210
382
|
const _bem = (block, blockSuffix, element, modifier, modifierValue) => {
|
|
211
383
|
let cls = block;
|
|
212
384
|
if (blockSuffix) {
|
|
@@ -243,6 +415,7 @@ const useClassName = (block) => {
|
|
|
243
415
|
m,
|
|
244
416
|
em,
|
|
245
417
|
mv,
|
|
418
|
+
// CSS
|
|
246
419
|
cssVar,
|
|
247
420
|
cssVarName
|
|
248
421
|
};
|
|
@@ -302,6 +475,34 @@ function useDetectBrowser(versions, userAgent) {
|
|
|
302
475
|
});
|
|
303
476
|
}
|
|
304
477
|
|
|
478
|
+
function isEnabled(options) {
|
|
479
|
+
if (options?.debug !== void 0)
|
|
480
|
+
return options.debug;
|
|
481
|
+
const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined";
|
|
482
|
+
if (isBrowser) {
|
|
483
|
+
const debug = localStorage.getItem("debug");
|
|
484
|
+
if (debug === "true")
|
|
485
|
+
return true;
|
|
486
|
+
if (debug === "false")
|
|
487
|
+
return false;
|
|
488
|
+
}
|
|
489
|
+
const isNode = typeof process !== "undefined" && process.versions != null && process.versions.node != null;
|
|
490
|
+
if (isNode) {
|
|
491
|
+
const debug = process.env["NODE_DEBUG"];
|
|
492
|
+
if (debug === "true")
|
|
493
|
+
return true;
|
|
494
|
+
if (debug === "false")
|
|
495
|
+
return false;
|
|
496
|
+
}
|
|
497
|
+
return false;
|
|
498
|
+
}
|
|
499
|
+
function useLogger(scope, options) {
|
|
500
|
+
const prefix = options?.tag ? `[${scope.toUpperCase()}.${options.tag}]:` : `[${scope.toUpperCase()}]:`;
|
|
501
|
+
const logger = logdown(prefix, { prefixColor: options?.color });
|
|
502
|
+
logger.state.isEnabled = isEnabled(options);
|
|
503
|
+
return logger;
|
|
504
|
+
}
|
|
505
|
+
|
|
305
506
|
function validatePhone(rule, value) {
|
|
306
507
|
const phoneNumber = libphonenumberJs.parsePhoneNumberFromString(value);
|
|
307
508
|
return phoneNumber ? phoneNumber.isValid() : false;
|
|
@@ -323,9 +524,13 @@ exports.isElementIgnored = isElementIgnored;
|
|
|
323
524
|
exports.isEmptyObject = isEmptyObject;
|
|
324
525
|
exports.parsePhone = parsePhone;
|
|
325
526
|
exports.unformatNumber = unformatNumber;
|
|
527
|
+
exports.useAbortController = useAbortController;
|
|
528
|
+
exports.useAsyncMultiselect = useAsyncMultiselect;
|
|
529
|
+
exports.useAsyncSelect = useAsyncSelect;
|
|
326
530
|
exports.useClassName = useClassName;
|
|
327
531
|
exports.useDayjs = useDayjs;
|
|
328
532
|
exports.useDetectBrowser = useDetectBrowser;
|
|
533
|
+
exports.useLogger = useLogger;
|
|
329
534
|
exports.validateNumber = validateNumber;
|
|
330
535
|
exports.validatePhone = validatePhone;
|
|
331
536
|
exports.viewExample = viewExample;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
+
import * as vue from 'vue';
|
|
2
|
+
import { ComputedRef, WritableComputedRef } from 'vue';
|
|
1
3
|
import dayjs from 'dayjs';
|
|
2
4
|
import { Browser } from 'detect-browser';
|
|
5
|
+
import logdown from 'logdown';
|
|
3
6
|
|
|
4
7
|
declare function viewExample(value: string): string;
|
|
5
8
|
|
|
@@ -13,18 +16,24 @@ declare function createClassName(className: string): (...classNames: string[]) =
|
|
|
13
16
|
|
|
14
17
|
declare function genRandomNumber(min: number, max: number): number;
|
|
15
18
|
|
|
16
|
-
interface Options {
|
|
19
|
+
interface Options$1 {
|
|
17
20
|
hasOnlyLast4: boolean;
|
|
18
21
|
isHidden: boolean;
|
|
19
22
|
dot: string;
|
|
20
23
|
}
|
|
21
|
-
declare function getCardNumber(number: string, options?: Partial<Options>): string;
|
|
24
|
+
declare function getCardNumber(number: string, options?: Partial<Options$1>): string;
|
|
22
25
|
|
|
23
26
|
declare function getCountryFromPhone(phone: string): string;
|
|
24
27
|
|
|
25
28
|
interface Dictionary {
|
|
26
29
|
[keys: string | number]: any;
|
|
27
30
|
}
|
|
31
|
+
interface ResponseError {
|
|
32
|
+
status: number;
|
|
33
|
+
statusMessage: string;
|
|
34
|
+
message: string;
|
|
35
|
+
data: Record<string, string | string[]> | null;
|
|
36
|
+
}
|
|
28
37
|
|
|
29
38
|
declare function genLoadingTableRows(columns: Dictionary[], limit: number): {
|
|
30
39
|
id: number | undefined;
|
|
@@ -85,6 +94,42 @@ declare function isElementIgnored({ ignoredClasses, event, }?: {
|
|
|
85
94
|
|
|
86
95
|
declare function isEmptyObject(object: {}): boolean;
|
|
87
96
|
|
|
97
|
+
declare function useAbortController<T extends (...args: any[]) => any>(callback: T): {
|
|
98
|
+
call(...args: Parameters<T>): Promise<Awaited<ReturnType<T>>>;
|
|
99
|
+
abort(): void;
|
|
100
|
+
isAborted(error: ResponseError): boolean;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
declare function useAsyncMultiselect<T extends (...args: any[]) => any, K extends (params: Awaited<ReturnType<T>>['results'][0]) => any>({ apiMethod, mapResultCallback, selectedValues, params, }: {
|
|
104
|
+
apiMethod: T;
|
|
105
|
+
mapResultCallback: K;
|
|
106
|
+
selectedValues: ComputedRef<Parameters<T>[0]['includedIds']> | WritableComputedRef<Parameters<T>[0]['includedIds']>;
|
|
107
|
+
params?: Parameters<T>[0];
|
|
108
|
+
}): {
|
|
109
|
+
search: vue.Ref<string>;
|
|
110
|
+
options: ComputedRef<ReturnType<K>[1][]>;
|
|
111
|
+
loading: vue.Ref<boolean>;
|
|
112
|
+
hasNextPage: ComputedRef<boolean>;
|
|
113
|
+
items: vue.Ref<Map<ReturnType<K>[0], ReturnType<K>[1]>>;
|
|
114
|
+
loadItems: (isSearch?: boolean) => Promise<void>;
|
|
115
|
+
handleSearch: (value: string) => Promise<void>;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
declare function useAsyncSelect<T extends (...args: any[]) => any, K extends (params: Awaited<ReturnType<T>>['results'][0]) => any>({ apiMethod, mapResultCallback, includedIds, params, }: {
|
|
119
|
+
apiMethod: T;
|
|
120
|
+
mapResultCallback: K;
|
|
121
|
+
includedIds?: Parameters<T>[0]['includedIds'];
|
|
122
|
+
params?: Parameters<T>[0];
|
|
123
|
+
}): {
|
|
124
|
+
search: vue.Ref<string>;
|
|
125
|
+
options: vue.ComputedRef<ReturnType<K>[1][]>;
|
|
126
|
+
loading: vue.Ref<boolean>;
|
|
127
|
+
hasNextPage: vue.ComputedRef<boolean>;
|
|
128
|
+
items: vue.Ref<Map<ReturnType<K>[0], ReturnType<K>[1]>>;
|
|
129
|
+
loadItems: (isSearch?: boolean) => Promise<void>;
|
|
130
|
+
handleSearch: (value: string) => Promise<void>;
|
|
131
|
+
};
|
|
132
|
+
|
|
88
133
|
declare const useClassName: (block: string) => {
|
|
89
134
|
b: (blockSuffix?: string) => string;
|
|
90
135
|
e: (element?: string) => string;
|
|
@@ -100,7 +145,7 @@ declare const useClassName: (block: string) => {
|
|
|
100
145
|
* @see https://day.js.org/en/
|
|
101
146
|
*
|
|
102
147
|
* Использовать параметр timezone нужно крайне аккуратно,
|
|
103
|
-
* может привести к
|
|
148
|
+
* может привести к утечкам памяти
|
|
104
149
|
* @todo Подумать как сделать оптимальнее
|
|
105
150
|
*/
|
|
106
151
|
declare function useDayjs(timezone?: string): (date?: dayjs.ConfigType, format?: dayjs.OptionType, locale?: string, strict?: boolean) => dayjs.Dayjs;
|
|
@@ -118,6 +163,17 @@ type VersionsList = {
|
|
|
118
163
|
};
|
|
119
164
|
declare function useDetectBrowser(versions: VersionsList, userAgent?: any): BrowserInfo;
|
|
120
165
|
|
|
166
|
+
/**
|
|
167
|
+
* @see https://github.com/caiogondim/logdown.js#readme
|
|
168
|
+
*/
|
|
169
|
+
|
|
170
|
+
interface Options {
|
|
171
|
+
tag?: string;
|
|
172
|
+
color?: string;
|
|
173
|
+
debug?: boolean;
|
|
174
|
+
}
|
|
175
|
+
declare function useLogger(scope: string, options?: Options): logdown.Logger;
|
|
176
|
+
|
|
121
177
|
declare function validatePhone(rule: string, value: string): boolean;
|
|
122
178
|
|
|
123
|
-
export { BrowserInfo, VersionsList, camelizeKeys, createClassName, formatMoney, genLoadingTableRows, genRandomNumber, genUUID, getCardNumber, getCountryFromPhone, getPaginationLimit, getPaginationOffset, getPaginationPages, getPaginationParams, isElementIgnored, isEmptyObject, parsePhone, unformatNumber, useClassName, useDayjs, useDetectBrowser, validateNumber, validatePhone, viewExample };
|
|
179
|
+
export { BrowserInfo, VersionsList, camelizeKeys, createClassName, formatMoney, genLoadingTableRows, genRandomNumber, genUUID, getCardNumber, getCountryFromPhone, getPaginationLimit, getPaginationOffset, getPaginationPages, getPaginationParams, isElementIgnored, isEmptyObject, parsePhone, unformatNumber, useAbortController, useAsyncMultiselect, useAsyncSelect, useClassName, useDayjs, useDetectBrowser, useLogger, validateNumber, validatePhone, viewExample };
|
package/dist/index.mjs
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import { parsePhoneNumberFromString } from 'libphonenumber-js';
|
|
2
2
|
import { camelCase } from 'lodash-es';
|
|
3
|
+
import { ref, computed, onBeforeUnmount } from 'vue';
|
|
4
|
+
import { useDebounceFn } from '@vueuse/core';
|
|
3
5
|
import dayjs from 'dayjs';
|
|
4
|
-
import utc from 'dayjs/plugin/utc';
|
|
5
|
-
import tz from 'dayjs/plugin/timezone';
|
|
6
|
-
import updateLocale from 'dayjs/plugin/updateLocale';
|
|
7
|
-
import isToday from 'dayjs/plugin/isToday';
|
|
6
|
+
import utc from 'dayjs/plugin/utc.js';
|
|
7
|
+
import tz from 'dayjs/plugin/timezone.js';
|
|
8
|
+
import updateLocale from 'dayjs/plugin/updateLocale.js';
|
|
9
|
+
import isToday from 'dayjs/plugin/isToday.js';
|
|
8
10
|
import { detect } from 'detect-browser';
|
|
11
|
+
import logdown from 'logdown';
|
|
9
12
|
|
|
10
13
|
function viewExample(value) {
|
|
11
14
|
return value;
|
|
@@ -205,6 +208,175 @@ function isEmptyObject(object) {
|
|
|
205
208
|
return !object || !Object.keys(object).length;
|
|
206
209
|
}
|
|
207
210
|
|
|
211
|
+
function useAbortController(callback) {
|
|
212
|
+
let controller = null;
|
|
213
|
+
return {
|
|
214
|
+
async call(...args) {
|
|
215
|
+
if (controller) {
|
|
216
|
+
controller.abort();
|
|
217
|
+
}
|
|
218
|
+
controller = new AbortController();
|
|
219
|
+
let response;
|
|
220
|
+
if (args.length > 0) {
|
|
221
|
+
response = await callback(...args, {
|
|
222
|
+
signal: controller.signal
|
|
223
|
+
});
|
|
224
|
+
} else {
|
|
225
|
+
response = await callback({
|
|
226
|
+
signal: controller.signal
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
controller = null;
|
|
230
|
+
return response;
|
|
231
|
+
},
|
|
232
|
+
abort() {
|
|
233
|
+
if (!controller)
|
|
234
|
+
return;
|
|
235
|
+
controller.abort();
|
|
236
|
+
controller = null;
|
|
237
|
+
},
|
|
238
|
+
isAborted(error) {
|
|
239
|
+
return error.status === 418;
|
|
240
|
+
}
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function useAsyncMultiselect({
|
|
245
|
+
apiMethod,
|
|
246
|
+
mapResultCallback,
|
|
247
|
+
selectedValues,
|
|
248
|
+
params = void 0
|
|
249
|
+
}) {
|
|
250
|
+
const items = ref(/* @__PURE__ */ new Map());
|
|
251
|
+
const loading = ref(false);
|
|
252
|
+
const search = ref("");
|
|
253
|
+
const count = ref(0);
|
|
254
|
+
const page = ref(1);
|
|
255
|
+
const options = computed(() => [...items.value.values()]);
|
|
256
|
+
const hasNextPage = computed(() => count.value > items.value.size);
|
|
257
|
+
const {
|
|
258
|
+
call: apiMethodWAC,
|
|
259
|
+
abort,
|
|
260
|
+
isAborted
|
|
261
|
+
} = useAbortController(apiMethod);
|
|
262
|
+
const handleSearch = async (value) => {
|
|
263
|
+
if (search.value === value)
|
|
264
|
+
return;
|
|
265
|
+
search.value = value;
|
|
266
|
+
await loadItemsWithDebounce();
|
|
267
|
+
};
|
|
268
|
+
const loadItems = async (isSearch) => {
|
|
269
|
+
loading.value = true;
|
|
270
|
+
try {
|
|
271
|
+
const { results, count: itemsCount } = await apiMethodWAC({
|
|
272
|
+
page: page.value,
|
|
273
|
+
search: search.value,
|
|
274
|
+
...selectedValues.value?.length && !search.value && { includedIds: selectedValues.value },
|
|
275
|
+
...params
|
|
276
|
+
});
|
|
277
|
+
count.value = itemsCount;
|
|
278
|
+
if (isSearch) {
|
|
279
|
+
const cashedMapItems = selectedValues.value.map(
|
|
280
|
+
(selectedValue) => [
|
|
281
|
+
selectedValue,
|
|
282
|
+
items.value.get(selectedValue)
|
|
283
|
+
]
|
|
284
|
+
);
|
|
285
|
+
items.value = new Map([...cashedMapItems]);
|
|
286
|
+
}
|
|
287
|
+
const resultsForMap = results.map(mapResultCallback);
|
|
288
|
+
items.value = new Map([...items.value, ...resultsForMap]);
|
|
289
|
+
page.value += 1;
|
|
290
|
+
loading.value = false;
|
|
291
|
+
} catch (error) {
|
|
292
|
+
if (isAborted(error))
|
|
293
|
+
return;
|
|
294
|
+
loading.value = false;
|
|
295
|
+
}
|
|
296
|
+
};
|
|
297
|
+
const loadItemsWithDebounce = useDebounceFn(async () => {
|
|
298
|
+
page.value = 1;
|
|
299
|
+
await loadItems(true);
|
|
300
|
+
}, 300);
|
|
301
|
+
onBeforeUnmount(() => {
|
|
302
|
+
abort?.();
|
|
303
|
+
});
|
|
304
|
+
return {
|
|
305
|
+
search,
|
|
306
|
+
options,
|
|
307
|
+
loading,
|
|
308
|
+
hasNextPage,
|
|
309
|
+
items,
|
|
310
|
+
loadItems,
|
|
311
|
+
handleSearch
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function useAsyncSelect({
|
|
316
|
+
apiMethod,
|
|
317
|
+
mapResultCallback,
|
|
318
|
+
includedIds = void 0,
|
|
319
|
+
params = void 0
|
|
320
|
+
}) {
|
|
321
|
+
const items = ref(/* @__PURE__ */ new Map());
|
|
322
|
+
const loading = ref(false);
|
|
323
|
+
const search = ref("");
|
|
324
|
+
const count = ref(0);
|
|
325
|
+
const page = ref(1);
|
|
326
|
+
const options = computed(() => [...items.value.values()]);
|
|
327
|
+
const hasNextPage = computed(() => count.value > items.value.size);
|
|
328
|
+
const {
|
|
329
|
+
call: apiMethodWAC,
|
|
330
|
+
abort,
|
|
331
|
+
isAborted
|
|
332
|
+
} = useAbortController(apiMethod);
|
|
333
|
+
const handleSearch = async (value) => {
|
|
334
|
+
if (search.value === value)
|
|
335
|
+
return;
|
|
336
|
+
search.value = value;
|
|
337
|
+
await loadItemsWithDebounce();
|
|
338
|
+
};
|
|
339
|
+
const loadItems = async (isSearch) => {
|
|
340
|
+
loading.value = true;
|
|
341
|
+
try {
|
|
342
|
+
const { results, count: itemsCount } = await apiMethodWAC({
|
|
343
|
+
page: page.value,
|
|
344
|
+
search: search.value,
|
|
345
|
+
...includedIds?.length && includedIds,
|
|
346
|
+
...params
|
|
347
|
+
});
|
|
348
|
+
count.value = itemsCount;
|
|
349
|
+
if (isSearch) {
|
|
350
|
+
items.value = /* @__PURE__ */ new Map();
|
|
351
|
+
}
|
|
352
|
+
const resultsForMap = results.map(mapResultCallback);
|
|
353
|
+
items.value = new Map([...items.value, ...resultsForMap]);
|
|
354
|
+
page.value += 1;
|
|
355
|
+
loading.value = false;
|
|
356
|
+
} catch (error) {
|
|
357
|
+
if (isAborted(error))
|
|
358
|
+
return;
|
|
359
|
+
loading.value = false;
|
|
360
|
+
}
|
|
361
|
+
};
|
|
362
|
+
const loadItemsWithDebounce = useDebounceFn(async () => {
|
|
363
|
+
page.value = 1;
|
|
364
|
+
await loadItems(true);
|
|
365
|
+
}, 300);
|
|
366
|
+
onBeforeUnmount(() => {
|
|
367
|
+
abort?.();
|
|
368
|
+
});
|
|
369
|
+
return {
|
|
370
|
+
search,
|
|
371
|
+
options,
|
|
372
|
+
loading,
|
|
373
|
+
hasNextPage,
|
|
374
|
+
items,
|
|
375
|
+
loadItems,
|
|
376
|
+
handleSearch
|
|
377
|
+
};
|
|
378
|
+
}
|
|
379
|
+
|
|
208
380
|
const _bem = (block, blockSuffix, element, modifier, modifierValue) => {
|
|
209
381
|
let cls = block;
|
|
210
382
|
if (blockSuffix) {
|
|
@@ -241,6 +413,7 @@ const useClassName = (block) => {
|
|
|
241
413
|
m,
|
|
242
414
|
em,
|
|
243
415
|
mv,
|
|
416
|
+
// CSS
|
|
244
417
|
cssVar,
|
|
245
418
|
cssVarName
|
|
246
419
|
};
|
|
@@ -300,9 +473,37 @@ function useDetectBrowser(versions, userAgent) {
|
|
|
300
473
|
});
|
|
301
474
|
}
|
|
302
475
|
|
|
476
|
+
function isEnabled(options) {
|
|
477
|
+
if (options?.debug !== void 0)
|
|
478
|
+
return options.debug;
|
|
479
|
+
const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined";
|
|
480
|
+
if (isBrowser) {
|
|
481
|
+
const debug = localStorage.getItem("debug");
|
|
482
|
+
if (debug === "true")
|
|
483
|
+
return true;
|
|
484
|
+
if (debug === "false")
|
|
485
|
+
return false;
|
|
486
|
+
}
|
|
487
|
+
const isNode = typeof process !== "undefined" && process.versions != null && process.versions.node != null;
|
|
488
|
+
if (isNode) {
|
|
489
|
+
const debug = process.env["NODE_DEBUG"];
|
|
490
|
+
if (debug === "true")
|
|
491
|
+
return true;
|
|
492
|
+
if (debug === "false")
|
|
493
|
+
return false;
|
|
494
|
+
}
|
|
495
|
+
return false;
|
|
496
|
+
}
|
|
497
|
+
function useLogger(scope, options) {
|
|
498
|
+
const prefix = options?.tag ? `[${scope.toUpperCase()}.${options.tag}]:` : `[${scope.toUpperCase()}]:`;
|
|
499
|
+
const logger = logdown(prefix, { prefixColor: options?.color });
|
|
500
|
+
logger.state.isEnabled = isEnabled(options);
|
|
501
|
+
return logger;
|
|
502
|
+
}
|
|
503
|
+
|
|
303
504
|
function validatePhone(rule, value) {
|
|
304
505
|
const phoneNumber = parsePhoneNumberFromString(value);
|
|
305
506
|
return phoneNumber ? phoneNumber.isValid() : false;
|
|
306
507
|
}
|
|
307
508
|
|
|
308
|
-
export { camelizeKeys, createClassName, formatMoney, genLoadingTableRows, genRandomNumber, genUUID, getCardNumber, getCountryFromPhone, getPaginationLimit, getPaginationOffset, getPaginationPages, getPaginationParams, isElementIgnored, isEmptyObject, parsePhone, unformatNumber, useClassName, useDayjs, useDetectBrowser, validateNumber, validatePhone, viewExample };
|
|
509
|
+
export { camelizeKeys, createClassName, formatMoney, genLoadingTableRows, genRandomNumber, genUUID, getCardNumber, getCountryFromPhone, getPaginationLimit, getPaginationOffset, getPaginationPages, getPaginationParams, isElementIgnored, isEmptyObject, parsePhone, unformatNumber, useAbortController, useAsyncMultiselect, useAsyncSelect, useClassName, useDayjs, useDetectBrowser, useLogger, validateNumber, validatePhone, viewExample };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@karta.io/utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.24.0",
|
|
4
4
|
"description": "karta.io utils for admin and application",
|
|
5
5
|
"author": "Vitaly Blokhin <karambafe@gmail.com>",
|
|
6
6
|
"homepage": "",
|
|
@@ -19,12 +19,17 @@
|
|
|
19
19
|
"files": [
|
|
20
20
|
"dist"
|
|
21
21
|
],
|
|
22
|
+
"peerDependencies": {
|
|
23
|
+
"vue": "3.2.45"
|
|
24
|
+
},
|
|
22
25
|
"dependencies": {
|
|
23
|
-
"@karta.io/common-data": "^0.
|
|
26
|
+
"@karta.io/common-data": "^0.24.0",
|
|
27
|
+
"@vueuse/core": "9.10.0",
|
|
24
28
|
"dayjs": "1.11.6",
|
|
25
29
|
"detect-browser": "5.3.0",
|
|
26
30
|
"libphonenumber-js": "1.9.44",
|
|
27
|
-
"lodash-es": "4.17.21"
|
|
31
|
+
"lodash-es": "4.17.21",
|
|
32
|
+
"logdown": "3.3.1"
|
|
28
33
|
},
|
|
29
34
|
"devDependencies": {
|
|
30
35
|
"@qvant/prettier-config": "0.1.0",
|
|
@@ -33,8 +38,9 @@
|
|
|
33
38
|
"eslint-plugin-import": "2.26.0",
|
|
34
39
|
"prettier": "2.8.0",
|
|
35
40
|
"typescript": "4.9.4",
|
|
36
|
-
"unbuild": "1.
|
|
37
|
-
"vitest": "0.25.3"
|
|
41
|
+
"unbuild": "1.1.1",
|
|
42
|
+
"vitest": "0.25.3",
|
|
43
|
+
"vue": "^3.2.45"
|
|
38
44
|
},
|
|
39
45
|
"scripts": {
|
|
40
46
|
"dev": "vitest",
|