@mongez/reinforcements 2.3.0 → 2.3.1
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 +11 -14
- package/cjs/array/groupBy.js +6 -6
- package/cjs/utils/debounce.d.ts +8 -1
- package/cjs/utils/debounce.js +12 -11
- package/esm/array/groupBy.js +6 -6
- package/esm/utils/debounce.d.ts +8 -1
- package/esm/utils/debounce.js +12 -11
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -384,9 +384,11 @@ const user = {
|
|
|
384
384
|
},
|
|
385
385
|
};
|
|
386
386
|
|
|
387
|
-
const
|
|
387
|
+
const unset(user, ["id", "address", "email"]); // { name: 'Hasan Zohdy', job: {title: 'Software Engineer'}}
|
|
388
388
|
```
|
|
389
389
|
|
|
390
|
+
> The `unset` removes the keys from the object, but it doesn't return a new object, it modifies the original object and returns it, if you want to return a new object, use `except` method.
|
|
391
|
+
|
|
390
392
|
### Flatten objects
|
|
391
393
|
|
|
392
394
|
We can flatten any big fat objects into one object, with only one dimension.
|
|
@@ -1883,11 +1885,7 @@ The section covers general utilities that can be used in any project.
|
|
|
1883
1885
|
|
|
1884
1886
|
You can debounce your functions using `debounce` to prevent multiple calls.
|
|
1885
1887
|
|
|
1886
|
-
> This debounce function will be called instantly and will not return a callback function.
|
|
1887
|
-
|
|
1888
1888
|
```tsx
|
|
1889
|
-
import { debounce } from "@mongez/reinforcements";
|
|
1890
|
-
|
|
1891
1889
|
function sendEmail(e: any) {
|
|
1892
1890
|
sendEmailApi(e.target);
|
|
1893
1891
|
}
|
|
@@ -1903,14 +1901,14 @@ Now when using `debounce`
|
|
|
1903
1901
|
import { debounce } from "@mongez/reinforcements";
|
|
1904
1902
|
|
|
1905
1903
|
function sendEmail(e: any) {
|
|
1906
|
-
|
|
1907
|
-
sendEmailApi(e.target);
|
|
1908
|
-
});
|
|
1904
|
+
sendEmailApi(e.target);
|
|
1909
1905
|
}
|
|
1910
1906
|
|
|
1907
|
+
const debouncedSendEmail = debounce(sendEmail);
|
|
1908
|
+
|
|
1911
1909
|
// If user clicked 5 times, it will make only one ajax call
|
|
1912
1910
|
|
|
1913
|
-
<button click={
|
|
1911
|
+
<button click={debouncedSendEmail}>Send Email</button>;
|
|
1914
1912
|
```
|
|
1915
1913
|
|
|
1916
1914
|
You can also set a timer when to trigger the function
|
|
@@ -1919,15 +1917,14 @@ You can also set a timer when to trigger the function
|
|
|
1919
1917
|
import { debounce } from "@mongez/reinforcements";
|
|
1920
1918
|
|
|
1921
1919
|
function sendEmail(e: any) {
|
|
1922
|
-
|
|
1923
|
-
debounce(() => {
|
|
1924
|
-
sendEmailApi(e.target);
|
|
1925
|
-
}, 150);
|
|
1920
|
+
sendEmailApi(e.target);
|
|
1926
1921
|
}
|
|
1927
1922
|
|
|
1923
|
+
const debouncedSendEmail = debounce(sendEmail, 1000);
|
|
1924
|
+
|
|
1928
1925
|
// If user clicked 5 times, it will make only one ajax call
|
|
1929
1926
|
|
|
1930
|
-
<button click={
|
|
1927
|
+
<button click={debouncedSendEmail}>Send Email</button>;
|
|
1931
1928
|
```
|
|
1932
1929
|
|
|
1933
1930
|
### Escape Regex
|
package/cjs/array/groupBy.js
CHANGED
|
@@ -9,7 +9,7 @@ require("../object/merge.js");
|
|
|
9
9
|
* The key(s) support dot notation.
|
|
10
10
|
*/
|
|
11
11
|
function groupBy(array, groupByKey, listAs = "data") {
|
|
12
|
-
const
|
|
12
|
+
const groupedData = {};
|
|
13
13
|
const groupByKeys =
|
|
14
14
|
typeof groupByKey === "string" ? [groupByKey] : groupByKey;
|
|
15
15
|
for (const item of array) {
|
|
@@ -23,20 +23,20 @@ function groupBy(array, groupByKey, listAs = "data") {
|
|
|
23
23
|
});
|
|
24
24
|
}
|
|
25
25
|
const dataKey = JSON.stringify(baseKeys);
|
|
26
|
-
if (!
|
|
26
|
+
if (!groupedData[dataKey]) {
|
|
27
27
|
const groupedDataList = {
|
|
28
28
|
data: [],
|
|
29
29
|
};
|
|
30
30
|
for (const groupByKeyData of baseKeys) {
|
|
31
31
|
groupedDataList[groupByKeyData.key] = groupByKeyData.value;
|
|
32
32
|
}
|
|
33
|
-
|
|
33
|
+
groupedData[dataKey] = groupedDataList;
|
|
34
34
|
}
|
|
35
|
-
|
|
35
|
+
groupedData[dataKey]?.data?.push(item);
|
|
36
36
|
}
|
|
37
37
|
const finalData = [];
|
|
38
|
-
for (const groupedKeyWithValue in
|
|
39
|
-
const { data, ...otherGroupedKeys } =
|
|
38
|
+
for (const groupedKeyWithValue in groupedData) {
|
|
39
|
+
const { data, ...otherGroupedKeys } = groupedData[groupedKeyWithValue];
|
|
40
40
|
finalData.push({
|
|
41
41
|
...otherGroupedKeys,
|
|
42
42
|
[listAs]: data,
|
package/cjs/utils/debounce.d.ts
CHANGED
|
@@ -1,2 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
declare type DebounceFn<T extends (...args: any[]) => any> = (this: ThisParameterType<T>, ...args: Parameters<T>) => void;
|
|
2
|
+
/**
|
|
3
|
+
* Debounce the callback function and return a new function.
|
|
4
|
+
* Example of usage:
|
|
5
|
+
* @example const debounced = debounce(() => console.log('Hello'), 1000);
|
|
6
|
+
*/
|
|
7
|
+
export default function debounce<T extends (...args: any[]) => any>(callback: T, time: number): DebounceFn<T>;
|
|
8
|
+
export {};
|
|
2
9
|
//# sourceMappingURL=debounce.d.ts.map
|
package/cjs/utils/debounce.js
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
/**
|
|
4
|
+
* Debounce the callback function and return a new function.
|
|
5
|
+
* Example of usage:
|
|
6
|
+
* @example const debounced = debounce(() => console.log('Hello'), 1000);
|
|
7
|
+
*/
|
|
8
|
+
function debounce(callback, time) {
|
|
9
|
+
let timeoutId;
|
|
10
|
+
return function (...args) {
|
|
7
11
|
clearTimeout(timeoutId);
|
|
8
|
-
timeoutId =
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
callback();
|
|
13
|
-
timeoutId = undefined; // Clear timeout
|
|
14
|
-
}, wait);
|
|
12
|
+
timeoutId = setTimeout(() => {
|
|
13
|
+
callback.apply(this, args);
|
|
14
|
+
}, time);
|
|
15
|
+
};
|
|
15
16
|
}
|
|
16
17
|
|
|
17
18
|
module.exports = debounce;
|
package/esm/array/groupBy.js
CHANGED
|
@@ -7,7 +7,7 @@ import "../object/merge.js";
|
|
|
7
7
|
* The key(s) support dot notation.
|
|
8
8
|
*/
|
|
9
9
|
function groupBy(array, groupByKey, listAs = "data") {
|
|
10
|
-
const
|
|
10
|
+
const groupedData = {};
|
|
11
11
|
const groupByKeys =
|
|
12
12
|
typeof groupByKey === "string" ? [groupByKey] : groupByKey;
|
|
13
13
|
for (const item of array) {
|
|
@@ -21,20 +21,20 @@ function groupBy(array, groupByKey, listAs = "data") {
|
|
|
21
21
|
});
|
|
22
22
|
}
|
|
23
23
|
const dataKey = JSON.stringify(baseKeys);
|
|
24
|
-
if (!
|
|
24
|
+
if (!groupedData[dataKey]) {
|
|
25
25
|
const groupedDataList = {
|
|
26
26
|
data: [],
|
|
27
27
|
};
|
|
28
28
|
for (const groupByKeyData of baseKeys) {
|
|
29
29
|
groupedDataList[groupByKeyData.key] = groupByKeyData.value;
|
|
30
30
|
}
|
|
31
|
-
|
|
31
|
+
groupedData[dataKey] = groupedDataList;
|
|
32
32
|
}
|
|
33
|
-
|
|
33
|
+
groupedData[dataKey]?.data?.push(item);
|
|
34
34
|
}
|
|
35
35
|
const finalData = [];
|
|
36
|
-
for (const groupedKeyWithValue in
|
|
37
|
-
const { data, ...otherGroupedKeys } =
|
|
36
|
+
for (const groupedKeyWithValue in groupedData) {
|
|
37
|
+
const { data, ...otherGroupedKeys } = groupedData[groupedKeyWithValue];
|
|
38
38
|
finalData.push({
|
|
39
39
|
...otherGroupedKeys,
|
|
40
40
|
[listAs]: data,
|
package/esm/utils/debounce.d.ts
CHANGED
|
@@ -1,2 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
declare type DebounceFn<T extends (...args: any[]) => any> = (this: ThisParameterType<T>, ...args: Parameters<T>) => void;
|
|
2
|
+
/**
|
|
3
|
+
* Debounce the callback function and return a new function.
|
|
4
|
+
* Example of usage:
|
|
5
|
+
* @example const debounced = debounce(() => console.log('Hello'), 1000);
|
|
6
|
+
*/
|
|
7
|
+
export default function debounce<T extends (...args: any[]) => any>(callback: T, time: number): DebounceFn<T>;
|
|
8
|
+
export {};
|
|
2
9
|
//# sourceMappingURL=debounce.d.ts.map
|
package/esm/utils/debounce.js
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Debounce the callback function and return a new function.
|
|
3
|
+
* Example of usage:
|
|
4
|
+
* @example const debounced = debounce(() => console.log('Hello'), 1000);
|
|
5
|
+
*/
|
|
6
|
+
function debounce(callback, time) {
|
|
7
|
+
let timeoutId;
|
|
8
|
+
return function (...args) {
|
|
5
9
|
clearTimeout(timeoutId);
|
|
6
|
-
timeoutId =
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
callback();
|
|
11
|
-
timeoutId = undefined; // Clear timeout
|
|
12
|
-
}, wait);
|
|
10
|
+
timeoutId = setTimeout(() => {
|
|
11
|
+
callback.apply(this, args);
|
|
12
|
+
}, time);
|
|
13
|
+
};
|
|
13
14
|
}
|
|
14
15
|
|
|
15
16
|
export { debounce as default };
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mongez/reinforcements",
|
|
3
|
-
"version": "2.3.
|
|
3
|
+
"version": "2.3.1",
|
|
4
4
|
"description": "A lightweight package to give a massive reinforcements to variant types of data in Nodejs/Javascript",
|
|
5
5
|
"main": "./cjs/index.js",
|
|
6
6
|
"dependencies": {
|
|
7
|
-
"@mongez/supportive-is": "^1.0.
|
|
7
|
+
"@mongez/supportive-is": "^1.0.12"
|
|
8
8
|
},
|
|
9
9
|
"scripts": {
|
|
10
10
|
"test": "jest ./src",
|
|
@@ -17,11 +17,11 @@
|
|
|
17
17
|
"url": "https://github.com/hassanzohdy/reinforcements"
|
|
18
18
|
},
|
|
19
19
|
"devDependencies": {
|
|
20
|
-
"@types/jest": "^
|
|
21
|
-
"jest": "^
|
|
20
|
+
"@types/jest": "^29.5.0",
|
|
21
|
+
"jest": "^29.5.0",
|
|
22
22
|
"jest-esm-jsx-transform": "^1.0.0",
|
|
23
|
-
"ts-jest": "^
|
|
24
|
-
"typescript": "^
|
|
23
|
+
"ts-jest": "^29.0.5",
|
|
24
|
+
"typescript": "^5.0.2"
|
|
25
25
|
},
|
|
26
26
|
"keywords": [
|
|
27
27
|
"array",
|