remove-anything 2.0.3 → 2.1.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 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +39 -0
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -49,6 +49,17 @@ removeProps(payload, {}, [])
|
|
|
49
49
|
// { a: 1, b: undefined }
|
|
50
50
|
```
|
|
51
51
|
|
|
52
|
+
### In-Place Mutation
|
|
53
|
+
|
|
54
|
+
Use `removePropInPlace` to mutate the original object instead of creating a new one:
|
|
55
|
+
|
|
56
|
+
```js
|
|
57
|
+
const payload = { a: 1, b: undefined, c: {} }
|
|
58
|
+
removePropInPlace(payload, undefined, {})
|
|
59
|
+
|
|
60
|
+
// payload is now mutated to: { a: 1 }
|
|
61
|
+
```
|
|
62
|
+
|
|
52
63
|
## Meet the family (more tiny utils with TS support)
|
|
53
64
|
|
|
54
65
|
- [is-what 🙉](https://github.com/mesqueeb/is-what)
|
package/dist/index.d.ts
CHANGED
|
@@ -4,6 +4,14 @@ export declare function removeProp(payload: {
|
|
|
4
4
|
}, valueToRemove: unknown, ...valuesToRemove: unknown[]): {
|
|
5
5
|
[key in string | number | symbol]: unknown;
|
|
6
6
|
};
|
|
7
|
+
/**
|
|
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
|
+
*/
|
|
12
|
+
export declare function removePropInPlace(payload: {
|
|
13
|
+
[key in string | number | symbol]: unknown;
|
|
14
|
+
}, valueToRemove: unknown, ...valuesToRemove: unknown[]): void;
|
|
7
15
|
/** @deprecated Use `removeProp` instead and pass multiple parameters */
|
|
8
16
|
export declare function removeProps(payload: {
|
|
9
17
|
[key in string | number | symbol]: unknown;
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
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` */
|
|
3
4
|
export function removeProp(payload, valueToRemove, ...valuesToRemove) {
|
|
@@ -13,6 +14,7 @@ export function removeProp(payload, valueToRemove, ...valuesToRemove) {
|
|
|
13
14
|
return carry;
|
|
14
15
|
if (remove.includes(value))
|
|
15
16
|
return carry;
|
|
17
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
16
18
|
const newVal = removeProp(value, remove[0], ...remove.slice(1));
|
|
17
19
|
if (removeEmptyObjects && isEmptyObject(newVal))
|
|
18
20
|
return carry;
|
|
@@ -22,6 +24,43 @@ export function removeProp(payload, valueToRemove, ...valuesToRemove) {
|
|
|
22
24
|
return carry;
|
|
23
25
|
}, {});
|
|
24
26
|
}
|
|
27
|
+
/**
|
|
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.
|
|
31
|
+
*/
|
|
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
|
+
}
|
|
25
64
|
/** @deprecated Use `removeProp` instead and pass multiple parameters */
|
|
26
65
|
export function removeProps(payload, valuesToRemove) {
|
|
27
66
|
return removeProp(payload, valuesToRemove[0], ...valuesToRemove);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "remove-anything",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.1",
|
|
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.2.
|
|
20
|
+
"is-what": "^5.2.1"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
|
-
"@cycraft/eslint": "^0.4.
|
|
23
|
+
"@cycraft/eslint": "^0.4.4",
|
|
24
24
|
"@cycraft/tsconfig": "^0.1.2",
|
|
25
25
|
"del-cli": "^6.0.0",
|
|
26
26
|
"np": "^10.2.0",
|
|
27
|
-
"vitest": "^3.
|
|
27
|
+
"vitest": "^3.2.4"
|
|
28
28
|
},
|
|
29
29
|
"files": [
|
|
30
30
|
"dist"
|