@nativerent/js-utils 1.0.0 → 1.0.2

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/tests/tests.ts ADDED
@@ -0,0 +1,521 @@
1
+ import {
2
+ areDiff,
3
+ arrayDiff,
4
+ capitalize,
5
+ debounce,
6
+ filterObjectKeysByValues,
7
+ formatNumber,
8
+ formatNumberWithSign,
9
+ formatWithSign,
10
+ getFromObject,
11
+ getRandomItem,
12
+ getRandomStr,
13
+ isBool,
14
+ isDefined,
15
+ isFn,
16
+ isNum,
17
+ isObject,
18
+ isStr,
19
+ isString,
20
+ isUndef,
21
+ objectHasProp,
22
+ objectToQueryString,
23
+ parseObjectPathStr,
24
+ parseURL,
25
+ roundBigNum,
26
+ roundDown,
27
+ roundUp,
28
+ sortByAlphabet,
29
+ sumArray,
30
+ toCamelCase,
31
+ toPercent,
32
+ toSnakeCase,
33
+ } from "../src";
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
+
225
+ test.each([
226
+ [
227
+ [1, 2, 3],
228
+ [4, 5, 6],
229
+ [1, 2, 3, 4, 5, 6],
230
+ ],
231
+ [
232
+ ["q", "w", "e", "r", "t", "y"],
233
+ ["w", "q", "r"],
234
+ ["e", "t", "y"],
235
+ ],
236
+ [
237
+ ["multi dimensional array", "t", "e", "y"],
238
+ ["e", "t", "multi dimensional array", "y"],
239
+ [],
240
+ ],
241
+ [
242
+ [true, false, false, true, 1, 0],
243
+ [true, true, false, 0, false, 0, false, false],
244
+ [1],
245
+ ],
246
+ [
247
+ [null, false, "657", "#000"],
248
+ ["black", undefined, 0, false, 657, null],
249
+ ["657", "#000", "black", undefined, 0, 657],
250
+ ],
251
+ ])("arrayDiff", (arr1, arr2, expected) => {
252
+ expect(arrayDiff(arr1, arr2)).toEqual(expected);
253
+ });
254
+
255
+ test.each([
256
+ [{ d: "d" }, true],
257
+ [["d"], false],
258
+ [null, false],
259
+ [undefined, false],
260
+ [0, false],
261
+ [NaN, false],
262
+ ["str", false],
263
+ [120, false],
264
+ [Symbol("s"), false],
265
+ [false, false],
266
+ [true, false],
267
+ ])("isObject", (val, expected) => {
268
+ expect(isObject(val)).toBe(expected);
269
+ });
270
+
271
+ test.each([
272
+ [{ d: "d" }, false],
273
+ [["d"], false],
274
+ [null, false],
275
+ [undefined, false],
276
+ [0, false],
277
+ [() => "g", true],
278
+ [NaN, false],
279
+ ["str", false],
280
+ [120, false],
281
+ [Symbol("s"), false],
282
+ [function () {}, true],
283
+ [false, false],
284
+ [true, false],
285
+ ])("isFn", (val, expected) => {
286
+ expect(isFn(val)).toBe(expected);
287
+ });
288
+
289
+ test.each([
290
+ [{ d: "d" }, false],
291
+ [["d"], false],
292
+ [null, false],
293
+ [undefined, false],
294
+ [0, false],
295
+ [() => "g", false],
296
+ [NaN, false],
297
+ ["str", true],
298
+ [120, false],
299
+ [Symbol("s"), false],
300
+ [function () {}, false],
301
+ [false, false],
302
+ [true, false],
303
+ ["false", true],
304
+ ["true", true],
305
+ ["1", true],
306
+ ])("isStr", (val, expected) => {
307
+ expect(isStr(val)).toBe(expected);
308
+ });
309
+
310
+ test.each([
311
+ [{ d: "d" }, false],
312
+ [["d"], false],
313
+ [null, false],
314
+ [undefined, false],
315
+ [0, false],
316
+ [() => "g", false],
317
+ [NaN, false],
318
+ ["str", true],
319
+ [120, false],
320
+ [Symbol("s"), false],
321
+ [function () {}, false],
322
+ [false, false],
323
+ [true, false],
324
+ ["false", true],
325
+ ["true", true],
326
+ ["1", true],
327
+ ])("isString", (val, expected) => {
328
+ expect(isString(val)).toBe(expected);
329
+ });
330
+
331
+ test.each([
332
+ [{ d: "d" }, false],
333
+ [["d"], false],
334
+ [null, false],
335
+ [undefined, false],
336
+ [0, true],
337
+ [() => "g", false],
338
+ [NaN, true],
339
+ ["str", false],
340
+ [120, true],
341
+ [Symbol("s"), false],
342
+ [function () {}, false],
343
+ [false, false],
344
+ [true, false],
345
+ ["1", false],
346
+ ])("isNum", (val, expected) => {
347
+ expect(isNum(val)).toBe(expected);
348
+ });
349
+
350
+ test.each([
351
+ [{ d: "d" }, false],
352
+ [["d"], false],
353
+ [null, false],
354
+ [undefined, false],
355
+ [0, false],
356
+ [() => "g", false],
357
+ [NaN, false],
358
+ ["str", false],
359
+ [120, false],
360
+ [Symbol("s"), false],
361
+ [function () {}, false],
362
+ [false, true],
363
+ [true, true],
364
+ ["1", false],
365
+ ])("isBool", (val, expected) => {
366
+ expect(isBool(val)).toBe(expected);
367
+ });
368
+
369
+ test.each([
370
+ [{ d: "d" }, false],
371
+ [["d"], false],
372
+ [null, false],
373
+ [undefined, true],
374
+ [0, false],
375
+ [() => "g", false],
376
+ [NaN, false],
377
+ ["str", false],
378
+ [120, false],
379
+ [Symbol("s"), false],
380
+ [function () {}, false],
381
+ [false, false],
382
+ [true, false],
383
+ ["1", false],
384
+ ])("isUndef", (val, expected) => {
385
+ expect(isUndef(val)).toBe(expected);
386
+ });
387
+
388
+ test.each([
389
+ [{ d: "d" }, true],
390
+ [["d"], true],
391
+ [null, true],
392
+ [undefined, false],
393
+ [0, true],
394
+ [() => "g", true],
395
+ [NaN, true],
396
+ ["str", true],
397
+ [120, true],
398
+ [Symbol("s"), true],
399
+ [function () {}, true],
400
+ [false, true],
401
+ [true, true],
402
+ ["1", true],
403
+ ])("isDefined", (val, expected) => {
404
+ expect(isDefined(val)).toBe(expected);
405
+ });
406
+
407
+ test.each([[isDefined], [parseURL]])("debounce", (fn) => {
408
+ expect(typeof debounce(fn, 100)).toBe("function");
409
+ });
410
+
411
+ test.each([
412
+ [
413
+ { start: 134356456456, end: null, status: [1, 2, 4, 7] },
414
+ "start=134356456456&end=&status[]=1&status[]=2&status[]=4&status[]=7",
415
+ ],
416
+ [{}, ""],
417
+ [{ 1: 2 }, "1=2"],
418
+ [
419
+ { params: { checked: true, num: 2 } },
420
+ "params=%7B%22checked%22%3Atrue%2C%22num%22%3A2%7D",
421
+ ],
422
+ ])("objectToQueryString", (obj, expected) => {
423
+ expect(objectToQueryString(obj)).toBe(expected);
424
+ });
425
+
426
+ test.each([
427
+ [{ b: "b" }, "b", true],
428
+ [{ a: "b" }, "b", false],
429
+ [{ bb: "b" }, "b", false],
430
+ ])("objectHasProp", (obj, prop, expected) => {
431
+ expect(objectHasProp(obj, prop)).toBe(expected);
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
+ });