@mongez/reinforcements 2.2.1 → 2.2.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.
Files changed (67) hide show
  1. package/README.md +917 -121
  2. package/cjs/array/countBy.d.ts +3 -1
  3. package/cjs/array/countBy.js +23 -0
  4. package/cjs/array/pushUnique.d.ts +1 -1
  5. package/cjs/array/unshiftUnique.d.ts +6 -0
  6. package/cjs/array/unshiftUnique.js +16 -0
  7. package/cjs/index.d.ts +3 -1
  8. package/cjs/index.js +6 -2
  9. package/cjs/mixed/shuffle/shuffle.d.ts +2 -2
  10. package/cjs/mixed/shuffle/shuffle.js +8 -6
  11. package/cjs/string/capitalize.js +1 -0
  12. package/cjs/string/extension.js +1 -0
  13. package/cjs/string/ltrim.js +1 -0
  14. package/cjs/string/readMoreChars.js +1 -0
  15. package/cjs/string/readMoreWords.js +1 -0
  16. package/cjs/string/removeFirst.js +1 -0
  17. package/cjs/string/removeLast.js +1 -0
  18. package/cjs/string/repeatsOf.js +1 -0
  19. package/cjs/string/replaceAll.js +1 -0
  20. package/cjs/string/replaceFirst.js +1 -0
  21. package/cjs/string/replaceLast.js +1 -0
  22. package/cjs/string/rtrim.js +1 -0
  23. package/cjs/string/startsWithArabic.d.ts +1 -1
  24. package/cjs/string/startsWithArabic.js +4 -3
  25. package/cjs/string/toCamelCase.js +1 -0
  26. package/cjs/string/toKebabCase.js +1 -0
  27. package/cjs/string/toSnakeCase.js +1 -0
  28. package/cjs/string/toStudlyCase.js +1 -0
  29. package/cjs/string/trim.js +1 -0
  30. package/cjs/string/ucfirst.js +1 -0
  31. package/esm/array/countBy.d.ts +3 -1
  32. package/esm/array/countBy.js +21 -0
  33. package/esm/array/pushUnique.d.ts +1 -1
  34. package/esm/array/unshiftUnique.d.ts +6 -0
  35. package/esm/array/unshiftUnique.js +14 -0
  36. package/esm/index.d.ts +3 -1
  37. package/esm/index.js +3 -1
  38. package/esm/mixed/shuffle/shuffle.d.ts +2 -2
  39. package/esm/mixed/shuffle/shuffle.js +8 -6
  40. package/esm/string/capitalize.js +1 -0
  41. package/esm/string/extension.js +1 -0
  42. package/esm/string/ltrim.js +1 -0
  43. package/esm/string/readMoreChars.js +1 -0
  44. package/esm/string/readMoreWords.js +1 -0
  45. package/esm/string/removeFirst.js +1 -0
  46. package/esm/string/removeLast.js +1 -0
  47. package/esm/string/repeatsOf.js +1 -0
  48. package/esm/string/replaceAll.js +1 -0
  49. package/esm/string/replaceFirst.js +1 -0
  50. package/esm/string/replaceLast.js +1 -0
  51. package/esm/string/rtrim.js +1 -0
  52. package/esm/string/startsWithArabic.d.ts +1 -1
  53. package/esm/string/startsWithArabic.js +4 -3
  54. package/esm/string/toCamelCase.js +1 -0
  55. package/esm/string/toKebabCase.js +1 -0
  56. package/esm/string/toSnakeCase.js +1 -0
  57. package/esm/string/toStudlyCase.js +1 -0
  58. package/esm/string/trim.js +1 -0
  59. package/esm/string/ucfirst.js +1 -0
  60. package/package.json +1 -1
  61. package/cjs/array/prependUnique.d.ts +0 -6
  62. package/cjs/object/obj.d.ts +0 -27
  63. package/cjs/object/obj.js +0 -30
  64. package/docs/VERSION-1.md +0 -1074
  65. package/esm/array/prependUnique.d.ts +0 -6
  66. package/esm/object/obj.d.ts +0 -27
  67. package/esm/object/obj.js +0 -28
