@karta.io/utils 0.1.1 → 0.3.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 +7 -0
- package/dist/index.cjs +37 -6
- package/dist/index.d.ts +8 -1
- package/dist/index.mjs +35 -7
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -10,12 +10,19 @@
|
|
|
10
10
|
- genLoadingTableRows
|
|
11
11
|
- genRandomNumber
|
|
12
12
|
- getCardNumber
|
|
13
|
+
- getCountryFromPhone
|
|
13
14
|
- getPaginationParams
|
|
14
15
|
- isElementIgnored
|
|
15
16
|
- isEmptyObject
|
|
17
|
+
- pagination
|
|
18
|
+
- parsePhone
|
|
16
19
|
- unformatNumber
|
|
17
20
|
- validateNumber
|
|
18
21
|
|
|
19
22
|
## Хуки:
|
|
20
23
|
|
|
21
24
|
- useClassName
|
|
25
|
+
|
|
26
|
+
## Кастомные правила для валидатора
|
|
27
|
+
|
|
28
|
+
- validatePhone
|
package/dist/index.cjs
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
+
const libphonenumberJs = require('libphonenumber-js');
|
|
5
6
|
const camelCase = require('lodash/camelCase');
|
|
6
7
|
|
|
7
8
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e["default"] : e; }
|
|
@@ -49,6 +50,14 @@ function getCardNumber(number, options) {
|
|
|
49
50
|
return dot === "\u2022" ? result : result.replaceAll("\u2022", dot);
|
|
50
51
|
}
|
|
51
52
|
|
|
53
|
+
function getCountryFromPhone(phone) {
|
|
54
|
+
if (!phone)
|
|
55
|
+
return "";
|
|
56
|
+
const replacedPhone = phone.replace(/[a-zA-Z]/g, "");
|
|
57
|
+
const phoneNumber = libphonenumberJs.parsePhoneNumberFromString(replacedPhone);
|
|
58
|
+
return phoneNumber?.country ?? "";
|
|
59
|
+
}
|
|
60
|
+
|
|
52
61
|
function genLoadingTableRows(columns, limit) {
|
|
53
62
|
const row = columns.reduce((acc, curr) => ({ ...acc, [curr.key]: void 0 }), {});
|
|
54
63
|
return Array.from(Array(limit).keys()).map((item) => ({ id: item, ...row }));
|
|
@@ -146,6 +155,15 @@ function getPaginationPages({
|
|
|
146
155
|
return Math.ceil(count / validateNumber(limit, LIMIT_DEFAULT));
|
|
147
156
|
}
|
|
148
157
|
|
|
158
|
+
function parsePhone(phone) {
|
|
159
|
+
const replacedPhone = phone.replace(/[a-zA-Z]/g, "");
|
|
160
|
+
if (replacedPhone.length === 1 && replacedPhone !== "+") {
|
|
161
|
+
return `+${replacedPhone}`;
|
|
162
|
+
}
|
|
163
|
+
const phoneNumber = libphonenumberJs.parsePhoneNumberFromString(replacedPhone);
|
|
164
|
+
return phoneNumber ? phoneNumber.formatInternational() : replacedPhone;
|
|
165
|
+
}
|
|
166
|
+
|
|
149
167
|
function isElementIgnored({
|
|
150
168
|
ignoredClasses,
|
|
151
169
|
event
|
|
@@ -164,7 +182,7 @@ function isEmptyObject(object) {
|
|
|
164
182
|
return !object || !Object.keys(object).length;
|
|
165
183
|
}
|
|
166
184
|
|
|
167
|
-
const _bem = (block, blockSuffix, element, modifier) => {
|
|
185
|
+
const _bem = (block, blockSuffix, element, modifier, modifierValue) => {
|
|
168
186
|
let cls = block;
|
|
169
187
|
if (blockSuffix) {
|
|
170
188
|
cls += `-${blockSuffix}`;
|
|
@@ -175,34 +193,47 @@ const _bem = (block, blockSuffix, element, modifier) => {
|
|
|
175
193
|
if (modifier) {
|
|
176
194
|
cls += `_${modifier}`;
|
|
177
195
|
}
|
|
196
|
+
if (modifierValue) {
|
|
197
|
+
cls += `_${modifierValue}`;
|
|
198
|
+
}
|
|
178
199
|
return cls;
|
|
179
200
|
};
|
|
180
201
|
const useClassName = (block) => {
|
|
181
|
-
const b = (blockSuffix = "") => _bem(block, blockSuffix, "", "");
|
|
182
|
-
const e = (element) => element ? _bem(block, "", element, "") : "";
|
|
183
|
-
const m = (modifier, condition = true) => modifier && condition ? _bem(block, "", "", modifier) : "";
|
|
184
|
-
const em = (element, modifier, condition = true) => element && modifier && condition ? _bem(block, "", element, modifier) : "";
|
|
202
|
+
const b = (blockSuffix = "") => _bem(block, blockSuffix, "", "", "");
|
|
203
|
+
const e = (element) => element ? _bem(block, "", element, "", "") : "";
|
|
204
|
+
const m = (modifier, condition = true) => modifier && condition ? _bem(block, "", "", modifier, "") : "";
|
|
205
|
+
const em = (element, modifier, condition = true) => element && modifier && condition ? _bem(block, "", element, modifier, "") : "";
|
|
206
|
+
const mv = (modifier, modifierValue, condition = true) => modifier && modifierValue && condition ? _bem(block, "", "", modifier, modifierValue) : "";
|
|
185
207
|
return {
|
|
186
208
|
b,
|
|
187
209
|
e,
|
|
188
210
|
m,
|
|
189
|
-
em
|
|
211
|
+
em,
|
|
212
|
+
mv
|
|
190
213
|
};
|
|
191
214
|
};
|
|
192
215
|
|
|
216
|
+
function validatePhone(rule, value) {
|
|
217
|
+
const phoneNumber = libphonenumberJs.parsePhoneNumberFromString(value);
|
|
218
|
+
return phoneNumber ? phoneNumber.isValid() : false;
|
|
219
|
+
}
|
|
220
|
+
|
|
193
221
|
exports.camelizeKeys = camelizeKeys;
|
|
194
222
|
exports.createClassName = createClassName;
|
|
195
223
|
exports.formatMoney = formatMoney;
|
|
196
224
|
exports.genLoadingTableRows = genLoadingTableRows;
|
|
197
225
|
exports.genRandomNumber = genRandomNumber;
|
|
198
226
|
exports.getCardNumber = getCardNumber;
|
|
227
|
+
exports.getCountryFromPhone = getCountryFromPhone;
|
|
199
228
|
exports.getPaginationLimit = getPaginationLimit;
|
|
200
229
|
exports.getPaginationOffset = getPaginationOffset;
|
|
201
230
|
exports.getPaginationPages = getPaginationPages;
|
|
202
231
|
exports.getPaginationParams = getPaginationParams;
|
|
203
232
|
exports.isElementIgnored = isElementIgnored;
|
|
204
233
|
exports.isEmptyObject = isEmptyObject;
|
|
234
|
+
exports.parsePhone = parsePhone;
|
|
205
235
|
exports.unformatNumber = unformatNumber;
|
|
206
236
|
exports.useClassName = useClassName;
|
|
207
237
|
exports.validateNumber = validateNumber;
|
|
238
|
+
exports.validatePhone = validatePhone;
|
|
208
239
|
exports.viewExample = viewExample;
|
package/dist/index.d.ts
CHANGED
|
@@ -17,6 +17,8 @@ interface Options {
|
|
|
17
17
|
}
|
|
18
18
|
declare function getCardNumber(number: string, options?: Partial<Options>): string;
|
|
19
19
|
|
|
20
|
+
declare function getCountryFromPhone(phone: string): string;
|
|
21
|
+
|
|
20
22
|
interface Dictionary {
|
|
21
23
|
[keys: string | number]: any;
|
|
22
24
|
}
|
|
@@ -69,6 +71,8 @@ declare function getPaginationPages({ count, limit, }: {
|
|
|
69
71
|
limit: string | number;
|
|
70
72
|
}): number;
|
|
71
73
|
|
|
74
|
+
declare function parsePhone(phone: string): string;
|
|
75
|
+
|
|
72
76
|
declare function isElementIgnored({ ignoredClasses, event, }?: {
|
|
73
77
|
ignoredClasses?: string[];
|
|
74
78
|
event?: Event;
|
|
@@ -81,6 +85,9 @@ declare const useClassName: (block: string) => {
|
|
|
81
85
|
e: (element?: string) => string;
|
|
82
86
|
m: (modifier?: string, condition?: boolean) => string;
|
|
83
87
|
em: (element?: string, modifier?: string, condition?: boolean) => string;
|
|
88
|
+
mv: (modifier: string, modifierValue: string, condition?: boolean) => string;
|
|
84
89
|
};
|
|
85
90
|
|
|
86
|
-
|
|
91
|
+
declare function validatePhone(rule: string, value: string): boolean;
|
|
92
|
+
|
|
93
|
+
export { camelizeKeys, createClassName, formatMoney, genLoadingTableRows, genRandomNumber, getCardNumber, getCountryFromPhone, getPaginationLimit, getPaginationOffset, getPaginationPages, getPaginationParams, isElementIgnored, isEmptyObject, parsePhone, unformatNumber, useClassName, validateNumber, validatePhone, viewExample };
|
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { parsePhoneNumberFromString } from 'libphonenumber-js';
|
|
1
2
|
import camelCase from 'lodash/camelCase';
|
|
2
3
|
|
|
3
4
|
function viewExample(value) {
|
|
@@ -41,6 +42,14 @@ function getCardNumber(number, options) {
|
|
|
41
42
|
return dot === "\u2022" ? result : result.replaceAll("\u2022", dot);
|
|
42
43
|
}
|
|
43
44
|
|
|
45
|
+
function getCountryFromPhone(phone) {
|
|
46
|
+
if (!phone)
|
|
47
|
+
return "";
|
|
48
|
+
const replacedPhone = phone.replace(/[a-zA-Z]/g, "");
|
|
49
|
+
const phoneNumber = parsePhoneNumberFromString(replacedPhone);
|
|
50
|
+
return phoneNumber?.country ?? "";
|
|
51
|
+
}
|
|
52
|
+
|
|
44
53
|
function genLoadingTableRows(columns, limit) {
|
|
45
54
|
const row = columns.reduce((acc, curr) => ({ ...acc, [curr.key]: void 0 }), {});
|
|
46
55
|
return Array.from(Array(limit).keys()).map((item) => ({ id: item, ...row }));
|
|
@@ -138,6 +147,15 @@ function getPaginationPages({
|
|
|
138
147
|
return Math.ceil(count / validateNumber(limit, LIMIT_DEFAULT));
|
|
139
148
|
}
|
|
140
149
|
|
|
150
|
+
function parsePhone(phone) {
|
|
151
|
+
const replacedPhone = phone.replace(/[a-zA-Z]/g, "");
|
|
152
|
+
if (replacedPhone.length === 1 && replacedPhone !== "+") {
|
|
153
|
+
return `+${replacedPhone}`;
|
|
154
|
+
}
|
|
155
|
+
const phoneNumber = parsePhoneNumberFromString(replacedPhone);
|
|
156
|
+
return phoneNumber ? phoneNumber.formatInternational() : replacedPhone;
|
|
157
|
+
}
|
|
158
|
+
|
|
141
159
|
function isElementIgnored({
|
|
142
160
|
ignoredClasses,
|
|
143
161
|
event
|
|
@@ -156,7 +174,7 @@ function isEmptyObject(object) {
|
|
|
156
174
|
return !object || !Object.keys(object).length;
|
|
157
175
|
}
|
|
158
176
|
|
|
159
|
-
const _bem = (block, blockSuffix, element, modifier) => {
|
|
177
|
+
const _bem = (block, blockSuffix, element, modifier, modifierValue) => {
|
|
160
178
|
let cls = block;
|
|
161
179
|
if (blockSuffix) {
|
|
162
180
|
cls += `-${blockSuffix}`;
|
|
@@ -167,19 +185,29 @@ const _bem = (block, blockSuffix, element, modifier) => {
|
|
|
167
185
|
if (modifier) {
|
|
168
186
|
cls += `_${modifier}`;
|
|
169
187
|
}
|
|
188
|
+
if (modifierValue) {
|
|
189
|
+
cls += `_${modifierValue}`;
|
|
190
|
+
}
|
|
170
191
|
return cls;
|
|
171
192
|
};
|
|
172
193
|
const useClassName = (block) => {
|
|
173
|
-
const b = (blockSuffix = "") => _bem(block, blockSuffix, "", "");
|
|
174
|
-
const e = (element) => element ? _bem(block, "", element, "") : "";
|
|
175
|
-
const m = (modifier, condition = true) => modifier && condition ? _bem(block, "", "", modifier) : "";
|
|
176
|
-
const em = (element, modifier, condition = true) => element && modifier && condition ? _bem(block, "", element, modifier) : "";
|
|
194
|
+
const b = (blockSuffix = "") => _bem(block, blockSuffix, "", "", "");
|
|
195
|
+
const e = (element) => element ? _bem(block, "", element, "", "") : "";
|
|
196
|
+
const m = (modifier, condition = true) => modifier && condition ? _bem(block, "", "", modifier, "") : "";
|
|
197
|
+
const em = (element, modifier, condition = true) => element && modifier && condition ? _bem(block, "", element, modifier, "") : "";
|
|
198
|
+
const mv = (modifier, modifierValue, condition = true) => modifier && modifierValue && condition ? _bem(block, "", "", modifier, modifierValue) : "";
|
|
177
199
|
return {
|
|
178
200
|
b,
|
|
179
201
|
e,
|
|
180
202
|
m,
|
|
181
|
-
em
|
|
203
|
+
em,
|
|
204
|
+
mv
|
|
182
205
|
};
|
|
183
206
|
};
|
|
184
207
|
|
|
185
|
-
|
|
208
|
+
function validatePhone(rule, value) {
|
|
209
|
+
const phoneNumber = parsePhoneNumberFromString(value);
|
|
210
|
+
return phoneNumber ? phoneNumber.isValid() : false;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export { camelizeKeys, createClassName, formatMoney, genLoadingTableRows, genRandomNumber, getCardNumber, getCountryFromPhone, getPaginationLimit, getPaginationOffset, getPaginationPages, getPaginationParams, isElementIgnored, isEmptyObject, parsePhone, unformatNumber, useClassName, 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.3.0",
|
|
4
4
|
"description": "karta.io utils for admin and application",
|
|
5
5
|
"author": "Vitaly Blokhin <karambafe@gmail.com>",
|
|
6
6
|
"homepage": "",
|
|
@@ -19,7 +19,8 @@
|
|
|
19
19
|
"dist"
|
|
20
20
|
],
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@karta.io/common-data": "^0.
|
|
22
|
+
"@karta.io/common-data": "^0.3.0",
|
|
23
|
+
"libphonenumber-js": "1.9.44",
|
|
23
24
|
"lodash": "4.17.21"
|
|
24
25
|
},
|
|
25
26
|
"devDependencies": {
|
|
@@ -43,5 +44,5 @@
|
|
|
43
44
|
"prepush": "pnpm run prettier && pnpm run lint",
|
|
44
45
|
"pub": "pnpm build && pnpm publish --no-git-checks"
|
|
45
46
|
},
|
|
46
|
-
"readme": "# @karta.io/utils\n\nБиблиотека с общими хелперами, мапперами и плагинами\n\n## Хелперы:\n\n- formatMoney\n- createClassName\n- camelizeKeys\n- genLoadingTableRows\n- genRandomNumber\n- getCardNumber\n- getPaginationParams\n- isElementIgnored\n- isEmptyObject\n- unformatNumber\n- validateNumber\n\n## Хуки:\n\n- useClassName\n"
|
|
47
|
+
"readme": "# @karta.io/utils\n\nБиблиотека с общими хелперами, мапперами и плагинами\n\n## Хелперы:\n\n- formatMoney\n- createClassName\n- camelizeKeys\n- genLoadingTableRows\n- genRandomNumber\n- getCardNumber\n- getCountryFromPhone\n- getPaginationParams\n- isElementIgnored\n- isEmptyObject\n- pagination\n- parsePhone\n- unformatNumber\n- validateNumber\n\n## Хуки:\n\n- useClassName\n\n## Кастомные правила для валидатора\n\n- validatePhone\n"
|
|
47
48
|
}
|