@mongez/reinforcements 2.2.0 → 2.2.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/README.md +917 -121
- package/cjs/array/countBy.d.ts +3 -1
- package/cjs/array/countBy.js +23 -0
- package/cjs/array/pushUnique.d.ts +1 -1
- package/cjs/array/unshiftUnique.d.ts +6 -0
- package/cjs/array/unshiftUnique.js +16 -0
- package/cjs/index.d.ts +3 -1
- package/cjs/index.js +6 -2
- package/cjs/mixed/shuffle/shuffle.d.ts +2 -2
- package/cjs/mixed/shuffle/shuffle.js +8 -6
- package/cjs/types.d.ts +0 -2
- package/esm/array/countBy.d.ts +3 -1
- package/esm/array/countBy.js +21 -0
- package/esm/array/pushUnique.d.ts +1 -1
- package/esm/array/unshiftUnique.d.ts +6 -0
- package/esm/array/unshiftUnique.js +14 -0
- package/esm/index.d.ts +3 -1
- package/esm/index.js +3 -1
- package/esm/mixed/shuffle/shuffle.d.ts +2 -2
- package/esm/mixed/shuffle/shuffle.js +8 -6
- package/esm/types.d.ts +0 -2
- package/package.json +1 -1
- package/cjs/array/prependUnique.d.ts +0 -6
- package/cjs/object/obj.d.ts +0 -27
- package/cjs/object/obj.js +0 -30
- package/esm/array/prependUnique.d.ts +0 -6
- package/esm/object/obj.d.ts +0 -27
- package/esm/object/obj.js +0 -28
package/README.md
CHANGED
|
@@ -14,7 +14,23 @@ or using `npm`
|
|
|
14
14
|
|
|
15
15
|
## Usage
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
We'll cover all reinforcements utilities by type, each type and mixed types and other utilities will be covered in a separate section.
|
|
18
|
+
|
|
19
|
+
## Objects
|
|
20
|
+
|
|
21
|
+
Here is the available utilities for objects:
|
|
22
|
+
|
|
23
|
+
- [get](#getting-value-from-an-object): Get value from an object using dot notation syntax.
|
|
24
|
+
- [set](#setting-value-in-object): Set value to an object using dot notation syntax.
|
|
25
|
+
- [merge](#merging-objects-deeply): Merge objects deeply (Not shallow).
|
|
26
|
+
- [clone](#clone-objects): Clone an object/array using deep clone (Not shallow).
|
|
27
|
+
- [only](#getting-only-certain-keys-from-object): Get only the given keys from an object and return it as a new object.
|
|
28
|
+
- [except](#getting-all-object-except-for-certain-keys): Get all object except for certain keys and return it as a new object.
|
|
29
|
+
- [unset](#unset-keys-from-object): Unset keys from an object.
|
|
30
|
+
- [flatten](#flatten-objects): Flatten a nested object into a single level object.
|
|
31
|
+
- [sort](#sort-object-by-its-keys): Sort object by its keys.
|
|
32
|
+
|
|
33
|
+
### Getting value from an object
|
|
18
34
|
|
|
19
35
|
To get a value from an object using `dot.notation` syntax, you can use `get` function.
|
|
20
36
|
|
|
@@ -285,7 +301,7 @@ console.log(cloned.name.first); // Ali
|
|
|
285
301
|
console.log(user.name.first); // Hasan
|
|
286
302
|
```
|
|
287
303
|
|
|
288
|
-
### Getting certain
|
|
304
|
+
### Getting only certain keys from object
|
|
289
305
|
|
|
290
306
|
To get a new object from the base object with only list of keys, use `only(object: object, keys: string[]): object`
|
|
291
307
|
|
|
@@ -593,188 +609,760 @@ Output:
|
|
|
593
609
|
}
|
|
594
610
|
```
|
|
595
611
|
|
|
596
|
-
##
|
|
612
|
+
## Arrays
|
|
597
613
|
|
|
598
|
-
|
|
614
|
+
Now let's move to arrays utilities.
|
|
615
|
+
|
|
616
|
+
- [Group By](#group-by): Group array of objects by a certain key/keys.
|
|
617
|
+
- [Pluck](#pluck): Get an array of values from an array of objects.
|
|
618
|
+
- [Chunk](#chunk): Split array into chunks.
|
|
619
|
+
- [Count](#count): Count the number of item that contains the given key or callback.
|
|
620
|
+
- [Count By](#count-by): Count total occurrence of values for the given key.
|
|
621
|
+
- [Even](#even): Get even numbers from an array or by given key.
|
|
622
|
+
- [Odd](#odd): Get odd numbers from an array or by given key.
|
|
623
|
+
- [Even Indexes](#even-indexes): Get elements in even indexes of an array.
|
|
624
|
+
- [Odd Indexes](#odd-indexes): Get elements in odd indexes of an array.
|
|
625
|
+
- [Min](#min): Get the minimum value from an array or by given key.
|
|
626
|
+
- [Max](#max): Get the maximum value from an array or by given key.
|
|
627
|
+
- [Sum](#sum): Get the sum of all values in an array or by given key.
|
|
628
|
+
- [Average](#average): Get the average of all values in an array or by given key.
|
|
629
|
+
- [Median](#median): Get the median of all values in an array or by given key.
|
|
630
|
+
- [Unique](#unique): Get unique values from an array.
|
|
631
|
+
- [Push Unique](#push-unique): Push a value or more to an array if it doesn't exist.
|
|
632
|
+
- [Unshift Unique](#unshift-unique): Add a value or more to the beginning of an array if it doesn't exist.
|
|
633
|
+
|
|
634
|
+
### Pluck
|
|
635
|
+
|
|
636
|
+
Pluck a certain key/keys from an array of objects.
|
|
637
|
+
|
|
638
|
+
`pluck(array: any[], key?: string | string[]): any[]`
|
|
599
639
|
|
|
600
640
|
```ts
|
|
601
|
-
import {
|
|
641
|
+
import { pluck } from "@mongez/reinforcements";
|
|
602
642
|
|
|
603
|
-
|
|
643
|
+
const array = [
|
|
644
|
+
{ name: "John", age: 20 },
|
|
645
|
+
{ name: "Jane", age: 25 },
|
|
646
|
+
{ name: "Jack", age: 30 },
|
|
647
|
+
];
|
|
604
648
|
|
|
605
|
-
console.log(
|
|
649
|
+
console.log(pluck(array, "name")); // ["John", "Jane", "Jack"]
|
|
650
|
+
```
|
|
606
651
|
|
|
607
|
-
|
|
652
|
+
You may also pluck multiple keys by passing an array of keys.
|
|
608
653
|
|
|
609
|
-
|
|
654
|
+
```ts
|
|
655
|
+
import { pluck } from "@mongez/reinforcements";
|
|
610
656
|
|
|
611
|
-
|
|
657
|
+
const array = [
|
|
658
|
+
{ name: "John", age: 20, job: "developer" },
|
|
659
|
+
{ name: "Jane", age: 25, job: "designer" },
|
|
660
|
+
{ name: "Jack", age: 30, job: "manager" },
|
|
661
|
+
];
|
|
612
662
|
|
|
613
|
-
console.log(
|
|
663
|
+
console.log(pluck(array, ["name", "job"])); // [{name: "John", job: "developer"}, {name: "Jane", job: "designer"}, {name: "Jack", job: "manager"}]
|
|
664
|
+
```
|
|
614
665
|
|
|
615
|
-
|
|
666
|
+
### Group By
|
|
616
667
|
|
|
617
|
-
|
|
668
|
+
Group an array of objects by a certain key or more.
|
|
618
669
|
|
|
619
|
-
|
|
670
|
+
`groupBy(array: Record<string, any>[], groupByKey: string | string[], listAs = "data"): Record<string, any>[]`
|
|
620
671
|
|
|
621
|
-
|
|
672
|
+
Group by a single key:
|
|
622
673
|
|
|
623
|
-
|
|
674
|
+
```ts
|
|
675
|
+
import { groupBy } from "@mongez/reinforcements";
|
|
624
676
|
|
|
625
|
-
|
|
677
|
+
const studentsClasses = [
|
|
678
|
+
{
|
|
679
|
+
id: 1,
|
|
680
|
+
class: "A",
|
|
681
|
+
grade: 1,
|
|
682
|
+
},
|
|
683
|
+
{
|
|
684
|
+
id: 2,
|
|
685
|
+
class: "B",
|
|
686
|
+
grade: 2,
|
|
687
|
+
},
|
|
688
|
+
{
|
|
689
|
+
id: 3,
|
|
690
|
+
class: "A",
|
|
691
|
+
grade: 3,
|
|
692
|
+
},
|
|
693
|
+
{
|
|
694
|
+
id: 4,
|
|
695
|
+
class: "B",
|
|
696
|
+
grade: 2,
|
|
697
|
+
},
|
|
698
|
+
{
|
|
699
|
+
id: 5,
|
|
700
|
+
class: "B",
|
|
701
|
+
grade: 2,
|
|
702
|
+
},
|
|
703
|
+
{
|
|
704
|
+
id: 6,
|
|
705
|
+
class: "C",
|
|
706
|
+
grade: 5,
|
|
707
|
+
},
|
|
708
|
+
];
|
|
626
709
|
|
|
627
|
-
console.log(
|
|
710
|
+
console.log(groupBy(studentsClasses, "class"));
|
|
628
711
|
```
|
|
629
712
|
|
|
630
|
-
|
|
713
|
+
Output:
|
|
631
714
|
|
|
632
|
-
|
|
715
|
+
```json
|
|
716
|
+
[
|
|
717
|
+
{
|
|
718
|
+
"class": "A",
|
|
719
|
+
"data": [
|
|
720
|
+
{
|
|
721
|
+
"id": 1,
|
|
722
|
+
"class": "A",
|
|
723
|
+
"grade": 1
|
|
724
|
+
},
|
|
725
|
+
{
|
|
726
|
+
"id": 3,
|
|
727
|
+
"class": "A",
|
|
728
|
+
"grade": 3
|
|
729
|
+
}
|
|
730
|
+
]
|
|
731
|
+
},
|
|
732
|
+
{
|
|
733
|
+
"class": "B",
|
|
734
|
+
"data": [
|
|
735
|
+
{
|
|
736
|
+
"id": 2,
|
|
737
|
+
"class": "B",
|
|
738
|
+
"grade": 2
|
|
739
|
+
},
|
|
740
|
+
{
|
|
741
|
+
"id": 4,
|
|
742
|
+
"class": "B",
|
|
743
|
+
"grade": 2
|
|
744
|
+
},
|
|
745
|
+
{
|
|
746
|
+
"id": 5,
|
|
747
|
+
"class": "B",
|
|
748
|
+
"grade": 2
|
|
749
|
+
}
|
|
750
|
+
]
|
|
751
|
+
},
|
|
752
|
+
{
|
|
753
|
+
"class": "C",
|
|
754
|
+
"data": [
|
|
755
|
+
{
|
|
756
|
+
"id": 6,
|
|
757
|
+
"class": "C",
|
|
758
|
+
"grade": 5
|
|
759
|
+
}
|
|
760
|
+
]
|
|
761
|
+
}
|
|
762
|
+
]
|
|
763
|
+
```
|
|
633
764
|
|
|
634
|
-
|
|
765
|
+
Group By Multiple Keys:
|
|
635
766
|
|
|
636
|
-
|
|
767
|
+
```ts
|
|
768
|
+
import { groupBy } from "@mongez/reinforcements";
|
|
769
|
+
|
|
770
|
+
const studentsClasses = [
|
|
771
|
+
{
|
|
772
|
+
id: 1,
|
|
773
|
+
class: "A",
|
|
774
|
+
grade: 1,
|
|
775
|
+
},
|
|
776
|
+
{
|
|
777
|
+
id: 2,
|
|
778
|
+
class: "B",
|
|
779
|
+
grade: 2,
|
|
780
|
+
},
|
|
781
|
+
{
|
|
782
|
+
id: 3,
|
|
783
|
+
class: "A",
|
|
784
|
+
grade: 3,
|
|
785
|
+
},
|
|
786
|
+
{
|
|
787
|
+
id: 4,
|
|
788
|
+
class: "B",
|
|
789
|
+
grade: 2,
|
|
790
|
+
},
|
|
791
|
+
{
|
|
792
|
+
id: 5,
|
|
793
|
+
class: "B",
|
|
794
|
+
grade: 2,
|
|
795
|
+
},
|
|
796
|
+
{
|
|
797
|
+
id: 6,
|
|
798
|
+
class: "C",
|
|
799
|
+
grade: 5,
|
|
800
|
+
},
|
|
801
|
+
];
|
|
802
|
+
|
|
803
|
+
console.log(groupBy(studentsClasses, ["class", "grade"]));
|
|
804
|
+
```
|
|
805
|
+
|
|
806
|
+
Output:
|
|
807
|
+
|
|
808
|
+
```json
|
|
809
|
+
[
|
|
810
|
+
{
|
|
811
|
+
"class": "A",
|
|
812
|
+
"grade": 1,
|
|
813
|
+
"data": [
|
|
814
|
+
{
|
|
815
|
+
"id": 1,
|
|
816
|
+
"class": "A",
|
|
817
|
+
"grade": 1
|
|
818
|
+
}
|
|
819
|
+
]
|
|
820
|
+
},
|
|
821
|
+
{
|
|
822
|
+
"class": "A",
|
|
823
|
+
"grade": 3,
|
|
824
|
+
"data": [
|
|
825
|
+
{
|
|
826
|
+
"id": 3,
|
|
827
|
+
"class": "A",
|
|
828
|
+
"grade": 3
|
|
829
|
+
}
|
|
830
|
+
]
|
|
831
|
+
},
|
|
832
|
+
{
|
|
833
|
+
"class": "B",
|
|
834
|
+
"grade": 2,
|
|
835
|
+
"data": [
|
|
836
|
+
{
|
|
837
|
+
"id": 2,
|
|
838
|
+
"class": "B",
|
|
839
|
+
"grade": 2
|
|
840
|
+
},
|
|
841
|
+
{
|
|
842
|
+
"id": 4,
|
|
843
|
+
"class": "B",
|
|
844
|
+
"grade": 2
|
|
845
|
+
},
|
|
846
|
+
{
|
|
847
|
+
"id": 5,
|
|
848
|
+
"class": "B",
|
|
849
|
+
"grade": 2
|
|
850
|
+
}
|
|
851
|
+
]
|
|
852
|
+
},
|
|
853
|
+
{
|
|
854
|
+
"class": "C",
|
|
855
|
+
"grade": 5,
|
|
856
|
+
"data": [
|
|
857
|
+
{
|
|
858
|
+
"id": 6,
|
|
859
|
+
"class": "C",
|
|
860
|
+
"grade": 5
|
|
861
|
+
}
|
|
862
|
+
]
|
|
863
|
+
}
|
|
864
|
+
]
|
|
865
|
+
```
|
|
866
|
+
|
|
867
|
+
You can also change the `data` key to any other key by passing the third argument.
|
|
637
868
|
|
|
638
869
|
```ts
|
|
639
|
-
import {
|
|
870
|
+
import { groupBy } from "@mongez/reinforcements";
|
|
640
871
|
|
|
641
|
-
|
|
872
|
+
const studentsClasses = [
|
|
873
|
+
{
|
|
874
|
+
id: 1,
|
|
875
|
+
class: "A",
|
|
876
|
+
grade: 1,
|
|
877
|
+
},
|
|
878
|
+
{
|
|
879
|
+
id: 2,
|
|
880
|
+
class: "B",
|
|
881
|
+
grade: 2,
|
|
882
|
+
},
|
|
883
|
+
{
|
|
884
|
+
id: 3,
|
|
885
|
+
class: "A",
|
|
886
|
+
grade: 3,
|
|
887
|
+
},
|
|
888
|
+
{
|
|
889
|
+
id: 4,
|
|
890
|
+
class: "B",
|
|
891
|
+
grade: 2,
|
|
892
|
+
},
|
|
893
|
+
{
|
|
894
|
+
id: 5,
|
|
895
|
+
class: "B",
|
|
896
|
+
grade: 2,
|
|
897
|
+
},
|
|
898
|
+
{
|
|
899
|
+
id: 6,
|
|
900
|
+
class: "C",
|
|
901
|
+
grade: 5,
|
|
902
|
+
},
|
|
903
|
+
];
|
|
904
|
+
|
|
905
|
+
console.log(groupBy(studentsClasses, ["class", "grade"], 'students'));
|
|
642
906
|
```
|
|
643
907
|
|
|
644
|
-
|
|
908
|
+
Output:
|
|
909
|
+
|
|
910
|
+
```json
|
|
911
|
+
[
|
|
912
|
+
{
|
|
913
|
+
"class": "A",
|
|
914
|
+
"grade": 1,
|
|
915
|
+
"students": [
|
|
916
|
+
{
|
|
917
|
+
"id": 1,
|
|
918
|
+
"class": "A",
|
|
919
|
+
"grade": 1
|
|
920
|
+
}
|
|
921
|
+
]
|
|
922
|
+
},
|
|
923
|
+
{
|
|
924
|
+
"class": "A",
|
|
925
|
+
"grade": 3,
|
|
926
|
+
"students": [
|
|
927
|
+
{
|
|
928
|
+
"id": 3,
|
|
929
|
+
"class": "A",
|
|
930
|
+
"grade": 3
|
|
931
|
+
}
|
|
932
|
+
]
|
|
933
|
+
},
|
|
934
|
+
{
|
|
935
|
+
"class": "B",
|
|
936
|
+
"grade": 2,
|
|
937
|
+
"students": [
|
|
938
|
+
{
|
|
939
|
+
"id": 2,
|
|
940
|
+
"class": "B",
|
|
941
|
+
"grade": 2
|
|
942
|
+
},
|
|
943
|
+
{
|
|
944
|
+
"id": 4,
|
|
945
|
+
"class": "B",
|
|
946
|
+
"grade": 2
|
|
947
|
+
},
|
|
948
|
+
{
|
|
949
|
+
"id": 5,
|
|
950
|
+
"class": "B",
|
|
951
|
+
"grade": 2
|
|
952
|
+
}
|
|
953
|
+
]
|
|
954
|
+
},
|
|
955
|
+
{
|
|
956
|
+
"class": "C",
|
|
957
|
+
"grade": 5,
|
|
958
|
+
"students": [
|
|
959
|
+
{
|
|
960
|
+
"id": 6,
|
|
961
|
+
"class": "C",
|
|
962
|
+
"grade": 5
|
|
963
|
+
}
|
|
964
|
+
]
|
|
965
|
+
}
|
|
966
|
+
]
|
|
967
|
+
```
|
|
968
|
+
|
|
969
|
+
### Chunk
|
|
970
|
+
|
|
971
|
+
Split array into chunks.
|
|
972
|
+
|
|
973
|
+
`chunk(array: any[] | string, size: number): any[]`
|
|
645
974
|
|
|
646
975
|
```ts
|
|
647
|
-
import {
|
|
976
|
+
import { chunk } from "@mongez/reinforcements";
|
|
648
977
|
|
|
649
|
-
|
|
978
|
+
const array = [1, 2, 3, 4, 5];
|
|
979
|
+
|
|
980
|
+
console.log(chunk(array, 2)); // [[1, 2], [3, 4], [5]]
|
|
650
981
|
```
|
|
651
982
|
|
|
652
|
-
###
|
|
983
|
+
### Count
|
|
653
984
|
|
|
654
|
-
|
|
985
|
+
Count the number of item that contains the given key or callback.
|
|
986
|
+
|
|
987
|
+
`count(data: any[], key: string | Parameters<typeof Array.prototype.filter>[0]): number`
|
|
655
988
|
|
|
656
989
|
```ts
|
|
657
|
-
import {
|
|
990
|
+
import { count } from "@mongez/reinforcements";
|
|
658
991
|
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
992
|
+
const array = [
|
|
993
|
+
{ id: 1, value: 1 },
|
|
994
|
+
{ id: 2, value: 2 },
|
|
995
|
+
{ id: 3, value: 3 },
|
|
996
|
+
{ id: 4, value: 4 },
|
|
997
|
+
{ id: 5, value: 5 },
|
|
998
|
+
];
|
|
999
|
+
|
|
1000
|
+
console.log(count(array, "value")); // 5
|
|
662
1001
|
```
|
|
663
1002
|
|
|
664
|
-
|
|
1003
|
+
We can also make a count using a callback.
|
|
665
1004
|
|
|
666
1005
|
```ts
|
|
667
|
-
import {
|
|
1006
|
+
import { count } from "@mongez/reinforcements";
|
|
668
1007
|
|
|
669
|
-
|
|
1008
|
+
const array = [
|
|
1009
|
+
{ id: 1, value: 1 },
|
|
1010
|
+
{ id: 2, value: 2 },
|
|
1011
|
+
{ id: 3, value: 3 },
|
|
1012
|
+
{ id: 4, value: 4 },
|
|
1013
|
+
{ id: 5, value: 5 },
|
|
1014
|
+
];
|
|
1015
|
+
|
|
1016
|
+
console.log(count(array, (item) => item.value > 2)); // 3
|
|
670
1017
|
```
|
|
671
1018
|
|
|
672
|
-
|
|
1019
|
+
### Count By
|
|
1020
|
+
|
|
1021
|
+
Count total occurrence of values for the given key.
|
|
1022
|
+
|
|
1023
|
+
`countBy(array: any[], key: string): { [key: string]: number`
|
|
673
1024
|
|
|
674
1025
|
```ts
|
|
675
|
-
import {
|
|
1026
|
+
import { countBy } from "@mongez/reinforcements";
|
|
676
1027
|
|
|
677
|
-
|
|
1028
|
+
const array = [
|
|
1029
|
+
{ id: 1, animal: 'dog' },
|
|
1030
|
+
{ id: 2, animal: 'cat' },
|
|
1031
|
+
{ id: 3, animal: 'dog' },
|
|
1032
|
+
{ id: 4, animal: 'cat' },
|
|
1033
|
+
{ id: 5, animal: 'dog' },
|
|
1034
|
+
];
|
|
1035
|
+
|
|
1036
|
+
console.log(countBy(array, 'animal')); // { dog: 3, cat: 2 }
|
|
678
1037
|
```
|
|
679
1038
|
|
|
680
|
-
|
|
1039
|
+
### Even
|
|
681
1040
|
|
|
682
|
-
|
|
1041
|
+
Get even numbers from the array or by the given key.
|
|
683
1042
|
|
|
684
|
-
|
|
1043
|
+
`even(array: any[], key?: string): any[]`
|
|
685
1044
|
|
|
686
1045
|
```ts
|
|
687
|
-
import {
|
|
1046
|
+
import { even } from "@mongez/reinforcements";
|
|
688
1047
|
|
|
689
|
-
|
|
690
|
-
|
|
1048
|
+
const array = [1, 2, 3, 4, 5];
|
|
1049
|
+
|
|
1050
|
+
console.log(even(array)); // [2, 4]
|
|
691
1051
|
```
|
|
692
1052
|
|
|
693
|
-
|
|
1053
|
+
We can also get even numbers by the given key.
|
|
694
1054
|
|
|
695
|
-
|
|
1055
|
+
```ts
|
|
1056
|
+
import { even } from "@mongez/reinforcements";
|
|
696
1057
|
|
|
697
|
-
|
|
1058
|
+
const array = [
|
|
1059
|
+
{ id: 1, value: 1 },
|
|
1060
|
+
{ id: 2, value: 2 },
|
|
1061
|
+
{ id: 3, value: 3 },
|
|
1062
|
+
{ id: 4, value: 4 },
|
|
1063
|
+
{ id: 5, value: 5 },
|
|
1064
|
+
];
|
|
1065
|
+
|
|
1066
|
+
console.log(even(array, "value")); // [2, 4]
|
|
1067
|
+
```
|
|
1068
|
+
|
|
1069
|
+
### Odd
|
|
1070
|
+
|
|
1071
|
+
Get odd numbers from the array or by the given key.
|
|
1072
|
+
|
|
1073
|
+
`odd(array: any[], key?: string): any[]`
|
|
698
1074
|
|
|
699
1075
|
```ts
|
|
700
|
-
import {
|
|
1076
|
+
import { odd } from "@mongez/reinforcements";
|
|
701
1077
|
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
Random.boolean(); // false
|
|
706
|
-
Random.boolean(); // true
|
|
707
|
-
Random.boolean(); // false
|
|
1078
|
+
const array = [1, 2, 3, 4, 5];
|
|
1079
|
+
|
|
1080
|
+
console.log(odd(array)); // [1, 3, 5]
|
|
708
1081
|
```
|
|
709
1082
|
|
|
710
|
-
|
|
1083
|
+
We can also get odd numbers by the given key.
|
|
711
1084
|
|
|
712
|
-
|
|
1085
|
+
```ts
|
|
1086
|
+
import { odd } from "@mongez/reinforcements";
|
|
1087
|
+
|
|
1088
|
+
const array = [
|
|
1089
|
+
{ id: 1, value: 1 },
|
|
1090
|
+
{ id: 2, value: 2 },
|
|
1091
|
+
{ id: 3, value: 3 },
|
|
1092
|
+
{ id: 4, value: 4 },
|
|
1093
|
+
{ id: 5, value: 5 },
|
|
1094
|
+
];
|
|
1095
|
+
|
|
1096
|
+
console.log(odd(array, "value")); // [1, 3, 5]
|
|
1097
|
+
```
|
|
1098
|
+
|
|
1099
|
+
### Even Indexes
|
|
1100
|
+
|
|
1101
|
+
Get only array values in even indexes.
|
|
1102
|
+
|
|
1103
|
+
`evenIndexes(array: any[]): any[]`
|
|
713
1104
|
|
|
714
1105
|
```ts
|
|
715
|
-
import {
|
|
1106
|
+
import { evenIndexes } from "@mongez/reinforcements";
|
|
716
1107
|
|
|
717
|
-
|
|
1108
|
+
const array = [1, 2, 3, 4, 5];
|
|
1109
|
+
|
|
1110
|
+
console.log(evenIndexes(array)); // [1, 3, 5]
|
|
718
1111
|
```
|
|
719
1112
|
|
|
720
|
-
###
|
|
1113
|
+
### Odd Indexes
|
|
721
1114
|
|
|
722
|
-
|
|
1115
|
+
Get only array values in odd indexes.
|
|
1116
|
+
|
|
1117
|
+
`oddIndexes(array: any[]): any[]`
|
|
723
1118
|
|
|
724
1119
|
```ts
|
|
725
|
-
import {
|
|
1120
|
+
import { oddIndexes } from "@mongez/reinforcements";
|
|
726
1121
|
|
|
727
|
-
|
|
1122
|
+
const array = [1, 2, 3, 4, 5];
|
|
728
1123
|
|
|
729
|
-
//
|
|
730
|
-
|
|
1124
|
+
console.log(oddIndexes(array)); // [2, 4]
|
|
1125
|
+
```
|
|
731
1126
|
|
|
732
|
-
|
|
733
|
-
Random.date(new Date(2010, 1, 1)); // 2015-12-12T12:12:12.000Z
|
|
1127
|
+
### min
|
|
734
1128
|
|
|
735
|
-
|
|
736
|
-
|
|
1129
|
+
Get the minimum value from the array or by the given key.
|
|
1130
|
+
|
|
1131
|
+
`min(array: any[], key?: string): number`
|
|
1132
|
+
|
|
1133
|
+
```ts
|
|
1134
|
+
import { min } from "@mongez/reinforcements";
|
|
1135
|
+
|
|
1136
|
+
const array = [1, 2, 3, 4, 5];
|
|
1137
|
+
|
|
1138
|
+
console.log(min(array)); // 1
|
|
737
1139
|
```
|
|
738
1140
|
|
|
739
|
-
|
|
1141
|
+
We can also get the minimum value by the given key.
|
|
740
1142
|
|
|
741
|
-
|
|
1143
|
+
```ts
|
|
1144
|
+
import { min } from "@mongez/reinforcements";
|
|
1145
|
+
|
|
1146
|
+
const array = [
|
|
1147
|
+
{ id: 1, value: 1 },
|
|
1148
|
+
{ id: 2, value: 2 },
|
|
1149
|
+
{ id: 3, value: 3 },
|
|
1150
|
+
{ id: 4, value: 4 },
|
|
1151
|
+
{ id: 5, value: 5 },
|
|
1152
|
+
];
|
|
1153
|
+
|
|
1154
|
+
console.log(min(array, "value")); // 1
|
|
1155
|
+
```
|
|
1156
|
+
|
|
1157
|
+
### max
|
|
1158
|
+
|
|
1159
|
+
Get the maximum value from the array or by the given key.
|
|
1160
|
+
|
|
1161
|
+
`max(array: any[], key?: string): number`
|
|
742
1162
|
|
|
743
1163
|
```ts
|
|
744
|
-
import {
|
|
1164
|
+
import { max } from "@mongez/reinforcements";
|
|
745
1165
|
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
console.log(
|
|
749
|
-
|
|
1166
|
+
const array = [1, 2, 3, 4, 5];
|
|
1167
|
+
|
|
1168
|
+
console.log(max(array)); // 5
|
|
1169
|
+
```
|
|
1170
|
+
|
|
1171
|
+
We can also get the maximum value by the given key.
|
|
1172
|
+
|
|
1173
|
+
```ts
|
|
1174
|
+
import { max } from "@mongez/reinforcements";
|
|
1175
|
+
|
|
1176
|
+
const array = [
|
|
1177
|
+
{ id: 1, value: 1 },
|
|
1178
|
+
{ id: 2, value: 2 },
|
|
1179
|
+
{ id: 3, value: 3 },
|
|
1180
|
+
{ id: 4, value: 4 },
|
|
1181
|
+
{ id: 5, value: 5 },
|
|
1182
|
+
];
|
|
1183
|
+
|
|
1184
|
+
console.log(max(array, "value")); // 5
|
|
1185
|
+
```
|
|
1186
|
+
|
|
1187
|
+
### sum
|
|
1188
|
+
|
|
1189
|
+
Get the sum of all values in the array or by the given key.
|
|
1190
|
+
|
|
1191
|
+
`sum(array: any[], key?: string): number`
|
|
1192
|
+
|
|
1193
|
+
```ts
|
|
1194
|
+
import { sum } from "@mongez/reinforcements";
|
|
1195
|
+
|
|
1196
|
+
const array = [1, 2, 3, 4, 5];
|
|
1197
|
+
|
|
1198
|
+
console.log(sum(array)); // 15
|
|
1199
|
+
```
|
|
1200
|
+
|
|
1201
|
+
We can also get the sum of all values by the given key.
|
|
1202
|
+
|
|
1203
|
+
```ts
|
|
1204
|
+
import { sum } from "@mongez/reinforcements";
|
|
1205
|
+
|
|
1206
|
+
const array = [
|
|
1207
|
+
{ id: 1, value: 1 },
|
|
1208
|
+
{ id: 2, value: 2 },
|
|
1209
|
+
{ id: 3, value: 3 },
|
|
1210
|
+
{ id: 4, value: 4 },
|
|
1211
|
+
{ id: 5, value: 5 },
|
|
1212
|
+
];
|
|
1213
|
+
|
|
1214
|
+
console.log(sum(array, "value")); // 15
|
|
1215
|
+
```
|
|
1216
|
+
|
|
1217
|
+
### Average
|
|
1218
|
+
|
|
1219
|
+
Calculate the average of an array.
|
|
1220
|
+
|
|
1221
|
+
`average(array: any[], key?: string): number`
|
|
1222
|
+
|
|
1223
|
+
```ts
|
|
1224
|
+
import { average } from "@mongez/reinforcements";
|
|
1225
|
+
|
|
1226
|
+
const array = [1, 2, 3, 4, 5];
|
|
1227
|
+
|
|
1228
|
+
console.log(average(array)); // 3
|
|
1229
|
+
```
|
|
1230
|
+
|
|
1231
|
+
You can also get an average of an array of objects by passing the key of the property you want to get the average of.
|
|
1232
|
+
|
|
1233
|
+
```ts
|
|
1234
|
+
import { average } from "@mongez/reinforcements";
|
|
1235
|
+
|
|
1236
|
+
const array = [
|
|
1237
|
+
{ id: 1, value: 1 },
|
|
1238
|
+
{ id: 2, value: 2 },
|
|
1239
|
+
{ id: 3, value: 3 },
|
|
1240
|
+
{ id: 4, value: 4 },
|
|
1241
|
+
{ id: 5, value: 5 },
|
|
1242
|
+
];
|
|
1243
|
+
|
|
1244
|
+
console.log(average(array, "value")); // 3
|
|
1245
|
+
```
|
|
1246
|
+
|
|
1247
|
+
### Median
|
|
1248
|
+
|
|
1249
|
+
Calculate the median of an array or by the given key.
|
|
1250
|
+
|
|
1251
|
+
`median(array: any[], key?: string): number`
|
|
1252
|
+
|
|
1253
|
+
```ts
|
|
1254
|
+
import { median } from "@mongez/reinforcements";
|
|
1255
|
+
|
|
1256
|
+
const array = [1, 2, 3, 4, 5];
|
|
1257
|
+
|
|
1258
|
+
console.log(median(array)); // 3
|
|
1259
|
+
```
|
|
1260
|
+
|
|
1261
|
+
We can also get the median by the given key.
|
|
1262
|
+
|
|
1263
|
+
```ts
|
|
1264
|
+
import { median } from "@mongez/reinforcements";
|
|
1265
|
+
|
|
1266
|
+
const array = [
|
|
1267
|
+
{ id: 1, value: 1 },
|
|
1268
|
+
{ id: 2, value: 2 },
|
|
1269
|
+
{ id: 3, value: 3 },
|
|
1270
|
+
{ id: 4, value: 4 },
|
|
1271
|
+
{ id: 5, value: 5 },
|
|
1272
|
+
];
|
|
1273
|
+
|
|
1274
|
+
console.log(median(array, "value")); // 3
|
|
1275
|
+
```
|
|
1276
|
+
|
|
1277
|
+
### Unique
|
|
1278
|
+
|
|
1279
|
+
Get unique values from the array or by the given key.
|
|
1280
|
+
|
|
1281
|
+
`unique(array: any[], key?: string): any[]`
|
|
1282
|
+
|
|
1283
|
+
```ts
|
|
1284
|
+
import { unique } from "@mongez/reinforcements";
|
|
1285
|
+
|
|
1286
|
+
const array = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5];
|
|
1287
|
+
|
|
1288
|
+
console.log(unique(array)); // [1, 2, 3, 4, 5]
|
|
1289
|
+
```
|
|
1290
|
+
|
|
1291
|
+
We can also get unique values by the given key.
|
|
1292
|
+
|
|
1293
|
+
```ts
|
|
1294
|
+
import { unique } from "@mongez/reinforcements";
|
|
1295
|
+
|
|
1296
|
+
const array = [
|
|
1297
|
+
{ id: 1, value: 1 },
|
|
1298
|
+
{ id: 2, value: 2 },
|
|
1299
|
+
{ id: 3, value: 3 },
|
|
1300
|
+
{ id: 4, value: 4 },
|
|
1301
|
+
{ id: 5, value: 5 },
|
|
1302
|
+
{ id: 6, value: 1 },
|
|
1303
|
+
{ id: 7, value: 2 },
|
|
1304
|
+
{ id: 8, value: 3 },
|
|
1305
|
+
{ id: 9, value: 4 },
|
|
1306
|
+
{ id: 10, value: 5 },
|
|
1307
|
+
];
|
|
1308
|
+
|
|
1309
|
+
console.log(unique(array, "value")); // [1, 2, 3, 4, 5]
|
|
1310
|
+
```
|
|
1311
|
+
|
|
1312
|
+
### Push Unique
|
|
1313
|
+
|
|
1314
|
+
Push a value or more to the array if it doesn't exist.
|
|
1315
|
+
|
|
1316
|
+
`pushUnique<T = any>(array: T[], ...items: T[]): T[]`
|
|
1317
|
+
|
|
1318
|
+
```ts
|
|
1319
|
+
import { pushUnique } from "@mongez/reinforcements";
|
|
1320
|
+
|
|
1321
|
+
const array = [1, 2, 3, 4, 5];
|
|
1322
|
+
|
|
1323
|
+
console.log(pushUnique(array, 6, 5, 6, 1, 2, 4, 3)); // [1, 2, 3, 4, 5, 6]
|
|
1324
|
+
```
|
|
1325
|
+
|
|
1326
|
+
### Unshift Unique
|
|
1327
|
+
|
|
1328
|
+
Add a value or more to the beginning array if it doesn't exist.
|
|
1329
|
+
|
|
1330
|
+
`unshiftUnique<T = any>(array: T[], ...items: T[]): T[]`
|
|
1331
|
+
|
|
1332
|
+
```ts
|
|
1333
|
+
import { unshiftUnique } from "@mongez/reinforcements";
|
|
1334
|
+
|
|
1335
|
+
const array = [1, 2, 3, 4, 5];
|
|
1336
|
+
|
|
1337
|
+
console.log(unshiftUnique(array, 6, 7, 5, 6, 1, 2, 4, 3)); // [7, 6, 1, 2, 3, 4, 5]
|
|
750
1338
|
```
|
|
751
1339
|
|
|
752
1340
|
## Working With Strings
|
|
753
1341
|
|
|
754
1342
|
The following list defines all available string utilities
|
|
755
1343
|
|
|
756
|
-
-
|
|
757
|
-
-
|
|
758
|
-
-
|
|
759
|
-
-
|
|
760
|
-
-
|
|
761
|
-
-
|
|
762
|
-
-
|
|
763
|
-
-
|
|
764
|
-
-
|
|
765
|
-
-
|
|
766
|
-
-
|
|
767
|
-
-
|
|
768
|
-
-
|
|
769
|
-
-
|
|
770
|
-
-
|
|
771
|
-
-
|
|
772
|
-
-
|
|
773
|
-
-
|
|
774
|
-
-
|
|
775
|
-
-
|
|
776
|
-
|
|
777
|
-
### Capitalize
|
|
1344
|
+
- [Capitalize](#capitalize): Capitalize the first letter of the given string.
|
|
1345
|
+
- [Camel Case](#camel-case): Convert the given string to camel case.
|
|
1346
|
+
- [Snake Case](#snake-case): Convert the given string to snake case.
|
|
1347
|
+
- [Kebab Case](#kebab-case): Convert the given string to kebab case.
|
|
1348
|
+
- [Studly/Pascal Case](#studly-case): Convert the given string to pascal/studly case.
|
|
1349
|
+
- [ucfirst](#ucfirst): Capitalize the first letter of each word in the given string.
|
|
1350
|
+
- [To Input Name](#toinputname): Convert a dot notation string to proper input name (Brackets).
|
|
1351
|
+
- [Extension](#extension): Get the extension of the given string.
|
|
1352
|
+
- [Read More Characters](#read-more-characters): Cut off the given string after the given number of characters.
|
|
1353
|
+
- [Read More Words](#read-more-words): Cut off the given string after the given number of words.
|
|
1354
|
+
- [Remove First](#remove-first-matched-string): Remove the first matched string from the given string.
|
|
1355
|
+
- [Remove Last](#remove-last-matched-string): Remove the last matched string from the given string.
|
|
1356
|
+
- [Replace First](#replace-first-matched-string): Replace the first matched string from the given string.
|
|
1357
|
+
- [Replace Last](#replace-last-matched-string): Replace the last matched string from the given string.
|
|
1358
|
+
- [Replace All](#replace-all-matched-string): Replace all matched strings from the given string.
|
|
1359
|
+
- [Repeats Of](#count-repeats-of-needle-in-a-string): Count Repeats of needle in a string.
|
|
1360
|
+
- [Trim](#trimming-values-from-string): Remove a string from the beginning and end of the given string.
|
|
1361
|
+
- [Trim Left](#trimming-values-from-string): Remove a string from the beginning of the given string.
|
|
1362
|
+
- [Trim Right](#trimming-values-from-string): Remove a string from the end of the given string.
|
|
1363
|
+
- [Starts With Arabic Letter](#detect-if-string-starts-with-arabic): Detect if string starts with Arabic letter.
|
|
1364
|
+
|
|
1365
|
+
### Capitalize
|
|
778
1366
|
|
|
779
1367
|
Capitalize each word in string Separated by whitespace `capitalize(string: string): string`.
|
|
780
1368
|
|
|
@@ -786,7 +1374,7 @@ const words = "hello world";
|
|
|
786
1374
|
console.log(capitalize(words)); // Hello World
|
|
787
1375
|
```
|
|
788
1376
|
|
|
789
|
-
###
|
|
1377
|
+
### Camel Case
|
|
790
1378
|
|
|
791
1379
|
Convert string to camel case, each word in string Separated by **whitespace** **underscores** or **dashes** `toCamelCase(string: string, separator: string = "\\s+|-|/|_|\\."): string`.
|
|
792
1380
|
|
|
@@ -800,7 +1388,7 @@ console.log(toCamelCase(words)); // helloWorld
|
|
|
800
1388
|
|
|
801
1389
|
Any of following will be used as a separator for the text, `.` | `-` | `whitespace` | `/`, you can set the separator as second argument though.
|
|
802
1390
|
|
|
803
|
-
###
|
|
1391
|
+
### Snake Case
|
|
804
1392
|
|
|
805
1393
|
Convert string to snake case, each word in string Separated by **whitespace** or **dashes** `toSnakeCase(string: string, separator: string = '_', lowerAll: boolean = true): string`.
|
|
806
1394
|
|
|
@@ -834,7 +1422,7 @@ const words = "Hello World";
|
|
|
834
1422
|
console.log(toSnakeCase(words, '-', false)); // Hello_World
|
|
835
1423
|
```
|
|
836
1424
|
|
|
837
|
-
###
|
|
1425
|
+
### Kebab Case
|
|
838
1426
|
|
|
839
1427
|
Convert string to kebab case, each word in string Separated by **whitespace** or **dashes** or **Upper Letters** `toKebabCase(string: string, lowerAll: boolean = true): string`.
|
|
840
1428
|
|
|
@@ -858,7 +1446,7 @@ const words = "Hello World";
|
|
|
858
1446
|
console.log(toKebabCase(words, false)); // Hello-World
|
|
859
1447
|
```
|
|
860
1448
|
|
|
861
|
-
###
|
|
1449
|
+
### Studly Case
|
|
862
1450
|
|
|
863
1451
|
Convert string to studly case, each word in string Separated by **whitespace**, **underscores** or **dashes** `toStudlyCase(string: string, separator: string = "-|\\.|_|\\s"): string`.
|
|
864
1452
|
|
|
@@ -872,7 +1460,7 @@ const words = "hello world";
|
|
|
872
1460
|
console.log(toStudlyCase(words)); // HelloWorld
|
|
873
1461
|
```
|
|
874
1462
|
|
|
875
|
-
###
|
|
1463
|
+
### Ucfirst
|
|
876
1464
|
|
|
877
1465
|
Capitalize only first word of string `ucfirst(string: string): string`.
|
|
878
1466
|
|
|
@@ -884,7 +1472,7 @@ const words = "hello world";
|
|
|
884
1472
|
console.log(ucfirst(words)); // Hello world
|
|
885
1473
|
```
|
|
886
1474
|
|
|
887
|
-
###
|
|
1475
|
+
### toInputName
|
|
888
1476
|
|
|
889
1477
|
Convert dot notation syntax to valid html input name `toInputName(string: string): string`.
|
|
890
1478
|
|
|
@@ -897,7 +1485,7 @@ console.log(toInputName(name)); // user[name]
|
|
|
897
1485
|
console.log(toInputName("keywords.en.list[]")); // keywords[en][list][]
|
|
898
1486
|
```
|
|
899
1487
|
|
|
900
|
-
###
|
|
1488
|
+
### Extension
|
|
901
1489
|
|
|
902
1490
|
Get the last extension in the string, the string that is suffix to last dot `.`.
|
|
903
1491
|
|
|
@@ -973,6 +1561,20 @@ const words = "welcome home buddy, your are not safe at your home!";
|
|
|
973
1561
|
console.log(removeFirst(words, "home")); // welcome buddy, your are not safe at your home!
|
|
974
1562
|
```
|
|
975
1563
|
|
|
1564
|
+
### Remove last matched string
|
|
1565
|
+
|
|
1566
|
+
Remove the last matched needle to the given string.
|
|
1567
|
+
|
|
1568
|
+
`removeLast(string: string, needle: string): string`
|
|
1569
|
+
|
|
1570
|
+
```ts
|
|
1571
|
+
import { removeLast } from "@mongez/reinforcements";
|
|
1572
|
+
|
|
1573
|
+
const words = "welcome home buddy, your are not safe at your home!";
|
|
1574
|
+
|
|
1575
|
+
console.log(removeLast(words, "home")); // welcome home buddy, your are not safe at your !
|
|
1576
|
+
```
|
|
1577
|
+
|
|
976
1578
|
### Replace first matched string
|
|
977
1579
|
|
|
978
1580
|
Replace the first matched needle to the given string.
|
|
@@ -1015,20 +1617,6 @@ const words = "welcome home buddy, your are not safe at your home!";
|
|
|
1015
1617
|
console.log(replaceAll(words, "home", "country")); // welcome country buddy, your are not safe at your country!
|
|
1016
1618
|
```
|
|
1017
1619
|
|
|
1018
|
-
### Remove last matched string
|
|
1019
|
-
|
|
1020
|
-
Remove the last matched needle to the given string.
|
|
1021
|
-
|
|
1022
|
-
`removeLast(string: string, needle: string): string`
|
|
1023
|
-
|
|
1024
|
-
```ts
|
|
1025
|
-
import { removeLast } from "@mongez/reinforcements";
|
|
1026
|
-
|
|
1027
|
-
const words = "welcome home buddy, your are not safe at your home!";
|
|
1028
|
-
|
|
1029
|
-
console.log(removeLast(words, "home")); // welcome home buddy, your are not safe at your !
|
|
1030
|
-
```
|
|
1031
|
-
|
|
1032
1620
|
### Count repeats of needle in a string
|
|
1033
1621
|
|
|
1034
1622
|
Count repeats of a needle in the given string.
|
|
@@ -1131,7 +1719,7 @@ const string = "home/";
|
|
|
1131
1719
|
console.log(rtrim(string, "/")); // /home
|
|
1132
1720
|
```
|
|
1133
1721
|
|
|
1134
|
-
|
|
1722
|
+
### Detect if string starts with Arabic
|
|
1135
1723
|
|
|
1136
1724
|
Determine if the string starts with Arabic letter.
|
|
1137
1725
|
|
|
@@ -1148,7 +1736,92 @@ console.log(startsWithArabic(string)); // false
|
|
|
1148
1736
|
console.log(startsWithArabic(arabicString)); // true
|
|
1149
1737
|
```
|
|
1150
1738
|
|
|
1151
|
-
##
|
|
1739
|
+
## Numbers
|
|
1740
|
+
|
|
1741
|
+
Here are the aviation numbers utilities.
|
|
1742
|
+
|
|
1743
|
+
- [Round](#round-float-numbers): Round numbers to a certain decimal places.
|
|
1744
|
+
|
|
1745
|
+
### Round float numbers
|
|
1746
|
+
|
|
1747
|
+
To round float numbers, use `round(value: number, precision: number = 2): number`.
|
|
1748
|
+
|
|
1749
|
+
```ts
|
|
1750
|
+
import { round } from "@mongez/reinforcements";
|
|
1751
|
+
|
|
1752
|
+
console.log(round(10.0001)); // 10
|
|
1753
|
+
console.log(round(10.0478878)); // 10.04
|
|
1754
|
+
console.log(round(10.6987894849)); // 10.69
|
|
1755
|
+
console.log(round(10.6987894849, 3)); // 10.698
|
|
1756
|
+
```
|
|
1757
|
+
|
|
1758
|
+
## Mixed Utilities
|
|
1759
|
+
|
|
1760
|
+
This section covers utilities that work with multiple types.
|
|
1761
|
+
|
|
1762
|
+
- [Are Equal](#equal-values): Check if the two values are equal regardless of their type.
|
|
1763
|
+
- [Shuffle](#shuffle): Shuffle an array or a string.
|
|
1764
|
+
|
|
1765
|
+
### Equal Values
|
|
1766
|
+
|
|
1767
|
+
Using `areEqual` function will check if the given two values equal to each other, it will validate against any type such as objects, arrays, strings, numbers, booleans, null, undefined, symbols.
|
|
1768
|
+
|
|
1769
|
+
```ts
|
|
1770
|
+
import { areEqual } from "@mongez/reinforcements";
|
|
1771
|
+
|
|
1772
|
+
console.log(areEqual(1, 1)); // true
|
|
1773
|
+
|
|
1774
|
+
console.log(areEqual("1", 1)); // false
|
|
1775
|
+
|
|
1776
|
+
console.log(areEqual("1", "1")); // true
|
|
1777
|
+
|
|
1778
|
+
console.log(areEqual(true, true)); // true
|
|
1779
|
+
|
|
1780
|
+
console.log(areEqual(true, false)); // false
|
|
1781
|
+
|
|
1782
|
+
console.log(areEqual(null, null)); // true
|
|
1783
|
+
|
|
1784
|
+
console.log(areEqual(undefined, undefined)); // true
|
|
1785
|
+
|
|
1786
|
+
console.log(areEqual(Symbol("1"), Symbol("1"))); // false
|
|
1787
|
+
|
|
1788
|
+
console.log(areEqual(Symbol("1"), Symbol("2"))); // false
|
|
1789
|
+
|
|
1790
|
+
console.log(areEqual([1, 2, 3], [1, 2, 3])); // true
|
|
1791
|
+
|
|
1792
|
+
console.log(areEqual([1, 2, 3], [1, 2, 3, 4])); // false
|
|
1793
|
+
|
|
1794
|
+
console.log(areEqual({ id: 1 }, { id: 1 })); // true
|
|
1795
|
+
|
|
1796
|
+
console.log(areEqual({ id: 1 }, { id: 2 })); // false
|
|
1797
|
+
```
|
|
1798
|
+
|
|
1799
|
+
### Shuffle
|
|
1800
|
+
|
|
1801
|
+
Shuffle an array or a string.
|
|
1802
|
+
|
|
1803
|
+
`shuffle(value: any[] | string): any[] | string`
|
|
1804
|
+
|
|
1805
|
+
```ts
|
|
1806
|
+
import { shuffle } from "@mongez/reinforcements";
|
|
1807
|
+
|
|
1808
|
+
const array = [1, 2, 3, 4, 5];
|
|
1809
|
+
|
|
1810
|
+
console.log(shuffle(array)); // [2, 4, 1, 5, 3]
|
|
1811
|
+
|
|
1812
|
+
const string = "Hello World";
|
|
1813
|
+
|
|
1814
|
+
console.log(shuffle(string)); // "WlloHrodl"
|
|
1815
|
+
```
|
|
1816
|
+
|
|
1817
|
+
## General Utilities
|
|
1818
|
+
|
|
1819
|
+
The section covers general utilities that can be used in any project.
|
|
1820
|
+
|
|
1821
|
+
- [Debounce](#debounce): Debounce a function to be called after a specific time.
|
|
1822
|
+
- [Escape Regex](#escape-regex): Escape regex special characters.
|
|
1823
|
+
|
|
1824
|
+
### Debounce
|
|
1152
1825
|
|
|
1153
1826
|
`debounce(callback: Function, timer: number = 0): void`
|
|
1154
1827
|
|
|
@@ -1201,6 +1874,129 @@ function sendEmail(e: any) {
|
|
|
1201
1874
|
<button click={sendEmail}>Send Email</button>;
|
|
1202
1875
|
```
|
|
1203
1876
|
|
|
1877
|
+
### Escape Regex
|
|
1878
|
+
|
|
1879
|
+
`escapeRegex(string: string): string`
|
|
1880
|
+
|
|
1881
|
+
Escape regex special characters in the given string.
|
|
1882
|
+
|
|
1883
|
+
```ts
|
|
1884
|
+
import { escapeRegex } from "@mongez/reinforcements";
|
|
1885
|
+
|
|
1886
|
+
const string = "This is a string with special characters like: . * + ? ^ $ { } ( ) | [ ] / \\";
|
|
1887
|
+
|
|
1888
|
+
console.log(escapeRegex(string)); // This is a string with special characters like: \\. \\* \\+ \\? \\^ \\$ \\{ \\} \\( \\) \\| \\[ \\] / \\\\
|
|
1889
|
+
```
|
|
1890
|
+
|
|
1891
|
+
## Random
|
|
1892
|
+
|
|
1893
|
+
Another good feature is `Random` object, which allows us to generate variant random values of different types.
|
|
1894
|
+
|
|
1895
|
+
### Generate random string
|
|
1896
|
+
|
|
1897
|
+
To generate a random string use `Random.string(length: number = 32): string` method.
|
|
1898
|
+
|
|
1899
|
+
```ts
|
|
1900
|
+
import { Random } from "@mongez/reinforcements";
|
|
1901
|
+
|
|
1902
|
+
Random.string(); // 4G8JyA4uM5YVMbkqVaoYnW6GzPcC64Fy
|
|
1903
|
+
```
|
|
1904
|
+
|
|
1905
|
+
To generate a random string with certain length, just pass the length value to the function.
|
|
1906
|
+
|
|
1907
|
+
```ts
|
|
1908
|
+
import { Random } from "@mongez/reinforcements";
|
|
1909
|
+
|
|
1910
|
+
Random.string(12); // P057C06VPwxl
|
|
1911
|
+
```
|
|
1912
|
+
|
|
1913
|
+
### Generate random integer
|
|
1914
|
+
|
|
1915
|
+
To generate a random integer use `Random.int(min: number = 1, max: number = 9999999): number` method.
|
|
1916
|
+
|
|
1917
|
+
```ts
|
|
1918
|
+
import { Random } from "@mongez/reinforcements";
|
|
1919
|
+
|
|
1920
|
+
Random.int(); // 7387115
|
|
1921
|
+
Random.int(); // 9411554
|
|
1922
|
+
Random.int(); // 691593
|
|
1923
|
+
```
|
|
1924
|
+
|
|
1925
|
+
To set min value, pass first argument with minimum value
|
|
1926
|
+
|
|
1927
|
+
```ts
|
|
1928
|
+
import { Random } from "@mongez/reinforcements";
|
|
1929
|
+
|
|
1930
|
+
Random.int(10); // 7387115
|
|
1931
|
+
```
|
|
1932
|
+
|
|
1933
|
+
To set min and max value, pass second argument as well with maximum value
|
|
1934
|
+
|
|
1935
|
+
```ts
|
|
1936
|
+
import { Random } from "@mongez/reinforcements";
|
|
1937
|
+
|
|
1938
|
+
Random.int(10, 100); // 36
|
|
1939
|
+
```
|
|
1940
|
+
|
|
1941
|
+
> `Random.integer` is an alias to `Random.int`.
|
|
1942
|
+
|
|
1943
|
+
### Generate random html id
|
|
1944
|
+
|
|
1945
|
+
This function will generate a valid random html id string `Random.id(length: number = 6, startsWith: string = "el-"): string`.
|
|
1946
|
+
|
|
1947
|
+
```ts
|
|
1948
|
+
import { Random } from "@mongez/reinforcements";
|
|
1949
|
+
|
|
1950
|
+
Random.id(); // el-SDFefdvgtr2e3qw
|
|
1951
|
+
Random.id(); // el-fasrg3q
|
|
1952
|
+
```
|
|
1953
|
+
|
|
1954
|
+
You may set the length as first argument and/or set the id prefix as second argument (**default is el-**).
|
|
1955
|
+
|
|
1956
|
+
### Generate random boolean value
|
|
1957
|
+
|
|
1958
|
+
To generate random boolean value use `Random.bool(): boolean` or `Random.boolean(): boolean`
|
|
1959
|
+
|
|
1960
|
+
```ts
|
|
1961
|
+
import { Random } from "@mongez/reinforcements";
|
|
1962
|
+
|
|
1963
|
+
Random.bool(); // true
|
|
1964
|
+
Random.bool(); // true
|
|
1965
|
+
Random.bool(); // false
|
|
1966
|
+
Random.boolean(); // false
|
|
1967
|
+
Random.boolean(); // true
|
|
1968
|
+
Random.boolean(); // false
|
|
1969
|
+
```
|
|
1970
|
+
|
|
1971
|
+
### Generate random color
|
|
1972
|
+
|
|
1973
|
+
To generate random color use `Random.color(): string` method.
|
|
1974
|
+
|
|
1975
|
+
```ts
|
|
1976
|
+
import { Random } from "@mongez/reinforcements";
|
|
1977
|
+
|
|
1978
|
+
Random.color(); // #f2f2f2
|
|
1979
|
+
```
|
|
1980
|
+
|
|
1981
|
+
### Generate random date
|
|
1982
|
+
|
|
1983
|
+
To generate random date use `Random.date(min: Date = new Date(1970, 1, 1), max: Date = new Date()): Date` method.
|
|
1984
|
+
|
|
1985
|
+
```ts
|
|
1986
|
+
import { Random } from "@mongez/reinforcements";
|
|
1987
|
+
|
|
1988
|
+
Random.date(); // 2020-12-12T12:12:12.000Z
|
|
1989
|
+
|
|
1990
|
+
// Get random date between two dates
|
|
1991
|
+
Random.date(new Date(2010, 1, 1), new Date(2020, 1, 1)); // 2015-12-12T12:12:12.000Z
|
|
1992
|
+
|
|
1993
|
+
// Get random date that is higher than the given date
|
|
1994
|
+
Random.date(new Date(2010, 1, 1)); // 2015-12-12T12:12:12.000Z
|
|
1995
|
+
|
|
1996
|
+
// Get random date that is lower than the given date
|
|
1997
|
+
Random.date(null, new Date(2010, 1, 1)); // 2005-12-12T12:12:12.000Z
|
|
1998
|
+
```
|
|
1999
|
+
|
|
1204
2000
|
## Tests
|
|
1205
2001
|
|
|
1206
2002
|
To run tests run `npm run test` or `yarn test`
|