remove-anything 2.0.2 → 2.1.0
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 +1 -1
- package/dist/index.d.ts +12 -12
- package/dist/index.js +42 -5
- package/package.json +7 -7
package/README.md
CHANGED
|
@@ -9,7 +9,7 @@ npm i remove-anything
|
|
|
9
9
|
|
|
10
10
|
Removes props (eg. undefined) from an object
|
|
11
11
|
|
|
12
|
-
An optimised way to remove any prop (eg. `undefined`, empty objects, ...) from an object. A small and simple integration.
|
|
12
|
+
An optimised way to remove any prop value (eg. `undefined`, empty objects, ...) from an object. A small and simple integration.
|
|
13
13
|
|
|
14
14
|
## Usage
|
|
15
15
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Recursively remove props from an object, if the prop's value matches `valueToRemove`
|
|
3
|
-
*/
|
|
1
|
+
/** Recursively remove props from an object, if the prop's value matches `valueToRemove` */
|
|
4
2
|
export declare function removeProp(payload: {
|
|
5
3
|
[key in string | number | symbol]: unknown;
|
|
6
4
|
}, valueToRemove: unknown, ...valuesToRemove: unknown[]): {
|
|
7
5
|
[key in string | number | symbol]: unknown;
|
|
8
6
|
};
|
|
9
7
|
/**
|
|
10
|
-
*
|
|
8
|
+
* Recursively remove props from an object, if the prop's value matches `valueToRemove`
|
|
9
|
+
*
|
|
10
|
+
* This function mutates the passed object in place and returns void.
|
|
11
11
|
*/
|
|
12
|
-
export declare
|
|
13
|
-
[
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
[
|
|
18
|
-
|
|
19
|
-
[
|
|
12
|
+
export declare function removePropInPlace(payload: {
|
|
13
|
+
[key in string | number | symbol]: unknown;
|
|
14
|
+
}, valueToRemove: unknown, ...valuesToRemove: unknown[]): void;
|
|
15
|
+
/** @deprecated Use `removeProp` instead and pass multiple parameters */
|
|
16
|
+
export declare function removeProps(payload: {
|
|
17
|
+
[key in string | number | symbol]: unknown;
|
|
18
|
+
}, valuesToRemove: unknown[]): {
|
|
19
|
+
[key in string | number | symbol]: unknown;
|
|
20
20
|
};
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-dynamic-delete */
|
|
1
2
|
import { isEmptyArray, isEmptyObject, isPlainObject } from 'is-what';
|
|
2
|
-
/**
|
|
3
|
-
* Recursively remove props from an object, if the prop's value matches `valueToRemove`
|
|
4
|
-
*/
|
|
3
|
+
/** Recursively remove props from an object, if the prop's value matches `valueToRemove` */
|
|
5
4
|
export function removeProp(payload, valueToRemove, ...valuesToRemove) {
|
|
6
5
|
if (!isPlainObject(payload))
|
|
7
6
|
return payload;
|
|
@@ -15,6 +14,7 @@ export function removeProp(payload, valueToRemove, ...valuesToRemove) {
|
|
|
15
14
|
return carry;
|
|
16
15
|
if (remove.includes(value))
|
|
17
16
|
return carry;
|
|
17
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
18
18
|
const newVal = removeProp(value, remove[0], ...remove.slice(1));
|
|
19
19
|
if (removeEmptyObjects && isEmptyObject(newVal))
|
|
20
20
|
return carry;
|
|
@@ -25,6 +25,43 @@ export function removeProp(payload, valueToRemove, ...valuesToRemove) {
|
|
|
25
25
|
}, {});
|
|
26
26
|
}
|
|
27
27
|
/**
|
|
28
|
-
*
|
|
28
|
+
* Recursively remove props from an object, if the prop's value matches `valueToRemove`
|
|
29
|
+
*
|
|
30
|
+
* This function mutates the passed object in place and returns void.
|
|
29
31
|
*/
|
|
30
|
-
export
|
|
32
|
+
export function removePropInPlace(payload, valueToRemove, ...valuesToRemove) {
|
|
33
|
+
if (!isPlainObject(payload))
|
|
34
|
+
return;
|
|
35
|
+
const remove = [valueToRemove, ...valuesToRemove];
|
|
36
|
+
const removeEmptyObjects = !!remove.find((val) => isEmptyObject(val));
|
|
37
|
+
const removeEmptyArrays = !!remove.find((val) => isEmptyArray(val));
|
|
38
|
+
Object.entries(payload).forEach(([key, value]) => {
|
|
39
|
+
if (removeEmptyObjects && isEmptyObject(value)) {
|
|
40
|
+
delete payload[key];
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
if (removeEmptyArrays && isEmptyArray(value)) {
|
|
44
|
+
delete payload[key];
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
if (remove.includes(value)) {
|
|
48
|
+
delete payload[key];
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
if (value && isPlainObject(value)) {
|
|
52
|
+
removePropInPlace(value, remove[0], ...remove.slice(1));
|
|
53
|
+
}
|
|
54
|
+
if (removeEmptyObjects && isEmptyObject(value)) {
|
|
55
|
+
delete payload[key];
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
if (removeEmptyArrays && isEmptyArray(value)) {
|
|
59
|
+
delete payload[key];
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
/** @deprecated Use `removeProp` instead and pass multiple parameters */
|
|
65
|
+
export function removeProps(payload, valuesToRemove) {
|
|
66
|
+
return removeProp(payload, valuesToRemove[0], ...valuesToRemove);
|
|
67
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "remove-anything",
|
|
3
|
-
"version": "2.0
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"description": "Remove any prop (eg. `undefined`) from an object",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -17,14 +17,14 @@
|
|
|
17
17
|
"release": "npm run lint && npm run build && np"
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"is-what": "^5.
|
|
20
|
+
"is-what": "^5.2.1"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
"
|
|
23
|
+
"@cycraft/eslint": "^0.4.4",
|
|
24
|
+
"@cycraft/tsconfig": "^0.1.2",
|
|
25
|
+
"del-cli": "^6.0.0",
|
|
26
|
+
"np": "^10.2.0",
|
|
27
|
+
"vitest": "^3.2.4"
|
|
28
28
|
},
|
|
29
29
|
"files": [
|
|
30
30
|
"dist"
|