@nativerent/js-utils 1.0.1 → 1.0.3
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.d.ts +23 -18
- package/dist/index.js +28 -19
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +28 -20
- package/dist/index.mjs.map +1 -1
- package/jest.config.mjs +5 -0
- package/package.json +6 -2
- package/src/index.ts +54 -40
- package/src/types.d.ts +1 -1
- package/tests/tests.ts +297 -0
package/tests/tests.ts
CHANGED
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
import {
|
|
2
|
+
areDiff,
|
|
2
3
|
arrayDiff,
|
|
4
|
+
capitalize,
|
|
3
5
|
debounce,
|
|
6
|
+
filterObjectKeysByValues,
|
|
7
|
+
formatNumber,
|
|
8
|
+
formatNumberWithSign,
|
|
9
|
+
formatWithSign,
|
|
10
|
+
getFromObject,
|
|
11
|
+
getRandomItem,
|
|
12
|
+
getRandomStr,
|
|
4
13
|
isBool,
|
|
5
14
|
isDefined,
|
|
6
15
|
isFn,
|
|
@@ -11,9 +20,208 @@ import {
|
|
|
11
20
|
isUndef,
|
|
12
21
|
objectHasProp,
|
|
13
22
|
objectToQueryString,
|
|
23
|
+
parseObjectPathStr,
|
|
14
24
|
parseURL,
|
|
25
|
+
roundBigNum,
|
|
26
|
+
roundDown,
|
|
27
|
+
roundUp,
|
|
28
|
+
sortByAlphabet,
|
|
29
|
+
sumArray,
|
|
30
|
+
toCamelCase,
|
|
31
|
+
toPercent,
|
|
32
|
+
toSnakeCase,
|
|
15
33
|
} from "../src";
|
|
16
34
|
|
|
35
|
+
test.each([
|
|
36
|
+
[20, 40, 50],
|
|
37
|
+
[12, 56, 21.428571428571427],
|
|
38
|
+
[489, 232, 210.77586206896552],
|
|
39
|
+
])("toPercent", (a, b, expected) => {
|
|
40
|
+
const res = toPercent(a, b);
|
|
41
|
+
expect(res).toBe(expected);
|
|
42
|
+
expect(typeof res).toBe("number");
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test.each([
|
|
46
|
+
["camelcase", "camelcase"],
|
|
47
|
+
["camelCase", "camelCase"],
|
|
48
|
+
["camel-case", "camelCase"],
|
|
49
|
+
["CAMEL_CASE", "CAMELCASE"],
|
|
50
|
+
["CAMEL.CASE", "CAMELCASE"],
|
|
51
|
+
["CAMELCASE", "CAMELCASE"],
|
|
52
|
+
["fullScreenTGB", "fullScreenTGB"],
|
|
53
|
+
["typesWith-POPUP_TEASER", "typesWithPOPUPTEASER"],
|
|
54
|
+
["typesWith-ADVICE", "typesWithADVICE"],
|
|
55
|
+
["typesWith-button_phone", "typesWithButtonPhone"],
|
|
56
|
+
["typesWith-buttonPhone", "typesWithButtonPhone"],
|
|
57
|
+
["item.button_phone", "itemButtonPhone"],
|
|
58
|
+
["item.buttonPhone", "itemButtonPhone"],
|
|
59
|
+
["camel.case-camel_CASE", "camelCaseCamelCASE"],
|
|
60
|
+
])("toCamelCase", (str, expected) => {
|
|
61
|
+
expect(toCamelCase(str)).toBe(expected);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
test.each([
|
|
65
|
+
["camelcase", "camelcase"],
|
|
66
|
+
["camelCase", "camel_case"],
|
|
67
|
+
["camel-case", "camel_case"],
|
|
68
|
+
["CAMEL_CASE", "c_a_m_e_l_c_a_s_e"],
|
|
69
|
+
["CAMEL.CASE", "c_a_m_e_l_c_a_s_e"],
|
|
70
|
+
["CAMELCASE", "c_a_m_e_l_c_a_s_e"],
|
|
71
|
+
["fullScreenTGB", "full_screen_t_g_b"],
|
|
72
|
+
["typesWith-POPUP_TEASER", "types_with_p_o_p_u_p_t_e_a_s_e_r"],
|
|
73
|
+
["typesWith-ADVICE", "types_with_a_d_v_i_c_e"],
|
|
74
|
+
["typesWith-button_phone", "types_with_button_phone"],
|
|
75
|
+
["typesWith-buttonPhone", "types_with_button_phone"],
|
|
76
|
+
["item.button_phone", "item_button_phone"],
|
|
77
|
+
["item.buttonPhone", "item_button_phone"],
|
|
78
|
+
["camel.case-camel_CASE", "camel_case_camel_c_a_s_e"],
|
|
79
|
+
])("toSnakeCase", (str, expected) => {
|
|
80
|
+
expect(toSnakeCase(str)).toBe(expected);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
test.each([
|
|
84
|
+
["title", "Title"],
|
|
85
|
+
["another title", "Another title"],
|
|
86
|
+
["UPPER CASE TITLE", "UPPER CASE TITLE"],
|
|
87
|
+
["CapitalizedTitle", "CapitalizedTitle"],
|
|
88
|
+
["item.title", "Item.title"],
|
|
89
|
+
["item-title", "Item-title"],
|
|
90
|
+
["item_title", "Item_title"],
|
|
91
|
+
])("capitalize", (str, expected) => {
|
|
92
|
+
expect(capitalize(str)).toBe(expected);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
test.each([
|
|
96
|
+
["title", "Title", -1],
|
|
97
|
+
["another title", "title", -1],
|
|
98
|
+
["simple title", "compound title", 1],
|
|
99
|
+
["UPPER CASE TITLE", "TITLE", 1],
|
|
100
|
+
["кириллица", "сортировка", -1],
|
|
101
|
+
["ясень", "береза", 1],
|
|
102
|
+
["Водолей", "Лев", -1],
|
|
103
|
+
["питер", "мск", 1],
|
|
104
|
+
["zebra", "зебра", -1],
|
|
105
|
+
["аббревиатура", "York", 1],
|
|
106
|
+
])("sortByAlphabet", (first, second, expected) => {
|
|
107
|
+
expect(sortByAlphabet(first, second)).toBe(expected);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
test.each([
|
|
111
|
+
[undefined, 10],
|
|
112
|
+
[1, 1],
|
|
113
|
+
[43, 43],
|
|
114
|
+
[71, 71],
|
|
115
|
+
[100, 100],
|
|
116
|
+
[450, 100],
|
|
117
|
+
])("getRandomStr", (len, expectedLen) => {
|
|
118
|
+
const str = getRandomStr(len);
|
|
119
|
+
expect(str.length).toBe(expectedLen);
|
|
120
|
+
expect(typeof str).toBe("string");
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
test.each([
|
|
124
|
+
[[3, "ff", 3434]],
|
|
125
|
+
[[423434234]],
|
|
126
|
+
[[{ ff: "dd" }, 7, ["rrr", "tt", "y"]]],
|
|
127
|
+
])("getRandomItem", (arr) => {
|
|
128
|
+
expect(arr).toContain(getRandomItem(arr));
|
|
129
|
+
expect(arr).toEqual(arr);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
test.each([
|
|
133
|
+
[[2, 2, 1, 2], 7],
|
|
134
|
+
[[52, 122, 17, 368], 559],
|
|
135
|
+
[[0.099999999, 1.3028475, 1556.54677, 1000000.02], 1001557.969617499],
|
|
136
|
+
[[undefined, 456, "99", 13, "qwerty", null, 0.46778], 568.46778],
|
|
137
|
+
])("sumArray", (arr, expected) => {
|
|
138
|
+
expect(sumArray(arr)).toBe(expected);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
test.each([
|
|
142
|
+
[{ wq: "qwqw", q: 22, add: { s: [1, 2] } }, [22, "qwqw"], ["add"]],
|
|
143
|
+
[{ wq: "qwqw", q: 22, add: { s: [1, 2] } }, ["q", 22], ["wq", "add"]],
|
|
144
|
+
[{ 1: 2, key: "value", t: [1, 2], tt: false }, [false], ["1", "key", "t"]],
|
|
145
|
+
[{ type: undefined, enabled: true, date: null }, [undefined, null, true], []],
|
|
146
|
+
])("filterObjectKeysByValues", (array, value, expected) => {
|
|
147
|
+
expect(filterObjectKeysByValues(array, value)).toEqual(expected);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
test.each([
|
|
151
|
+
[{ k: "d" }, "k", "d"],
|
|
152
|
+
[{ title: null }, "title", null],
|
|
153
|
+
[{ 2: null, POS: undefined, g: { f: "roubles" } }, "POS", undefined],
|
|
154
|
+
[{ 2: null, POS: undefined, g: { f: "roubles" } }, "g", { f: "roubles" }],
|
|
155
|
+
[
|
|
156
|
+
{
|
|
157
|
+
array: [5, 657, 7],
|
|
158
|
+
obj: { q: 3, fdf: "gdfg", d: null, e: undefined },
|
|
159
|
+
},
|
|
160
|
+
"obj",
|
|
161
|
+
{ q: 3, fdf: "gdfg", d: null, e: undefined },
|
|
162
|
+
],
|
|
163
|
+
[
|
|
164
|
+
{
|
|
165
|
+
array: [5, 657, 7],
|
|
166
|
+
obj: {
|
|
167
|
+
q: 3,
|
|
168
|
+
fdf: "gdfg",
|
|
169
|
+
d: null,
|
|
170
|
+
e: undefined,
|
|
171
|
+
g: () => {},
|
|
172
|
+
},
|
|
173
|
+
},
|
|
174
|
+
"array",
|
|
175
|
+
[5, 657, 7],
|
|
176
|
+
],
|
|
177
|
+
])("getFromObject", (obj, key, expected) => {
|
|
178
|
+
expect(getFromObject(obj, key)).toEqual(expected);
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
test.each([
|
|
182
|
+
["item.title.value", ["item", "title", "value"]],
|
|
183
|
+
["item[title][value]", ["item", "title", "value"]],
|
|
184
|
+
["rules[1][fields.title]", ["rules", "1", "fields", "title"]],
|
|
185
|
+
["rules[1][fields[title]]", ["rules", "1", "fields", "title"]],
|
|
186
|
+
["rules.1[fields.title]", ["rules", "1", "fields", "title"]],
|
|
187
|
+
[
|
|
188
|
+
"rules.1[fields.title[regular][NR]]",
|
|
189
|
+
["rules", "1", "fields", "title", "regular", "NR"],
|
|
190
|
+
],
|
|
191
|
+
])("parseObjectPathStr", (path, expected) => {
|
|
192
|
+
expect(parseObjectPathStr(path)).toEqual(expected);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
test.each([
|
|
196
|
+
[34, 5, "34.00000"],
|
|
197
|
+
["4546.77", 1, "4 546.8"],
|
|
198
|
+
["1000.009", 4, "1 000.0090"],
|
|
199
|
+
["1000.009", 2, "1 000.01"],
|
|
200
|
+
])("formatNumber", (num, digits, expected) => {
|
|
201
|
+
expect(formatNumber(num, digits)).toBe(expected);
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
test.each([
|
|
205
|
+
[34, 5, "+34.00000"],
|
|
206
|
+
[-4545, 2, "-4 545.00"],
|
|
207
|
+
["4546.77", 1, "+4 546.8"],
|
|
208
|
+
["-1000.009", 4, "-1 000.0090"],
|
|
209
|
+
["1000.009", 2, "+1 000.01"],
|
|
210
|
+
])("formatNumberWithSign", (num, digits, expected) => {
|
|
211
|
+
expect(formatNumberWithSign(num, digits)).toBe(expected);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
test.each([
|
|
215
|
+
[34, undefined, "+34"],
|
|
216
|
+
[4545, undefined, "+4545"],
|
|
217
|
+
[4546.77, "4 546.77 руб", "+4 546.77 руб"],
|
|
218
|
+
[-1000.009, "1 000.01 ₽", "1 000.01 ₽"],
|
|
219
|
+
[1000.009, "1000.01$", "+1000.01$"],
|
|
220
|
+
[-0.009, undefined, "-0.009"],
|
|
221
|
+
])("formatWithSign", (num, numStr, expected) => {
|
|
222
|
+
expect(formatWithSign(num, numStr)).toBe(expected);
|
|
223
|
+
});
|
|
224
|
+
|
|
17
225
|
test.each([
|
|
18
226
|
[
|
|
19
227
|
[1, 2, 3],
|
|
@@ -222,3 +430,92 @@ test.each([
|
|
|
222
430
|
])("objectHasProp", (obj, prop, expected) => {
|
|
223
431
|
expect(objectHasProp(obj, prop)).toBe(expected);
|
|
224
432
|
});
|
|
433
|
+
|
|
434
|
+
test.each([
|
|
435
|
+
["str", null, true, true],
|
|
436
|
+
["str", null, false, true],
|
|
437
|
+
["", null, false, false],
|
|
438
|
+
["", null, true, true],
|
|
439
|
+
["", undefined, false, false],
|
|
440
|
+
["", undefined, true, true],
|
|
441
|
+
["", "", true, false],
|
|
442
|
+
["", "", false, false],
|
|
443
|
+
["1", "11", false, true],
|
|
444
|
+
["1", "11", true, true],
|
|
445
|
+
["{}", "object", true, true],
|
|
446
|
+
["{}", "object", false, true],
|
|
447
|
+
["{}", "[]", true, true],
|
|
448
|
+
["{}", "[]", false, true],
|
|
449
|
+
["{}", "[]", true, true],
|
|
450
|
+
["title", "Title", false, true],
|
|
451
|
+
["title", "Title", true, true],
|
|
452
|
+
["title", "title", true, false],
|
|
453
|
+
[true, "true", true, true],
|
|
454
|
+
[true, "true", false, true],
|
|
455
|
+
["4", 4, false, false],
|
|
456
|
+
["4", 4, true, true],
|
|
457
|
+
[1, true, false, false],
|
|
458
|
+
["1", true, false, false],
|
|
459
|
+
[1, true, true, true],
|
|
460
|
+
[0, false, false, false],
|
|
461
|
+
[0, false, true, true],
|
|
462
|
+
["0", false, false, false],
|
|
463
|
+
])("areDiff", (val1, val2, strict, expected) => {
|
|
464
|
+
expect(areDiff(val1, val2, strict)).toBe(expected);
|
|
465
|
+
});
|
|
466
|
+
|
|
467
|
+
test.each([
|
|
468
|
+
[0, 0],
|
|
469
|
+
[27, 27],
|
|
470
|
+
[99, 99],
|
|
471
|
+
[101, 100],
|
|
472
|
+
[277, 300],
|
|
473
|
+
[937, 900],
|
|
474
|
+
[967, 1000],
|
|
475
|
+
[1836, 1800],
|
|
476
|
+
[9284, 9300],
|
|
477
|
+
[29487, 30000],
|
|
478
|
+
[2569424, 2570000],
|
|
479
|
+
[11111111, 11110000],
|
|
480
|
+
[9999999000, 10000000000],
|
|
481
|
+
])("roundBigNum", (num, expected) => {
|
|
482
|
+
expect(roundBigNum(num)).toBe(expected);
|
|
483
|
+
});
|
|
484
|
+
|
|
485
|
+
test.each([
|
|
486
|
+
[0.9999889999, 0, 1.0],
|
|
487
|
+
[0.9999889999, 1, 1.0],
|
|
488
|
+
[0.9999889999, 2, 1.0],
|
|
489
|
+
[0.0000000001, 0, 1],
|
|
490
|
+
[0.0000000001, 1, 0.1],
|
|
491
|
+
[0.0000000001, 2, 0.01],
|
|
492
|
+
[0.0000000001, 3, 0.001],
|
|
493
|
+
[0.0000000001, 14, 0.0000000001],
|
|
494
|
+
[78.3333333333, 1, 78.4],
|
|
495
|
+
[78.3333333333, 0, 79],
|
|
496
|
+
[10000.354, 3, 10000.354],
|
|
497
|
+
[10000.354, 2, 10000.36],
|
|
498
|
+
[10000.354, 1, 10000.4],
|
|
499
|
+
[10000.354, 0, 10001],
|
|
500
|
+
])("roundUp", (num, precision, expected) => {
|
|
501
|
+
expect(roundUp(num, precision)).toBe(expected);
|
|
502
|
+
});
|
|
503
|
+
|
|
504
|
+
test.each([
|
|
505
|
+
[0.9999889999, 0, 0],
|
|
506
|
+
[0.9999889999, 1, 0.9],
|
|
507
|
+
[0.9999889999, 2, 0.99],
|
|
508
|
+
[0.0000000001, 0, 0],
|
|
509
|
+
[0.0000000001, 1, 0],
|
|
510
|
+
[0.0000000001, 2, 0],
|
|
511
|
+
[0.0000000001, 3, 0],
|
|
512
|
+
[0.0000000001, 14, 0.0000000001],
|
|
513
|
+
[78.3333333333, 1, 78.3],
|
|
514
|
+
[78.3333333333, 0, 78],
|
|
515
|
+
[10000.354, 3, 10000.354],
|
|
516
|
+
[10000.354, 2, 10000.35],
|
|
517
|
+
[10000.354, 1, 10000.3],
|
|
518
|
+
[10000.354, 0, 10000],
|
|
519
|
+
])("roundDown", (num, precision, expected) => {
|
|
520
|
+
expect(roundDown(num, precision)).toBe(expected);
|
|
521
|
+
});
|