@karta.io/utils 0.24.0 → 0.25.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 +5 -3
- package/dist/index.cjs +109 -97
- package/dist/index.d.ts +47 -46
- package/dist/index.mjs +109 -97
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -4,20 +4,22 @@
|
|
|
4
4
|
|
|
5
5
|
## Хелперы:
|
|
6
6
|
|
|
7
|
-
- formatMoney
|
|
8
|
-
- createClassName
|
|
9
7
|
- camelizeKeys
|
|
8
|
+
- createClassName
|
|
9
|
+
- formatMoney
|
|
10
10
|
- genLoadingTableRows
|
|
11
11
|
- genRandomNumber
|
|
12
|
+
- genUUID
|
|
12
13
|
- getCardNumber
|
|
13
14
|
- getCountryFromPhone
|
|
14
|
-
- getPaginationParams
|
|
15
15
|
- isElementIgnored
|
|
16
16
|
- isEmptyObject
|
|
17
17
|
- pagination
|
|
18
18
|
- parsePhone
|
|
19
|
+
- snakelizeKeys
|
|
19
20
|
- unformatNumber
|
|
20
21
|
- validateNumber
|
|
22
|
+
- viewExample
|
|
21
23
|
|
|
22
24
|
## Хуки:
|
|
23
25
|
|
package/dist/index.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const libphonenumberJs = require('libphonenumber-js');
|
|
4
3
|
const lodashEs = require('lodash-es');
|
|
4
|
+
const libphonenumberJs = require('libphonenumber-js');
|
|
5
5
|
const vue = require('vue');
|
|
6
6
|
const core = require('@vueuse/core');
|
|
7
7
|
const dayjs = require('dayjs');
|
|
@@ -12,8 +12,27 @@ const isToday = require('dayjs/plugin/isToday.js');
|
|
|
12
12
|
const detectBrowser = require('detect-browser');
|
|
13
13
|
const logdown = require('logdown');
|
|
14
14
|
|
|
15
|
-
function
|
|
16
|
-
|
|
15
|
+
function camelizeKeys(object) {
|
|
16
|
+
if (Array.isArray(object)) {
|
|
17
|
+
return object.map((v) => camelizeKeys(v));
|
|
18
|
+
}
|
|
19
|
+
if (object !== null && object.constructor === Object) {
|
|
20
|
+
return Object.keys(object).reduce(
|
|
21
|
+
(result, key) => ({
|
|
22
|
+
...result,
|
|
23
|
+
[lodashEs.camelCase(key)]: camelizeKeys(object[key])
|
|
24
|
+
}),
|
|
25
|
+
{}
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
return object;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function createClassName(className) {
|
|
32
|
+
return function f(...classNames) {
|
|
33
|
+
const result = classNames.join("");
|
|
34
|
+
return (...args) => args.length ? f.apply(null, [result, ...args])() : result;
|
|
35
|
+
}(className);
|
|
17
36
|
}
|
|
18
37
|
|
|
19
38
|
function formatMoney(value, currency, options = { locale: "en-US", minimumFractionDigits: 2, maximumFractionDigits: 2 }) {
|
|
@@ -32,17 +51,33 @@ function formatMoney(value, currency, options = { locale: "en-US", minimumFracti
|
|
|
32
51
|
return formatter.format(convertedValue);
|
|
33
52
|
}
|
|
34
53
|
|
|
35
|
-
function
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
54
|
+
function genLoadingTableRows(columns, limit) {
|
|
55
|
+
const row = columns.reduce(
|
|
56
|
+
(acc, curr) => ({ ...acc, [curr.key]: void 0 }),
|
|
57
|
+
{}
|
|
58
|
+
);
|
|
59
|
+
return Array.from(Array(limit).keys()).map((item) => ({ id: item, ...row }));
|
|
40
60
|
}
|
|
41
61
|
|
|
42
62
|
function genRandomNumber(min, max) {
|
|
43
63
|
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
44
64
|
}
|
|
45
65
|
|
|
66
|
+
function genUUID() {
|
|
67
|
+
let d = new Date().getTime(), d2 = performance && performance.now && performance.now() * 1e3 || 0;
|
|
68
|
+
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
|
|
69
|
+
let r = Math.random() * 16;
|
|
70
|
+
if (d > 0) {
|
|
71
|
+
r = (d + r) % 16 | 0;
|
|
72
|
+
d = Math.floor(d / 16);
|
|
73
|
+
} else {
|
|
74
|
+
r = (d2 + r) % 16 | 0;
|
|
75
|
+
d2 = Math.floor(d2 / 16);
|
|
76
|
+
}
|
|
77
|
+
return (c == "x" ? r : r & 7 | 8).toString(16);
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
|
|
46
81
|
function getCardNumber(number, options) {
|
|
47
82
|
const { hasOnlyLast4 = false, isHidden = false, dot = "\u2022" } = options || {};
|
|
48
83
|
let result;
|
|
@@ -65,43 +100,22 @@ function getCountryFromPhone(phone) {
|
|
|
65
100
|
return phoneNumber?.country ?? "";
|
|
66
101
|
}
|
|
67
102
|
|
|
68
|
-
function
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
)
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
if (d > 0) {
|
|
81
|
-
r = (d + r) % 16 | 0;
|
|
82
|
-
d = Math.floor(d / 16);
|
|
83
|
-
} else {
|
|
84
|
-
r = (d2 + r) % 16 | 0;
|
|
85
|
-
d2 = Math.floor(d2 / 16);
|
|
86
|
-
}
|
|
87
|
-
return (c == "x" ? r : r & 7 | 8).toString(16);
|
|
88
|
-
});
|
|
103
|
+
function isElementIgnored({
|
|
104
|
+
ignoredClasses,
|
|
105
|
+
event
|
|
106
|
+
} = {}) {
|
|
107
|
+
if (ignoredClasses && event) {
|
|
108
|
+
return Array.from(event.composedPath()).filter((item) => item.nodeType && item.nodeType !== 9).reduce((acc, item) => {
|
|
109
|
+
const elementClasses = item.getAttribute("class");
|
|
110
|
+
const array = elementClasses ? elementClasses.split(" ") : [];
|
|
111
|
+
return acc.concat(array);
|
|
112
|
+
}, []).some((item) => ignoredClasses.includes(item));
|
|
113
|
+
}
|
|
114
|
+
return false;
|
|
89
115
|
}
|
|
90
116
|
|
|
91
|
-
function
|
|
92
|
-
|
|
93
|
-
return object.map((v) => camelizeKeys(v));
|
|
94
|
-
}
|
|
95
|
-
if (object !== null && object.constructor === Object) {
|
|
96
|
-
return Object.keys(object).reduce(
|
|
97
|
-
(result, key) => ({
|
|
98
|
-
...result,
|
|
99
|
-
[lodashEs.camelCase(key)]: camelizeKeys(object[key])
|
|
100
|
-
}),
|
|
101
|
-
{}
|
|
102
|
-
);
|
|
103
|
-
}
|
|
104
|
-
return object;
|
|
117
|
+
function isEmptyObject(object) {
|
|
118
|
+
return !object || !Object.keys(object).length;
|
|
105
119
|
}
|
|
106
120
|
|
|
107
121
|
function validateNumber(value, defaultValue) {
|
|
@@ -110,46 +124,6 @@ function validateNumber(value, defaultValue) {
|
|
|
110
124
|
return Number.isNaN(Number(value)) ? defaultValue : Number(value);
|
|
111
125
|
}
|
|
112
126
|
|
|
113
|
-
const LOCALE_RU = "ru-RU";
|
|
114
|
-
const LOCALE_EN = "en-US";
|
|
115
|
-
function unformatNumber(value) {
|
|
116
|
-
if (!value || typeof value !== "string")
|
|
117
|
-
return value;
|
|
118
|
-
const string = value.trim();
|
|
119
|
-
let locale;
|
|
120
|
-
const spaceIndex = string.lastIndexOf(" ");
|
|
121
|
-
const commaIndex = string.lastIndexOf(",");
|
|
122
|
-
const dotIndex = string.lastIndexOf(".");
|
|
123
|
-
const hasSpace = spaceIndex !== -1;
|
|
124
|
-
const hasComma = commaIndex !== -1;
|
|
125
|
-
const hasDot = dotIndex !== -1;
|
|
126
|
-
const commasCount = (string.match(/,/g) || []).length;
|
|
127
|
-
if (!Number.isNaN(Number(string)))
|
|
128
|
-
return Number(string);
|
|
129
|
-
if (hasSpace && hasComma && commaIndex > spaceIndex) {
|
|
130
|
-
locale = LOCALE_RU;
|
|
131
|
-
}
|
|
132
|
-
if (hasSpace && !hasComma && !hasDot) {
|
|
133
|
-
locale = LOCALE_RU;
|
|
134
|
-
}
|
|
135
|
-
if (hasComma && hasDot && dotIndex > commaIndex) {
|
|
136
|
-
locale = LOCALE_EN;
|
|
137
|
-
}
|
|
138
|
-
if (hasComma && !hasSpace && !hasDot && commasCount > 1) {
|
|
139
|
-
locale = LOCALE_EN;
|
|
140
|
-
}
|
|
141
|
-
if (commasCount === 1 && !hasSpace && !hasDot) {
|
|
142
|
-
locale = string.length - 1 - commaIndex === 3 ? LOCALE_EN : LOCALE_RU;
|
|
143
|
-
}
|
|
144
|
-
if (locale === LOCALE_RU) {
|
|
145
|
-
return Number(string.replaceAll(" ", "").replace(",", "."));
|
|
146
|
-
}
|
|
147
|
-
if (locale === LOCALE_EN) {
|
|
148
|
-
return Number(string.replaceAll(",", ""));
|
|
149
|
-
}
|
|
150
|
-
return value;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
127
|
const PAGE_DEFAULT = 1;
|
|
154
128
|
const LIMIT_DEFAULT = 25;
|
|
155
129
|
function getPaginationParams({
|
|
@@ -192,22 +166,60 @@ function parsePhone(phone) {
|
|
|
192
166
|
return phoneNumber ? phoneNumber.formatInternational() : replacedPhone;
|
|
193
167
|
}
|
|
194
168
|
|
|
195
|
-
function
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
} = {}) {
|
|
199
|
-
if (ignoredClasses && event) {
|
|
200
|
-
return Array.from(event.composedPath()).filter((item) => item.nodeType && item.nodeType !== 9).reduce((acc, item) => {
|
|
201
|
-
const elementClasses = item.getAttribute("class");
|
|
202
|
-
const array = elementClasses ? elementClasses.split(" ") : [];
|
|
203
|
-
return acc.concat(array);
|
|
204
|
-
}, []).some((item) => ignoredClasses.includes(item));
|
|
169
|
+
function snakelizeKeys(prop) {
|
|
170
|
+
if (Array.isArray(prop)) {
|
|
171
|
+
return prop.map((item) => snakelizeKeys(item));
|
|
205
172
|
}
|
|
206
|
-
|
|
173
|
+
if (prop !== null && prop !== void 0 && prop.constructor === Object) {
|
|
174
|
+
return Object.keys(prop).reduce(
|
|
175
|
+
(accum, current) => ({
|
|
176
|
+
...accum,
|
|
177
|
+
[lodashEs.snakeCase(current)]: snakelizeKeys(prop[current])
|
|
178
|
+
}),
|
|
179
|
+
{}
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
return prop;
|
|
207
183
|
}
|
|
208
184
|
|
|
209
|
-
|
|
210
|
-
|
|
185
|
+
const LOCALE_RU = "ru-RU";
|
|
186
|
+
const LOCALE_EN = "en-US";
|
|
187
|
+
function unformatNumber(value) {
|
|
188
|
+
if (!value || typeof value !== "string")
|
|
189
|
+
return value;
|
|
190
|
+
const string = value.trim();
|
|
191
|
+
let locale;
|
|
192
|
+
const spaceIndex = string.lastIndexOf(" ");
|
|
193
|
+
const commaIndex = string.lastIndexOf(",");
|
|
194
|
+
const dotIndex = string.lastIndexOf(".");
|
|
195
|
+
const hasSpace = spaceIndex !== -1;
|
|
196
|
+
const hasComma = commaIndex !== -1;
|
|
197
|
+
const hasDot = dotIndex !== -1;
|
|
198
|
+
const commasCount = (string.match(/,/g) || []).length;
|
|
199
|
+
if (!Number.isNaN(Number(string)))
|
|
200
|
+
return Number(string);
|
|
201
|
+
if (hasSpace && hasComma && commaIndex > spaceIndex) {
|
|
202
|
+
locale = LOCALE_RU;
|
|
203
|
+
}
|
|
204
|
+
if (hasSpace && !hasComma && !hasDot) {
|
|
205
|
+
locale = LOCALE_RU;
|
|
206
|
+
}
|
|
207
|
+
if (hasComma && hasDot && dotIndex > commaIndex) {
|
|
208
|
+
locale = LOCALE_EN;
|
|
209
|
+
}
|
|
210
|
+
if (hasComma && !hasSpace && !hasDot && commasCount > 1) {
|
|
211
|
+
locale = LOCALE_EN;
|
|
212
|
+
}
|
|
213
|
+
if (commasCount === 1 && !hasSpace && !hasDot) {
|
|
214
|
+
locale = string.length - 1 - commaIndex === 3 ? LOCALE_EN : LOCALE_RU;
|
|
215
|
+
}
|
|
216
|
+
if (locale === LOCALE_RU) {
|
|
217
|
+
return Number(string.replaceAll(" ", "").replace(",", "."));
|
|
218
|
+
}
|
|
219
|
+
if (locale === LOCALE_EN) {
|
|
220
|
+
return Number(string.replaceAll(",", ""));
|
|
221
|
+
}
|
|
222
|
+
return value;
|
|
211
223
|
}
|
|
212
224
|
|
|
213
225
|
function useAbortController(callback) {
|
|
@@ -523,6 +535,7 @@ exports.getPaginationParams = getPaginationParams;
|
|
|
523
535
|
exports.isElementIgnored = isElementIgnored;
|
|
524
536
|
exports.isEmptyObject = isEmptyObject;
|
|
525
537
|
exports.parsePhone = parsePhone;
|
|
538
|
+
exports.snakelizeKeys = snakelizeKeys;
|
|
526
539
|
exports.unformatNumber = unformatNumber;
|
|
527
540
|
exports.useAbortController = useAbortController;
|
|
528
541
|
exports.useAsyncMultiselect = useAsyncMultiselect;
|
|
@@ -533,4 +546,3 @@ exports.useDetectBrowser = useDetectBrowser;
|
|
|
533
546
|
exports.useLogger = useLogger;
|
|
534
547
|
exports.validateNumber = validateNumber;
|
|
535
548
|
exports.validatePhone = validatePhone;
|
|
536
|
-
exports.viewExample = viewExample;
|
package/dist/index.d.ts
CHANGED
|
@@ -4,7 +4,19 @@ import dayjs from 'dayjs';
|
|
|
4
4
|
import { Browser } from 'detect-browser';
|
|
5
5
|
import logdown from 'logdown';
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
interface Dictionary {
|
|
8
|
+
[keys: string | number]: any;
|
|
9
|
+
}
|
|
10
|
+
interface ResponseError {
|
|
11
|
+
status: number;
|
|
12
|
+
statusMessage: string;
|
|
13
|
+
message: string;
|
|
14
|
+
data: Record<string, string | string[]> | null;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
declare function camelizeKeys(object: Dictionary): Dictionary;
|
|
18
|
+
|
|
19
|
+
declare function createClassName(className: string): (...classNames: string[]) => string;
|
|
8
20
|
|
|
9
21
|
declare function formatMoney(value: string | number | null | undefined, currency?: string, options?: {
|
|
10
22
|
locale: string;
|
|
@@ -12,10 +24,15 @@ declare function formatMoney(value: string | number | null | undefined, currency
|
|
|
12
24
|
maximumFractionDigits: number;
|
|
13
25
|
}): string;
|
|
14
26
|
|
|
15
|
-
declare function
|
|
27
|
+
declare function genLoadingTableRows(columns: Dictionary[], limit: number): {
|
|
28
|
+
id: number | undefined;
|
|
29
|
+
key?: undefined;
|
|
30
|
+
}[];
|
|
16
31
|
|
|
17
32
|
declare function genRandomNumber(min: number, max: number): number;
|
|
18
33
|
|
|
34
|
+
declare function genUUID(): string;
|
|
35
|
+
|
|
19
36
|
interface Options$1 {
|
|
20
37
|
hasOnlyLast4: boolean;
|
|
21
38
|
isHidden: boolean;
|
|
@@ -25,26 +42,36 @@ declare function getCardNumber(number: string, options?: Partial<Options$1>): st
|
|
|
25
42
|
|
|
26
43
|
declare function getCountryFromPhone(phone: string): string;
|
|
27
44
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
status: number;
|
|
33
|
-
statusMessage: string;
|
|
34
|
-
message: string;
|
|
35
|
-
data: Record<string, string | string[]> | null;
|
|
36
|
-
}
|
|
45
|
+
declare function isElementIgnored({ ignoredClasses, event, }?: {
|
|
46
|
+
ignoredClasses?: string[];
|
|
47
|
+
event?: Event;
|
|
48
|
+
}): boolean;
|
|
37
49
|
|
|
38
|
-
declare function
|
|
39
|
-
id: number | undefined;
|
|
40
|
-
key?: undefined;
|
|
41
|
-
}[];
|
|
50
|
+
declare function isEmptyObject(object: {}): boolean;
|
|
42
51
|
|
|
43
|
-
declare function
|
|
52
|
+
declare function getPaginationParams({ page, limit, }: {
|
|
53
|
+
page: string | number;
|
|
54
|
+
limit: string | number;
|
|
55
|
+
}): {
|
|
56
|
+
limit: number;
|
|
57
|
+
offset: number;
|
|
58
|
+
};
|
|
59
|
+
declare function getPaginationLimit({ limit, }: {
|
|
60
|
+
limit: string | number;
|
|
61
|
+
}): number;
|
|
62
|
+
declare function getPaginationOffset({ page, limit, }: {
|
|
63
|
+
page: string | number;
|
|
64
|
+
limit: string | number;
|
|
65
|
+
}): number;
|
|
66
|
+
declare function getPaginationPages({ count, limit, }: {
|
|
67
|
+
count: number;
|
|
68
|
+
limit: string | number;
|
|
69
|
+
}): number;
|
|
44
70
|
|
|
45
|
-
declare function
|
|
71
|
+
declare function parsePhone(phone: string): string;
|
|
46
72
|
|
|
47
|
-
|
|
73
|
+
type SnakelizeKeysProp = Record<string | number, any>;
|
|
74
|
+
declare function snakelizeKeys(prop: SnakelizeKeysProp): SnakelizeKeysProp;
|
|
48
75
|
|
|
49
76
|
/**
|
|
50
77
|
* Принимает на вход число в виде строки или числа,
|
|
@@ -66,33 +93,7 @@ declare function validateNumber(value: string | number, defaultValue: number): n
|
|
|
66
93
|
*/
|
|
67
94
|
declare function unformatNumber(value: string | number): string | number;
|
|
68
95
|
|
|
69
|
-
declare function
|
|
70
|
-
page: string | number;
|
|
71
|
-
limit: string | number;
|
|
72
|
-
}): {
|
|
73
|
-
limit: number;
|
|
74
|
-
offset: number;
|
|
75
|
-
};
|
|
76
|
-
declare function getPaginationLimit({ limit, }: {
|
|
77
|
-
limit: string | number;
|
|
78
|
-
}): number;
|
|
79
|
-
declare function getPaginationOffset({ page, limit, }: {
|
|
80
|
-
page: string | number;
|
|
81
|
-
limit: string | number;
|
|
82
|
-
}): number;
|
|
83
|
-
declare function getPaginationPages({ count, limit, }: {
|
|
84
|
-
count: number;
|
|
85
|
-
limit: string | number;
|
|
86
|
-
}): number;
|
|
87
|
-
|
|
88
|
-
declare function parsePhone(phone: string): string;
|
|
89
|
-
|
|
90
|
-
declare function isElementIgnored({ ignoredClasses, event, }?: {
|
|
91
|
-
ignoredClasses?: string[];
|
|
92
|
-
event?: Event;
|
|
93
|
-
}): boolean;
|
|
94
|
-
|
|
95
|
-
declare function isEmptyObject(object: {}): boolean;
|
|
96
|
+
declare function validateNumber(value: string | number, defaultValue: number): number;
|
|
96
97
|
|
|
97
98
|
declare function useAbortController<T extends (...args: any[]) => any>(callback: T): {
|
|
98
99
|
call(...args: Parameters<T>): Promise<Awaited<ReturnType<T>>>;
|
|
@@ -176,4 +177,4 @@ declare function useLogger(scope: string, options?: Options): logdown.Logger;
|
|
|
176
177
|
|
|
177
178
|
declare function validatePhone(rule: string, value: string): boolean;
|
|
178
179
|
|
|
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
|
|
180
|
+
export { BrowserInfo, Dictionary, ResponseError, VersionsList, camelizeKeys, createClassName, formatMoney, genLoadingTableRows, genRandomNumber, genUUID, getCardNumber, getCountryFromPhone, getPaginationLimit, getPaginationOffset, getPaginationPages, getPaginationParams, isElementIgnored, isEmptyObject, parsePhone, snakelizeKeys, unformatNumber, useAbortController, useAsyncMultiselect, useAsyncSelect, useClassName, useDayjs, useDetectBrowser, useLogger, validateNumber, validatePhone };
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
+
import { camelCase, snakeCase } from 'lodash-es';
|
|
1
2
|
import { parsePhoneNumberFromString } from 'libphonenumber-js';
|
|
2
|
-
import { camelCase } from 'lodash-es';
|
|
3
3
|
import { ref, computed, onBeforeUnmount } from 'vue';
|
|
4
4
|
import { useDebounceFn } from '@vueuse/core';
|
|
5
5
|
import dayjs from 'dayjs';
|
|
@@ -10,8 +10,27 @@ import isToday from 'dayjs/plugin/isToday.js';
|
|
|
10
10
|
import { detect } from 'detect-browser';
|
|
11
11
|
import logdown from 'logdown';
|
|
12
12
|
|
|
13
|
-
function
|
|
14
|
-
|
|
13
|
+
function camelizeKeys(object) {
|
|
14
|
+
if (Array.isArray(object)) {
|
|
15
|
+
return object.map((v) => camelizeKeys(v));
|
|
16
|
+
}
|
|
17
|
+
if (object !== null && object.constructor === Object) {
|
|
18
|
+
return Object.keys(object).reduce(
|
|
19
|
+
(result, key) => ({
|
|
20
|
+
...result,
|
|
21
|
+
[camelCase(key)]: camelizeKeys(object[key])
|
|
22
|
+
}),
|
|
23
|
+
{}
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
return object;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function createClassName(className) {
|
|
30
|
+
return function f(...classNames) {
|
|
31
|
+
const result = classNames.join("");
|
|
32
|
+
return (...args) => args.length ? f.apply(null, [result, ...args])() : result;
|
|
33
|
+
}(className);
|
|
15
34
|
}
|
|
16
35
|
|
|
17
36
|
function formatMoney(value, currency, options = { locale: "en-US", minimumFractionDigits: 2, maximumFractionDigits: 2 }) {
|
|
@@ -30,17 +49,33 @@ function formatMoney(value, currency, options = { locale: "en-US", minimumFracti
|
|
|
30
49
|
return formatter.format(convertedValue);
|
|
31
50
|
}
|
|
32
51
|
|
|
33
|
-
function
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
52
|
+
function genLoadingTableRows(columns, limit) {
|
|
53
|
+
const row = columns.reduce(
|
|
54
|
+
(acc, curr) => ({ ...acc, [curr.key]: void 0 }),
|
|
55
|
+
{}
|
|
56
|
+
);
|
|
57
|
+
return Array.from(Array(limit).keys()).map((item) => ({ id: item, ...row }));
|
|
38
58
|
}
|
|
39
59
|
|
|
40
60
|
function genRandomNumber(min, max) {
|
|
41
61
|
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
42
62
|
}
|
|
43
63
|
|
|
64
|
+
function genUUID() {
|
|
65
|
+
let d = new Date().getTime(), d2 = performance && performance.now && performance.now() * 1e3 || 0;
|
|
66
|
+
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
|
|
67
|
+
let r = Math.random() * 16;
|
|
68
|
+
if (d > 0) {
|
|
69
|
+
r = (d + r) % 16 | 0;
|
|
70
|
+
d = Math.floor(d / 16);
|
|
71
|
+
} else {
|
|
72
|
+
r = (d2 + r) % 16 | 0;
|
|
73
|
+
d2 = Math.floor(d2 / 16);
|
|
74
|
+
}
|
|
75
|
+
return (c == "x" ? r : r & 7 | 8).toString(16);
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
44
79
|
function getCardNumber(number, options) {
|
|
45
80
|
const { hasOnlyLast4 = false, isHidden = false, dot = "\u2022" } = options || {};
|
|
46
81
|
let result;
|
|
@@ -63,43 +98,22 @@ function getCountryFromPhone(phone) {
|
|
|
63
98
|
return phoneNumber?.country ?? "";
|
|
64
99
|
}
|
|
65
100
|
|
|
66
|
-
function
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
)
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
if (d > 0) {
|
|
79
|
-
r = (d + r) % 16 | 0;
|
|
80
|
-
d = Math.floor(d / 16);
|
|
81
|
-
} else {
|
|
82
|
-
r = (d2 + r) % 16 | 0;
|
|
83
|
-
d2 = Math.floor(d2 / 16);
|
|
84
|
-
}
|
|
85
|
-
return (c == "x" ? r : r & 7 | 8).toString(16);
|
|
86
|
-
});
|
|
101
|
+
function isElementIgnored({
|
|
102
|
+
ignoredClasses,
|
|
103
|
+
event
|
|
104
|
+
} = {}) {
|
|
105
|
+
if (ignoredClasses && event) {
|
|
106
|
+
return Array.from(event.composedPath()).filter((item) => item.nodeType && item.nodeType !== 9).reduce((acc, item) => {
|
|
107
|
+
const elementClasses = item.getAttribute("class");
|
|
108
|
+
const array = elementClasses ? elementClasses.split(" ") : [];
|
|
109
|
+
return acc.concat(array);
|
|
110
|
+
}, []).some((item) => ignoredClasses.includes(item));
|
|
111
|
+
}
|
|
112
|
+
return false;
|
|
87
113
|
}
|
|
88
114
|
|
|
89
|
-
function
|
|
90
|
-
|
|
91
|
-
return object.map((v) => camelizeKeys(v));
|
|
92
|
-
}
|
|
93
|
-
if (object !== null && object.constructor === Object) {
|
|
94
|
-
return Object.keys(object).reduce(
|
|
95
|
-
(result, key) => ({
|
|
96
|
-
...result,
|
|
97
|
-
[camelCase(key)]: camelizeKeys(object[key])
|
|
98
|
-
}),
|
|
99
|
-
{}
|
|
100
|
-
);
|
|
101
|
-
}
|
|
102
|
-
return object;
|
|
115
|
+
function isEmptyObject(object) {
|
|
116
|
+
return !object || !Object.keys(object).length;
|
|
103
117
|
}
|
|
104
118
|
|
|
105
119
|
function validateNumber(value, defaultValue) {
|
|
@@ -108,46 +122,6 @@ function validateNumber(value, defaultValue) {
|
|
|
108
122
|
return Number.isNaN(Number(value)) ? defaultValue : Number(value);
|
|
109
123
|
}
|
|
110
124
|
|
|
111
|
-
const LOCALE_RU = "ru-RU";
|
|
112
|
-
const LOCALE_EN = "en-US";
|
|
113
|
-
function unformatNumber(value) {
|
|
114
|
-
if (!value || typeof value !== "string")
|
|
115
|
-
return value;
|
|
116
|
-
const string = value.trim();
|
|
117
|
-
let locale;
|
|
118
|
-
const spaceIndex = string.lastIndexOf(" ");
|
|
119
|
-
const commaIndex = string.lastIndexOf(",");
|
|
120
|
-
const dotIndex = string.lastIndexOf(".");
|
|
121
|
-
const hasSpace = spaceIndex !== -1;
|
|
122
|
-
const hasComma = commaIndex !== -1;
|
|
123
|
-
const hasDot = dotIndex !== -1;
|
|
124
|
-
const commasCount = (string.match(/,/g) || []).length;
|
|
125
|
-
if (!Number.isNaN(Number(string)))
|
|
126
|
-
return Number(string);
|
|
127
|
-
if (hasSpace && hasComma && commaIndex > spaceIndex) {
|
|
128
|
-
locale = LOCALE_RU;
|
|
129
|
-
}
|
|
130
|
-
if (hasSpace && !hasComma && !hasDot) {
|
|
131
|
-
locale = LOCALE_RU;
|
|
132
|
-
}
|
|
133
|
-
if (hasComma && hasDot && dotIndex > commaIndex) {
|
|
134
|
-
locale = LOCALE_EN;
|
|
135
|
-
}
|
|
136
|
-
if (hasComma && !hasSpace && !hasDot && commasCount > 1) {
|
|
137
|
-
locale = LOCALE_EN;
|
|
138
|
-
}
|
|
139
|
-
if (commasCount === 1 && !hasSpace && !hasDot) {
|
|
140
|
-
locale = string.length - 1 - commaIndex === 3 ? LOCALE_EN : LOCALE_RU;
|
|
141
|
-
}
|
|
142
|
-
if (locale === LOCALE_RU) {
|
|
143
|
-
return Number(string.replaceAll(" ", "").replace(",", "."));
|
|
144
|
-
}
|
|
145
|
-
if (locale === LOCALE_EN) {
|
|
146
|
-
return Number(string.replaceAll(",", ""));
|
|
147
|
-
}
|
|
148
|
-
return value;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
125
|
const PAGE_DEFAULT = 1;
|
|
152
126
|
const LIMIT_DEFAULT = 25;
|
|
153
127
|
function getPaginationParams({
|
|
@@ -190,22 +164,60 @@ function parsePhone(phone) {
|
|
|
190
164
|
return phoneNumber ? phoneNumber.formatInternational() : replacedPhone;
|
|
191
165
|
}
|
|
192
166
|
|
|
193
|
-
function
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
} = {}) {
|
|
197
|
-
if (ignoredClasses && event) {
|
|
198
|
-
return Array.from(event.composedPath()).filter((item) => item.nodeType && item.nodeType !== 9).reduce((acc, item) => {
|
|
199
|
-
const elementClasses = item.getAttribute("class");
|
|
200
|
-
const array = elementClasses ? elementClasses.split(" ") : [];
|
|
201
|
-
return acc.concat(array);
|
|
202
|
-
}, []).some((item) => ignoredClasses.includes(item));
|
|
167
|
+
function snakelizeKeys(prop) {
|
|
168
|
+
if (Array.isArray(prop)) {
|
|
169
|
+
return prop.map((item) => snakelizeKeys(item));
|
|
203
170
|
}
|
|
204
|
-
|
|
171
|
+
if (prop !== null && prop !== void 0 && prop.constructor === Object) {
|
|
172
|
+
return Object.keys(prop).reduce(
|
|
173
|
+
(accum, current) => ({
|
|
174
|
+
...accum,
|
|
175
|
+
[snakeCase(current)]: snakelizeKeys(prop[current])
|
|
176
|
+
}),
|
|
177
|
+
{}
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
return prop;
|
|
205
181
|
}
|
|
206
182
|
|
|
207
|
-
|
|
208
|
-
|
|
183
|
+
const LOCALE_RU = "ru-RU";
|
|
184
|
+
const LOCALE_EN = "en-US";
|
|
185
|
+
function unformatNumber(value) {
|
|
186
|
+
if (!value || typeof value !== "string")
|
|
187
|
+
return value;
|
|
188
|
+
const string = value.trim();
|
|
189
|
+
let locale;
|
|
190
|
+
const spaceIndex = string.lastIndexOf(" ");
|
|
191
|
+
const commaIndex = string.lastIndexOf(",");
|
|
192
|
+
const dotIndex = string.lastIndexOf(".");
|
|
193
|
+
const hasSpace = spaceIndex !== -1;
|
|
194
|
+
const hasComma = commaIndex !== -1;
|
|
195
|
+
const hasDot = dotIndex !== -1;
|
|
196
|
+
const commasCount = (string.match(/,/g) || []).length;
|
|
197
|
+
if (!Number.isNaN(Number(string)))
|
|
198
|
+
return Number(string);
|
|
199
|
+
if (hasSpace && hasComma && commaIndex > spaceIndex) {
|
|
200
|
+
locale = LOCALE_RU;
|
|
201
|
+
}
|
|
202
|
+
if (hasSpace && !hasComma && !hasDot) {
|
|
203
|
+
locale = LOCALE_RU;
|
|
204
|
+
}
|
|
205
|
+
if (hasComma && hasDot && dotIndex > commaIndex) {
|
|
206
|
+
locale = LOCALE_EN;
|
|
207
|
+
}
|
|
208
|
+
if (hasComma && !hasSpace && !hasDot && commasCount > 1) {
|
|
209
|
+
locale = LOCALE_EN;
|
|
210
|
+
}
|
|
211
|
+
if (commasCount === 1 && !hasSpace && !hasDot) {
|
|
212
|
+
locale = string.length - 1 - commaIndex === 3 ? LOCALE_EN : LOCALE_RU;
|
|
213
|
+
}
|
|
214
|
+
if (locale === LOCALE_RU) {
|
|
215
|
+
return Number(string.replaceAll(" ", "").replace(",", "."));
|
|
216
|
+
}
|
|
217
|
+
if (locale === LOCALE_EN) {
|
|
218
|
+
return Number(string.replaceAll(",", ""));
|
|
219
|
+
}
|
|
220
|
+
return value;
|
|
209
221
|
}
|
|
210
222
|
|
|
211
223
|
function useAbortController(callback) {
|
|
@@ -506,4 +518,4 @@ function validatePhone(rule, value) {
|
|
|
506
518
|
return phoneNumber ? phoneNumber.isValid() : false;
|
|
507
519
|
}
|
|
508
520
|
|
|
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
|
|
521
|
+
export { camelizeKeys, createClassName, formatMoney, genLoadingTableRows, genRandomNumber, genUUID, getCardNumber, getCountryFromPhone, getPaginationLimit, getPaginationOffset, getPaginationPages, getPaginationParams, isElementIgnored, isEmptyObject, parsePhone, snakelizeKeys, unformatNumber, useAbortController, useAsyncMultiselect, useAsyncSelect, useClassName, useDayjs, useDetectBrowser, useLogger, validateNumber, validatePhone };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@karta.io/utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.25.0",
|
|
4
4
|
"description": "karta.io utils for admin and application",
|
|
5
5
|
"author": "Vitaly Blokhin <karambafe@gmail.com>",
|
|
6
6
|
"homepage": "",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"vue": "3.2.45"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@karta.io/common-data": "^0.
|
|
26
|
+
"@karta.io/common-data": "^0.25.0",
|
|
27
27
|
"@vueuse/core": "9.10.0",
|
|
28
28
|
"dayjs": "1.11.6",
|
|
29
29
|
"detect-browser": "5.3.0",
|