package/docs/VERSION-1.md DELETED
@@ -1,1074 +0,0 @@
1
- # Reinforcements
2
-
3
- `Reinforcements` is a node js package written in `Typescript` to give a massive support for variant data like `Strings`, `Arrays`, `Objects` ... and so on.
4
-
5
- ## Table Of Contents
6
-
7
- - [Installation](#installation)
8
- - [Usage](#usage)
9
- - [Working with objects](#working0with-objects)
10
- - [Generating Random Values](#generating-random-values)
11
-
12
- ## Installation
13
-
14
- `yarn add @mongez/reinforcements`
15
-
16
- or using `npm`
17
-
18
- `npm i @mongez/reinforcements`
19
-
20
- ## Usage
21
-
22
- The following list illustrates what's included in this package.
23
-
24
- - `Obj` object/functions.
25
- - `Random` object.
26
- - `Str` object/functions.
27
- - Other utilities.
28
-
29
- ## Working with objects
30
-
31
- The following list illustrates all available object utilities, which are wrapped in `Obj` object.
32
-
33
- - `objGet` or `Obj.get`
34
- - `objSet` or `Obj.set`
35
- - `objMerge` or `Obj.merge`
36
- - `objClone` or `Obj.clone`
37
- - `objSort` or `Obj.sort`
38
- - `objOnly` or `Obj.only`
39
- - `objExcept` or `Obj.except`
40
- - `objFlatten` or `Obj.flatten`
41
-
42
- ### Getting value from object
43
-
44
- Using `Obj.get(object, key, defaultValue)` will allow us to get a value based on the given key.
45
-
46
- ```js
47
- import { Obj } from "@mongez/reinforcements";
48
-
49
- let user = {
50
- id: 1,
51
- name: {
52
- first: "Hasan",
53
- last: "Zohdy",
54
- },
55
- address: {
56
- country: "Egypt",
57
- building: {
58
- number: 12,
59
- floor: {
60
- number: 3,
61
- },
62
- },
63
- },
64
- };
65
-
66
- Obj.get(user, "id"); // 1
67
- Obj.get(user, "name"); // {first: 'Hasan', last: 'Zohdy'}
68
- Obj.get(user, "name.first"); // Hasan
69
- Obj.get(user, "address.country"); // Egypt
70
- Obj.get(user, "address.building.number"); // 12
71
- Obj.get(user, "address.building.floor.number"); // 3
72
- ```
73
-
74
- As we can see in the previous example, we can get values from objects using **dot.notation.syntax**.
75
-
76
- If the key is missing in the object, we may return default value instead.
77
-
78
- ```js
79
- Obj.get(user, "email", "no-email"); // no-email
80
- ```
81
-
82
- ### Setting value in object
83
-
84
- This works exactly but `Obj.set(object, key, value)` will set the value instead of getting it.
85
-
86
- ```js
87
- import { Obj } from "@mongez/reinforcements";
88
-
89
- let user = {
90
- id: 1,
91
- name: {
92
- first: "Hasan",
93
- last: "Zohdy",
94
- },
95
- address: {
96
- country: "Egypt",
97
- building: {
98
- number: 12,
99
- floor: {
100
- number: 3,
101
- },
102
- },
103
- },
104
- };
105
-
106
- Obj.set(user, "email", "hassanzohdy@gmail.com");
107
- Obj.set(user, "address.building.floor.apartment", 36);
108
- Obj.set(user, "job.title", "Software Engineer");
109
- ```
110
-
111
- In the previous example, we've three different cases, first case which would not be used with `Obj.set` which is setting one level key to the given object `user`, in this case we added `email` key.
112
-
113
- In the second scenario, we added a new nested key in `address.building.floor` object, which is `apartment`, this would be a nice case to use `Obj.set`.
114
-
115
- The last scenario, we don't have `job` key, the function will create `job` key then set `job.title` inside it.
116
-
117
- The final user object will be:
118
-
119
- ```json
120
- {
121
- "id": 1,
122
- "email": "hassanzohdy@gmail.com",
123
- "job": {
124
- "title": "Software Engineer"
125
- },
126
- "name": {
127
- "first": "Hasan",
128
- "last": "Zohdy"
129
- },
130
- "address": {
131
- "country": "Egypt",
132
- "building": {
133
- "number": 12,
134
- "floor": {
135
- "number": 3,
136
- "apartment": 36
137
- }
138
- }
139
- }
140
- }
141
- ```
142
-
143
- ### Merging objects deeply
144
-
145
- Another good feature from `Obj` object is to merge objects deeply.
146
-
147
- You may use `Obj.merge` or import `objMerge` directly from the package.
148
-
149
- ```js
150
- import { Obj } from "@mongez/reinforcements";
151
-
152
- const user = {
153
- id: 1,
154
- name: "Hasan Zohdy",
155
- };
156
-
157
- const userJob = {
158
- job: {
159
- title: "Software Engineer",
160
- level: "Senior",
161
- },
162
- };
163
-
164
- const userWithJob = Obj.merge(user, userJob);
165
- ```
166
-
167
- Final output:
168
-
169
- ```json
170
- {
171
- "id": 1,
172
- "name": "Hasan Zohdy",
173
- "job": {
174
- "title": "Software Engineer",
175
- "level": "Senior"
176
- }
177
- }
178
- ```
179
-
180
- But why not simply using the following syntax?
181
-
182
- ```js
183
- const user = {
184
- id: 1,
185
- name: "Hasan Zohdy",
186
- };
187
-
188
- const userJob = {
189
- job: {
190
- title: "Software Engineer",
191
- level: "Senior",
192
- },
193
- };
194
-
195
- const userWithJob = { ...user, ...userJob };
196
- // OR
197
- const userWithJob = Object.assign({}, user, userJob);
198
- ```
199
-
200
- In the previous example, that would be the proper approach as the merging depth here is simple, but let's take another example.
201
-
202
- ```js
203
- import { Obj } from "@mongez/reinforcements";
204
-
205
- const user = {
206
- id: 1,
207
- name: "Hasan Zohdy",
208
- job: {
209
- title: "Software Engineer",
210
- },
211
- };
212
-
213
- const userJob = {
214
- job: {
215
- level: "Senior",
216
- },
217
- };
218
-
219
- const userWithJob = Obj.merge(user, userJob);
220
- ```
221
-
222
- The output will be:
223
-
224
- ```json
225
- {
226
- "id": 1,
227
- "name": "Hasan Zohdy",
228
- "job": {
229
- "title": "Software Engineer",
230
- "level": "Senior"
231
- }
232
- }
233
- ```
234
-
235
- But when using spread syntax or `Object.assign` will give us a different value.
236
-
237
- ```js
238
- const user = {
239
- id: 1,
240
- name: "Hasan Zohdy",
241
- job: {
242
- title: "Software Engineer",
243
- },
244
- };
245
-
246
- const userJob = {
247
- job: {
248
- level: "Senior",
249
- },
250
- };
251
-
252
- const userWithJob = { ...user, ...userJob };
253
- // OR
254
- const userWithJob = Object.assign({}, user, userJob);
255
- ```
256
-
257
- ```json
258
- {
259
- "id": 1,
260
- "name": "Hasan Zohdy",
261
- "job": {
262
- "level": "Senior"
263
- }
264
- }
265
- ```
266
-
267
- ### Clone objects
268
-
269
- You can also make a **deep copy** for the given object using `Obj.clone` or `objClone`
270
-
271
- ```js
272
- const user = {
273
- id: 1,
274
- name: {
275
- first: "Hasan",
276
- },
277
- };
278
-
279
- const normalClonedUser = { ...user };
280
-
281
- normalClonedUser.name.first = "Ali";
282
-
283
- // both will be the same as only the top level is deeply copied but nested objects are shallow copies
284
- console.log(normalClonedUser.name.first); // Ali
285
- console.log(user.name.first); // Ali
286
- ```
287
-
288
- Now using `Obj.clone`
289
-
290
- ```js
291
- import { Obj } from "@mongez/reinforcements";
292
-
293
- const user = {
294
- id: 1,
295
- name: {
296
- first: "Hasan",
297
- },
298
- };
299
-
300
- const normalClonedUser = Obj.clone(user);
301
-
302
- cloned.name.first = "Ali";
303
-
304
- console.log(cloned.name.first); // Ali
305
- // Here the original object is kept untouched
306
- console.log(user.name.first); // Hasan
307
- ```
308
-
309
- ### Getting certain values from object
310
-
311
- To get a new object from the base object with only list of keys, use `Obj.only(object: object, keys: string[]): object`
312
-
313
- ```js
314
- import { Obj } from "@mongez/reinforcements";
315
-
316
- const user = {
317
- id: 1,
318
- name: "Hasan Zohdy",
319
- email: "hassanzohdy@gmail.com",
320
- job: {
321
- title: "Software Engineer",
322
- },
323
- address: {
324
- country: "Egypt",
325
- building: {
326
- number: 12,
327
- floor: {
328
- number: 3,
329
- },
330
- },
331
- },
332
- };
333
-
334
- const simpleUserData = Obj.only(user, ["id", "name", "email"]); // {id: 1, name: 'Hasan Zohdy', email: 'hassanzohdy@gmail.com'}
335
- ```
336
-
337
- ### Getting all object except for certain keys
338
-
339
- This is the reverse of `obj.only`, which returns the entire object except for the given keys.
340
-
341
- ```js
342
- import { Obj } from "@mongez/reinforcements";
343
-
344
- const user = {
345
- id: 1,
346
- name: "Hasan Zohdy",
347
- email: "hassanzohdy@gmail.com",
348
- job: {
349
- title: "Software Engineer",
350
- },
351
- address: {
352
- country: "Egypt",
353
- building: {
354
- number: 12,
355
- floor: {
356
- number: 3,
357
- },
358
- },
359
- },
360
- };
361
-
362
- const simpleUserData = Obj.except(user, ["id", "address", "email"]); // { name: 'Hasan Zohdy', email: 'hassanzohdy@gmail.com', job: {title: 'Software Engineer'}}
363
- ```
364
-
365
- ### Flatten objects
366
-
367
- We can flatten any big fat objects into one object, with only one dimension.
368
-
369
- ```js
370
- import { Obj } from "@mongez/reinforcements";
371
-
372
- const user = {
373
- id: 1,
374
- name: "Hasan Zohdy",
375
- email: "hassanzohdy@gmail.com",
376
- job: {
377
- title: "Software Engineer",
378
- },
379
- address: {
380
- country: "Egypt",
381
- building: {
382
- number: 12,
383
- floor: {
384
- number: 3,
385
- },
386
- },
387
- },
388
- };
389
-
390
- console.log(Obj.flatten(user));
391
- ```
392
-
393
- Output:
394
-
395
- ```json
396
- {
397
- "id": 1,
398
- "name": "Hasan Zohdy",
399
- "email": "hassanzohdy@gmail.com",
400
- "job.title": "Software Engineer",
401
- "address.country": "Egypt",
402
- "address.building.number": 12,
403
- "address.building.floor.number": 3
404
- }
405
- ```
406
-
407
- You may set the separator by passing second argument to the function.
408
-
409
- ```js
410
- import { Obj } from "@mongez/reinforcements";
411
-
412
- const user = {
413
- id: 1,
414
- name: "Hasan Zohdy",
415
- email: "hassanzohdy@gmail.com",
416
- job: {
417
- title: "Software Engineer",
418
- },
419
- address: {
420
- country: "Egypt",
421
- building: {
422
- number: 12,
423
- floor: {
424
- number: 3,
425
- },
426
- },
427
- },
428
- };
429
-
430
- console.log(Obj.flatten(user, "->"));
431
- ```
432
-
433
- Output:
434
-
435
- ```json
436
- {
437
- "id": 1,
438
- "name": "Hasan Zohdy",
439
- "email": "hassanzohdy@gmail.com",
440
- "job->title": "Software Engineer",
441
- "address->country": "Egypt",
442
- "address->building->number": 12,
443
- "address->building->floor->number": 3
444
- }
445
- ```
446
-
447
- ### Sort object by its keys
448
-
449
- To sort objects based on their keys alphabets recursively use `Obj.sort(object: object, recursive: boolean = true): object` function.
450
-
451
- ```js
452
- import { Obj } from "@mongez/reinforcements";
453
-
454
- const user = {
455
- id: 1,
456
- name: "Hasan Zohdy",
457
- email: "hassanzohdy@gmail.com",
458
- job: {
459
- title: "Software Engineer",
460
- },
461
- address: {
462
- country: "Egypt",
463
- building: {
464
- number: 12,
465
- floor: {
466
- number: 3,
467
- },
468
- },
469
- },
470
- };
471
-
472
- console.log(Obj.sort(user));
473
- ```
474
-
475
- Output:
476
-
477
- ```json
478
- {
479
- "address": {
480
- "building": {
481
- "floor": {
482
- "number": 3
483
- },
484
- "number": 12
485
- },
486
- "country": "Egypt"
487
- },
488
- "email": "hassanzohdy@gmail.com",
489
- "id": 1,
490
- "job": {
491
- "title": "Software Engineer"
492
- },
493
- "name": "Hasan Zohdy"
494
- }
495
- ```
496
-
497
- To sort the object only the first level, pass the second argument as false.
498
-
499
- ```js
500
- import { Obj } from "@mongez/reinforcements";
501
-
502
- const user = {
503
- id: 1,
504
- name: "Hasan Zohdy",
505
- email: "hassanzohdy@gmail.com",
506
- job: {
507
- title: "Software Engineer",
508
- },
509
- address: {
510
- country: "Egypt",
511
- building: {
512
- number: 12,
513
- floor: {
514
- number: 3,
515
- },
516
- },
517
- },
518
- };
519
-
520
- console.log(Obj.sort(user, false));
521
- ```
522
-
523
- Output:
524
-
525
- ```json
526
- {
527
- "address": {
528
- "country": "Egypt",
529
- "building": {
530
- "number": 12,
531
- "floor": {
532
- "number": 3
533
- }
534
- }
535
- },
536
- "email": "hassanzohdy@gmail.com",
537
- "id": 1,
538
- "job": {
539
- "title": "Software Engineer"
540
- },
541
- "name": "Hasan Zohdy"
542
- }
543
- ```
544
-
545
- ## Generating Random Values
546
-
547
- Another good feature is `Random` object, which allows us to generate variant random values of different types.
548
-
549
- ### Generate random string
550
-
551
- To generate a random string use `Random.string(length: number = 32): string` method.
552
-
553
- ```js
554
- import { Random } from "@mongez/reinforcements";
555
-
556
- Random.string(); // 4G8JyA4uM5YVMbkqVaoYnW6GzPcC64Fy
557
- ```
558
-
559
- To generate a random string with certain length, just pass the length value to the function.
560
-
561
- ```js
562
- import { Random } from "@mongez/reinforcements";
563
-
564
- Random.string(12); // P057C06VPwxl
565
- ```
566
-
567
- ### Generate random integer
568
-
569
- To generate a random integer use `Random.int(min: number = 1, max: number = 9999999): number` method.
570
-
571
- ```js
572
- import { Random } from "@mongez/reinforcements";
573
-
574
- Random.int(); // 7387115
575
- Random.int(); // 9411554
576
- Random.int(); // 691593
577
- ```
578
-
579
- To set min value, pass first argument with minimum value
580
-
581
- ```js
582
- import { Random } from "@mongez/reinforcements";
583
-
584
- Random.int(10); // 7387115
585
- ```
586
-
587
- To set min and max value, pass second argument as well with maximum value
588
-
589
- ```js
590
- import { Random } from "@mongez/reinforcements";
591
-
592
- Random.int(10, 100); // 36
593
- ```
594
-
595
- ### Generate random html id
596
-
597
- This function will generate a valid random html id string `Random.id(length: number = 6, startsWith: string = "el-"): string`.
598
-
599
- ```js
600
- import { Random } from "@mongez/reinforcements";
601
-
602
- Random.id(); // el-SDFefdvgtr2e3qw
603
- Random.id(); // el-fasrg3q
604
- ```
605
-
606
- You may set the length as first argument and/or set the id prefix as second argument (**default is el-**).
607
-
608
- ### Generate random boolean value
609
-
610
- To generate random boolean value use `Random.bool(): boolean` or `Random.boolean(): boolean`
611
-
612
- ```js
613
- import { Random } from "@mongez/reinforcements";
614
-
615
- Random.bool(); // true
616
- Random.bool(); // true
617
- Random.bool(); // false
618
- Random.boolean(); // false
619
- Random.boolean(); // true
620
- Random.boolean(); // false
621
- ```
622
-
623
- ## Round float numbers
624
-
625
- To round float numbers, use `round(value: number, precision: number = 2): number`.
626
-
627
- ```js
628
- import { round } from "@mongez/reinforcements";
629
-
630
- console.log(round(10.0001)); // 10
631
- console.log(round(10.0478878)); // 10.04
632
- console.log(round(10.6987894849)); // 10.69
633
- console.log(round(10.6987894849, 3)); // 10.698
634
- ```
635
-
636
- ## Working With Strings (Str)
637
-
638
- The following list defines all available string utilities
639
-
640
- - `capitalize`
641
- - `toCamelCase`
642
- - `toSnakeCase`
643
- - `toStudlyCase`
644
- - `ucfirst`
645
- - `toInputName`
646
- - `extension`
647
- - `readMoreChars`
648
- - `readMoreWords`
649
- - `replaceFirst`
650
- - `replaceLast`
651
- - `replaceAll`
652
- - `removeFirst`
653
- - `removeLast`
654
- - `repeatsOf`
655
- - `ltrim`
656
- - `trim`
657
- - `rtrim`
658
- - `startsWithArabic`
659
-
660
- ### Capitalize words
661
-
662
- Capitalize each word in string Separated by whitespace `capitalize(string: string): string`.
663
-
664
- ```js
665
- import { capitalize } from "@mongez/reinforcements";
666
-
667
- const words = "hello world";
668
-
669
- console.log(capitalize(words)); // Hello World
670
- ```
671
-
672
- ### Convert string to camel case
673
-
674
- Convert string to camel case, each word in string Separated by **whitespace** **underscores** or **dashes** `toCamelCase(string: string, separator: string = "\\s+|-|/|_|\\."): string`.
675
-
676
- ```js
677
- import { toCamelCase } from "@mongez/reinforcements";
678
-
679
- const words = "hello world";
680
-
681
- console.log(toCamelCase(words)); // helloWorld
682
- ```
683
-
684
- Any of following will be used as a separator for the text, `.` | `-` | `whitespace` | `/`, you can set the separator as second argument though.
685
-
686
- ### Convert string to snake case
687
-
688
- Convert string to snake case, each word in string Separated by **whitespace** or **dashes** `toSnakeCase(string: string): string`.
689
-
690
- The final output of the text will be all letters in lower case string separated by \_ **underscores**.
691
-
692
- ```js
693
- import { toSnakeCase } from "@mongez/reinforcements";
694
-
695
- const words = "hello world";
696
-
697
- console.log(toSnakeCase(words)); // hello_world
698
- ```
699
-
700
- ### Convert string to studly case
701
-
702
- Convert string to studly case, each word in string Separated by **whitespace**, **underscores** or **dashes** `toStudlyCase(string: string, separator: string = "-|\\.|_|\\s"): string`.
703
-
704
- The final output will be capitalizing each word and glue it together without any separators such as **whitespace**, **under scores** or **dashes**.
705
-
706
- ```js
707
- import { toStudlyCase } from "@mongez/reinforcements";
708
-
709
- const words = "hello world";
710
-
711
- console.log(toStudlyCase(words)); // HelloWorld
712
- ```
713
-
714
- ### Capitalize first word of string
715
-
716
- Capitalize only first word of string `ucfirst(string: string): string`.
717
-
718
- ```js
719
- import { ucfirst } from "@mongez/reinforcements";
720
-
721
- const words = "hello world";
722
-
723
- console.log(ucfirst(words)); // Hello world
724
- ```
725
-
726
- ### To input name
727
-
728
- Convert dot notation syntax to valid html input name `toInputName(string: string): string`.
729
-
730
- ```js
731
- import { toInputName } from "@mongez/reinforcements";
732
-
733
- const name = "user.name";
734
-
735
- console.log(toInputName(name)); // user[name]
736
- console.log(toInputName("keywords.en.list[]")); // keywords[en][list][]
737
- ```
738
-
739
- ### Get extension of string
740
-
741
- Get the last extension in the string, the string that is suffix to last dot `.`.
742
-
743
- `extension(string: string): string`
744
-
745
- ```js
746
- import { extension } from "@mongez/reinforcements";
747
-
748
- const file = "my-image.png";
749
-
750
- console.log(extension(file)); //png
751
- ```
752
-
753
- ### Read more characters
754
-
755
- This function will cut off the string when characters reach limit, and append three dots `...` at the end of the string.
756
-
757
- `readMoreChars(string: string, length: number, readMoreDots: string = '...'): string`
758
-
759
- ```js
760
- import { readMoreChars } from "@mongez/reinforcements";
761
-
762
- const string = "This is a fine words list";
763
-
764
- console.log(readMoreChars(string, 20)); // This is a fine words...
765
-
766
- // if the given limit is equal to or more than string length, then the entire string will be returned without any dots
767
- console.log(readMoreChars(string, 30)); // This is a fine words list
768
-
769
- // change the three dots to something else
770
-
771
- const string = "This is a fine words list";
772
-
773
- console.log(readMoreChars(string, 20, " >>")); // This is a fine words >>
774
- ```
775
-
776
- ### Read more words
777
-
778
- This function will cut off the string when words reach the given limit, and append three dots `...` at the end of the string.
779
-
780
- This works based on total number of whitespace in the string.
781
-
782
- `readMoreWords(string: string, length: number, readMoreDots: string = '...'): string`
783
-
784
- ```js
785
- import { readMoreWords } from "@mongez/reinforcements";
786
-
787
- const string = "This is a fine words list";
788
-
789
- console.log(readMoreWords(string, 4)); // This is a fine...
790
-
791
- // if the given limit is equal to or more than words length, then the entire string will be returned without any dots
792
- console.log(readMoreWords(string, 6)); // This is a fine words list
793
-
794
- // change the three dots to something else
795
-
796
- const string = "This is a fine words list";
797
-
798
- console.log(readMoreWords(string, 4, " >>")); // This is a fine >>
799
- ```
800
-
801
- ### Remove first matched string
802
-
803
- Remove the first matched needle to the given string.
804
-
805
- `removeFirst(string: string, needle: string): string`
806
-
807
- ```js
808
- import { removeFirst } from "@mongez/reinforcements";
809
-
810
- const words = "welcome home buddy, your are not safe at your home!";
811
-
812
- console.log(removeFirst(words, "home")); // welcome buddy, your are not safe at your home!
813
- ```
814
-
815
- ### Replace first matched string
816
-
817
- Replace the first matched needle to the given string.
818
-
819
- `replaceFirst(string:string, needle: string, replacement: string): string`
820
-
821
- ```js
822
- import { replaceFirst } from "@mongez/reinforcements";
823
-
824
- const words = "welcome home buddy, your are not safe at your home!";
825
-
826
- console.log(replaceFirst(words, "home", "country")); // welcome country buddy, your are not safe at your home!
827
- ```
828
-
829
- ### Replace last matched string
830
-
831
- Replace the last matched needle to the given string.
832
-
833
- `replaceLast(string:string, needle: string, replacement: string): string`
834
-
835
- ```js
836
- import { replaceLast } from "@mongez/reinforcements";
837
-
838
- const words = "welcome home buddy, your are not safe at your home!";
839
-
840
- console.log(replaceLast(words, "home", "country")); // welcome home buddy, your are not safe at your country!
841
- ```
842
-
843
- ### Replace all matched string
844
-
845
- Replace all matched words to the given string.
846
-
847
- `replaceAll(string: string, searchText:string, replacement: string): string`
848
-
849
- ```js
850
- import { replaceAll } from "@mongez/reinforcements";
851
-
852
- const words = "welcome home buddy, your are not safe at your home!";
853
-
854
- console.log(replaceAll(words, "home", "country")); // welcome country buddy, your are not safe at your country!
855
- ```
856
-
857
- ### Remove last matched string
858
-
859
- Remove the last matched needle to the given string.
860
-
861
- `removeLast(string: string, needle: string): string`
862
-
863
- ```js
864
- import { removeLast } from "@mongez/reinforcements";
865
-
866
- const words = "welcome home buddy, your are not safe at your home!";
867
-
868
- console.log(removeLast(words, "home")); // welcome home buddy, your are not safe at your !
869
- ```
870
-
871
- ### Count repeats of needle in a string
872
-
873
- Count repeats of a needle in the given string.
874
-
875
- `repeatsOf(string: string, needle: string, caseSensitive: boolean = true): number`
876
-
877
- ```js
878
- import { repeatsOf } from "@mongez/reinforcements";
879
-
880
- const words = "welcome home buddy, your are not safe at your home!";
881
-
882
- console.log(repeatsOf(words, "home")); // 2
883
- ```
884
-
885
- You may also detect number of repetitions ignoring case sensitive.
886
-
887
- ```js
888
- import { repeatsOf } from "@mongez/reinforcements";
889
-
890
- // note the first Home is capitalized
891
- const words = "welcome Home buddy, your are not safe at your home!";
892
-
893
- // case sensitive
894
- console.log(repeatsOf(words, "home")); // 1
895
- // case insensitive
896
- console.log(repeatsOf(words, "home", false)); // 2
897
- ```
898
-
899
- ### Trimming values from string
900
-
901
- Trim value from the start and the end of a string.
902
-
903
- `trim(string: string, needle: string = ' '): string`
904
-
905
- ```js
906
- import { trim } from "@mongez/reinforcements";
907
-
908
- const string = " space at the start and at the end ";
909
-
910
- console.log(trim(string)); // "space at the start and at the end"
911
- ```
912
-
913
- But why not use [String.trim()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim) directly? well they both will give you same functionality except that `trim()` function trims any value not only white space.
914
-
915
- Remove certain value:
916
-
917
- ```js
918
- import { trim } from "@mongez/reinforcements";
919
-
920
- const string = "/home/";
921
-
922
- console.log(trim(string, "/")); // home
923
- ```
924
-
925
- ### Left Trimming values from string
926
-
927
- Trim value from the start of a string.
928
-
929
- `ltrim(string: string, needle: string = ' '): string`
930
-
931
- ```js
932
- import { ltrim } from "@mongez/reinforcements";
933
-
934
- const string = " A space at the start and keep space at the end ";
935
-
936
- console.log(ltrim(string)); // "A space at the start and keep space at the end "
937
- ```
938
-
939
- Remove certain value:
940
-
941
- ```js
942
- import { ltrim } from "@mongez/reinforcements";
943
-
944
- const string = "home/";
945
-
946
- console.log(ltrim(string, "/")); // home/
947
- ```
948
-
949
- ### Right Trimming values from string
950
-
951
- Trim value from the end of a string.
952
-
953
- `rtrim(string: string, needle: string = ' '): string`
954
-
955
- ```js
956
- import { rtrim } from "@mongez/reinforcements";
957
-
958
- const string = " Keep A space at the start and remove space at the end ";
959
-
960
- console.log(rtrim(string)); // " Keep A space at the start and remove space at the end"
961
- ```
962
-
963
- Remove certain value:
964
-
965
- ```js
966
- import { ltrim } from "@mongez/reinforcements";
967
-
968
- const string = "home/";
969
-
970
- console.log(rtrim(string, "/")); // /home
971
- ```
972
-
973
- ## Detect if string starts with Arabic
974
-
975
- Determine if the string starts with Arabic letter.
976
-
977
- `startsWithArabic(text: string, trimmed: boolean = true): boolean {`
978
-
979
- ```js
980
- import { startsWithArabic } from "@mongez/reinforcements";
981
-
982
- const string = "English Text";
983
-
984
- const arabicString = "مرحبا";
985
-
986
- console.log(startsWithArabic(string)); // false
987
- console.log(startsWithArabic(arabicString)); // true
988
- ```
989
-
990
- ## Debounce
991
-
992
- `debounce(callback: Function, timer: number = 0): void`
993
-
994
- You can debounce your functions using `debounce` to prevent multiple calls.
995
-
996
- > This debounce function will be called instantly and will not return a callback function.
997
-
998
- ```tsx
999
- import { debounce } from "@mongez/reinforcements";
1000
-
1001
- function sendEmail(e: any) {
1002
- sendEmailApi(e.target);
1003
- }
1004
-
1005
- // If user clicked 5 times, it will make 5 ajax calls
1006
-
1007
- <button click={sendEmail}>Send Email</button>;
1008
- ```
1009
-
1010
- Now when using `debounce`
1011
-
1012
- ```tsx
1013
- import { debounce } from "@mongez/reinforcements";
1014
-
1015
- function sendEmail(e: any) {
1016
- debounce(() => {
1017
- sendEmailApi(e.target);
1018
- });
1019
- }
1020
-
1021
- // If user clicked 5 times, it will make only one ajax call
1022
-
1023
- <button click={sendEmail}>Send Email</button>;
1024
- ```
1025
-
1026
- You can also set a timer when to trigger the function
1027
-
1028
- ```tsx
1029
- import { debounce } from "@mongez/reinforcements";
1030
-
1031
- function sendEmail(e: any) {
1032
- // wait 3 seconds before calling the function
1033
- debounce(() => {
1034
- sendEmailApi(e.target);
1035
- }, 3000);
1036
- }
1037
-
1038
- // If user clicked 5 times, it will make only one ajax call
1039
-
1040
- <button click={sendEmail}>Send Email</button>;
1041
- ```
1042
-
1043
- ## Tests
1044
-
1045
- To run tests run `npm run test` or `yarn test`
1046
-
1047
- ## Change Log
1048
-
1049
- - 1.0.28 (11 Aug 2022)
1050
- - Fixed object merge call.
1051
- - 1.0.27 (11 Aug 2022)
1052
- - Added test.
1053
- - `toCamelCase` now will use the dot `.` as separator.
1054
- - `toCamelCase`'s separator is not explicit as second argument.
1055
- - 1.0.26 (08 Jun 2022)
1056
- - Removed `sprintf-js` from dependencies.
1057
- - 1.0.25 (08 Jun 2022)
1058
- - Fixed Flatten method with empty arrays.
1059
- - 1.0.23 (03 Jun 2022)
1060
- - Added [debounce](#debounce) function.
1061
- - Added `/` to be replaced in `toCamelCase` `toStudlyCase` and `toSnakeCase`.
1062
- - 1.0.22 (10 Feb 2022)
1063
- - Added [Obj.except](#getting-all-object-except-for-certain-keys) method.
1064
- - 1.0.21 (28 Jan 2022)
1065
- - Fixed `objOnly` method that adds undefined values if key does not exist on the given object.
1066
- - 1.0.19 (15 Jan 2022)
1067
- - Added [Clone objects](#clone-objects) function.
1068
- - 1.0.18 (15 Jan 2022)
1069
- - Added [Flatten objects](#flatten-objects) function.
1070
-
1071
- ## TODO
1072
-
1073
- - Create tests.
1074
- - Implements Array helpers list.